blob: b58ac25ce19d6b41089880ca85476579eea27837 [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;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001318 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1319 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001320 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001321
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;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001329 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001330
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001331 if (imm_data && imm_data_len == data_len)
1332 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001333 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1334 if (rc < 0) {
1335 return 0;
1336 } else if (rc > 0) {
1337 dump_payload = true;
1338 goto sequence_cmd;
1339 }
1340
1341 if (!imm_data)
1342 return 0;
1343
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001344 if (imm_data_len != data_len) {
1345 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1346 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1347 &rx_desc->data[0], imm_data_len);
1348 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1349 sg_nents, imm_data_len);
1350 } else {
1351 sg_init_table(&isert_cmd->sg, 1);
1352 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1353 cmd->se_cmd.t_data_nents = 1;
1354 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1355 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1356 imm_data_len);
1357 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001358
1359 cmd->write_data_done += imm_data_len;
1360
1361 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1362 spin_lock_bh(&cmd->istate_lock);
1363 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1364 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1365 spin_unlock_bh(&cmd->istate_lock);
1366 }
1367
1368sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001369 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001370
1371 if (!rc && dump_payload == false && unsol_data)
1372 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001373 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001374 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001375
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001376 return 0;
1377}
1378
1379static int
1380isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1381 struct iser_rx_desc *rx_desc, unsigned char *buf)
1382{
1383 struct scatterlist *sg_start;
1384 struct iscsi_conn *conn = isert_conn->conn;
1385 struct iscsi_cmd *cmd = NULL;
1386 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1387 u32 unsol_data_len = ntoh24(hdr->dlength);
1388 int rc, sg_nents, sg_off, page_off;
1389
1390 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1391 if (rc < 0)
1392 return rc;
1393 else if (!cmd)
1394 return 0;
1395 /*
1396 * FIXME: Unexpected unsolicited_data out
1397 */
1398 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001399 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400 dump_stack();
1401 return -1;
1402 }
1403
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001404 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1405 "write_data_done: %u, data_length: %u\n",
1406 unsol_data_len, cmd->write_data_done,
1407 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001408
1409 sg_off = cmd->write_data_done / PAGE_SIZE;
1410 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1411 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1412 page_off = cmd->write_data_done % PAGE_SIZE;
1413 /*
1414 * FIXME: Non page-aligned unsolicited_data out
1415 */
1416 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001417 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001418 dump_stack();
1419 return -1;
1420 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001421 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1422 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1423 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001424
1425 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1426 unsol_data_len);
1427
1428 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1429 if (rc < 0)
1430 return rc;
1431
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001432 /*
1433 * multiple data-outs on the same command can arrive -
1434 * so post the buffer before hand
1435 */
1436 rc = isert_post_recv(isert_conn, rx_desc);
1437 if (rc) {
1438 isert_err("ib_post_recv failed with %d\n", rc);
1439 return rc;
1440 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001441 return 0;
1442}
1443
1444static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001445isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001446 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1447 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001448{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001449 struct iscsi_conn *conn = isert_conn->conn;
1450 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1451 int rc;
1452
1453 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1454 if (rc < 0)
1455 return rc;
1456 /*
1457 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1458 */
1459
1460 return iscsit_process_nop_out(conn, cmd, hdr);
1461}
1462
1463static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001464isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001465 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1466 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001467{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001468 struct iscsi_conn *conn = isert_conn->conn;
1469 u32 payload_length = ntoh24(hdr->dlength);
1470 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001471 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001472
1473 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1474 if (rc < 0)
1475 return rc;
1476
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001477 if (payload_length) {
1478 text_in = kzalloc(payload_length, GFP_KERNEL);
1479 if (!text_in) {
1480 isert_err("Unable to allocate text_in of payload_length: %u\n",
1481 payload_length);
1482 return -ENOMEM;
1483 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001484 }
1485 cmd->text_in_ptr = text_in;
1486
1487 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1488
1489 return iscsit_process_text_cmd(conn, cmd, hdr);
1490}
1491
1492static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001493isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1494 uint32_t read_stag, uint64_t read_va,
1495 uint32_t write_stag, uint64_t write_va)
1496{
1497 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1498 struct iscsi_conn *conn = isert_conn->conn;
1499 struct iscsi_cmd *cmd;
1500 struct isert_cmd *isert_cmd;
1501 int ret = -EINVAL;
1502 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1503
Sagi Grimbergfb140272015-03-29 15:52:08 +03001504 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001505 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001506 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001507 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001508 return 0;
1509 }
1510
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001511 switch (opcode) {
1512 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001513 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001514 if (!cmd)
1515 break;
1516
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001517 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001518 isert_cmd->read_stag = read_stag;
1519 isert_cmd->read_va = read_va;
1520 isert_cmd->write_stag = write_stag;
1521 isert_cmd->write_va = write_va;
1522
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001523 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001524 rx_desc, (unsigned char *)hdr);
1525 break;
1526 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001527 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001528 if (!cmd)
1529 break;
1530
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001531 isert_cmd = iscsit_priv_cmd(cmd);
1532 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001533 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001534 break;
1535 case ISCSI_OP_SCSI_DATA_OUT:
1536 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1537 (unsigned char *)hdr);
1538 break;
1539 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001540 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001541 if (!cmd)
1542 break;
1543
1544 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1545 (unsigned char *)hdr);
1546 break;
1547 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001548 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001549 if (!cmd)
1550 break;
1551
1552 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001553 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001554 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001555 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001556 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001557 else
1558 cmd = isert_allocate_cmd(conn, rx_desc);
1559
1560 if (!cmd)
1561 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001562
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001563 isert_cmd = iscsit_priv_cmd(cmd);
1564 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001565 rx_desc, (struct iscsi_text *)hdr);
1566 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001567 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001568 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001569 dump_stack();
1570 break;
1571 }
1572
1573 return ret;
1574}
1575
1576static void
1577isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1578{
1579 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1580 uint64_t read_va = 0, write_va = 0;
1581 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001582
1583 switch (iser_hdr->flags & 0xF0) {
1584 case ISCSI_CTRL:
1585 if (iser_hdr->flags & ISER_RSV) {
1586 read_stag = be32_to_cpu(iser_hdr->read_stag);
1587 read_va = be64_to_cpu(iser_hdr->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001588 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1589 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001590 }
1591 if (iser_hdr->flags & ISER_WSV) {
1592 write_stag = be32_to_cpu(iser_hdr->write_stag);
1593 write_va = be64_to_cpu(iser_hdr->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001594 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1595 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 }
1597
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001598 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599 break;
1600 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001601 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001602 break;
1603 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001604 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001605 break;
1606 }
1607
Bart Van Assche6c760b32015-10-22 11:14:18 -07001608 isert_rx_opcode(isert_conn, rx_desc,
1609 read_stag, read_va, write_stag, write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001610}
1611
1612static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001613isert_rcv_completion(struct iser_rx_desc *desc,
1614 struct isert_conn *isert_conn,
1615 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001617 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001618 struct iscsi_hdr *hdr;
1619 u64 rx_dma;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001620 int rx_buflen;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621
1622 if ((char *)desc == isert_conn->login_req_buf) {
1623 rx_dma = isert_conn->login_req_dma;
1624 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001625 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001626 rx_dma, rx_buflen);
1627 } else {
1628 rx_dma = desc->dma_addr;
1629 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001630 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001631 rx_dma, rx_buflen);
1632 }
1633
1634 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1635
1636 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001637 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638 hdr->opcode, hdr->itt, hdr->flags,
1639 (int)(xfer_len - ISER_HEADERS_LEN));
1640
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001641 if ((char *)desc == isert_conn->login_req_buf) {
1642 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1643 if (isert_conn->conn) {
1644 struct iscsi_login *login = isert_conn->conn->conn_login;
1645
1646 if (login && !login->first_request)
1647 isert_rx_login_req(isert_conn);
1648 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001649 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001650 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001651 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001652 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001653 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001654 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001655
1656 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1657 DMA_FROM_DEVICE);
1658
1659 isert_conn->post_recv_buf_count--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001660}
1661
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001662static int
1663isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1664 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1665 enum iser_ib_op_code op, struct isert_data_buf *data)
1666{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001667 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001668
1669 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1670 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1671
1672 data->len = length - offset;
1673 data->offset = offset;
1674 data->sg_off = data->offset / PAGE_SIZE;
1675
1676 data->sg = &sg[data->sg_off];
1677 data->nents = min_t(unsigned int, nents - data->sg_off,
1678 ISCSI_ISER_SG_TABLESIZE);
1679 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1680 PAGE_SIZE);
1681
1682 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1683 data->dma_dir);
1684 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001685 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001686 return -EINVAL;
1687 }
1688
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001689 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001690 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001691
1692 return 0;
1693}
1694
1695static void
1696isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1697{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001698 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001699
1700 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1701 memset(data, 0, sizeof(*data));
1702}
1703
1704
1705
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706static void
1707isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1708{
1709 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001710
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001711 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001712
1713 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001714 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001715 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001716 }
1717
Vu Pham90ecc6e2013-08-28 23:23:33 +03001718 if (wr->send_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001719 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001720 kfree(wr->send_wr);
1721 wr->send_wr = NULL;
1722 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001723
Vu Pham90ecc6e2013-08-28 23:23:33 +03001724 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001725 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001726 kfree(wr->ib_sge);
1727 wr->ib_sge = NULL;
1728 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001729}
1730
1731static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001732isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001733{
1734 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001735
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001736 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001737
1738 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001739 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001740 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1741 isert_unmap_data_buf(isert_conn, &wr->prot);
1742 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1743 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001744 spin_lock_bh(&isert_conn->pool_lock);
1745 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1746 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001747 wr->fr_desc = NULL;
1748 }
1749
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001750 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001751 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001752 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001753 }
1754
1755 wr->ib_sge = NULL;
1756 wr->send_wr = NULL;
1757}
1758
1759static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001760isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001761{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001762 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001763 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001764 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001765 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001766 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001767
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001768 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001769
1770 switch (cmd->iscsi_opcode) {
1771 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001772 spin_lock_bh(&conn->cmd_lock);
1773 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001774 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001775 spin_unlock_bh(&conn->cmd_lock);
1776
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001777 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001778 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001779 /*
1780 * Check for special case during comp_err where
1781 * WRITE_PENDING has been handed off from core,
1782 * but requires an extra target_put_sess_cmd()
1783 * before transport_generic_free_cmd() below.
1784 */
1785 if (comp_err &&
1786 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1787 struct se_cmd *se_cmd = &cmd->se_cmd;
1788
Bart Van Asscheafc16602015-04-27 13:52:36 +02001789 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001790 }
1791 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001792
Vu Phamd40945d2013-08-28 23:23:34 +03001793 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001794 transport_generic_free_cmd(&cmd->se_cmd, 0);
1795 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001797 spin_lock_bh(&conn->cmd_lock);
1798 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001799 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001800 spin_unlock_bh(&conn->cmd_lock);
1801
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001802 transport_generic_free_cmd(&cmd->se_cmd, 0);
1803 break;
1804 case ISCSI_OP_REJECT:
1805 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001806 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001807 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1808 /* If the continue bit is on, keep the command alive */
1809 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1810 break;
1811
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001812 spin_lock_bh(&conn->cmd_lock);
1813 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001814 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001815 spin_unlock_bh(&conn->cmd_lock);
1816
1817 /*
1818 * Handle special case for REJECT when iscsi_add_reject*() has
1819 * overwritten the original iscsi_opcode assignment, and the
1820 * associated cmd->se_cmd needs to be released.
1821 */
1822 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001823 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001824 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001825 transport_generic_free_cmd(&cmd->se_cmd, 0);
1826 break;
1827 }
1828 /*
1829 * Fall-through
1830 */
1831 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001832 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001833 break;
1834 }
1835}
1836
1837static void
1838isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1839{
1840 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001841 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001842 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1843 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1844 tx_desc->dma_addr = 0;
1845 }
1846}
1847
1848static void
1849isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001850 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001851{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001852 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001853 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001854 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1855 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1856 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001857 }
1858
1859 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001860 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861}
1862
Sagi Grimberg96b79732014-03-17 12:52:18 +02001863static int
1864isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1865{
1866 struct ib_mr_status mr_status;
1867 int ret;
1868
1869 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1870 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001871 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001872 goto fail_mr_status;
1873 }
1874
1875 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1876 u64 sec_offset_err;
1877 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1878
1879 switch (mr_status.sig_err.err_type) {
1880 case IB_SIG_BAD_GUARD:
1881 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1882 break;
1883 case IB_SIG_BAD_REFTAG:
1884 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1885 break;
1886 case IB_SIG_BAD_APPTAG:
1887 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1888 break;
1889 }
1890 sec_offset_err = mr_status.sig_err.sig_err_offset;
1891 do_div(sec_offset_err, block_size);
1892 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1893
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001894 isert_err("PI error found type %d at sector 0x%llx "
1895 "expected 0x%x vs actual 0x%x\n",
1896 mr_status.sig_err.err_type,
1897 (unsigned long long)se_cmd->bad_sector,
1898 mr_status.sig_err.expected,
1899 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001900 ret = 1;
1901 }
1902
1903fail_mr_status:
1904 return ret;
1905}
1906
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001907static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001908isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1909 struct isert_cmd *isert_cmd)
1910{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001911 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001912 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001913 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001914 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001915 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001916 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001917
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001918 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001919 ret = isert_check_pi_status(se_cmd,
1920 wr->fr_desc->pi_ctx->sig_mr);
1921 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001922 }
1923
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001924 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001925 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001926 if (ret)
1927 transport_send_check_condition_and_sense(se_cmd,
1928 se_cmd->pi_err, 0);
1929 else
1930 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001931}
1932
1933static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001934isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1935 struct isert_cmd *isert_cmd)
1936{
1937 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001938 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001939 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001940 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001941 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001942 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001943
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001944 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001945 ret = isert_check_pi_status(se_cmd,
1946 wr->fr_desc->pi_ctx->sig_mr);
1947 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001948 }
1949
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001950 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001951 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001952 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001953 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001954
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001955 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956 spin_lock_bh(&cmd->istate_lock);
1957 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1958 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1959 spin_unlock_bh(&cmd->istate_lock);
1960
Sagi Grimberg364189f2015-03-29 15:52:03 +03001961 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001962 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001963 transport_send_check_condition_and_sense(se_cmd,
1964 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001965 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001966 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001967 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001968}
1969
1970static void
1971isert_do_control_comp(struct work_struct *work)
1972{
1973 struct isert_cmd *isert_cmd = container_of(work,
1974 struct isert_cmd, comp_work);
1975 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001976 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001977 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001978
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001979 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1980
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001981 switch (cmd->i_state) {
1982 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001983 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001984 case ISTATE_SEND_REJECT: /* FALLTHRU */
1985 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001986 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001987 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1988 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001989 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001991 iscsit_logout_post_handler(cmd, cmd->conn);
1992 break;
1993 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001994 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001995 dump_stack();
1996 break;
1997 }
1998}
1999
2000static void
2001isert_response_completion(struct iser_tx_desc *tx_desc,
2002 struct isert_cmd *isert_cmd,
2003 struct isert_conn *isert_conn,
2004 struct ib_device *ib_dev)
2005{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002006 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002007
2008 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002009 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002010 cmd->i_state == ISTATE_SEND_REJECT ||
2011 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002012 isert_unmap_tx_desc(tx_desc, ib_dev);
2013
2014 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
2015 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2016 return;
2017 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002018
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002019 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002020 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002021}
2022
2023static void
Sagi Grimberg77486812015-03-29 15:52:13 +03002024isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002025 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002027 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002028 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2029 struct isert_rdma_wr *wr;
2030
2031 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002032 isert_unmap_tx_desc(tx_desc, ib_dev);
2033 return;
2034 }
2035 wr = &isert_cmd->rdma_wr;
2036
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002037 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2038
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002040 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002041 isert_response_completion(tx_desc, isert_cmd,
2042 isert_conn, ib_dev);
2043 break;
2044 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02002045 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002046 break;
2047 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002048 isert_completion_rdma_read(tx_desc, isert_cmd);
2049 break;
2050 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002051 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052 dump_stack();
2053 break;
2054 }
2055}
2056
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002057/**
2058 * is_isert_tx_desc() - Indicate if the completion wr_id
2059 * is a TX descriptor or not.
2060 * @isert_conn: iser connection
2061 * @wr_id: completion WR identifier
2062 *
2063 * Since we cannot rely on wc opcode in FLUSH errors
2064 * we must work around it by checking if the wr_id address
2065 * falls in the iser connection rx_descs buffer. If so
2066 * it is an RX descriptor, otherwize it is a TX.
2067 */
2068static inline bool
2069is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002071 void *start = isert_conn->rx_descs;
2072 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002073
2074 if (wr_id >= start && wr_id < start + len)
2075 return false;
2076
2077 return true;
2078}
2079
2080static void
2081isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2082{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002083 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002084 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002085 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002086 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002087 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002088 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002089 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002090 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002092 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2093 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002094 if (!isert_cmd)
2095 isert_unmap_tx_desc(desc, ib_dev);
2096 else
2097 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002098 } else {
2099 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002100 if (!isert_conn->post_recv_buf_count)
2101 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002102 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002103}
2104
2105static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002106isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002107{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108 struct isert_conn *isert_conn;
2109 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002110 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002111
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002112 isert_conn = wc->qp->qp_context;
2113 if (likely(wc->status == IB_WC_SUCCESS)) {
2114 if (wc->opcode == IB_WC_RECV) {
2115 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002116 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002117 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002118 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002119 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002120 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002121 } else {
2122 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002123 isert_err("%s (%d): wr id %llx vend_err %x\n",
2124 ib_wc_status_msg(wc->status), wc->status,
2125 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002126 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002127 isert_dbg("%s (%d): wr id %llx\n",
2128 ib_wc_status_msg(wc->status), wc->status,
2129 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002130
2131 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2132 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002133 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002134}
2135
2136static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002137isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002138{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002139 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002140 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002141 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002142 struct ib_wc *const wcs = comp->wcs;
2143 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002144
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002145 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2146 for (i = 0; i < n; i++)
2147 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002148
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002149 completed += n;
2150 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002151 break;
2152 }
2153
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002154 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002155}
2156
2157static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002158isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002159{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002160 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002161
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002162 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002163}
2164
2165static int
2166isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2167{
2168 struct ib_send_wr *wr_failed;
2169 int ret;
2170
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002171 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2172 if (ret) {
2173 isert_err("ib_post_recv failed with %d\n", ret);
2174 return ret;
2175 }
2176
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002177 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002178 &wr_failed);
2179 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002180 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002181 return ret;
2182 }
2183 return ret;
2184}
2185
2186static int
2187isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2188{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002189 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002190 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002191 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2192 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2193 &isert_cmd->tx_desc.iscsi_header;
2194
2195 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2196 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2197 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2198 /*
2199 * Attach SENSE DATA payload to iSCSI Response PDU
2200 */
2201 if (cmd->se_cmd.sense_buffer &&
2202 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2203 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002204 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002205 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002206 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002207 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002208
2209 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2210 cmd->sense_buffer);
2211 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2212
2213 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2214 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002215 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002216
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002217 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2218 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002219 DMA_TO_DEVICE);
2220
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002221 isert_cmd->pdu_buf_len = pdu_len;
2222 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2223 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002224 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002225 isert_cmd->tx_desc.num_sge = 2;
2226 }
2227
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002228 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002229
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002230 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231
2232 return isert_post_response(isert_conn, isert_cmd);
2233}
2234
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002235static void
2236isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2237{
2238 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002239 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002240 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002241
2242 spin_lock_bh(&conn->cmd_lock);
2243 if (!list_empty(&cmd->i_conn_node))
2244 list_del_init(&cmd->i_conn_node);
2245 spin_unlock_bh(&conn->cmd_lock);
2246
2247 if (cmd->data_direction == DMA_TO_DEVICE)
2248 iscsit_stop_dataout_timer(cmd);
2249
2250 device->unreg_rdma_mem(isert_cmd, isert_conn);
2251}
2252
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002253static enum target_prot_op
2254isert_get_sup_prot_ops(struct iscsi_conn *conn)
2255{
Sagi Grimberg67004252015-03-29 15:52:09 +03002256 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002257 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002258
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002259 if (conn->tpg->tpg_attrib.t10_pi) {
2260 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002261 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002262 isert_conn->pi_support = true;
2263 return TARGET_PROT_ALL;
2264 }
2265 }
2266
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002267 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002268 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002269
2270 return TARGET_PROT_NORMAL;
2271}
2272
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002273static int
2274isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2275 bool nopout_response)
2276{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002277 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002278 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002279 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2280
2281 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2282 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2283 &isert_cmd->tx_desc.iscsi_header,
2284 nopout_response);
2285 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002286 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002287
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002288 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289
2290 return isert_post_response(isert_conn, isert_cmd);
2291}
2292
2293static int
2294isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2295{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002296 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002297 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002298 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2299
2300 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2301 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2302 &isert_cmd->tx_desc.iscsi_header);
2303 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002304 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002306 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002307
2308 return isert_post_response(isert_conn, isert_cmd);
2309}
2310
2311static int
2312isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2313{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002314 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002315 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002316 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2317
2318 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2319 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2320 &isert_cmd->tx_desc.iscsi_header);
2321 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002322 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002323
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002324 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002325
2326 return isert_post_response(isert_conn, isert_cmd);
2327}
2328
2329static int
2330isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2331{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002332 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002333 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002334 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002335 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002336 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002337 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2338 struct iscsi_reject *hdr =
2339 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340
2341 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002342 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002343 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002344
2345 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002346 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002347 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2348 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002349 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2350 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002351 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002352 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002353 isert_cmd->tx_desc.num_sge = 2;
2354
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002355 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002356
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002357 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002358
2359 return isert_post_response(isert_conn, isert_cmd);
2360}
2361
2362static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002363isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2364{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002365 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002366 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002367 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2368 struct iscsi_text_rsp *hdr =
2369 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2370 u32 txt_rsp_len;
2371 int rc;
2372
2373 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002374 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002375 if (rc < 0)
2376 return rc;
2377
2378 txt_rsp_len = rc;
2379 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2380
2381 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002382 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002383 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002384 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2385 void *txt_rsp_buf = cmd->buf_ptr;
2386
2387 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2388 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2389
2390 isert_cmd->pdu_buf_len = txt_rsp_len;
2391 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2392 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002393 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002394 isert_cmd->tx_desc.num_sge = 2;
2395 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002396 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002397
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002398 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002399
2400 return isert_post_response(isert_conn, isert_cmd);
2401}
2402
2403static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002404isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2405 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2406 u32 data_left, u32 offset)
2407{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002408 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002409 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002410 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002411 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412 u32 sg_off, page_off;
2413 int i = 0, sg_nents;
2414
2415 sg_off = offset / PAGE_SIZE;
2416 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2417 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2418 page_off = offset % PAGE_SIZE;
2419
2420 send_wr->sg_list = ib_sge;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002421 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422 /*
2423 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2424 */
2425 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002426 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2427 "page_off: %u\n",
2428 (unsigned long long)tmp_sg->dma_address,
2429 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430
2431 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2432 ib_sge->length = min_t(u32, data_left,
2433 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002434 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002436 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2437 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438 page_off = 0;
2439 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002440 if (!data_left)
2441 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002443 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002444 }
2445
Sagi Grimberg9253e662015-06-04 19:49:19 +03002446 send_wr->num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002447 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002448 send_wr->sg_list, send_wr->num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002449
Sagi Grimberg9253e662015-06-04 19:49:19 +03002450 return send_wr->num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451}
2452
2453static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002454isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2455 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002456{
2457 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002458 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002459 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002460 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002461 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002462 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002463 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2464 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002466 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002467
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002468 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2469 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2470 se_cmd->t_data_nents, se_cmd->data_length,
2471 offset, wr->iser_ib_op, &wr->data);
2472 if (ret)
2473 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002475 data_left = data->len;
2476 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002477
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002478 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002479 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002480 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002481 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002482 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002483 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002484 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002485
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002486 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002487 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2488 GFP_KERNEL);
2489 if (!wr->send_wr) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002490 isert_dbg("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002491 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002492 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002493 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002494
2495 wr->isert_cmd = isert_cmd;
2496 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002497
2498 for (i = 0; i < wr->send_wr_num; i++) {
2499 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2500 data_len = min(data_left, rdma_write_max);
2501
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002502 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002503 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2504 send_wr->opcode = IB_WR_RDMA_WRITE;
2505 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2506 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2507 if (i + 1 == wr->send_wr_num)
2508 send_wr->next = &isert_cmd->tx_desc.send_wr;
2509 else
2510 send_wr->next = &wr->send_wr[i + 1];
2511 } else {
2512 send_wr->opcode = IB_WR_RDMA_READ;
2513 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2514 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2515 if (i + 1 == wr->send_wr_num)
2516 send_wr->send_flags = IB_SEND_SIGNALED;
2517 else
2518 send_wr->next = &wr->send_wr[i + 1];
2519 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002520
2521 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2522 send_wr, data_len, offset);
2523 ib_sge += ib_sge_cnt;
2524
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002525 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002526 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002527 data_left -= data_len;
2528 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002529
2530 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002531unmap_cmd:
2532 isert_unmap_data_buf(isert_conn, data);
2533
Vu Pham90ecc6e2013-08-28 23:23:33 +03002534 return ret;
2535}
2536
2537static int
Vu Pham59464ef2013-08-28 23:23:35 +03002538isert_map_fr_pagelist(struct ib_device *ib_dev,
2539 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2540{
2541 u64 start_addr, end_addr, page, chunk_start = 0;
2542 struct scatterlist *tmp_sg;
2543 int i = 0, new_chunk, last_ent, n_pages;
2544
2545 n_pages = 0;
2546 new_chunk = 1;
2547 last_ent = sg_nents - 1;
2548 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2549 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2550 if (new_chunk)
2551 chunk_start = start_addr;
2552 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2553
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002554 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2555 i, (unsigned long long)tmp_sg->dma_address,
2556 tmp_sg->length);
Vu Pham59464ef2013-08-28 23:23:35 +03002557
2558 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2559 new_chunk = 0;
2560 continue;
2561 }
2562 new_chunk = 1;
2563
2564 page = chunk_start & PAGE_MASK;
2565 do {
2566 fr_pl[n_pages++] = page;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002567 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2568 n_pages - 1, page);
Vu Pham59464ef2013-08-28 23:23:35 +03002569 page += PAGE_SIZE;
2570 } while (page < end_addr);
2571 }
2572
2573 return n_pages;
2574}
2575
Sagi Grimberg10633c32014-12-07 13:12:04 +02002576static inline void
2577isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2578{
2579 u32 rkey;
2580
2581 memset(inv_wr, 0, sizeof(*inv_wr));
2582 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2583 inv_wr->opcode = IB_WR_LOCAL_INV;
2584 inv_wr->ex.invalidate_rkey = mr->rkey;
2585
2586 /* Bump the key */
2587 rkey = ib_inc_rkey(mr->rkey);
2588 ib_update_fast_reg_key(mr, rkey);
2589}
2590
Vu Pham59464ef2013-08-28 23:23:35 +03002591static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002592isert_fast_reg_mr(struct isert_conn *isert_conn,
2593 struct fast_reg_descriptor *fr_desc,
2594 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002595 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002596 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002597{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002598 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002599 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002600 struct ib_mr *mr;
2601 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002602 struct ib_send_wr fr_wr, inv_wr;
2603 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002604 int ret, pagelist_len;
2605 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002606
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002607 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002608 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002609 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2610 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002611 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2612 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002613 return 0;
2614 }
2615
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002616 if (ind == ISERT_DATA_KEY_VALID) {
2617 /* Registering data buffer */
2618 mr = fr_desc->data_mr;
2619 frpl = fr_desc->data_frpl;
2620 } else {
2621 /* Registering protection buffer */
2622 mr = fr_desc->pi_ctx->prot_mr;
2623 frpl = fr_desc->pi_ctx->prot_frpl;
2624 }
2625
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002626 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002627
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002628 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002629 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002630
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002631 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002632 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002633
Sagi Grimberg10633c32014-12-07 13:12:04 +02002634 if (!(fr_desc->ind & ind)) {
2635 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002636 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002637 }
2638
2639 /* Prepare FASTREG WR */
2640 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002641 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002642 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002643 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2644 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002645 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2646 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002647 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002648 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002649 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2650
2651 if (!wr)
2652 wr = &fr_wr;
2653 else
2654 wr->next = &fr_wr;
2655
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002656 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002657 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002658 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002659 return ret;
2660 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002661 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002662
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002663 sge->lkey = mr->lkey;
2664 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002665 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002666
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002667 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2668 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002669
2670 return ret;
2671}
2672
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002673static inline void
2674isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2675 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002676{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002677 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002678 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2679 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2680 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002681 /*
2682 * At the moment we hard code those, but if in the future
2683 * the target core would like to use it, we will take it
2684 * from se_cmd.
2685 */
2686 domain->sig.dif.apptag_check_mask = 0xffff;
2687 domain->sig.dif.app_escape = true;
2688 domain->sig.dif.ref_escape = true;
2689 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2690 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2691 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002692};
2693
2694static int
2695isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2696{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002697 switch (se_cmd->prot_op) {
2698 case TARGET_PROT_DIN_INSERT:
2699 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002700 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002701 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002702 break;
2703 case TARGET_PROT_DOUT_INSERT:
2704 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002705 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002706 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002707 break;
2708 case TARGET_PROT_DIN_PASS:
2709 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002710 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2711 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002712 break;
2713 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002714 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002715 return -EINVAL;
2716 }
2717
2718 return 0;
2719}
2720
2721static inline u8
2722isert_set_prot_checks(u8 prot_checks)
2723{
2724 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2725 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2726 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2727}
2728
2729static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002730isert_reg_sig_mr(struct isert_conn *isert_conn,
2731 struct se_cmd *se_cmd,
2732 struct isert_rdma_wr *rdma_wr,
2733 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002734{
2735 struct ib_send_wr sig_wr, inv_wr;
2736 struct ib_send_wr *bad_wr, *wr = NULL;
2737 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2738 struct ib_sig_attrs sig_attrs;
2739 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002740
2741 memset(&sig_attrs, 0, sizeof(sig_attrs));
2742 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2743 if (ret)
2744 goto err;
2745
2746 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2747
2748 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002749 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002750 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002751 }
2752
2753 memset(&sig_wr, 0, sizeof(sig_wr));
2754 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002755 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002756 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002757 sig_wr.num_sge = 1;
2758 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2759 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2760 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2761 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002762 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002763
2764 if (!wr)
2765 wr = &sig_wr;
2766 else
2767 wr->next = &sig_wr;
2768
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002769 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002770 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002771 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002772 goto err;
2773 }
2774 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2775
Sagi Grimberg570db172014-12-02 16:57:31 +02002776 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2777 rdma_wr->ib_sg[SIG].addr = 0;
2778 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002779 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2780 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2781 /*
2782 * We have protection guards on the wire
2783 * so we need to set a larget transfer
2784 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002785 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002786
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002787 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002788 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2789 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002790err:
2791 return ret;
2792}
2793
Vu Pham59464ef2013-08-28 23:23:35 +03002794static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002795isert_handle_prot_cmd(struct isert_conn *isert_conn,
2796 struct isert_cmd *isert_cmd,
2797 struct isert_rdma_wr *wr)
2798{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002799 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002800 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2801 int ret;
2802
2803 if (!wr->fr_desc->pi_ctx) {
2804 ret = isert_create_pi_ctx(wr->fr_desc,
2805 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002806 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002807 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002808 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002809 isert_conn);
2810 return ret;
2811 }
2812 }
2813
2814 if (se_cmd->t_prot_sg) {
2815 ret = isert_map_data_buf(isert_conn, isert_cmd,
2816 se_cmd->t_prot_sg,
2817 se_cmd->t_prot_nents,
2818 se_cmd->prot_length,
2819 0, wr->iser_ib_op, &wr->prot);
2820 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002821 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002822 isert_conn);
2823 return ret;
2824 }
2825
2826 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2827 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2828 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2829 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002830 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002831 isert_conn);
2832 goto unmap_prot_cmd;
2833 }
2834 }
2835
2836 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2837 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002838 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002839 isert_conn);
2840 goto unmap_prot_cmd;
2841 }
2842 wr->fr_desc->ind |= ISERT_PROTECTED;
2843
2844 return 0;
2845
2846unmap_prot_cmd:
2847 if (se_cmd->t_prot_sg)
2848 isert_unmap_data_buf(isert_conn, &wr->prot);
2849
2850 return ret;
2851}
2852
2853static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002854isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2855 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002856{
2857 struct se_cmd *se_cmd = &cmd->se_cmd;
2858 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002859 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002860 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002861 struct ib_send_wr *send_wr;
2862 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002863 u32 offset;
2864 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002865 unsigned long flags;
2866
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002867 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2868
2869 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2870 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2871 se_cmd->t_data_nents, se_cmd->data_length,
2872 offset, wr->iser_ib_op, &wr->data);
2873 if (ret)
2874 return ret;
2875
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002876 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002877 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2878 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002879 struct fast_reg_descriptor, list);
2880 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002881 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002882 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002883 }
2884
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002885 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002886 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002887 if (ret)
2888 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002889
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002890 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002891 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002892 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002893 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002894
Sagi Grimberg570db172014-12-02 16:57:31 +02002895 ib_sg = &wr->ib_sg[SIG];
2896 } else {
2897 ib_sg = &wr->ib_sg[DATA];
2898 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002899
Sagi Grimberg570db172014-12-02 16:57:31 +02002900 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002901 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002902 wr->send_wr_num = 1;
2903 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2904 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002905 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002906
2907 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002908 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002909 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002910 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002911 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2912 send_wr->opcode = IB_WR_RDMA_WRITE;
2913 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2914 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002915 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002916 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002917 } else {
2918 send_wr->opcode = IB_WR_RDMA_READ;
2919 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2920 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2921 send_wr->send_flags = IB_SEND_SIGNALED;
2922 }
2923
Vu Pham59464ef2013-08-28 23:23:35 +03002924 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002925
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002926unmap_cmd:
2927 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002928 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2929 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2930 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002931 }
2932 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002933
Vu Pham59464ef2013-08-28 23:23:35 +03002934 return ret;
2935}
2936
2937static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002938isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2939{
2940 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002941 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002942 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002943 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002944 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002945 struct ib_send_wr *wr_failed;
2946 int rc;
2947
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002948 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002949 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002950
Vu Pham90ecc6e2013-08-28 23:23:33 +03002951 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002952 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002953 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002954 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002955 return rc;
2956 }
2957
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002958 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002959 /*
2960 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2961 */
2962 isert_create_send_desc(isert_conn, isert_cmd,
2963 &isert_cmd->tx_desc);
2964 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2965 &isert_cmd->tx_desc.iscsi_header);
2966 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2967 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002968 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002969 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002970 wr->send_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002971
2972 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2973 if (rc) {
2974 isert_err("ib_post_recv failed with %d\n", rc);
2975 return rc;
2976 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002977 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002978
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002979 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002980 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002981 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002982
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002983 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002984 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002985 "READ\n", isert_cmd);
2986 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002987 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002988 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002989
Vu Pham90ecc6e2013-08-28 23:23:33 +03002990 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002991}
2992
2993static int
2994isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2995{
2996 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002997 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002998 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002999 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003000 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03003001 struct ib_send_wr *wr_failed;
3002 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003003
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003004 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03003005 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003006 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03003007 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03003008 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003009 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03003010 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003011 }
3012
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003013 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003014 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003015 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003016
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003017 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03003018 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003019
Vu Pham90ecc6e2013-08-28 23:23:33 +03003020 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003021}
3022
3023static int
3024isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3025{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03003026 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3027 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003028
3029 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03003030 case ISTATE_REMOVE:
3031 spin_lock_bh(&conn->cmd_lock);
3032 list_del_init(&cmd->i_conn_node);
3033 spin_unlock_bh(&conn->cmd_lock);
3034 isert_put_cmd(isert_cmd, true);
3035 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003036 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3037 ret = isert_put_nopin(cmd, conn, false);
3038 break;
3039 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003040 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003041 ret = -EINVAL;
3042 break;
3043 }
3044
3045 return ret;
3046}
3047
3048static int
3049isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3050{
Sagi Grimberg991bb762014-12-07 13:12:01 +02003051 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003052 int ret;
3053
3054 switch (state) {
3055 case ISTATE_SEND_LOGOUTRSP:
3056 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003057 if (!ret)
3058 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003059 break;
3060 case ISTATE_SEND_NOPIN:
3061 ret = isert_put_nopin(cmd, conn, true);
3062 break;
3063 case ISTATE_SEND_TASKMGTRSP:
3064 ret = isert_put_tm_rsp(cmd, conn);
3065 break;
3066 case ISTATE_SEND_REJECT:
3067 ret = isert_put_reject(cmd, conn);
3068 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003069 case ISTATE_SEND_TEXTRSP:
3070 ret = isert_put_text_rsp(cmd, conn);
3071 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003072 case ISTATE_SEND_STATUS:
3073 /*
3074 * Special case for sending non GOOD SCSI status from TX thread
3075 * context during pre se_cmd excecution failure.
3076 */
3077 ret = isert_put_response(conn, cmd);
3078 break;
3079 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003080 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003081 ret = -EINVAL;
3082 break;
3083 }
3084
3085 return ret;
3086}
3087
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003088struct rdma_cm_id *
3089isert_setup_id(struct isert_np *isert_np)
3090{
3091 struct iscsi_np *np = isert_np->np;
3092 struct rdma_cm_id *id;
3093 struct sockaddr *sa;
3094 int ret;
3095
3096 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003097 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003098
3099 id = rdma_create_id(isert_cma_handler, isert_np,
3100 RDMA_PS_TCP, IB_QPT_RC);
3101 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003102 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003103 ret = PTR_ERR(id);
3104 goto out;
3105 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003106 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003107
3108 ret = rdma_bind_addr(id, sa);
3109 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003110 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003111 goto out_id;
3112 }
3113
Sagi Grimberg992607e2015-03-29 15:52:18 +03003114 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003115 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003116 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003117 goto out_id;
3118 }
3119
3120 return id;
3121out_id:
3122 rdma_destroy_id(id);
3123out:
3124 return ERR_PTR(ret);
3125}
3126
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003127static int
3128isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003129 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003130{
3131 struct isert_np *isert_np;
3132 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003133 int ret;
3134
3135 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3136 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003137 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003138 return -ENOMEM;
3139 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003140 sema_init(&isert_np->sem, 0);
3141 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003142 INIT_LIST_HEAD(&isert_np->accepted);
3143 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003144 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003145
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003146 /*
3147 * Setup the np->np_sockaddr from the passed sockaddr setup
3148 * in iscsi_target_configfs.c code..
3149 */
3150 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003151 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003152
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003153 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003154 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003155 ret = PTR_ERR(isert_lid);
3156 goto out;
3157 }
3158
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003159 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003160 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003161
3162 return 0;
3163
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003164out:
3165 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003166
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003167 return ret;
3168}
3169
3170static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003171isert_rdma_accept(struct isert_conn *isert_conn)
3172{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003173 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003174 struct rdma_conn_param cp;
3175 int ret;
3176
3177 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003178 cp.initiator_depth = isert_conn->initiator_depth;
3179 cp.retry_count = 7;
3180 cp.rnr_retry_count = 7;
3181
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003182 ret = rdma_accept(cm_id, &cp);
3183 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003184 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003185 return ret;
3186 }
3187
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003188 return 0;
3189}
3190
3191static int
3192isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3193{
Sagi Grimberg67004252015-03-29 15:52:09 +03003194 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003195 int ret;
3196
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003197 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003198 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3199 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003200 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003201 isert_conn);
3202 return ret;
3203 }
3204 reinit_completion(&isert_conn->login_req_comp);
3205
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003206 /*
3207 * For login requests after the first PDU, isert_rx_login_req() will
3208 * kick schedule_delayed_work(&conn->login_work) as the packet is
3209 * received, which turns this callback from iscsi_target_do_login_rx()
3210 * into a NOP.
3211 */
3212 if (!login->first_request)
3213 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003214
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003215 isert_rx_login_req(isert_conn);
3216
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003217 isert_info("before login_comp conn: %p\n", conn);
3218 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003219 if (ret)
3220 return ret;
3221
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003222 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003223
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003224 return 0;
3225}
3226
3227static void
3228isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3229 struct isert_conn *isert_conn)
3230{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003231 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003232 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003233
3234 conn->login_family = np->np_sockaddr.ss_family;
3235
Andy Groverdc58f762015-08-24 10:26:05 -07003236 conn->login_sockaddr = cm_route->addr.dst_addr;
3237 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003238}
3239
3240static int
3241isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3242{
Sagi Grimberg67004252015-03-29 15:52:09 +03003243 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003244 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003245 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003246
3247accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003248 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003249 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003250 return -ENODEV;
3251
3252 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003253 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003254 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003255 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003256 np->np_thread_state);
3257 /**
3258 * No point in stalling here when np_thread
3259 * is in state RESET/SHUTDOWN/EXIT - bail
3260 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003261 return -ENODEV;
3262 }
3263 spin_unlock_bh(&np->np_thread_lock);
3264
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003265 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003266 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003267 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003268 goto accept_wait;
3269 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003270 isert_conn = list_first_entry(&isert_np->pending,
3271 struct isert_conn, node);
3272 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003273 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003274
3275 conn->context = isert_conn;
3276 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003277
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003278 isert_set_conn_info(np, conn, isert_conn);
3279
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003280 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003281
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003282 return 0;
3283}
3284
3285static void
3286isert_free_np(struct iscsi_np *np)
3287{
Sagi Grimberg67004252015-03-29 15:52:09 +03003288 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003289 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003290
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003291 if (isert_np->cm_id)
3292 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003293
Sagi Grimberg268e6812014-12-02 16:57:36 +02003294 /*
3295 * FIXME: At this point we don't have a good way to insure
3296 * that at this point we don't have hanging connections that
3297 * completed RDMA establishment but didn't start iscsi login
3298 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003299 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003300 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003301 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003302 if (!list_empty(&isert_np->pending)) {
3303 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003304 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003305 &isert_np->pending,
3306 node) {
3307 isert_info("cleaning isert_conn %p state (%d)\n",
3308 isert_conn, isert_conn->state);
3309 isert_connect_release(isert_conn);
3310 }
3311 }
3312
3313 if (!list_empty(&isert_np->accepted)) {
3314 isert_info("Still have isert accepted connections\n");
3315 list_for_each_entry_safe(isert_conn, n,
3316 &isert_np->accepted,
3317 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003318 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003319 isert_conn, isert_conn->state);
3320 isert_connect_release(isert_conn);
3321 }
3322 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003323 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003324
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003325 np->np_context = NULL;
3326 kfree(isert_np);
3327}
3328
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003329static void isert_release_work(struct work_struct *work)
3330{
3331 struct isert_conn *isert_conn = container_of(work,
3332 struct isert_conn,
3333 release_work);
3334
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003335 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003336
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003337 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003338
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003339 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003340 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003341 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003342
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003343 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003344 isert_put_conn(isert_conn);
3345}
3346
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003347static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003348isert_wait4logout(struct isert_conn *isert_conn)
3349{
3350 struct iscsi_conn *conn = isert_conn->conn;
3351
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003352 isert_info("conn %p\n", isert_conn);
3353
Sagi Grimberg991bb762014-12-07 13:12:01 +02003354 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003355 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003356 wait_for_completion_timeout(&conn->conn_logout_comp,
3357 SECONDS_FOR_LOGOUT_COMP * HZ);
3358 }
3359}
3360
3361static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003362isert_wait4cmds(struct iscsi_conn *conn)
3363{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003364 isert_info("iscsi_conn %p\n", conn);
3365
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003366 if (conn->sess) {
3367 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3368 target_wait_for_sess_cmds(conn->sess->se_sess);
3369 }
3370}
3371
3372static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003373isert_wait4flush(struct isert_conn *isert_conn)
3374{
3375 struct ib_recv_wr *bad_wr;
3376
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003377 isert_info("conn %p\n", isert_conn);
3378
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003379 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003380 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3381 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003382 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003383 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003384 return;
3385 }
3386
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003387 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003388}
3389
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003390/**
3391 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3392 * unsolicitate dataout
3393 * @conn: iscsi connection
3394 *
3395 * We might still have commands that are waiting for unsolicited
3396 * dataouts messages. We must put the extra reference on those
3397 * before blocking on the target_wait_for_session_cmds
3398 */
3399static void
3400isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3401{
3402 struct iscsi_cmd *cmd, *tmp;
3403 static LIST_HEAD(drop_cmd_list);
3404
3405 spin_lock_bh(&conn->cmd_lock);
3406 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3407 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3408 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3409 (cmd->write_data_done < cmd->se_cmd.data_length))
3410 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3411 }
3412 spin_unlock_bh(&conn->cmd_lock);
3413
3414 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3415 list_del_init(&cmd->i_conn_node);
3416 if (cmd->i_state != ISTATE_REMOVE) {
3417 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3418
3419 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3420 isert_put_cmd(isert_cmd, true);
3421 }
3422 }
3423}
3424
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003425static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003426{
3427 struct isert_conn *isert_conn = conn->context;
3428
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003429 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003430
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003431 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003432 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003433 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003434 * into full feature phase..
3435 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003436 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003437 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003438 return;
3439 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003440 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003441 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003442
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003443 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003444 isert_put_unsol_pending_cmds(conn);
3445 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003446 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003447
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003448 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003449}
3450
3451static void isert_free_conn(struct iscsi_conn *conn)
3452{
3453 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003454
Sagi Grimberg524630d2015-06-04 19:49:21 +03003455 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003456 isert_put_conn(isert_conn);
3457}
3458
3459static struct iscsit_transport iser_target_transport = {
3460 .name = "IB/iSER",
3461 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003462 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003463 .owner = THIS_MODULE,
3464 .iscsit_setup_np = isert_setup_np,
3465 .iscsit_accept_np = isert_accept_np,
3466 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003467 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003468 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003469 .iscsit_get_login_rx = isert_get_login_rx,
3470 .iscsit_put_login_tx = isert_put_login_tx,
3471 .iscsit_immediate_queue = isert_immediate_queue,
3472 .iscsit_response_queue = isert_response_queue,
3473 .iscsit_get_dataout = isert_get_dataout,
3474 .iscsit_queue_data_in = isert_put_datain,
3475 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003476 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003477 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003478};
3479
3480static int __init isert_init(void)
3481{
3482 int ret;
3483
Sagi Grimberg631af552015-01-25 19:09:50 +02003484 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3485 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003486 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003487 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003488 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003489 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003490 }
3491
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003492 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3493 WQ_UNBOUND_MAX_ACTIVE);
3494 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003495 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003496 ret = -ENOMEM;
3497 goto destroy_comp_wq;
3498 }
3499
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003500 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003501 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003502
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003503 return 0;
3504
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003505destroy_comp_wq:
3506 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003507
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003508 return ret;
3509}
3510
3511static void __exit isert_exit(void)
3512{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003513 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003514 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003515 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003516 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003517 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003518}
3519
3520MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003521MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003522MODULE_AUTHOR("nab@Linux-iSCSI.org");
3523MODULE_LICENSE("GPL");
3524
3525module_init(isert_init);
3526module_exit(isert_exit);