blob: 233b8c7a78df912c9f7a0a753937c7796b9eb7e3 [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 Grimberg2f1b6b72015-06-04 19:49:20 +030068static void isert_release_work(struct work_struct *work);
69
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020070static inline bool
71isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
72{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020073 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020074 cmd->prot_op != TARGET_PROT_NORMAL);
75}
76
77
Vu Phamd40945d2013-08-28 23:23:34 +030078static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080079isert_qp_event_callback(struct ib_event *e, void *context)
80{
Sagi Grimberg67004252015-03-29 15:52:09 +030081 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080082
Sagi Grimbergea8a1612015-05-18 13:40:31 +030083 isert_err("%s (%d): conn %p\n",
84 ib_event_msg(e->event), e->event, isert_conn);
85
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080086 switch (e->event) {
87 case IB_EVENT_COMM_EST:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +030088 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080089 break;
90 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020091 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080092 break;
93 default:
94 break;
95 }
96}
97
98static int
99isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
100{
101 int ret;
102
103 ret = ib_query_device(ib_dev, devattr);
104 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200105 isert_err("ib_query_device() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106 return ret;
107 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200108 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
109 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800110
111 return 0;
112}
113
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300114static struct isert_comp *
115isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800116{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300117 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200118 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300119 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800120
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800121 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200122 for (i = 0; i < device->comps_used; i++)
123 if (device->comps[i].active_qps <
124 device->comps[min].active_qps)
125 min = i;
126 comp = &device->comps[min];
127 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300128 mutex_unlock(&device_list_mutex);
129
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200130 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200131 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300132
133 return comp;
134}
135
136static void
137isert_comp_put(struct isert_comp *comp)
138{
139 mutex_lock(&device_list_mutex);
140 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800141 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300142}
143
144static struct ib_qp *
145isert_create_qp(struct isert_conn *isert_conn,
146 struct isert_comp *comp,
147 struct rdma_cm_id *cma_id)
148{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300149 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300150 struct ib_qp_init_attr attr;
151 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800152
153 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
154 attr.event_handler = isert_qp_event_callback;
155 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200156 attr.send_cq = comp->cq;
157 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800158 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200159 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800160 /*
161 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700162 * work-around for RDMA_READs with ConnectX-2.
163 *
164 * Also, still make sure to have at least two SGEs for
165 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800166 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700167 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800168 isert_conn->max_sge = attr.cap.max_send_sge;
169
170 attr.cap.max_recv_sge = 1;
171 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
172 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200173 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200174 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800175
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300176 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800177 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200178 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300179 return ERR_PTR(ret);
180 }
181
182 return cma_id->qp;
183}
184
185static int
186isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
187{
188 struct isert_comp *comp;
189 int ret;
190
191 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300192 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
193 if (IS_ERR(isert_conn->qp)) {
194 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200195 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800196 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800197
198 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200199err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300200 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200201 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800202}
203
204static void
205isert_cq_event_callback(struct ib_event *e, void *context)
206{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200207 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800208}
209
210static int
211isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
212{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300213 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300214 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800215 struct iser_rx_desc *rx_desc;
216 struct ib_sge *rx_sg;
217 u64 dma_addr;
218 int i, j;
219
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300220 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300222 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800223 goto fail;
224
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300225 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800226
227 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
228 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
229 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
230 if (ib_dma_mapping_error(ib_dev, dma_addr))
231 goto dma_map_fail;
232
233 rx_desc->dma_addr = dma_addr;
234
235 rx_sg = &rx_desc->rx_sg;
236 rx_sg->addr = rx_desc->dma_addr;
237 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600238 rx_sg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 }
240
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++) {
Matan Barak8e372102015-06-11 16:35:21 +0300321 struct ib_cq_init_attr cq_attr = {};
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200322 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800323
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200324 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200325 INIT_WORK(&comp->work, isert_cq_work);
Matan Barak8e372102015-06-11 16:35:21 +0300326 cq_attr.cqe = max_cqe;
327 cq_attr.comp_vector = i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200328 comp->cq = ib_create_cq(device->ib_device,
329 isert_cq_callback,
330 isert_cq_event_callback,
331 (void *)comp,
Matan Barak8e372102015-06-11 16:35:21 +0300332 &cq_attr);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200333 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300334 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200335 ret = PTR_ERR(comp->cq);
336 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800337 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800338 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800339
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200340 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800341 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800342 goto out_cq;
343 }
344
Sagi Grimberg172369c2015-03-29 15:52:11 +0300345 return 0;
346out_cq:
347 isert_free_comps(device);
348 return ret;
349}
350
351static int
352isert_create_device_ib_res(struct isert_device *device)
353{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300354 struct ib_device_attr *dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300355 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300356
357 dev_attr = &device->dev_attr;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300358 ret = isert_query_device(device->ib_device, dev_attr);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300359 if (ret)
360 return ret;
361
362 /* asign function handlers */
363 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
364 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
365 device->use_fastreg = 1;
366 device->reg_rdma_mem = isert_reg_rdma;
367 device->unreg_rdma_mem = isert_unreg_rdma;
368 } else {
369 device->use_fastreg = 0;
370 device->reg_rdma_mem = isert_map_rdma;
371 device->unreg_rdma_mem = isert_unmap_cmd;
372 }
373
374 ret = isert_alloc_comps(device, dev_attr);
375 if (ret)
376 return ret;
377
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300378 device->pd = ib_alloc_pd(device->ib_device);
379 if (IS_ERR(device->pd)) {
380 ret = PTR_ERR(device->pd);
381 isert_err("failed to allocate pd, device %p, ret=%d\n",
382 device, ret);
383 goto out_cq;
384 }
385
Sagi Grimberg172369c2015-03-29 15:52:11 +0300386 /* Check signature cap */
387 device->pi_capable = dev_attr->device_cap_flags &
388 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300389
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800390 return 0;
391
392out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300393 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800394 return ret;
395}
396
397static void
398isert_free_device_ib_res(struct isert_device *device)
399{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200400 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800401
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300402 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300403 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800404}
405
406static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300407isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800408{
409 mutex_lock(&device_list_mutex);
410 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200411 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800412 if (!device->refcount) {
413 isert_free_device_ib_res(device);
414 list_del(&device->dev_node);
415 kfree(device);
416 }
417 mutex_unlock(&device_list_mutex);
418}
419
420static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300421isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800422{
423 struct isert_device *device;
424 int ret;
425
426 mutex_lock(&device_list_mutex);
427 list_for_each_entry(device, &device_list, dev_node) {
428 if (device->ib_device->node_guid == cma_id->device->node_guid) {
429 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200430 isert_info("Found iser device %p refcount %d\n",
431 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800432 mutex_unlock(&device_list_mutex);
433 return device;
434 }
435 }
436
437 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
438 if (!device) {
439 mutex_unlock(&device_list_mutex);
440 return ERR_PTR(-ENOMEM);
441 }
442
443 INIT_LIST_HEAD(&device->dev_node);
444
445 device->ib_device = cma_id->device;
446 ret = isert_create_device_ib_res(device);
447 if (ret) {
448 kfree(device);
449 mutex_unlock(&device_list_mutex);
450 return ERR_PTR(ret);
451 }
452
453 device->refcount++;
454 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200455 isert_info("Created a new iser device %p refcount %d\n",
456 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800457 mutex_unlock(&device_list_mutex);
458
459 return device;
460}
461
Vu Pham59464ef2013-08-28 23:23:35 +0300462static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200463isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300464{
465 struct fast_reg_descriptor *fr_desc, *tmp;
466 int i = 0;
467
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300468 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300469 return;
470
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200471 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300472
473 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300474 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300475 list_del(&fr_desc->list);
476 ib_free_fast_reg_page_list(fr_desc->data_frpl);
477 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200478 if (fr_desc->pi_ctx) {
479 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
480 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300481 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200482 kfree(fr_desc->pi_ctx);
483 }
Vu Pham59464ef2013-08-28 23:23:35 +0300484 kfree(fr_desc);
485 ++i;
486 }
487
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300488 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200489 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300490 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300491}
492
493static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200494isert_create_pi_ctx(struct fast_reg_descriptor *desc,
495 struct ib_device *device,
496 struct ib_pd *pd)
497{
Sagi Grimberg570db172014-12-02 16:57:31 +0200498 struct pi_context *pi_ctx;
499 int ret;
500
501 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
502 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200503 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200504 return -ENOMEM;
505 }
506
507 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
508 ISCSI_ISER_SG_TABLESIZE);
509 if (IS_ERR(pi_ctx->prot_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200510 isert_err("Failed to allocate prot frpl err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200511 PTR_ERR(pi_ctx->prot_frpl));
512 ret = PTR_ERR(pi_ctx->prot_frpl);
513 goto err_pi_ctx;
514 }
515
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300516 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
517 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200518 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200519 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200520 PTR_ERR(pi_ctx->prot_mr));
521 ret = PTR_ERR(pi_ctx->prot_mr);
522 goto err_prot_frpl;
523 }
524 desc->ind |= ISERT_PROT_KEY_VALID;
525
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300526 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200527 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200528 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200529 PTR_ERR(pi_ctx->sig_mr));
530 ret = PTR_ERR(pi_ctx->sig_mr);
531 goto err_prot_mr;
532 }
533
534 desc->pi_ctx = pi_ctx;
535 desc->ind |= ISERT_SIG_KEY_VALID;
536 desc->ind &= ~ISERT_PROTECTED;
537
538 return 0;
539
540err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700541 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200542err_prot_frpl:
Roland Dreierb2feda42015-05-29 23:12:10 -0700543 ib_free_fast_reg_page_list(pi_ctx->prot_frpl);
Sagi Grimberg570db172014-12-02 16:57:31 +0200544err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700545 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200546
547 return ret;
548}
549
550static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200551isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200552 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200553{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200554 int ret;
555
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200556 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
557 ISCSI_ISER_SG_TABLESIZE);
558 if (IS_ERR(fr_desc->data_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200559 isert_err("Failed to allocate data frpl err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200560 PTR_ERR(fr_desc->data_frpl));
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200561 return PTR_ERR(fr_desc->data_frpl);
562 }
563
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300564 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
565 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200566 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200567 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200568 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200569 ret = PTR_ERR(fr_desc->data_mr);
570 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200571 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200572 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200573
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200574 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200575
576 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200577
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200578err_data_frpl:
579 ib_free_fast_reg_page_list(fr_desc->data_frpl);
580
581 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200582}
583
584static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200585isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300586{
587 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300588 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700589 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
590 struct se_node_acl *se_nacl = se_sess->se_node_acl;
591 int i, ret, tag_num;
592 /*
593 * Setup the number of FRMRs based upon the number of tags
594 * available to session in iscsi_target_locate_portal().
595 */
596 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
597 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300598
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300599 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700600 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300601 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
602 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200603 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300604 ret = -ENOMEM;
605 goto err;
606 }
607
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200608 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300609 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200610 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200611 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200612 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800613 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300614 goto err;
615 }
616
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300617 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
618 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300619 }
620
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200621 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300622 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300623
624 return 0;
625
626err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200627 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300628 return ret;
629}
630
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300631static void
632isert_init_conn(struct isert_conn *isert_conn)
633{
634 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300635 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300636 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300637 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300638 init_completion(&isert_conn->wait);
639 kref_init(&isert_conn->kref);
640 mutex_init(&isert_conn->mutex);
641 spin_lock_init(&isert_conn->pool_lock);
642 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300643 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300644}
645
646static void
647isert_free_login_buf(struct isert_conn *isert_conn)
648{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300649 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300650
651 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
652 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
653 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
654 ISCSI_DEF_MAX_RECV_SEG_LEN,
655 DMA_FROM_DEVICE);
656 kfree(isert_conn->login_buf);
657}
658
659static int
660isert_alloc_login_buf(struct isert_conn *isert_conn,
661 struct ib_device *ib_dev)
662{
663 int ret;
664
665 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
666 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
667 if (!isert_conn->login_buf) {
668 isert_err("Unable to allocate isert_conn->login_buf\n");
669 return -ENOMEM;
670 }
671
672 isert_conn->login_req_buf = isert_conn->login_buf;
673 isert_conn->login_rsp_buf = isert_conn->login_buf +
674 ISCSI_DEF_MAX_RECV_SEG_LEN;
675
676 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
677 isert_conn->login_buf, isert_conn->login_req_buf,
678 isert_conn->login_rsp_buf);
679
680 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
681 (void *)isert_conn->login_req_buf,
682 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
683
684 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
685 if (ret) {
686 isert_err("login_req_dma mapping error: %d\n", ret);
687 isert_conn->login_req_dma = 0;
688 goto out_login_buf;
689 }
690
691 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
692 (void *)isert_conn->login_rsp_buf,
693 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
694
695 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
696 if (ret) {
697 isert_err("login_rsp_dma mapping error: %d\n", ret);
698 isert_conn->login_rsp_dma = 0;
699 goto out_req_dma_map;
700 }
701
702 return 0;
703
704out_req_dma_map:
705 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
706 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
707out_login_buf:
708 kfree(isert_conn->login_buf);
709 return ret;
710}
711
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800712static int
713isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
714{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200715 struct isert_np *isert_np = cma_id->context;
716 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800717 struct isert_conn *isert_conn;
718 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800719 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300720
721 spin_lock_bh(&np->np_thread_lock);
722 if (!np->enabled) {
723 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200724 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300725 return rdma_reject(cma_id, NULL, 0);
726 }
727 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800728
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200729 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800730 cma_id, cma_id->context);
731
732 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300733 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800734 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800735
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300736 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300737 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800738
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300739 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
740 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800741 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800742
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300743 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800744 if (IS_ERR(device)) {
745 ret = PTR_ERR(device);
746 goto out_rsp_dma_map;
747 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300748 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800749
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300750 /* Set max inflight RDMA READ requests */
751 isert_conn->initiator_depth = min_t(u8,
752 event->param.conn.initiator_depth,
753 device->dev_attr.max_qp_init_rd_atom);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200754 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300755
Sagi Grimberg570db172014-12-02 16:57:31 +0200756 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800757 if (ret)
758 goto out_conn_dev;
759
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200760 ret = isert_rdma_post_recvl(isert_conn);
761 if (ret)
762 goto out_conn_dev;
763
764 ret = isert_rdma_accept(isert_conn);
765 if (ret)
766 goto out_conn_dev;
767
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300768 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300769 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300770 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800771
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800772 return 0;
773
774out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300775 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800776out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300777 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800778out:
779 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200780 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800781 return ret;
782}
783
784static void
785isert_connect_release(struct isert_conn *isert_conn)
786{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300787 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800788
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200789 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800790
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300791 BUG_ON(!device);
792
793 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200794 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300795
Sagi Grimberg19e20902014-12-02 16:57:26 +0200796 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300797 if (isert_conn->cm_id)
798 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200799
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300800 if (isert_conn->qp) {
801 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200802
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300803 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300804 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800805 }
806
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300807 if (isert_conn->login_buf)
808 isert_free_login_buf(isert_conn);
809
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300810 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800811
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300812 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800813}
814
815static void
816isert_connected_handler(struct rdma_cm_id *cma_id)
817{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200818 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300819 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300820
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200821 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200822
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300823 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300824 isert_conn->state = ISER_CONN_UP;
825 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300826 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300827
828 mutex_lock(&isert_np->mutex);
829 list_move_tail(&isert_conn->node, &isert_np->pending);
830 mutex_unlock(&isert_np->mutex);
831
832 isert_info("np %p: Allow accept_np to continue\n", isert_np);
833 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800834}
835
836static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300837isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800838{
839 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300840 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800841
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200842 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
843 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800844
845 isert_connect_release(isert_conn);
846}
847
848static void
849isert_put_conn(struct isert_conn *isert_conn)
850{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300851 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800852}
853
Sagi Grimberg954f2372014-12-02 16:57:17 +0200854/**
855 * isert_conn_terminate() - Initiate connection termination
856 * @isert_conn: isert connection struct
857 *
858 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200859 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200860 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200861 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200862 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300863 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200864 * safe to call multiple times.
865 */
866static void
867isert_conn_terminate(struct isert_conn *isert_conn)
868{
869 int err;
870
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200871 switch (isert_conn->state) {
872 case ISER_CONN_TERMINATING:
873 break;
874 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200875 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200876 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200877 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200878 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300879 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200880 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200881 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200882 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200883 break;
884 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200885 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200886 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200887 }
888}
889
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700890static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200891isert_np_cma_handler(struct isert_np *isert_np,
892 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800893{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300894 isert_dbg("%s (%d): isert np %p\n",
895 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200896
897 switch (event) {
898 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300899 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200900 break;
901 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300902 isert_np->cm_id = isert_setup_id(isert_np);
903 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200904 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300905 isert_np, PTR_ERR(isert_np->cm_id));
906 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200907 }
908 break;
909 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200910 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200911 isert_np, event);
912 }
913
914 return -1;
915}
916
917static int
918isert_disconnected_handler(struct rdma_cm_id *cma_id,
919 enum rdma_cm_event_type event)
920{
921 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700922 struct isert_conn *isert_conn;
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300923 bool terminating = false;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700924
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300925 if (isert_np->cm_id == cma_id)
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200926 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700927
Sagi Grimberg19e20902014-12-02 16:57:26 +0200928 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800929
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300930 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300931 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200932 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300933 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200934
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300935 isert_info("conn %p completing wait\n", isert_conn);
936 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700937
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300938 if (terminating)
939 goto out;
940
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300941 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300942 if (!list_empty(&isert_conn->node)) {
943 list_del_init(&isert_conn->node);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300944 isert_put_conn(isert_conn);
945 queue_work(isert_release_wq, &isert_conn->release_work);
946 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300947 mutex_unlock(&isert_np->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300948
949out:
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700950 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800951}
952
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300953static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200954isert_connect_error(struct rdma_cm_id *cma_id)
955{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200956 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200957
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300958 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300959 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200960 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300961
962 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200963}
964
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800965static int
966isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
967{
968 int ret = 0;
969
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300970 isert_info("%s (%d): status %d id %p np %p\n",
971 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200972 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800973
974 switch (event->event) {
975 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800976 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700977 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200978 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800979 break;
980 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981 isert_connected_handler(cma_id);
982 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300983 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
984 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
985 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300986 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200987 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800988 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200989 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
990 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800991 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300992 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200993 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800994 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200995 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800996 break;
997 }
998
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800999 return ret;
1000}
1001
1002static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001003isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001004{
1005 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1006 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001007 struct iser_rx_desc *rx_desc;
1008
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001009 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001010 rx_desc = &isert_conn->rx_descs[i];
1011 rx_wr->wr_id = (uintptr_t)rx_desc;
1012 rx_wr->sg_list = &rx_desc->rx_sg;
1013 rx_wr->num_sge = 1;
1014 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001015 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001016 rx_wr--;
1017 rx_wr->next = NULL; /* mark end of work requests list */
1018
1019 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001020 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001021 &rx_wr_failed);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001022 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001023 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001024 isert_conn->post_recv_buf_count -= count;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001025 }
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001026
1027 return ret;
1028}
1029
1030static int
1031isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
1032{
1033 struct ib_recv_wr *rx_wr_failed, rx_wr;
1034 int ret;
1035
1036 rx_wr.wr_id = (uintptr_t)rx_desc;
1037 rx_wr.sg_list = &rx_desc->rx_sg;
1038 rx_wr.num_sge = 1;
1039 rx_wr.next = NULL;
1040
1041 isert_conn->post_recv_buf_count++;
1042 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
1043 if (ret) {
1044 isert_err("ib_post_recv() failed with ret: %d\n", ret);
1045 isert_conn->post_recv_buf_count--;
1046 }
1047
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001048 return ret;
1049}
1050
1051static int
1052isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1053{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001054 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001055 struct ib_send_wr send_wr, *send_wr_failed;
1056 int ret;
1057
1058 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1059 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1060
1061 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001062 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001063 send_wr.sg_list = tx_desc->tx_sg;
1064 send_wr.num_sge = tx_desc->num_sge;
1065 send_wr.opcode = IB_WR_SEND;
1066 send_wr.send_flags = IB_SEND_SIGNALED;
1067
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001068 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001069 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001070 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001071
1072 return ret;
1073}
1074
1075static void
1076isert_create_send_desc(struct isert_conn *isert_conn,
1077 struct isert_cmd *isert_cmd,
1078 struct iser_tx_desc *tx_desc)
1079{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001080 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001081 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001082
1083 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1084 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1085
1086 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1087 tx_desc->iser_header.flags = ISER_VER;
1088
1089 tx_desc->num_sge = 1;
1090 tx_desc->isert_cmd = isert_cmd;
1091
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001092 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1093 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001094 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001095 }
1096}
1097
1098static int
1099isert_init_tx_hdrs(struct isert_conn *isert_conn,
1100 struct iser_tx_desc *tx_desc)
1101{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001102 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001103 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001104 u64 dma_addr;
1105
1106 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1107 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1108 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001109 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001110 return -ENOMEM;
1111 }
1112
1113 tx_desc->dma_addr = dma_addr;
1114 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1115 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001116 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001117
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001118 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1119 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1120 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001121
1122 return 0;
1123}
1124
1125static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001126isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001127 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001128{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001129 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1130
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001131 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001132 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001133 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001134 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001135 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001136 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001137}
1138
1139static int
1140isert_rdma_post_recvl(struct isert_conn *isert_conn)
1141{
1142 struct ib_recv_wr rx_wr, *rx_wr_fail;
1143 struct ib_sge sge;
1144 int ret;
1145
1146 memset(&sge, 0, sizeof(struct ib_sge));
1147 sge.addr = isert_conn->login_req_dma;
1148 sge.length = ISER_RX_LOGIN_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001149 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001150
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001151 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001152 sge.addr, sge.length, sge.lkey);
1153
1154 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001155 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001156 rx_wr.sg_list = &sge;
1157 rx_wr.num_sge = 1;
1158
1159 isert_conn->post_recv_buf_count++;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001160 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001161 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001162 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001163 isert_conn->post_recv_buf_count--;
1164 }
1165
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001166 return ret;
1167}
1168
1169static int
1170isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1171 u32 length)
1172{
1173 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001174 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001175 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001176 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001177 int ret;
1178
1179 isert_create_send_desc(isert_conn, NULL, tx_desc);
1180
1181 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1182 sizeof(struct iscsi_hdr));
1183
1184 isert_init_tx_hdrs(isert_conn, tx_desc);
1185
1186 if (length > 0) {
1187 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1188
1189 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1190 length, DMA_TO_DEVICE);
1191
1192 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1193
1194 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1195 length, DMA_TO_DEVICE);
1196
1197 tx_dsg->addr = isert_conn->login_rsp_dma;
1198 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001199 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001200 tx_desc->num_sge = 2;
1201 }
1202 if (!login->login_failed) {
1203 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001204 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001205 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001206 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001207 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001208 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001209 " fastreg pool\n", isert_conn);
1210 return ret;
1211 }
1212 }
1213
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001214 ret = isert_alloc_rx_descriptors(isert_conn);
1215 if (ret)
1216 return ret;
1217
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001218 ret = isert_post_recvm(isert_conn,
1219 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220 if (ret)
1221 return ret;
1222
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001223 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001224 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001225 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001226 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227 goto post_send;
1228 }
1229
1230 ret = isert_rdma_post_recvl(isert_conn);
1231 if (ret)
1232 return ret;
1233 }
1234post_send:
1235 ret = isert_post_send(isert_conn, tx_desc);
1236 if (ret)
1237 return ret;
1238
1239 return 0;
1240}
1241
1242static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001243isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001244{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001245 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1246 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001247 struct iscsi_conn *conn = isert_conn->conn;
1248 struct iscsi_login *login = conn->conn_login;
1249 int size;
1250
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001251 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001252
1253 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254
1255 if (login->first_request) {
1256 struct iscsi_login_req *login_req =
1257 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1258 /*
1259 * Setup the initial iscsi_login values from the leading
1260 * login request PDU.
1261 */
1262 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1263 login->current_stage =
1264 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1265 >> 2;
1266 login->version_min = login_req->min_version;
1267 login->version_max = login_req->max_version;
1268 memcpy(login->isid, login_req->isid, 6);
1269 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1270 login->init_task_tag = login_req->itt;
1271 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1272 login->cid = be16_to_cpu(login_req->cid);
1273 login->tsih = be16_to_cpu(login_req->tsih);
1274 }
1275
1276 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1277
1278 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001279 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1280 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1281 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282 memcpy(login->req_buf, &rx_desc->data[0], size);
1283
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001284 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001285 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001286 return;
1287 }
1288 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001289}
1290
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001291static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001292*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293{
Sagi Grimberg67004252015-03-29 15:52:09 +03001294 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001295 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001296 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001297
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001298 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001299 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001300 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001301 return NULL;
1302 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001303 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001304 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001305 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001306 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001307
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001308 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001309}
1310
1311static int
1312isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001313 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1314 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001315{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001316 struct iscsi_conn *conn = isert_conn->conn;
1317 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1318 struct scatterlist *sg;
1319 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1320 bool dump_payload = false;
1321
1322 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1323 if (rc < 0)
1324 return rc;
1325
1326 imm_data = cmd->immediate_data;
1327 imm_data_len = cmd->first_burst_len;
1328 unsol_data = cmd->unsolicited_data;
1329
1330 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1331 if (rc < 0) {
1332 return 0;
1333 } else if (rc > 0) {
1334 dump_payload = true;
1335 goto sequence_cmd;
1336 }
1337
1338 if (!imm_data)
1339 return 0;
1340
1341 sg = &cmd->se_cmd.t_data_sg[0];
1342 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1343
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001344 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001345 sg, sg_nents, &rx_desc->data[0], imm_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001346
1347 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1348
1349 cmd->write_data_done += imm_data_len;
1350
1351 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1352 spin_lock_bh(&cmd->istate_lock);
1353 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1354 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1355 spin_unlock_bh(&cmd->istate_lock);
1356 }
1357
1358sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001359 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001360
1361 if (!rc && dump_payload == false && unsol_data)
1362 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001363 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001364 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001365
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001366 return 0;
1367}
1368
1369static int
1370isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1371 struct iser_rx_desc *rx_desc, unsigned char *buf)
1372{
1373 struct scatterlist *sg_start;
1374 struct iscsi_conn *conn = isert_conn->conn;
1375 struct iscsi_cmd *cmd = NULL;
1376 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1377 u32 unsol_data_len = ntoh24(hdr->dlength);
1378 int rc, sg_nents, sg_off, page_off;
1379
1380 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1381 if (rc < 0)
1382 return rc;
1383 else if (!cmd)
1384 return 0;
1385 /*
1386 * FIXME: Unexpected unsolicited_data out
1387 */
1388 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001389 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001390 dump_stack();
1391 return -1;
1392 }
1393
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001394 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1395 "write_data_done: %u, data_length: %u\n",
1396 unsol_data_len, cmd->write_data_done,
1397 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001398
1399 sg_off = cmd->write_data_done / PAGE_SIZE;
1400 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1401 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1402 page_off = cmd->write_data_done % PAGE_SIZE;
1403 /*
1404 * FIXME: Non page-aligned unsolicited_data out
1405 */
1406 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001407 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001408 dump_stack();
1409 return -1;
1410 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001411 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1412 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1413 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001414
1415 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1416 unsol_data_len);
1417
1418 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1419 if (rc < 0)
1420 return rc;
1421
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001422 /*
1423 * multiple data-outs on the same command can arrive -
1424 * so post the buffer before hand
1425 */
1426 rc = isert_post_recv(isert_conn, rx_desc);
1427 if (rc) {
1428 isert_err("ib_post_recv failed with %d\n", rc);
1429 return rc;
1430 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001431 return 0;
1432}
1433
1434static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001435isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001436 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1437 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001438{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001439 struct iscsi_conn *conn = isert_conn->conn;
1440 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1441 int rc;
1442
1443 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1444 if (rc < 0)
1445 return rc;
1446 /*
1447 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1448 */
1449
1450 return iscsit_process_nop_out(conn, cmd, hdr);
1451}
1452
1453static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001454isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001455 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1456 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001457{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001458 struct iscsi_conn *conn = isert_conn->conn;
1459 u32 payload_length = ntoh24(hdr->dlength);
1460 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001461 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001462
1463 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1464 if (rc < 0)
1465 return rc;
1466
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001467 if (payload_length) {
1468 text_in = kzalloc(payload_length, GFP_KERNEL);
1469 if (!text_in) {
1470 isert_err("Unable to allocate text_in of payload_length: %u\n",
1471 payload_length);
1472 return -ENOMEM;
1473 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001474 }
1475 cmd->text_in_ptr = text_in;
1476
1477 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1478
1479 return iscsit_process_text_cmd(conn, cmd, hdr);
1480}
1481
1482static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001483isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1484 uint32_t read_stag, uint64_t read_va,
1485 uint32_t write_stag, uint64_t write_va)
1486{
1487 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1488 struct iscsi_conn *conn = isert_conn->conn;
1489 struct iscsi_cmd *cmd;
1490 struct isert_cmd *isert_cmd;
1491 int ret = -EINVAL;
1492 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1493
Sagi Grimbergfb140272015-03-29 15:52:08 +03001494 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001495 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001496 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001497 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001498 return 0;
1499 }
1500
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001501 switch (opcode) {
1502 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001503 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001504 if (!cmd)
1505 break;
1506
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001507 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001508 isert_cmd->read_stag = read_stag;
1509 isert_cmd->read_va = read_va;
1510 isert_cmd->write_stag = write_stag;
1511 isert_cmd->write_va = write_va;
1512
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001513 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001514 rx_desc, (unsigned char *)hdr);
1515 break;
1516 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001517 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001518 if (!cmd)
1519 break;
1520
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001521 isert_cmd = iscsit_priv_cmd(cmd);
1522 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001523 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001524 break;
1525 case ISCSI_OP_SCSI_DATA_OUT:
1526 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1527 (unsigned char *)hdr);
1528 break;
1529 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001530 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001531 if (!cmd)
1532 break;
1533
1534 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1535 (unsigned char *)hdr);
1536 break;
1537 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001538 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001539 if (!cmd)
1540 break;
1541
1542 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001543 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001544 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001545 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001546 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001547 else
1548 cmd = isert_allocate_cmd(conn, rx_desc);
1549
1550 if (!cmd)
1551 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001552
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001553 isert_cmd = iscsit_priv_cmd(cmd);
1554 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001555 rx_desc, (struct iscsi_text *)hdr);
1556 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001557 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001558 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001559 dump_stack();
1560 break;
1561 }
1562
1563 return ret;
1564}
1565
1566static void
1567isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1568{
1569 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1570 uint64_t read_va = 0, write_va = 0;
1571 uint32_t read_stag = 0, write_stag = 0;
1572 int rc;
1573
1574 switch (iser_hdr->flags & 0xF0) {
1575 case ISCSI_CTRL:
1576 if (iser_hdr->flags & ISER_RSV) {
1577 read_stag = be32_to_cpu(iser_hdr->read_stag);
1578 read_va = be64_to_cpu(iser_hdr->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001579 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1580 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001581 }
1582 if (iser_hdr->flags & ISER_WSV) {
1583 write_stag = be32_to_cpu(iser_hdr->write_stag);
1584 write_va = be64_to_cpu(iser_hdr->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001585 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1586 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001587 }
1588
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001589 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590 break;
1591 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001592 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593 break;
1594 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001595 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 break;
1597 }
1598
1599 rc = isert_rx_opcode(isert_conn, rx_desc,
1600 read_stag, read_va, write_stag, write_va);
1601}
1602
1603static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001604isert_rcv_completion(struct iser_rx_desc *desc,
1605 struct isert_conn *isert_conn,
1606 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001607{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001608 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001609 struct iscsi_hdr *hdr;
1610 u64 rx_dma;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001611 int rx_buflen;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001612
1613 if ((char *)desc == isert_conn->login_req_buf) {
1614 rx_dma = isert_conn->login_req_dma;
1615 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001616 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001617 rx_dma, rx_buflen);
1618 } else {
1619 rx_dma = desc->dma_addr;
1620 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001621 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001622 rx_dma, rx_buflen);
1623 }
1624
1625 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1626
1627 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001628 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001629 hdr->opcode, hdr->itt, hdr->flags,
1630 (int)(xfer_len - ISER_HEADERS_LEN));
1631
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001632 if ((char *)desc == isert_conn->login_req_buf) {
1633 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1634 if (isert_conn->conn) {
1635 struct iscsi_login *login = isert_conn->conn->conn_login;
1636
1637 if (login && !login->first_request)
1638 isert_rx_login_req(isert_conn);
1639 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001640 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001641 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001642 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001643 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001644 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001645 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001646
1647 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1648 DMA_FROM_DEVICE);
1649
1650 isert_conn->post_recv_buf_count--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651}
1652
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001653static int
1654isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1655 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1656 enum iser_ib_op_code op, struct isert_data_buf *data)
1657{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001658 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001659
1660 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1661 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1662
1663 data->len = length - offset;
1664 data->offset = offset;
1665 data->sg_off = data->offset / PAGE_SIZE;
1666
1667 data->sg = &sg[data->sg_off];
1668 data->nents = min_t(unsigned int, nents - data->sg_off,
1669 ISCSI_ISER_SG_TABLESIZE);
1670 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1671 PAGE_SIZE);
1672
1673 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1674 data->dma_dir);
1675 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001676 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001677 return -EINVAL;
1678 }
1679
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001680 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001681 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001682
1683 return 0;
1684}
1685
1686static void
1687isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1688{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001689 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001690
1691 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1692 memset(data, 0, sizeof(*data));
1693}
1694
1695
1696
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001697static void
1698isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1699{
1700 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001702 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001703
1704 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001705 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001706 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707 }
1708
Vu Pham90ecc6e2013-08-28 23:23:33 +03001709 if (wr->send_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001710 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001711 kfree(wr->send_wr);
1712 wr->send_wr = NULL;
1713 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001714
Vu Pham90ecc6e2013-08-28 23:23:33 +03001715 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001716 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001717 kfree(wr->ib_sge);
1718 wr->ib_sge = NULL;
1719 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001720}
1721
1722static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001723isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001724{
1725 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001726
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001727 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001728
1729 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001730 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001731 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1732 isert_unmap_data_buf(isert_conn, &wr->prot);
1733 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1734 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001735 spin_lock_bh(&isert_conn->pool_lock);
1736 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1737 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001738 wr->fr_desc = NULL;
1739 }
1740
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001741 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001742 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001743 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001744 }
1745
1746 wr->ib_sge = NULL;
1747 wr->send_wr = NULL;
1748}
1749
1750static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001751isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001752{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001753 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001754 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001755 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001756 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001757 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001758
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001759 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001760
1761 switch (cmd->iscsi_opcode) {
1762 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001763 spin_lock_bh(&conn->cmd_lock);
1764 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001765 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001766 spin_unlock_bh(&conn->cmd_lock);
1767
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001768 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001769 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001770 /*
1771 * Check for special case during comp_err where
1772 * WRITE_PENDING has been handed off from core,
1773 * but requires an extra target_put_sess_cmd()
1774 * before transport_generic_free_cmd() below.
1775 */
1776 if (comp_err &&
1777 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1778 struct se_cmd *se_cmd = &cmd->se_cmd;
1779
Bart Van Asscheafc16602015-04-27 13:52:36 +02001780 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001781 }
1782 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001783
Vu Phamd40945d2013-08-28 23:23:34 +03001784 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001785 transport_generic_free_cmd(&cmd->se_cmd, 0);
1786 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001787 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001788 spin_lock_bh(&conn->cmd_lock);
1789 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001790 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001791 spin_unlock_bh(&conn->cmd_lock);
1792
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001793 transport_generic_free_cmd(&cmd->se_cmd, 0);
1794 break;
1795 case ISCSI_OP_REJECT:
1796 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001797 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001798 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1799 /* If the continue bit is on, keep the command alive */
1800 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1801 break;
1802
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803 spin_lock_bh(&conn->cmd_lock);
1804 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001805 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001806 spin_unlock_bh(&conn->cmd_lock);
1807
1808 /*
1809 * Handle special case for REJECT when iscsi_add_reject*() has
1810 * overwritten the original iscsi_opcode assignment, and the
1811 * associated cmd->se_cmd needs to be released.
1812 */
1813 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001814 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001815 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001816 transport_generic_free_cmd(&cmd->se_cmd, 0);
1817 break;
1818 }
1819 /*
1820 * Fall-through
1821 */
1822 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001823 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001824 break;
1825 }
1826}
1827
1828static void
1829isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1830{
1831 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001832 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001833 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1834 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1835 tx_desc->dma_addr = 0;
1836 }
1837}
1838
1839static void
1840isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001841 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001842{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001843 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001844 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001845 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1846 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1847 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001848 }
1849
1850 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001851 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001852}
1853
Sagi Grimberg96b79732014-03-17 12:52:18 +02001854static int
1855isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1856{
1857 struct ib_mr_status mr_status;
1858 int ret;
1859
1860 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1861 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001862 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001863 goto fail_mr_status;
1864 }
1865
1866 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1867 u64 sec_offset_err;
1868 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1869
1870 switch (mr_status.sig_err.err_type) {
1871 case IB_SIG_BAD_GUARD:
1872 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1873 break;
1874 case IB_SIG_BAD_REFTAG:
1875 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1876 break;
1877 case IB_SIG_BAD_APPTAG:
1878 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1879 break;
1880 }
1881 sec_offset_err = mr_status.sig_err.sig_err_offset;
1882 do_div(sec_offset_err, block_size);
1883 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1884
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001885 isert_err("PI error found type %d at sector 0x%llx "
1886 "expected 0x%x vs actual 0x%x\n",
1887 mr_status.sig_err.err_type,
1888 (unsigned long long)se_cmd->bad_sector,
1889 mr_status.sig_err.expected,
1890 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001891 ret = 1;
1892 }
1893
1894fail_mr_status:
1895 return ret;
1896}
1897
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001898static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001899isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1900 struct isert_cmd *isert_cmd)
1901{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001902 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001903 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001904 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001905 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001906 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001907 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001908
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001909 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001910 ret = isert_check_pi_status(se_cmd,
1911 wr->fr_desc->pi_ctx->sig_mr);
1912 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001913 }
1914
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001915 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001916 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001917 if (ret)
1918 transport_send_check_condition_and_sense(se_cmd,
1919 se_cmd->pi_err, 0);
1920 else
1921 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001922}
1923
1924static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1926 struct isert_cmd *isert_cmd)
1927{
1928 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001929 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001930 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001931 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001932 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001933 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001934
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001935 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001936 ret = isert_check_pi_status(se_cmd,
1937 wr->fr_desc->pi_ctx->sig_mr);
1938 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001939 }
1940
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001941 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001942 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001943 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001944 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001945
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001946 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001947 spin_lock_bh(&cmd->istate_lock);
1948 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1949 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1950 spin_unlock_bh(&cmd->istate_lock);
1951
Sagi Grimberg364189f2015-03-29 15:52:03 +03001952 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001953 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001954 transport_send_check_condition_and_sense(se_cmd,
1955 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001956 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001957 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001958 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001959}
1960
1961static void
1962isert_do_control_comp(struct work_struct *work)
1963{
1964 struct isert_cmd *isert_cmd = container_of(work,
1965 struct isert_cmd, comp_work);
1966 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001967 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001968 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001970 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1971
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001972 switch (cmd->i_state) {
1973 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001974 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001975 case ISTATE_SEND_REJECT: /* FALLTHRU */
1976 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001978 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1979 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001980 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001981 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982 iscsit_logout_post_handler(cmd, cmd->conn);
1983 break;
1984 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001985 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001986 dump_stack();
1987 break;
1988 }
1989}
1990
1991static void
1992isert_response_completion(struct iser_tx_desc *tx_desc,
1993 struct isert_cmd *isert_cmd,
1994 struct isert_conn *isert_conn,
1995 struct ib_device *ib_dev)
1996{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001997 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001998
1999 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002000 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002001 cmd->i_state == ISTATE_SEND_REJECT ||
2002 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003 isert_unmap_tx_desc(tx_desc, ib_dev);
2004
2005 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
2006 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2007 return;
2008 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002009
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002010 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002011 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002012}
2013
2014static void
Sagi Grimberg77486812015-03-29 15:52:13 +03002015isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002016 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002017{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002018 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002019 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2020 struct isert_rdma_wr *wr;
2021
2022 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002023 isert_unmap_tx_desc(tx_desc, ib_dev);
2024 return;
2025 }
2026 wr = &isert_cmd->rdma_wr;
2027
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002028 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2029
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002031 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002032 isert_response_completion(tx_desc, isert_cmd,
2033 isert_conn, ib_dev);
2034 break;
2035 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002036 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002037 break;
2038 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039 isert_completion_rdma_read(tx_desc, isert_cmd);
2040 break;
2041 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002042 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043 dump_stack();
2044 break;
2045 }
2046}
2047
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002048/**
2049 * is_isert_tx_desc() - Indicate if the completion wr_id
2050 * is a TX descriptor or not.
2051 * @isert_conn: iser connection
2052 * @wr_id: completion WR identifier
2053 *
2054 * Since we cannot rely on wc opcode in FLUSH errors
2055 * we must work around it by checking if the wr_id address
2056 * falls in the iser connection rx_descs buffer. If so
2057 * it is an RX descriptor, otherwize it is a TX.
2058 */
2059static inline bool
2060is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002061{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002062 void *start = isert_conn->rx_descs;
2063 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002064
2065 if (wr_id >= start && wr_id < start + len)
2066 return false;
2067
2068 return true;
2069}
2070
2071static void
2072isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2073{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002074 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002075 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002076 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002077 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002078 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002079 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002080 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002081 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002082
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002083 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2084 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002085 if (!isert_cmd)
2086 isert_unmap_tx_desc(desc, ib_dev);
2087 else
2088 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002089 } else {
2090 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002091 if (!isert_conn->post_recv_buf_count)
2092 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002093 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002094}
2095
2096static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002097isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002098{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002099 struct isert_conn *isert_conn;
2100 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002101 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002102
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002103 isert_conn = wc->qp->qp_context;
2104 if (likely(wc->status == IB_WC_SUCCESS)) {
2105 if (wc->opcode == IB_WC_RECV) {
2106 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002107 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002109 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002110 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002111 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002112 } else {
2113 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002114 isert_err("%s (%d): wr id %llx vend_err %x\n",
2115 ib_wc_status_msg(wc->status), wc->status,
2116 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002117 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002118 isert_dbg("%s (%d): wr id %llx\n",
2119 ib_wc_status_msg(wc->status), wc->status,
2120 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002121
2122 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2123 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002124 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002125}
2126
2127static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002128isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002129{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002130 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002131 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002132 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002133 struct ib_wc *const wcs = comp->wcs;
2134 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002135
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002136 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2137 for (i = 0; i < n; i++)
2138 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002139
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002140 completed += n;
2141 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002142 break;
2143 }
2144
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002145 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002146}
2147
2148static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002149isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002150{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002151 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002152
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002153 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002154}
2155
2156static int
2157isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2158{
2159 struct ib_send_wr *wr_failed;
2160 int ret;
2161
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002162 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2163 if (ret) {
2164 isert_err("ib_post_recv failed with %d\n", ret);
2165 return ret;
2166 }
2167
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002168 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002169 &wr_failed);
2170 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002171 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002172 return ret;
2173 }
2174 return ret;
2175}
2176
2177static int
2178isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2179{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002180 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002181 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002182 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2183 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2184 &isert_cmd->tx_desc.iscsi_header;
2185
2186 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2187 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2188 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2189 /*
2190 * Attach SENSE DATA payload to iSCSI Response PDU
2191 */
2192 if (cmd->se_cmd.sense_buffer &&
2193 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2194 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002195 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002196 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002197 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002198 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199
2200 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2201 cmd->sense_buffer);
2202 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2203
2204 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2205 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002206 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002207
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002208 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2209 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002210 DMA_TO_DEVICE);
2211
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002212 isert_cmd->pdu_buf_len = pdu_len;
2213 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2214 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002215 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002216 isert_cmd->tx_desc.num_sge = 2;
2217 }
2218
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002219 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002220
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002221 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002222
2223 return isert_post_response(isert_conn, isert_cmd);
2224}
2225
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002226static void
2227isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2228{
2229 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002230 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002231 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002232
2233 spin_lock_bh(&conn->cmd_lock);
2234 if (!list_empty(&cmd->i_conn_node))
2235 list_del_init(&cmd->i_conn_node);
2236 spin_unlock_bh(&conn->cmd_lock);
2237
2238 if (cmd->data_direction == DMA_TO_DEVICE)
2239 iscsit_stop_dataout_timer(cmd);
2240
2241 device->unreg_rdma_mem(isert_cmd, isert_conn);
2242}
2243
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002244static enum target_prot_op
2245isert_get_sup_prot_ops(struct iscsi_conn *conn)
2246{
Sagi Grimberg67004252015-03-29 15:52:09 +03002247 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002248 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002249
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002250 if (conn->tpg->tpg_attrib.t10_pi) {
2251 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002252 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002253 isert_conn->pi_support = true;
2254 return TARGET_PROT_ALL;
2255 }
2256 }
2257
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002258 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002259 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002260
2261 return TARGET_PROT_NORMAL;
2262}
2263
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002264static int
2265isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2266 bool nopout_response)
2267{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002268 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002269 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002270 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2271
2272 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2273 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2274 &isert_cmd->tx_desc.iscsi_header,
2275 nopout_response);
2276 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002277 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002279 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002280
2281 return isert_post_response(isert_conn, isert_cmd);
2282}
2283
2284static int
2285isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2286{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002287 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002288 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2290
2291 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2292 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2293 &isert_cmd->tx_desc.iscsi_header);
2294 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002295 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002296
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002297 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002298
2299 return isert_post_response(isert_conn, isert_cmd);
2300}
2301
2302static int
2303isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2304{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002305 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002306 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002307 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2308
2309 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2310 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2311 &isert_cmd->tx_desc.iscsi_header);
2312 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002313 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002314
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002315 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002316
2317 return isert_post_response(isert_conn, isert_cmd);
2318}
2319
2320static int
2321isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2322{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002323 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002324 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002325 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002326 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002327 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002328 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2329 struct iscsi_reject *hdr =
2330 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002331
2332 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002333 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002334 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002335
2336 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002337 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002338 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2339 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002340 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2341 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002342 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002343 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002344 isert_cmd->tx_desc.num_sge = 2;
2345
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002346 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002347
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002348 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349
2350 return isert_post_response(isert_conn, isert_cmd);
2351}
2352
2353static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002354isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2355{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002356 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002357 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002358 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2359 struct iscsi_text_rsp *hdr =
2360 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2361 u32 txt_rsp_len;
2362 int rc;
2363
2364 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002365 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002366 if (rc < 0)
2367 return rc;
2368
2369 txt_rsp_len = rc;
2370 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2371
2372 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002373 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002374 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002375 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2376 void *txt_rsp_buf = cmd->buf_ptr;
2377
2378 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2379 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2380
2381 isert_cmd->pdu_buf_len = txt_rsp_len;
2382 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2383 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002384 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002385 isert_cmd->tx_desc.num_sge = 2;
2386 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002387 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002388
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002389 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002390
2391 return isert_post_response(isert_conn, isert_cmd);
2392}
2393
2394static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002395isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2396 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2397 u32 data_left, u32 offset)
2398{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002399 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002401 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002402 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 u32 sg_off, page_off;
2404 int i = 0, sg_nents;
2405
2406 sg_off = offset / PAGE_SIZE;
2407 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2408 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2409 page_off = offset % PAGE_SIZE;
2410
2411 send_wr->sg_list = ib_sge;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002412 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002413 /*
2414 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2415 */
2416 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002417 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2418 "page_off: %u\n",
2419 (unsigned long long)tmp_sg->dma_address,
2420 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002421
2422 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2423 ib_sge->length = min_t(u32, data_left,
2424 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002425 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002427 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2428 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429 page_off = 0;
2430 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002431 if (!data_left)
2432 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002433 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002434 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435 }
2436
Sagi Grimberg9253e662015-06-04 19:49:19 +03002437 send_wr->num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002438 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002439 send_wr->sg_list, send_wr->num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002440
Sagi Grimberg9253e662015-06-04 19:49:19 +03002441 return send_wr->num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442}
2443
2444static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002445isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2446 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002447{
2448 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002449 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002450 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002451 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002452 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002453 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002454 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2455 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002456
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002457 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002459 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2460 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2461 se_cmd->t_data_nents, se_cmd->data_length,
2462 offset, wr->iser_ib_op, &wr->data);
2463 if (ret)
2464 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002466 data_left = data->len;
2467 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002468
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002469 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002470 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002471 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002472 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002473 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002475 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002476
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002477 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002478 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2479 GFP_KERNEL);
2480 if (!wr->send_wr) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002481 isert_dbg("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002482 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002483 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002484 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002485
2486 wr->isert_cmd = isert_cmd;
2487 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002488
2489 for (i = 0; i < wr->send_wr_num; i++) {
2490 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2491 data_len = min(data_left, rdma_write_max);
2492
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002493 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002494 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2495 send_wr->opcode = IB_WR_RDMA_WRITE;
2496 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2497 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2498 if (i + 1 == wr->send_wr_num)
2499 send_wr->next = &isert_cmd->tx_desc.send_wr;
2500 else
2501 send_wr->next = &wr->send_wr[i + 1];
2502 } else {
2503 send_wr->opcode = IB_WR_RDMA_READ;
2504 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2505 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2506 if (i + 1 == wr->send_wr_num)
2507 send_wr->send_flags = IB_SEND_SIGNALED;
2508 else
2509 send_wr->next = &wr->send_wr[i + 1];
2510 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002511
2512 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2513 send_wr, data_len, offset);
2514 ib_sge += ib_sge_cnt;
2515
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002516 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002517 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002518 data_left -= data_len;
2519 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002520
2521 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002522unmap_cmd:
2523 isert_unmap_data_buf(isert_conn, data);
2524
Vu Pham90ecc6e2013-08-28 23:23:33 +03002525 return ret;
2526}
2527
2528static int
Vu Pham59464ef2013-08-28 23:23:35 +03002529isert_map_fr_pagelist(struct ib_device *ib_dev,
2530 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2531{
2532 u64 start_addr, end_addr, page, chunk_start = 0;
2533 struct scatterlist *tmp_sg;
2534 int i = 0, new_chunk, last_ent, n_pages;
2535
2536 n_pages = 0;
2537 new_chunk = 1;
2538 last_ent = sg_nents - 1;
2539 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2540 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2541 if (new_chunk)
2542 chunk_start = start_addr;
2543 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2544
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002545 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2546 i, (unsigned long long)tmp_sg->dma_address,
2547 tmp_sg->length);
Vu Pham59464ef2013-08-28 23:23:35 +03002548
2549 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2550 new_chunk = 0;
2551 continue;
2552 }
2553 new_chunk = 1;
2554
2555 page = chunk_start & PAGE_MASK;
2556 do {
2557 fr_pl[n_pages++] = page;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002558 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2559 n_pages - 1, page);
Vu Pham59464ef2013-08-28 23:23:35 +03002560 page += PAGE_SIZE;
2561 } while (page < end_addr);
2562 }
2563
2564 return n_pages;
2565}
2566
Sagi Grimberg10633c32014-12-07 13:12:04 +02002567static inline void
2568isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2569{
2570 u32 rkey;
2571
2572 memset(inv_wr, 0, sizeof(*inv_wr));
2573 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2574 inv_wr->opcode = IB_WR_LOCAL_INV;
2575 inv_wr->ex.invalidate_rkey = mr->rkey;
2576
2577 /* Bump the key */
2578 rkey = ib_inc_rkey(mr->rkey);
2579 ib_update_fast_reg_key(mr, rkey);
2580}
2581
Vu Pham59464ef2013-08-28 23:23:35 +03002582static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002583isert_fast_reg_mr(struct isert_conn *isert_conn,
2584 struct fast_reg_descriptor *fr_desc,
2585 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002586 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002587 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002588{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002589 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002590 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002591 struct ib_mr *mr;
2592 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002593 struct ib_send_wr fr_wr, inv_wr;
2594 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002595 int ret, pagelist_len;
2596 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002597
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002598 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002599 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002600 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2601 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002602 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2603 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002604 return 0;
2605 }
2606
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002607 if (ind == ISERT_DATA_KEY_VALID) {
2608 /* Registering data buffer */
2609 mr = fr_desc->data_mr;
2610 frpl = fr_desc->data_frpl;
2611 } else {
2612 /* Registering protection buffer */
2613 mr = fr_desc->pi_ctx->prot_mr;
2614 frpl = fr_desc->pi_ctx->prot_frpl;
2615 }
2616
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002617 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002618
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002619 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002620 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002621
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002622 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002623 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002624
Sagi Grimberg10633c32014-12-07 13:12:04 +02002625 if (!(fr_desc->ind & ind)) {
2626 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002627 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002628 }
2629
2630 /* Prepare FASTREG WR */
2631 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002632 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002633 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002634 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2635 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002636 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2637 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002638 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002639 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002640 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2641
2642 if (!wr)
2643 wr = &fr_wr;
2644 else
2645 wr->next = &fr_wr;
2646
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002647 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002648 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002649 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002650 return ret;
2651 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002652 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002653
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002654 sge->lkey = mr->lkey;
2655 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002656 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002657
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002658 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2659 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002660
2661 return ret;
2662}
2663
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002664static inline void
2665isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2666 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002667{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002668 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002669 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2670 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2671 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002672 /*
2673 * At the moment we hard code those, but if in the future
2674 * the target core would like to use it, we will take it
2675 * from se_cmd.
2676 */
2677 domain->sig.dif.apptag_check_mask = 0xffff;
2678 domain->sig.dif.app_escape = true;
2679 domain->sig.dif.ref_escape = true;
2680 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2681 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2682 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002683};
2684
2685static int
2686isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2687{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002688 switch (se_cmd->prot_op) {
2689 case TARGET_PROT_DIN_INSERT:
2690 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002691 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002692 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002693 break;
2694 case TARGET_PROT_DOUT_INSERT:
2695 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002696 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002697 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002698 break;
2699 case TARGET_PROT_DIN_PASS:
2700 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002701 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2702 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002703 break;
2704 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002705 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002706 return -EINVAL;
2707 }
2708
2709 return 0;
2710}
2711
2712static inline u8
2713isert_set_prot_checks(u8 prot_checks)
2714{
2715 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2716 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2717 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2718}
2719
2720static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002721isert_reg_sig_mr(struct isert_conn *isert_conn,
2722 struct se_cmd *se_cmd,
2723 struct isert_rdma_wr *rdma_wr,
2724 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002725{
2726 struct ib_send_wr sig_wr, inv_wr;
2727 struct ib_send_wr *bad_wr, *wr = NULL;
2728 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2729 struct ib_sig_attrs sig_attrs;
2730 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002731
2732 memset(&sig_attrs, 0, sizeof(sig_attrs));
2733 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2734 if (ret)
2735 goto err;
2736
2737 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2738
2739 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002740 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002741 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002742 }
2743
2744 memset(&sig_wr, 0, sizeof(sig_wr));
2745 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002746 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002747 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002748 sig_wr.num_sge = 1;
2749 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2750 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2751 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2752 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002753 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002754
2755 if (!wr)
2756 wr = &sig_wr;
2757 else
2758 wr->next = &sig_wr;
2759
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002760 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002761 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002762 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002763 goto err;
2764 }
2765 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2766
Sagi Grimberg570db172014-12-02 16:57:31 +02002767 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2768 rdma_wr->ib_sg[SIG].addr = 0;
2769 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002770 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2771 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2772 /*
2773 * We have protection guards on the wire
2774 * so we need to set a larget transfer
2775 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002776 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002777
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002778 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002779 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2780 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002781err:
2782 return ret;
2783}
2784
Vu Pham59464ef2013-08-28 23:23:35 +03002785static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002786isert_handle_prot_cmd(struct isert_conn *isert_conn,
2787 struct isert_cmd *isert_cmd,
2788 struct isert_rdma_wr *wr)
2789{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002790 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002791 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2792 int ret;
2793
2794 if (!wr->fr_desc->pi_ctx) {
2795 ret = isert_create_pi_ctx(wr->fr_desc,
2796 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002797 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002798 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002799 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002800 isert_conn);
2801 return ret;
2802 }
2803 }
2804
2805 if (se_cmd->t_prot_sg) {
2806 ret = isert_map_data_buf(isert_conn, isert_cmd,
2807 se_cmd->t_prot_sg,
2808 se_cmd->t_prot_nents,
2809 se_cmd->prot_length,
2810 0, wr->iser_ib_op, &wr->prot);
2811 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002812 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002813 isert_conn);
2814 return ret;
2815 }
2816
2817 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2818 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2819 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2820 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002821 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002822 isert_conn);
2823 goto unmap_prot_cmd;
2824 }
2825 }
2826
2827 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2828 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002829 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002830 isert_conn);
2831 goto unmap_prot_cmd;
2832 }
2833 wr->fr_desc->ind |= ISERT_PROTECTED;
2834
2835 return 0;
2836
2837unmap_prot_cmd:
2838 if (se_cmd->t_prot_sg)
2839 isert_unmap_data_buf(isert_conn, &wr->prot);
2840
2841 return ret;
2842}
2843
2844static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002845isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2846 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002847{
2848 struct se_cmd *se_cmd = &cmd->se_cmd;
2849 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002850 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002851 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002852 struct ib_send_wr *send_wr;
2853 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002854 u32 offset;
2855 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002856 unsigned long flags;
2857
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002858 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2859
2860 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2861 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2862 se_cmd->t_data_nents, se_cmd->data_length,
2863 offset, wr->iser_ib_op, &wr->data);
2864 if (ret)
2865 return ret;
2866
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002867 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002868 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2869 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002870 struct fast_reg_descriptor, list);
2871 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002872 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002873 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002874 }
2875
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002876 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002877 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002878 if (ret)
2879 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002880
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002881 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002882 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002883 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002884 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002885
Sagi Grimberg570db172014-12-02 16:57:31 +02002886 ib_sg = &wr->ib_sg[SIG];
2887 } else {
2888 ib_sg = &wr->ib_sg[DATA];
2889 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002890
Sagi Grimberg570db172014-12-02 16:57:31 +02002891 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002892 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002893 wr->send_wr_num = 1;
2894 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2895 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002896 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002897
2898 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002899 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002900 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002901 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002902 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2903 send_wr->opcode = IB_WR_RDMA_WRITE;
2904 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2905 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002906 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002907 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002908 } else {
2909 send_wr->opcode = IB_WR_RDMA_READ;
2910 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2911 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2912 send_wr->send_flags = IB_SEND_SIGNALED;
2913 }
2914
Vu Pham59464ef2013-08-28 23:23:35 +03002915 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002916
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002917unmap_cmd:
2918 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002919 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2920 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2921 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002922 }
2923 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002924
Vu Pham59464ef2013-08-28 23:23:35 +03002925 return ret;
2926}
2927
2928static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002929isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2930{
2931 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002932 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002933 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002934 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002935 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002936 struct ib_send_wr *wr_failed;
2937 int rc;
2938
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002939 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002940 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002941
Vu Pham90ecc6e2013-08-28 23:23:33 +03002942 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002943 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002944 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002945 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002946 return rc;
2947 }
2948
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002949 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002950 /*
2951 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2952 */
2953 isert_create_send_desc(isert_conn, isert_cmd,
2954 &isert_cmd->tx_desc);
2955 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2956 &isert_cmd->tx_desc.iscsi_header);
2957 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2958 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002959 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002960 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002961 wr->send_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002962
2963 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2964 if (rc) {
2965 isert_err("ib_post_recv failed with %d\n", rc);
2966 return rc;
2967 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002968 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002969
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002970 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002971 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002972 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002973
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002974 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002975 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002976 "READ\n", isert_cmd);
2977 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002978 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002979 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002980
Vu Pham90ecc6e2013-08-28 23:23:33 +03002981 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002982}
2983
2984static int
2985isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2986{
2987 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002988 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002989 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002990 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002991 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002992 struct ib_send_wr *wr_failed;
2993 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002994
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002995 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002996 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002997 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002998 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002999 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003000 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03003001 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003002 }
3003
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003004 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003005 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003006 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003007
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003008 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03003009 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003010
Vu Pham90ecc6e2013-08-28 23:23:33 +03003011 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003012}
3013
3014static int
3015isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3016{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03003017 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3018 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003019
3020 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03003021 case ISTATE_REMOVE:
3022 spin_lock_bh(&conn->cmd_lock);
3023 list_del_init(&cmd->i_conn_node);
3024 spin_unlock_bh(&conn->cmd_lock);
3025 isert_put_cmd(isert_cmd, true);
3026 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003027 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3028 ret = isert_put_nopin(cmd, conn, false);
3029 break;
3030 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003031 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003032 ret = -EINVAL;
3033 break;
3034 }
3035
3036 return ret;
3037}
3038
3039static int
3040isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3041{
Sagi Grimberg991bb762014-12-07 13:12:01 +02003042 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003043 int ret;
3044
3045 switch (state) {
3046 case ISTATE_SEND_LOGOUTRSP:
3047 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003048 if (!ret)
3049 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003050 break;
3051 case ISTATE_SEND_NOPIN:
3052 ret = isert_put_nopin(cmd, conn, true);
3053 break;
3054 case ISTATE_SEND_TASKMGTRSP:
3055 ret = isert_put_tm_rsp(cmd, conn);
3056 break;
3057 case ISTATE_SEND_REJECT:
3058 ret = isert_put_reject(cmd, conn);
3059 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003060 case ISTATE_SEND_TEXTRSP:
3061 ret = isert_put_text_rsp(cmd, conn);
3062 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003063 case ISTATE_SEND_STATUS:
3064 /*
3065 * Special case for sending non GOOD SCSI status from TX thread
3066 * context during pre se_cmd excecution failure.
3067 */
3068 ret = isert_put_response(conn, cmd);
3069 break;
3070 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003071 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003072 ret = -EINVAL;
3073 break;
3074 }
3075
3076 return ret;
3077}
3078
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003079struct rdma_cm_id *
3080isert_setup_id(struct isert_np *isert_np)
3081{
3082 struct iscsi_np *np = isert_np->np;
3083 struct rdma_cm_id *id;
3084 struct sockaddr *sa;
3085 int ret;
3086
3087 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003088 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003089
3090 id = rdma_create_id(isert_cma_handler, isert_np,
3091 RDMA_PS_TCP, IB_QPT_RC);
3092 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003093 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003094 ret = PTR_ERR(id);
3095 goto out;
3096 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003097 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003098
3099 ret = rdma_bind_addr(id, sa);
3100 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003101 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003102 goto out_id;
3103 }
3104
Sagi Grimberg992607e2015-03-29 15:52:18 +03003105 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003106 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003107 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003108 goto out_id;
3109 }
3110
3111 return id;
3112out_id:
3113 rdma_destroy_id(id);
3114out:
3115 return ERR_PTR(ret);
3116}
3117
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003118static int
3119isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003120 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003121{
3122 struct isert_np *isert_np;
3123 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124 int ret;
3125
3126 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3127 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003128 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003129 return -ENOMEM;
3130 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003131 sema_init(&isert_np->sem, 0);
3132 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003133 INIT_LIST_HEAD(&isert_np->accepted);
3134 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003135 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003136
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003137 /*
3138 * Setup the np->np_sockaddr from the passed sockaddr setup
3139 * in iscsi_target_configfs.c code..
3140 */
3141 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003142 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003143
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003144 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003145 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003146 ret = PTR_ERR(isert_lid);
3147 goto out;
3148 }
3149
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003150 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003151 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003152
3153 return 0;
3154
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003155out:
3156 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003157
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003158 return ret;
3159}
3160
3161static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003162isert_rdma_accept(struct isert_conn *isert_conn)
3163{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003164 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003165 struct rdma_conn_param cp;
3166 int ret;
3167
3168 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003169 cp.initiator_depth = isert_conn->initiator_depth;
3170 cp.retry_count = 7;
3171 cp.rnr_retry_count = 7;
3172
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003173 ret = rdma_accept(cm_id, &cp);
3174 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003175 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003176 return ret;
3177 }
3178
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003179 return 0;
3180}
3181
3182static int
3183isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3184{
Sagi Grimberg67004252015-03-29 15:52:09 +03003185 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003186 int ret;
3187
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003188 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003189 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3190 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003191 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003192 isert_conn);
3193 return ret;
3194 }
3195 reinit_completion(&isert_conn->login_req_comp);
3196
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003197 /*
3198 * For login requests after the first PDU, isert_rx_login_req() will
3199 * kick schedule_delayed_work(&conn->login_work) as the packet is
3200 * received, which turns this callback from iscsi_target_do_login_rx()
3201 * into a NOP.
3202 */
3203 if (!login->first_request)
3204 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003205
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003206 isert_rx_login_req(isert_conn);
3207
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003208 isert_info("before login_comp conn: %p\n", conn);
3209 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003210 if (ret)
3211 return ret;
3212
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003213 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003214
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003215 return 0;
3216}
3217
3218static void
3219isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3220 struct isert_conn *isert_conn)
3221{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003222 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003223 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003224
3225 conn->login_family = np->np_sockaddr.ss_family;
3226
Andy Groverdc58f762015-08-24 10:26:05 -07003227 conn->login_sockaddr = cm_route->addr.dst_addr;
3228 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003229}
3230
3231static int
3232isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3233{
Sagi Grimberg67004252015-03-29 15:52:09 +03003234 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003235 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003236 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003237
3238accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003239 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003240 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003241 return -ENODEV;
3242
3243 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003244 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003245 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003246 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003247 np->np_thread_state);
3248 /**
3249 * No point in stalling here when np_thread
3250 * is in state RESET/SHUTDOWN/EXIT - bail
3251 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003252 return -ENODEV;
3253 }
3254 spin_unlock_bh(&np->np_thread_lock);
3255
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003256 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003257 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003258 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003259 goto accept_wait;
3260 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003261 isert_conn = list_first_entry(&isert_np->pending,
3262 struct isert_conn, node);
3263 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003264 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265
3266 conn->context = isert_conn;
3267 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003268
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003269 isert_set_conn_info(np, conn, isert_conn);
3270
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003271 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003272
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003273 return 0;
3274}
3275
3276static void
3277isert_free_np(struct iscsi_np *np)
3278{
Sagi Grimberg67004252015-03-29 15:52:09 +03003279 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003280 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003281
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003282 if (isert_np->cm_id)
3283 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003284
Sagi Grimberg268e6812014-12-02 16:57:36 +02003285 /*
3286 * FIXME: At this point we don't have a good way to insure
3287 * that at this point we don't have hanging connections that
3288 * completed RDMA establishment but didn't start iscsi login
3289 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003290 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003291 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003292 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003293 if (!list_empty(&isert_np->pending)) {
3294 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003295 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003296 &isert_np->pending,
3297 node) {
3298 isert_info("cleaning isert_conn %p state (%d)\n",
3299 isert_conn, isert_conn->state);
3300 isert_connect_release(isert_conn);
3301 }
3302 }
3303
3304 if (!list_empty(&isert_np->accepted)) {
3305 isert_info("Still have isert accepted connections\n");
3306 list_for_each_entry_safe(isert_conn, n,
3307 &isert_np->accepted,
3308 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003309 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003310 isert_conn, isert_conn->state);
3311 isert_connect_release(isert_conn);
3312 }
3313 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003314 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003315
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003316 np->np_context = NULL;
3317 kfree(isert_np);
3318}
3319
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003320static void isert_release_work(struct work_struct *work)
3321{
3322 struct isert_conn *isert_conn = container_of(work,
3323 struct isert_conn,
3324 release_work);
3325
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003326 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003327
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003328 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003329
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003330 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003331 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003332 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003333
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003334 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003335 isert_put_conn(isert_conn);
3336}
3337
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003338static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003339isert_wait4logout(struct isert_conn *isert_conn)
3340{
3341 struct iscsi_conn *conn = isert_conn->conn;
3342
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003343 isert_info("conn %p\n", isert_conn);
3344
Sagi Grimberg991bb762014-12-07 13:12:01 +02003345 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003346 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003347 wait_for_completion_timeout(&conn->conn_logout_comp,
3348 SECONDS_FOR_LOGOUT_COMP * HZ);
3349 }
3350}
3351
3352static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003353isert_wait4cmds(struct iscsi_conn *conn)
3354{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003355 isert_info("iscsi_conn %p\n", conn);
3356
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003357 if (conn->sess) {
3358 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3359 target_wait_for_sess_cmds(conn->sess->se_sess);
3360 }
3361}
3362
3363static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003364isert_wait4flush(struct isert_conn *isert_conn)
3365{
3366 struct ib_recv_wr *bad_wr;
3367
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003368 isert_info("conn %p\n", isert_conn);
3369
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003370 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003371 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3372 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003373 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003374 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003375 return;
3376 }
3377
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003378 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003379}
3380
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003381/**
3382 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3383 * unsolicitate dataout
3384 * @conn: iscsi connection
3385 *
3386 * We might still have commands that are waiting for unsolicited
3387 * dataouts messages. We must put the extra reference on those
3388 * before blocking on the target_wait_for_session_cmds
3389 */
3390static void
3391isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3392{
3393 struct iscsi_cmd *cmd, *tmp;
3394 static LIST_HEAD(drop_cmd_list);
3395
3396 spin_lock_bh(&conn->cmd_lock);
3397 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3398 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3399 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3400 (cmd->write_data_done < cmd->se_cmd.data_length))
3401 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3402 }
3403 spin_unlock_bh(&conn->cmd_lock);
3404
3405 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3406 list_del_init(&cmd->i_conn_node);
3407 if (cmd->i_state != ISTATE_REMOVE) {
3408 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3409
3410 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3411 isert_put_cmd(isert_cmd, true);
3412 }
3413 }
3414}
3415
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003416static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003417{
3418 struct isert_conn *isert_conn = conn->context;
3419
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003420 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003421
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003422 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003423 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003424 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003425 * into full feature phase..
3426 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003427 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003428 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003429 return;
3430 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003431 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003432 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003433
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003434 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003435 isert_put_unsol_pending_cmds(conn);
3436 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003437 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003438
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003439 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003440}
3441
3442static void isert_free_conn(struct iscsi_conn *conn)
3443{
3444 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003445
Sagi Grimberg524630d2015-06-04 19:49:21 +03003446 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003447 isert_put_conn(isert_conn);
3448}
3449
3450static struct iscsit_transport iser_target_transport = {
3451 .name = "IB/iSER",
3452 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003453 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003454 .owner = THIS_MODULE,
3455 .iscsit_setup_np = isert_setup_np,
3456 .iscsit_accept_np = isert_accept_np,
3457 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003458 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003459 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003460 .iscsit_get_login_rx = isert_get_login_rx,
3461 .iscsit_put_login_tx = isert_put_login_tx,
3462 .iscsit_immediate_queue = isert_immediate_queue,
3463 .iscsit_response_queue = isert_response_queue,
3464 .iscsit_get_dataout = isert_get_dataout,
3465 .iscsit_queue_data_in = isert_put_datain,
3466 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003467 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003468 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003469};
3470
3471static int __init isert_init(void)
3472{
3473 int ret;
3474
Sagi Grimberg631af552015-01-25 19:09:50 +02003475 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3476 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003477 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003478 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003479 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003480 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003481 }
3482
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003483 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3484 WQ_UNBOUND_MAX_ACTIVE);
3485 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003486 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003487 ret = -ENOMEM;
3488 goto destroy_comp_wq;
3489 }
3490
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003491 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003492 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003493
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003494 return 0;
3495
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003496destroy_comp_wq:
3497 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003498
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003499 return ret;
3500}
3501
3502static void __exit isert_exit(void)
3503{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003504 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003505 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003506 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003507 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003508 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003509}
3510
3511MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003512MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003513MODULE_AUTHOR("nab@Linux-iSCSI.org");
3514MODULE_LICENSE("GPL");
3515
3516module_init(isert_init);
3517module_exit(isert_exit);