blob: dafbd731e90b14bed1c8e9b2e903e96ca57bf8e9 [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
Sagi Grimberg40fc0692015-03-29 15:52:10 +030098static struct isert_comp *
99isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800100{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300101 struct isert_device *device = isert_conn->device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200102 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300103 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800104
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800105 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200106 for (i = 0; i < device->comps_used; i++)
107 if (device->comps[i].active_qps <
108 device->comps[min].active_qps)
109 min = i;
110 comp = &device->comps[min];
111 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300112 mutex_unlock(&device_list_mutex);
113
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200114 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200115 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300116
117 return comp;
118}
119
120static void
121isert_comp_put(struct isert_comp *comp)
122{
123 mutex_lock(&device_list_mutex);
124 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800125 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300126}
127
128static struct ib_qp *
129isert_create_qp(struct isert_conn *isert_conn,
130 struct isert_comp *comp,
131 struct rdma_cm_id *cma_id)
132{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300133 struct isert_device *device = isert_conn->device;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300134 struct ib_qp_init_attr attr;
135 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800136
137 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
138 attr.event_handler = isert_qp_event_callback;
139 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200140 attr.send_cq = comp->cq;
141 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800142 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200143 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200144 attr.cap.max_send_sge = device->ib_device->attrs.max_sge;
145 isert_conn->max_sge = min(device->ib_device->attrs.max_sge,
146 device->ib_device->attrs.max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800147 attr.cap.max_recv_sge = 1;
148 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
149 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200150 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200151 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800152
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300153 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800154 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200155 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300156 return ERR_PTR(ret);
157 }
158
159 return cma_id->qp;
160}
161
162static int
163isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
164{
165 struct isert_comp *comp;
166 int ret;
167
168 comp = isert_comp_get(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300169 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
170 if (IS_ERR(isert_conn->qp)) {
171 ret = PTR_ERR(isert_conn->qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200172 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800173 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800174
175 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200176err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300177 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200178 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800179}
180
181static void
182isert_cq_event_callback(struct ib_event *e, void *context)
183{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200184 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800185}
186
187static int
188isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
189{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300190 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300191 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800192 struct iser_rx_desc *rx_desc;
193 struct ib_sge *rx_sg;
194 u64 dma_addr;
195 int i, j;
196
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300197 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800198 sizeof(struct iser_rx_desc), GFP_KERNEL);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300199 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800200 goto fail;
201
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300202 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800203
204 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
205 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
206 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
207 if (ib_dma_mapping_error(ib_dev, dma_addr))
208 goto dma_map_fail;
209
210 rx_desc->dma_addr = dma_addr;
211
212 rx_sg = &rx_desc->rx_sg;
213 rx_sg->addr = rx_desc->dma_addr;
214 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -0600215 rx_sg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800216 }
217
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800218 return 0;
219
220dma_map_fail:
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300221 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800222 for (j = 0; j < i; j++, rx_desc++) {
223 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
224 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
225 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300226 kfree(isert_conn->rx_descs);
227 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800228fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200229 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
230
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800231 return -ENOMEM;
232}
233
234static void
235isert_free_rx_descriptors(struct isert_conn *isert_conn)
236{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300237 struct ib_device *ib_dev = isert_conn->device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800238 struct iser_rx_desc *rx_desc;
239 int i;
240
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300241 if (!isert_conn->rx_descs)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800242 return;
243
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300244 rx_desc = isert_conn->rx_descs;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800245 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
246 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
247 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
248 }
249
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300250 kfree(isert_conn->rx_descs);
251 isert_conn->rx_descs = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800252}
253
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200254static void isert_cq_work(struct work_struct *);
255static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800256
Sagi Grimberg172369c2015-03-29 15:52:11 +0300257static void
258isert_free_comps(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800259{
Sagi Grimberg172369c2015-03-29 15:52:11 +0300260 int i;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800261
Sagi Grimberg172369c2015-03-29 15:52:11 +0300262 for (i = 0; i < device->comps_used; i++) {
263 struct isert_comp *comp = &device->comps[i];
Vu Pham59464ef2013-08-28 23:23:35 +0300264
Sagi Grimberg172369c2015-03-29 15:52:11 +0300265 if (comp->cq) {
266 cancel_work_sync(&comp->work);
267 ib_destroy_cq(comp->cq);
268 }
Vu Pham59464ef2013-08-28 23:23:35 +0300269 }
Sagi Grimberg172369c2015-03-29 15:52:11 +0300270 kfree(device->comps);
271}
Vu Phamd40945d2013-08-28 23:23:34 +0300272
Sagi Grimberg172369c2015-03-29 15:52:11 +0300273static int
Or Gerlitz4a061b22015-12-18 10:59:46 +0200274isert_alloc_comps(struct isert_device *device)
Sagi Grimberg172369c2015-03-29 15:52:11 +0300275{
276 int i, max_cqe, ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200277
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200278 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
Sagi Grimberg172369c2015-03-29 15:52:11 +0300279 device->ib_device->num_comp_vectors));
280
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200281 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200282 "Fast registration %d pi_capable %d\n",
283 device->comps_used, device->ib_device->name,
284 device->ib_device->num_comp_vectors, device->use_fastreg,
285 device->pi_capable);
286
287 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
288 GFP_KERNEL);
289 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200290 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800291 return -ENOMEM;
292 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800293
Or Gerlitz4a061b22015-12-18 10:59:46 +0200294 max_cqe = min(ISER_MAX_CQ_LEN, device->ib_device->attrs.max_cqe);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300295
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200296 for (i = 0; i < device->comps_used; i++) {
Matan Barak8e372102015-06-11 16:35:21 +0300297 struct ib_cq_init_attr cq_attr = {};
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200298 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800299
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200300 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200301 INIT_WORK(&comp->work, isert_cq_work);
Matan Barak8e372102015-06-11 16:35:21 +0300302 cq_attr.cqe = max_cqe;
303 cq_attr.comp_vector = i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200304 comp->cq = ib_create_cq(device->ib_device,
305 isert_cq_callback,
306 isert_cq_event_callback,
307 (void *)comp,
Matan Barak8e372102015-06-11 16:35:21 +0300308 &cq_attr);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200309 if (IS_ERR(comp->cq)) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300310 isert_err("Unable to allocate cq\n");
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200311 ret = PTR_ERR(comp->cq);
312 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800313 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800314 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800315
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200316 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800317 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800318 goto out_cq;
319 }
320
Sagi Grimberg172369c2015-03-29 15:52:11 +0300321 return 0;
322out_cq:
323 isert_free_comps(device);
324 return ret;
325}
326
327static int
328isert_create_device_ib_res(struct isert_device *device)
329{
Or Gerlitz4a061b22015-12-18 10:59:46 +0200330 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergfd8205e2015-03-29 15:52:12 +0300331 int ret;
Sagi Grimberg172369c2015-03-29 15:52:11 +0300332
Or Gerlitz4a061b22015-12-18 10:59:46 +0200333 isert_dbg("devattr->max_sge: %d\n", ib_dev->attrs.max_sge);
334 isert_dbg("devattr->max_sge_rd: %d\n", ib_dev->attrs.max_sge_rd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300335
336 /* asign function handlers */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200337 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
338 ib_dev->attrs.device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberg172369c2015-03-29 15:52:11 +0300339 device->use_fastreg = 1;
340 device->reg_rdma_mem = isert_reg_rdma;
341 device->unreg_rdma_mem = isert_unreg_rdma;
342 } else {
343 device->use_fastreg = 0;
344 device->reg_rdma_mem = isert_map_rdma;
345 device->unreg_rdma_mem = isert_unmap_cmd;
346 }
347
Or Gerlitz4a061b22015-12-18 10:59:46 +0200348 ret = isert_alloc_comps(device);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300349 if (ret)
350 return ret;
351
Or Gerlitz4a061b22015-12-18 10:59:46 +0200352 device->pd = ib_alloc_pd(ib_dev);
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300353 if (IS_ERR(device->pd)) {
354 ret = PTR_ERR(device->pd);
355 isert_err("failed to allocate pd, device %p, ret=%d\n",
356 device, ret);
357 goto out_cq;
358 }
359
Sagi Grimberg172369c2015-03-29 15:52:11 +0300360 /* Check signature cap */
Or Gerlitz4a061b22015-12-18 10:59:46 +0200361 device->pi_capable = ib_dev->attrs.device_cap_flags &
Sagi Grimberg172369c2015-03-29 15:52:11 +0300362 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300363
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800364 return 0;
365
366out_cq:
Sagi Grimberg172369c2015-03-29 15:52:11 +0300367 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800368 return ret;
369}
370
371static void
372isert_free_device_ib_res(struct isert_device *device)
373{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200374 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800375
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300376 ib_dealloc_pd(device->pd);
Sagi Grimberg172369c2015-03-29 15:52:11 +0300377 isert_free_comps(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800378}
379
380static void
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300381isert_device_put(struct isert_device *device)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800382{
383 mutex_lock(&device_list_mutex);
384 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200385 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800386 if (!device->refcount) {
387 isert_free_device_ib_res(device);
388 list_del(&device->dev_node);
389 kfree(device);
390 }
391 mutex_unlock(&device_list_mutex);
392}
393
394static struct isert_device *
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300395isert_device_get(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800396{
397 struct isert_device *device;
398 int ret;
399
400 mutex_lock(&device_list_mutex);
401 list_for_each_entry(device, &device_list, dev_node) {
402 if (device->ib_device->node_guid == cma_id->device->node_guid) {
403 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200404 isert_info("Found iser device %p refcount %d\n",
405 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800406 mutex_unlock(&device_list_mutex);
407 return device;
408 }
409 }
410
411 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
412 if (!device) {
413 mutex_unlock(&device_list_mutex);
414 return ERR_PTR(-ENOMEM);
415 }
416
417 INIT_LIST_HEAD(&device->dev_node);
418
419 device->ib_device = cma_id->device;
420 ret = isert_create_device_ib_res(device);
421 if (ret) {
422 kfree(device);
423 mutex_unlock(&device_list_mutex);
424 return ERR_PTR(ret);
425 }
426
427 device->refcount++;
428 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200429 isert_info("Created a new iser device %p refcount %d\n",
430 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800431 mutex_unlock(&device_list_mutex);
432
433 return device;
434}
435
Vu Pham59464ef2013-08-28 23:23:35 +0300436static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200437isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300438{
439 struct fast_reg_descriptor *fr_desc, *tmp;
440 int i = 0;
441
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300442 if (list_empty(&isert_conn->fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300443 return;
444
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200445 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300446
447 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300448 &isert_conn->fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300449 list_del(&fr_desc->list);
Vu Pham59464ef2013-08-28 23:23:35 +0300450 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200451 if (fr_desc->pi_ctx) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200452 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300453 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200454 kfree(fr_desc->pi_ctx);
455 }
Vu Pham59464ef2013-08-28 23:23:35 +0300456 kfree(fr_desc);
457 ++i;
458 }
459
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300460 if (i < isert_conn->fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200461 isert_warn("Pool still has %d regions registered\n",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300462 isert_conn->fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300463}
464
465static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200466isert_create_pi_ctx(struct fast_reg_descriptor *desc,
467 struct ib_device *device,
468 struct ib_pd *pd)
469{
Sagi Grimberg570db172014-12-02 16:57:31 +0200470 struct pi_context *pi_ctx;
471 int ret;
472
473 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
474 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200475 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200476 return -ENOMEM;
477 }
478
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300479 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
480 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimberg570db172014-12-02 16:57:31 +0200481 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200482 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200483 PTR_ERR(pi_ctx->prot_mr));
484 ret = PTR_ERR(pi_ctx->prot_mr);
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300485 goto err_pi_ctx;
Sagi Grimberg570db172014-12-02 16:57:31 +0200486 }
487 desc->ind |= ISERT_PROT_KEY_VALID;
488
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300489 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg570db172014-12-02 16:57:31 +0200490 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200491 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200492 PTR_ERR(pi_ctx->sig_mr));
493 ret = PTR_ERR(pi_ctx->sig_mr);
494 goto err_prot_mr;
495 }
496
497 desc->pi_ctx = pi_ctx;
498 desc->ind |= ISERT_SIG_KEY_VALID;
499 desc->ind &= ~ISERT_PROTECTED;
500
501 return 0;
502
503err_prot_mr:
Roland Dreierb2feda42015-05-29 23:12:10 -0700504 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg570db172014-12-02 16:57:31 +0200505err_pi_ctx:
Roland Dreierb2feda42015-05-29 23:12:10 -0700506 kfree(pi_ctx);
Sagi Grimberg570db172014-12-02 16:57:31 +0200507
508 return ret;
509}
510
511static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200512isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200513 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200514{
Sagi Grimberga89be2c2015-07-30 10:32:37 +0300515 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
516 ISCSI_ISER_SG_TABLESIZE);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200517 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200518 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200519 PTR_ERR(fr_desc->data_mr));
Sagi Grimberg16c2d702015-10-13 19:11:34 +0300520 return PTR_ERR(fr_desc->data_mr);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200521 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200522 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200523
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200524 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200525
526 return 0;
527}
528
529static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200530isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300531{
532 struct fast_reg_descriptor *fr_desc;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300533 struct isert_device *device = isert_conn->device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700534 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
535 struct se_node_acl *se_nacl = se_sess->se_node_acl;
536 int i, ret, tag_num;
537 /*
538 * Setup the number of FRMRs based upon the number of tags
539 * available to session in iscsi_target_locate_portal().
540 */
541 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
542 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300543
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300544 isert_conn->fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700545 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300546 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
547 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200548 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300549 ret = -ENOMEM;
550 goto err;
551 }
552
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200553 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300554 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200555 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200556 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200557 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800558 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300559 goto err;
560 }
561
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300562 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
563 isert_conn->fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300564 }
565
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200566 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300567 isert_conn, isert_conn->fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300568
569 return 0;
570
571err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200572 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300573 return ret;
574}
575
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300576static void
577isert_init_conn(struct isert_conn *isert_conn)
578{
579 isert_conn->state = ISER_CONN_INIT;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300580 INIT_LIST_HEAD(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300581 init_completion(&isert_conn->login_comp);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300582 init_completion(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300583 init_completion(&isert_conn->wait);
584 kref_init(&isert_conn->kref);
585 mutex_init(&isert_conn->mutex);
586 spin_lock_init(&isert_conn->pool_lock);
587 INIT_LIST_HEAD(&isert_conn->fr_pool);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300588 INIT_WORK(&isert_conn->release_work, isert_release_work);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300589}
590
591static void
592isert_free_login_buf(struct isert_conn *isert_conn)
593{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300594 struct ib_device *ib_dev = isert_conn->device->ib_device;
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300595
596 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
597 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
598 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
599 ISCSI_DEF_MAX_RECV_SEG_LEN,
600 DMA_FROM_DEVICE);
601 kfree(isert_conn->login_buf);
602}
603
604static int
605isert_alloc_login_buf(struct isert_conn *isert_conn,
606 struct ib_device *ib_dev)
607{
608 int ret;
609
610 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
611 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
612 if (!isert_conn->login_buf) {
613 isert_err("Unable to allocate isert_conn->login_buf\n");
614 return -ENOMEM;
615 }
616
617 isert_conn->login_req_buf = isert_conn->login_buf;
618 isert_conn->login_rsp_buf = isert_conn->login_buf +
619 ISCSI_DEF_MAX_RECV_SEG_LEN;
620
621 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
622 isert_conn->login_buf, isert_conn->login_req_buf,
623 isert_conn->login_rsp_buf);
624
625 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
626 (void *)isert_conn->login_req_buf,
627 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
628
629 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
630 if (ret) {
631 isert_err("login_req_dma mapping error: %d\n", ret);
632 isert_conn->login_req_dma = 0;
633 goto out_login_buf;
634 }
635
636 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
637 (void *)isert_conn->login_rsp_buf,
638 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
639
640 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
641 if (ret) {
642 isert_err("login_rsp_dma mapping error: %d\n", ret);
643 isert_conn->login_rsp_dma = 0;
644 goto out_req_dma_map;
645 }
646
647 return 0;
648
649out_req_dma_map:
650 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
651 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
652out_login_buf:
653 kfree(isert_conn->login_buf);
654 return ret;
655}
656
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800657static int
658isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
659{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200660 struct isert_np *isert_np = cma_id->context;
661 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800662 struct isert_conn *isert_conn;
663 struct isert_device *device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800664 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300665
666 spin_lock_bh(&np->np_thread_lock);
667 if (!np->enabled) {
668 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200669 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300670 return rdma_reject(cma_id, NULL, 0);
671 }
672 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800673
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200674 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800675 cma_id, cma_id->context);
676
677 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300678 if (!isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800679 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800680
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300681 isert_init_conn(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300682 isert_conn->cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800683
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300684 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
685 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 goto out;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800687
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300688 device = isert_device_get(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800689 if (IS_ERR(device)) {
690 ret = PTR_ERR(device);
691 goto out_rsp_dma_map;
692 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300693 isert_conn->device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800694
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300695 /* Set max inflight RDMA READ requests */
696 isert_conn->initiator_depth = min_t(u8,
697 event->param.conn.initiator_depth,
Or Gerlitz4a061b22015-12-18 10:59:46 +0200698 device->ib_device->attrs.max_qp_init_rd_atom);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200699 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300700
Sagi Grimberg570db172014-12-02 16:57:31 +0200701 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800702 if (ret)
703 goto out_conn_dev;
704
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200705 ret = isert_rdma_post_recvl(isert_conn);
706 if (ret)
707 goto out_conn_dev;
708
709 ret = isert_rdma_accept(isert_conn);
710 if (ret)
711 goto out_conn_dev;
712
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300713 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300714 list_add_tail(&isert_conn->node, &isert_np->accepted);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300715 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800716
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800717 return 0;
718
719out_conn_dev:
Sagi Grimbergcf8ae952015-03-29 15:52:14 +0300720 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800721out_rsp_dma_map:
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300722 isert_free_login_buf(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800723out:
724 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200725 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800726 return ret;
727}
728
729static void
730isert_connect_release(struct isert_conn *isert_conn)
731{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300732 struct isert_device *device = isert_conn->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800733
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200734 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800735
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300736 BUG_ON(!device);
737
738 if (device->use_fastreg)
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200739 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300740
Sagi Grimberg19e20902014-12-02 16:57:26 +0200741 isert_free_rx_descriptors(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300742 if (isert_conn->cm_id)
743 rdma_destroy_id(isert_conn->cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200744
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300745 if (isert_conn->qp) {
746 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200747
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300748 isert_comp_put(comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300749 ib_destroy_qp(isert_conn->qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800750 }
751
Sagi Grimbergae9ea9e2015-03-29 15:52:15 +0300752 if (isert_conn->login_buf)
753 isert_free_login_buf(isert_conn);
754
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300755 isert_device_put(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756
Sagi Grimberg57df81e2015-03-29 15:52:17 +0300757 kfree(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800758}
759
760static void
761isert_connected_handler(struct rdma_cm_id *cma_id)
762{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200763 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300764 struct isert_np *isert_np = cma_id->context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300765
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200766 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200767
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300768 mutex_lock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300769 isert_conn->state = ISER_CONN_UP;
770 kref_get(&isert_conn->kref);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300771 mutex_unlock(&isert_conn->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300772
773 mutex_lock(&isert_np->mutex);
774 list_move_tail(&isert_conn->node, &isert_np->pending);
775 mutex_unlock(&isert_np->mutex);
776
777 isert_info("np %p: Allow accept_np to continue\n", isert_np);
778 up(&isert_np->sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800779}
780
781static void
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300782isert_release_kref(struct kref *kref)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800783{
784 struct isert_conn *isert_conn = container_of(kref,
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300785 struct isert_conn, kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800786
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200787 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
788 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800789
790 isert_connect_release(isert_conn);
791}
792
793static void
794isert_put_conn(struct isert_conn *isert_conn)
795{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300796 kref_put(&isert_conn->kref, isert_release_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800797}
798
Sagi Grimberg954f2372014-12-02 16:57:17 +0200799/**
800 * isert_conn_terminate() - Initiate connection termination
801 * @isert_conn: isert connection struct
802 *
803 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200804 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200805 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200806 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200807 *
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300808 * This routine must be called with mutex held. Thus it is
Sagi Grimberg954f2372014-12-02 16:57:17 +0200809 * safe to call multiple times.
810 */
811static void
812isert_conn_terminate(struct isert_conn *isert_conn)
813{
814 int err;
815
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200816 switch (isert_conn->state) {
817 case ISER_CONN_TERMINATING:
818 break;
819 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200820 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200821 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200822 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200823 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300824 err = rdma_disconnect(isert_conn->cm_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200825 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200826 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200827 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200828 break;
829 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200830 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200831 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200832 }
833}
834
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700835static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200836isert_np_cma_handler(struct isert_np *isert_np,
837 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800838{
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300839 isert_dbg("%s (%d): isert np %p\n",
840 rdma_event_msg(event), event, isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200841
842 switch (event) {
843 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300844 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200845 break;
846 case RDMA_CM_EVENT_ADDR_CHANGE:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300847 isert_np->cm_id = isert_setup_id(isert_np);
848 if (IS_ERR(isert_np->cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200849 isert_err("isert np %p setup id failed: %ld\n",
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300850 isert_np, PTR_ERR(isert_np->cm_id));
851 isert_np->cm_id = NULL;
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200852 }
853 break;
854 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200855 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200856 isert_np, event);
857 }
858
859 return -1;
860}
861
862static int
863isert_disconnected_handler(struct rdma_cm_id *cma_id,
864 enum rdma_cm_event_type event)
865{
866 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700867 struct isert_conn *isert_conn;
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300868 bool terminating = false;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700869
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300870 if (isert_np->cm_id == cma_id)
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200871 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700872
Sagi Grimberg19e20902014-12-02 16:57:26 +0200873 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800874
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300875 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300876 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200877 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300878 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200879
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300880 isert_info("conn %p completing wait\n", isert_conn);
881 complete(&isert_conn->wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700882
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300883 if (terminating)
884 goto out;
885
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300886 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300887 if (!list_empty(&isert_conn->node)) {
888 list_del_init(&isert_conn->node);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300889 isert_put_conn(isert_conn);
890 queue_work(isert_release_wq, &isert_conn->release_work);
891 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +0300892 mutex_unlock(&isert_np->mutex);
Sagi Grimberg2f1b6b72015-06-04 19:49:20 +0300893
894out:
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700895 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800896}
897
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300898static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200899isert_connect_error(struct rdma_cm_id *cma_id)
900{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200901 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200902
Jenny Derzhavetzbd379222015-09-06 14:52:24 +0300903 list_del_init(&isert_conn->node);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300904 isert_conn->cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200905 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300906
907 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200908}
909
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800910static int
911isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
912{
913 int ret = 0;
914
Sagi Grimbergea8a1612015-05-18 13:40:31 +0300915 isert_info("%s (%d): status %d id %p np %p\n",
916 rdma_event_msg(event->event), event->event,
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200917 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800918
919 switch (event->event) {
920 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800921 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700922 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200923 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800924 break;
925 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800926 isert_connected_handler(cma_id);
927 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300928 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
929 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
930 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300931 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200932 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200934 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
935 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800936 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300937 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200938 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800939 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200940 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800941 break;
942 }
943
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800944 return ret;
945}
946
947static int
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300948isert_post_recvm(struct isert_conn *isert_conn, u32 count)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800949{
950 struct ib_recv_wr *rx_wr, *rx_wr_failed;
951 int i, ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800952 struct iser_rx_desc *rx_desc;
953
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300954 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300955 rx_desc = &isert_conn->rx_descs[i];
956 rx_wr->wr_id = (uintptr_t)rx_desc;
957 rx_wr->sg_list = &rx_desc->rx_sg;
958 rx_wr->num_sge = 1;
959 rx_wr->next = rx_wr + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800960 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800961 rx_wr--;
962 rx_wr->next = NULL; /* mark end of work requests list */
963
964 isert_conn->post_recv_buf_count += count;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300965 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300966 &rx_wr_failed);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800967 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200968 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969 isert_conn->post_recv_buf_count -= count;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800970 }
Jenny Derzhavetz4366b192015-09-06 14:52:25 +0300971
972 return ret;
973}
974
975static int
976isert_post_recv(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc)
977{
978 struct ib_recv_wr *rx_wr_failed, rx_wr;
979 int ret;
980
981 rx_wr.wr_id = (uintptr_t)rx_desc;
982 rx_wr.sg_list = &rx_desc->rx_sg;
983 rx_wr.num_sge = 1;
984 rx_wr.next = NULL;
985
986 isert_conn->post_recv_buf_count++;
987 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_failed);
988 if (ret) {
989 isert_err("ib_post_recv() failed with ret: %d\n", ret);
990 isert_conn->post_recv_buf_count--;
991 }
992
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800993 return ret;
994}
995
996static int
997isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
998{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +0300999 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001000 struct ib_send_wr send_wr, *send_wr_failed;
1001 int ret;
1002
1003 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1004 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1005
1006 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001007 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001008 send_wr.sg_list = tx_desc->tx_sg;
1009 send_wr.num_sge = tx_desc->num_sge;
1010 send_wr.opcode = IB_WR_SEND;
1011 send_wr.send_flags = IB_SEND_SIGNALED;
1012
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001013 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001014 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001015 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001016
1017 return ret;
1018}
1019
1020static void
1021isert_create_send_desc(struct isert_conn *isert_conn,
1022 struct isert_cmd *isert_cmd,
1023 struct iser_tx_desc *tx_desc)
1024{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001025 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001026 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001027
1028 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1029 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1030
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001031 memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
1032 tx_desc->iser_header.flags = ISCSI_CTRL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033
1034 tx_desc->num_sge = 1;
1035 tx_desc->isert_cmd = isert_cmd;
1036
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001037 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1038 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001039 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001040 }
1041}
1042
1043static int
1044isert_init_tx_hdrs(struct isert_conn *isert_conn,
1045 struct iser_tx_desc *tx_desc)
1046{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001047 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001048 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001049 u64 dma_addr;
1050
1051 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1052 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1053 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001054 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001055 return -ENOMEM;
1056 }
1057
1058 tx_desc->dma_addr = dma_addr;
1059 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1060 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001061 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001062
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001063 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1064 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1065 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001066
1067 return 0;
1068}
1069
1070static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001071isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001072 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001073{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001074 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1075
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001076 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001077 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001078 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001079 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001080 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001081 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001082}
1083
1084static int
1085isert_rdma_post_recvl(struct isert_conn *isert_conn)
1086{
1087 struct ib_recv_wr rx_wr, *rx_wr_fail;
1088 struct ib_sge sge;
1089 int ret;
1090
1091 memset(&sge, 0, sizeof(struct ib_sge));
1092 sge.addr = isert_conn->login_req_dma;
1093 sge.length = ISER_RX_LOGIN_SIZE;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001094 sge.lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001095
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001096 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001097 sge.addr, sge.length, sge.lkey);
1098
1099 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001100 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001101 rx_wr.sg_list = &sge;
1102 rx_wr.num_sge = 1;
1103
1104 isert_conn->post_recv_buf_count++;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001105 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001106 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001107 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001108 isert_conn->post_recv_buf_count--;
1109 }
1110
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001111 return ret;
1112}
1113
1114static int
1115isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1116 u32 length)
1117{
1118 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001119 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001120 struct ib_device *ib_dev = device->ib_device;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001121 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001122 int ret;
1123
1124 isert_create_send_desc(isert_conn, NULL, tx_desc);
1125
1126 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1127 sizeof(struct iscsi_hdr));
1128
1129 isert_init_tx_hdrs(isert_conn, tx_desc);
1130
1131 if (length > 0) {
1132 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1133
1134 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1135 length, DMA_TO_DEVICE);
1136
1137 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1138
1139 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1140 length, DMA_TO_DEVICE);
1141
1142 tx_dsg->addr = isert_conn->login_rsp_dma;
1143 tx_dsg->length = length;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06001144 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001145 tx_desc->num_sge = 2;
1146 }
1147 if (!login->login_failed) {
1148 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001149 if (!conn->sess->sess_ops->SessionType &&
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001150 isert_conn->device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001151 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001152 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001153 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001154 " fastreg pool\n", isert_conn);
1155 return ret;
1156 }
1157 }
1158
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001159 ret = isert_alloc_rx_descriptors(isert_conn);
1160 if (ret)
1161 return ret;
1162
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001163 ret = isert_post_recvm(isert_conn,
1164 ISERT_QP_MAX_RECV_DTOS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001165 if (ret)
1166 return ret;
1167
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001168 /* Now we are in FULL_FEATURE phase */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001169 mutex_lock(&isert_conn->mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001170 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001171 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001172 goto post_send;
1173 }
1174
1175 ret = isert_rdma_post_recvl(isert_conn);
1176 if (ret)
1177 return ret;
1178 }
1179post_send:
1180 ret = isert_post_send(isert_conn, tx_desc);
1181 if (ret)
1182 return ret;
1183
1184 return 0;
1185}
1186
1187static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001188isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001189{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001190 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1191 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001192 struct iscsi_conn *conn = isert_conn->conn;
1193 struct iscsi_login *login = conn->conn_login;
1194 int size;
1195
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001196 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001197
1198 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001199
1200 if (login->first_request) {
1201 struct iscsi_login_req *login_req =
1202 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1203 /*
1204 * Setup the initial iscsi_login values from the leading
1205 * login request PDU.
1206 */
1207 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1208 login->current_stage =
1209 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1210 >> 2;
1211 login->version_min = login_req->min_version;
1212 login->version_max = login_req->max_version;
1213 memcpy(login->isid, login_req->isid, 6);
1214 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1215 login->init_task_tag = login_req->itt;
1216 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1217 login->cid = be16_to_cpu(login_req->cid);
1218 login->tsih = be16_to_cpu(login_req->tsih);
1219 }
1220
1221 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1222
1223 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001224 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1225 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1226 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227 memcpy(login->req_buf, &rx_desc->data[0], size);
1228
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001229 if (login->first_request) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001230 complete(&isert_conn->login_comp);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001231 return;
1232 }
1233 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001234}
1235
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001236static struct iscsi_cmd
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001237*isert_allocate_cmd(struct iscsi_conn *conn, struct iser_rx_desc *rx_desc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001238{
Sagi Grimberg67004252015-03-29 15:52:09 +03001239 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001240 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001241 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001242
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001243 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001244 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001245 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001246 return NULL;
1247 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001248 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001249 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001250 isert_cmd->iscsi_cmd = cmd;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001251 isert_cmd->rx_desc = rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001253 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254}
1255
1256static int
1257isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001258 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1259 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001260{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001261 struct iscsi_conn *conn = isert_conn->conn;
1262 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001263 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1264 bool dump_payload = false;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001265 unsigned int data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001266
1267 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1268 if (rc < 0)
1269 return rc;
1270
1271 imm_data = cmd->immediate_data;
1272 imm_data_len = cmd->first_burst_len;
1273 unsol_data = cmd->unsolicited_data;
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001274 data_len = cmd->se_cmd.data_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001275
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001276 if (imm_data && imm_data_len == data_len)
1277 cmd->se_cmd.se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001278 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1279 if (rc < 0) {
1280 return 0;
1281 } else if (rc > 0) {
1282 dump_payload = true;
1283 goto sequence_cmd;
1284 }
1285
1286 if (!imm_data)
1287 return 0;
1288
Jenny Derzhavetz9fd60082015-09-06 14:52:26 +03001289 if (imm_data_len != data_len) {
1290 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1291 sg_copy_from_buffer(cmd->se_cmd.t_data_sg, sg_nents,
1292 &rx_desc->data[0], imm_data_len);
1293 isert_dbg("Copy Immediate sg_nents: %u imm_data_len: %d\n",
1294 sg_nents, imm_data_len);
1295 } else {
1296 sg_init_table(&isert_cmd->sg, 1);
1297 cmd->se_cmd.t_data_sg = &isert_cmd->sg;
1298 cmd->se_cmd.t_data_nents = 1;
1299 sg_set_buf(&isert_cmd->sg, &rx_desc->data[0], imm_data_len);
1300 isert_dbg("Transfer Immediate imm_data_len: %d\n",
1301 imm_data_len);
1302 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001303
1304 cmd->write_data_done += imm_data_len;
1305
1306 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1307 spin_lock_bh(&cmd->istate_lock);
1308 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1309 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1310 spin_unlock_bh(&cmd->istate_lock);
1311 }
1312
1313sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001314 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001315
1316 if (!rc && dump_payload == false && unsol_data)
1317 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001318 else if (dump_payload && imm_data)
Bart Van Asscheafc16602015-04-27 13:52:36 +02001319 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001320
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001321 return 0;
1322}
1323
1324static int
1325isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1326 struct iser_rx_desc *rx_desc, unsigned char *buf)
1327{
1328 struct scatterlist *sg_start;
1329 struct iscsi_conn *conn = isert_conn->conn;
1330 struct iscsi_cmd *cmd = NULL;
1331 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1332 u32 unsol_data_len = ntoh24(hdr->dlength);
1333 int rc, sg_nents, sg_off, page_off;
1334
1335 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1336 if (rc < 0)
1337 return rc;
1338 else if (!cmd)
1339 return 0;
1340 /*
1341 * FIXME: Unexpected unsolicited_data out
1342 */
1343 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001344 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001345 dump_stack();
1346 return -1;
1347 }
1348
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001349 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1350 "write_data_done: %u, data_length: %u\n",
1351 unsol_data_len, cmd->write_data_done,
1352 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001353
1354 sg_off = cmd->write_data_done / PAGE_SIZE;
1355 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1356 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1357 page_off = cmd->write_data_done % PAGE_SIZE;
1358 /*
1359 * FIXME: Non page-aligned unsolicited_data out
1360 */
1361 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001362 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001363 dump_stack();
1364 return -1;
1365 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001366 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1367 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1368 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001369
1370 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1371 unsol_data_len);
1372
1373 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1374 if (rc < 0)
1375 return rc;
1376
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001377 /*
1378 * multiple data-outs on the same command can arrive -
1379 * so post the buffer before hand
1380 */
1381 rc = isert_post_recv(isert_conn, rx_desc);
1382 if (rc) {
1383 isert_err("ib_post_recv failed with %d\n", rc);
1384 return rc;
1385 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001386 return 0;
1387}
1388
1389static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001390isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001391 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1392 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001393{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001394 struct iscsi_conn *conn = isert_conn->conn;
1395 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1396 int rc;
1397
1398 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1399 if (rc < 0)
1400 return rc;
1401 /*
1402 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1403 */
1404
1405 return iscsit_process_nop_out(conn, cmd, hdr);
1406}
1407
1408static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001409isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001410 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1411 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001412{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001413 struct iscsi_conn *conn = isert_conn->conn;
1414 u32 payload_length = ntoh24(hdr->dlength);
1415 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001416 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001417
1418 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1419 if (rc < 0)
1420 return rc;
1421
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001422 if (payload_length) {
1423 text_in = kzalloc(payload_length, GFP_KERNEL);
1424 if (!text_in) {
1425 isert_err("Unable to allocate text_in of payload_length: %u\n",
1426 payload_length);
1427 return -ENOMEM;
1428 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001429 }
1430 cmd->text_in_ptr = text_in;
1431
1432 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1433
1434 return iscsit_process_text_cmd(conn, cmd, hdr);
1435}
1436
1437static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001438isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1439 uint32_t read_stag, uint64_t read_va,
1440 uint32_t write_stag, uint64_t write_va)
1441{
1442 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1443 struct iscsi_conn *conn = isert_conn->conn;
1444 struct iscsi_cmd *cmd;
1445 struct isert_cmd *isert_cmd;
1446 int ret = -EINVAL;
1447 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1448
Sagi Grimbergfb140272015-03-29 15:52:08 +03001449 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001450 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001451 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001452 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001453 return 0;
1454 }
1455
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001456 switch (opcode) {
1457 case ISCSI_OP_SCSI_CMD:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001458 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001459 if (!cmd)
1460 break;
1461
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001462 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001463 isert_cmd->read_stag = read_stag;
1464 isert_cmd->read_va = read_va;
1465 isert_cmd->write_stag = write_stag;
1466 isert_cmd->write_va = write_va;
1467
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001468 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001469 rx_desc, (unsigned char *)hdr);
1470 break;
1471 case ISCSI_OP_NOOP_OUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001472 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001473 if (!cmd)
1474 break;
1475
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001476 isert_cmd = iscsit_priv_cmd(cmd);
1477 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001478 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001479 break;
1480 case ISCSI_OP_SCSI_DATA_OUT:
1481 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1482 (unsigned char *)hdr);
1483 break;
1484 case ISCSI_OP_SCSI_TMFUNC:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001485 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001486 if (!cmd)
1487 break;
1488
1489 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1490 (unsigned char *)hdr);
1491 break;
1492 case ISCSI_OP_LOGOUT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001493 cmd = isert_allocate_cmd(conn, rx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001494 if (!cmd)
1495 break;
1496
1497 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001498 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001499 case ISCSI_OP_TEXT:
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001500 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF)
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001501 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001502 else
1503 cmd = isert_allocate_cmd(conn, rx_desc);
1504
1505 if (!cmd)
1506 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001507
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001508 isert_cmd = iscsit_priv_cmd(cmd);
1509 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001510 rx_desc, (struct iscsi_text *)hdr);
1511 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001512 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001513 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001514 dump_stack();
1515 break;
1516 }
1517
1518 return ret;
1519}
1520
1521static void
1522isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1523{
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001524 struct iser_ctrl *iser_ctrl = &rx_desc->iser_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001525 uint64_t read_va = 0, write_va = 0;
1526 uint32_t read_stag = 0, write_stag = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001528 switch (iser_ctrl->flags & 0xF0) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001529 case ISCSI_CTRL:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001530 if (iser_ctrl->flags & ISER_RSV) {
1531 read_stag = be32_to_cpu(iser_ctrl->read_stag);
1532 read_va = be64_to_cpu(iser_ctrl->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001533 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1534 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001535 }
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001536 if (iser_ctrl->flags & ISER_WSV) {
1537 write_stag = be32_to_cpu(iser_ctrl->write_stag);
1538 write_va = be64_to_cpu(iser_ctrl->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001539 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1540 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001541 }
1542
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001543 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001544 break;
1545 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001546 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001547 break;
1548 default:
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +02001549 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_ctrl->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001550 break;
1551 }
1552
Bart Van Assche6c760b32015-10-22 11:14:18 -07001553 isert_rx_opcode(isert_conn, rx_desc,
1554 read_stag, read_va, write_stag, write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001555}
1556
1557static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001558isert_rcv_completion(struct iser_rx_desc *desc,
1559 struct isert_conn *isert_conn,
1560 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001561{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001562 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001563 struct iscsi_hdr *hdr;
1564 u64 rx_dma;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03001565 int rx_buflen;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001566
1567 if ((char *)desc == isert_conn->login_req_buf) {
1568 rx_dma = isert_conn->login_req_dma;
1569 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001570 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001571 rx_dma, rx_buflen);
1572 } else {
1573 rx_dma = desc->dma_addr;
1574 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001575 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001576 rx_dma, rx_buflen);
1577 }
1578
1579 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1580
1581 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001582 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001583 hdr->opcode, hdr->itt, hdr->flags,
1584 (int)(xfer_len - ISER_HEADERS_LEN));
1585
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001586 if ((char *)desc == isert_conn->login_req_buf) {
1587 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1588 if (isert_conn->conn) {
1589 struct iscsi_login *login = isert_conn->conn->conn_login;
1590
1591 if (login && !login->first_request)
1592 isert_rx_login_req(isert_conn);
1593 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001594 mutex_lock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001595 complete(&isert_conn->login_req_comp);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001596 mutex_unlock(&isert_conn->mutex);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001597 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001598 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001599 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001600
1601 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1602 DMA_FROM_DEVICE);
1603
1604 isert_conn->post_recv_buf_count--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001605}
1606
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001607static int
1608isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1609 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1610 enum iser_ib_op_code op, struct isert_data_buf *data)
1611{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001612 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001613
1614 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1615 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1616
1617 data->len = length - offset;
1618 data->offset = offset;
1619 data->sg_off = data->offset / PAGE_SIZE;
1620
1621 data->sg = &sg[data->sg_off];
1622 data->nents = min_t(unsigned int, nents - data->sg_off,
1623 ISCSI_ISER_SG_TABLESIZE);
1624 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1625 PAGE_SIZE);
1626
1627 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1628 data->dma_dir);
1629 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001630 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001631 return -EINVAL;
1632 }
1633
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001634 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001635 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001636
1637 return 0;
1638}
1639
1640static void
1641isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1642{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001643 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001644
1645 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1646 memset(data, 0, sizeof(*data));
1647}
1648
1649
1650
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651static void
1652isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1653{
1654 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001655
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001656 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001657
1658 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001659 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001660 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001661 }
1662
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001663 if (wr->rdma_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001664 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001665 kfree(wr->rdma_wr);
1666 wr->rdma_wr = NULL;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001667 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001668
Vu Pham90ecc6e2013-08-28 23:23:33 +03001669 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001670 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001671 kfree(wr->ib_sge);
1672 wr->ib_sge = NULL;
1673 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001674}
1675
1676static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001677isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001678{
1679 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001680
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001681 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001682
1683 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001684 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001685 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1686 isert_unmap_data_buf(isert_conn, &wr->prot);
1687 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1688 }
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001689 spin_lock_bh(&isert_conn->pool_lock);
1690 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1691 spin_unlock_bh(&isert_conn->pool_lock);
Vu Pham59464ef2013-08-28 23:23:35 +03001692 wr->fr_desc = NULL;
1693 }
1694
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001695 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001696 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001697 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001698 }
1699
1700 wr->ib_sge = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001701 wr->rdma_wr = NULL;
Vu Pham59464ef2013-08-28 23:23:35 +03001702}
1703
1704static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001705isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001707 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001708 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001709 struct iscsi_conn *conn = isert_conn->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001710 struct isert_device *device = isert_conn->device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001711 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001712
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001713 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001714
1715 switch (cmd->iscsi_opcode) {
1716 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001717 spin_lock_bh(&conn->cmd_lock);
1718 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001719 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001720 spin_unlock_bh(&conn->cmd_lock);
1721
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001722 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001723 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001724 /*
1725 * Check for special case during comp_err where
1726 * WRITE_PENDING has been handed off from core,
1727 * but requires an extra target_put_sess_cmd()
1728 * before transport_generic_free_cmd() below.
1729 */
1730 if (comp_err &&
1731 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1732 struct se_cmd *se_cmd = &cmd->se_cmd;
1733
Bart Van Asscheafc16602015-04-27 13:52:36 +02001734 target_put_sess_cmd(se_cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001735 }
1736 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001737
Vu Phamd40945d2013-08-28 23:23:34 +03001738 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001739 transport_generic_free_cmd(&cmd->se_cmd, 0);
1740 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001741 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001742 spin_lock_bh(&conn->cmd_lock);
1743 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001744 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001745 spin_unlock_bh(&conn->cmd_lock);
1746
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747 transport_generic_free_cmd(&cmd->se_cmd, 0);
1748 break;
1749 case ISCSI_OP_REJECT:
1750 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001751 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001752 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1753 /* If the continue bit is on, keep the command alive */
1754 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1755 break;
1756
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001757 spin_lock_bh(&conn->cmd_lock);
1758 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001759 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001760 spin_unlock_bh(&conn->cmd_lock);
1761
1762 /*
1763 * Handle special case for REJECT when iscsi_add_reject*() has
1764 * overwritten the original iscsi_opcode assignment, and the
1765 * associated cmd->se_cmd needs to be released.
1766 */
1767 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001768 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001769 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001770 transport_generic_free_cmd(&cmd->se_cmd, 0);
1771 break;
1772 }
1773 /*
1774 * Fall-through
1775 */
1776 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001777 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001778 break;
1779 }
1780}
1781
1782static void
1783isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1784{
1785 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001786 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001787 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1788 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1789 tx_desc->dma_addr = 0;
1790 }
1791}
1792
1793static void
1794isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001795 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001797 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001798 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001799 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1800 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1801 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001802 }
1803
1804 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001805 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001806}
1807
Sagi Grimberg96b79732014-03-17 12:52:18 +02001808static int
1809isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1810{
1811 struct ib_mr_status mr_status;
1812 int ret;
1813
1814 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1815 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001816 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001817 goto fail_mr_status;
1818 }
1819
1820 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1821 u64 sec_offset_err;
1822 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1823
1824 switch (mr_status.sig_err.err_type) {
1825 case IB_SIG_BAD_GUARD:
1826 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1827 break;
1828 case IB_SIG_BAD_REFTAG:
1829 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1830 break;
1831 case IB_SIG_BAD_APPTAG:
1832 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1833 break;
1834 }
1835 sec_offset_err = mr_status.sig_err.sig_err_offset;
1836 do_div(sec_offset_err, block_size);
1837 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1838
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001839 isert_err("PI error found type %d at sector 0x%llx "
1840 "expected 0x%x vs actual 0x%x\n",
1841 mr_status.sig_err.err_type,
1842 (unsigned long long)se_cmd->bad_sector,
1843 mr_status.sig_err.expected,
1844 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001845 ret = 1;
1846 }
1847
1848fail_mr_status:
1849 return ret;
1850}
1851
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001852static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001853isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1854 struct isert_cmd *isert_cmd)
1855{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001856 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001857 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001858 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001859 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001860 struct isert_device *device = isert_conn->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001861 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001862
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001863 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001864 ret = isert_check_pi_status(se_cmd,
1865 wr->fr_desc->pi_ctx->sig_mr);
1866 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001867 }
1868
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001869 device->unreg_rdma_mem(isert_cmd, isert_conn);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001870 wr->rdma_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001871 if (ret)
1872 transport_send_check_condition_and_sense(se_cmd,
1873 se_cmd->pi_err, 0);
1874 else
1875 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001876}
1877
1878static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001879isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1880 struct isert_cmd *isert_cmd)
1881{
1882 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001883 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001884 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001885 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001886 struct isert_device *device = isert_conn->device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001887 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001888
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001889 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001890 ret = isert_check_pi_status(se_cmd,
1891 wr->fr_desc->pi_ctx->sig_mr);
1892 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001893 }
1894
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001895 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001896 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001897 cmd->write_data_done = wr->data.len;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001898 wr->rdma_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001899
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001900 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001901 spin_lock_bh(&cmd->istate_lock);
1902 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1903 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1904 spin_unlock_bh(&cmd->istate_lock);
1905
Sagi Grimberg364189f2015-03-29 15:52:03 +03001906 if (ret) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001907 target_put_sess_cmd(se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001908 transport_send_check_condition_and_sense(se_cmd,
1909 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001910 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001911 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001912 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913}
1914
1915static void
1916isert_do_control_comp(struct work_struct *work)
1917{
1918 struct isert_cmd *isert_cmd = container_of(work,
1919 struct isert_cmd, comp_work);
1920 struct isert_conn *isert_conn = isert_cmd->conn;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001921 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001922 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001923
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001924 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1925
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001926 switch (cmd->i_state) {
1927 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001928 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001929 case ISTATE_SEND_REJECT: /* FALLTHRU */
1930 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001931 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001932 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1933 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001934 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001935 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001936 iscsit_logout_post_handler(cmd, cmd->conn);
1937 break;
1938 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001939 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001940 dump_stack();
1941 break;
1942 }
1943}
1944
1945static void
1946isert_response_completion(struct iser_tx_desc *tx_desc,
1947 struct isert_cmd *isert_cmd,
1948 struct isert_conn *isert_conn,
1949 struct ib_device *ib_dev)
1950{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001951 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001952
1953 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001954 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001955 cmd->i_state == ISTATE_SEND_REJECT ||
1956 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001957 isert_unmap_tx_desc(tx_desc, ib_dev);
1958
1959 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1960 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1961 return;
1962 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001963
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001964 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001965 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001966}
1967
1968static void
Sagi Grimberg77486812015-03-29 15:52:13 +03001969isert_snd_completion(struct iser_tx_desc *tx_desc,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001970 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001971{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03001972 struct ib_device *ib_dev = isert_conn->cm_id->device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001973 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1974 struct isert_rdma_wr *wr;
1975
1976 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977 isert_unmap_tx_desc(tx_desc, ib_dev);
1978 return;
1979 }
1980 wr = &isert_cmd->rdma_wr;
1981
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001982 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
1983
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001984 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001985 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001986 isert_response_completion(tx_desc, isert_cmd,
1987 isert_conn, ib_dev);
1988 break;
1989 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001990 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001991 break;
1992 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001993 isert_completion_rdma_read(tx_desc, isert_cmd);
1994 break;
1995 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001996 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001997 dump_stack();
1998 break;
1999 }
2000}
2001
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002002/**
2003 * is_isert_tx_desc() - Indicate if the completion wr_id
2004 * is a TX descriptor or not.
2005 * @isert_conn: iser connection
2006 * @wr_id: completion WR identifier
2007 *
2008 * Since we cannot rely on wc opcode in FLUSH errors
2009 * we must work around it by checking if the wr_id address
2010 * falls in the iser connection rx_descs buffer. If so
2011 * it is an RX descriptor, otherwize it is a TX.
2012 */
2013static inline bool
2014is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002015{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002016 void *start = isert_conn->rx_descs;
2017 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002018
2019 if (wr_id >= start && wr_id < start + len)
2020 return false;
2021
2022 return true;
2023}
2024
2025static void
2026isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2027{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002028 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002029 isert_info("conn %p completing wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002030 isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002031 complete(&isert_conn->wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002032 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002033 struct ib_device *ib_dev = isert_conn->cm_id->device;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002034 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002035 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002036
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002037 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2038 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002039 if (!isert_cmd)
2040 isert_unmap_tx_desc(desc, ib_dev);
2041 else
2042 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002043 } else {
2044 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002045 if (!isert_conn->post_recv_buf_count)
2046 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002047 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002048}
2049
2050static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002051isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002053 struct isert_conn *isert_conn;
2054 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002055 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002056
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002057 isert_conn = wc->qp->qp_context;
2058 if (likely(wc->status == IB_WC_SUCCESS)) {
2059 if (wc->opcode == IB_WC_RECV) {
2060 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002061 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002062 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002063 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg77486812015-03-29 15:52:13 +03002064 isert_snd_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002066 } else {
2067 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002068 isert_err("%s (%d): wr id %llx vend_err %x\n",
2069 ib_wc_status_msg(wc->status), wc->status,
2070 wc->wr_id, wc->vendor_err);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002071 else
Sagi Grimbergea8a1612015-05-18 13:40:31 +03002072 isert_dbg("%s (%d): wr id %llx\n",
2073 ib_wc_status_msg(wc->status), wc->status,
2074 wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002075
2076 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2077 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002078 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002079}
2080
2081static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002082isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002084 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002085 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002086 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002087 struct ib_wc *const wcs = comp->wcs;
2088 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002089
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002090 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2091 for (i = 0; i < n; i++)
2092 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002093
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002094 completed += n;
2095 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002096 break;
2097 }
2098
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002099 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100}
2101
2102static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002103isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002105 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002106
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002107 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002108}
2109
2110static int
2111isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2112{
2113 struct ib_send_wr *wr_failed;
2114 int ret;
2115
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002116 ret = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2117 if (ret) {
2118 isert_err("ib_post_recv failed with %d\n", ret);
2119 return ret;
2120 }
2121
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002122 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002123 &wr_failed);
2124 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002125 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002126 return ret;
2127 }
2128 return ret;
2129}
2130
2131static int
2132isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2133{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002134 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002135 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002136 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2137 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2138 &isert_cmd->tx_desc.iscsi_header;
2139
2140 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2141 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2142 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2143 /*
2144 * Attach SENSE DATA payload to iSCSI Response PDU
2145 */
2146 if (cmd->se_cmd.sense_buffer &&
2147 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2148 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002149 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002150 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002151 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002152 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002153
2154 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2155 cmd->sense_buffer);
2156 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2157
2158 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2159 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002160 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002161
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002162 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2163 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002164 DMA_TO_DEVICE);
2165
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002166 isert_cmd->pdu_buf_len = pdu_len;
2167 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2168 tx_dsg->length = pdu_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002169 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002170 isert_cmd->tx_desc.num_sge = 2;
2171 }
2172
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002173 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002174
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002175 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002176
2177 return isert_post_response(isert_conn, isert_cmd);
2178}
2179
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002180static void
2181isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2182{
2183 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002184 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002185 struct isert_device *device = isert_conn->device;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002186
2187 spin_lock_bh(&conn->cmd_lock);
2188 if (!list_empty(&cmd->i_conn_node))
2189 list_del_init(&cmd->i_conn_node);
2190 spin_unlock_bh(&conn->cmd_lock);
2191
2192 if (cmd->data_direction == DMA_TO_DEVICE)
2193 iscsit_stop_dataout_timer(cmd);
2194
2195 device->unreg_rdma_mem(isert_cmd, isert_conn);
2196}
2197
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002198static enum target_prot_op
2199isert_get_sup_prot_ops(struct iscsi_conn *conn)
2200{
Sagi Grimberg67004252015-03-29 15:52:09 +03002201 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002202 struct isert_device *device = isert_conn->device;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002203
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002204 if (conn->tpg->tpg_attrib.t10_pi) {
2205 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002206 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002207 isert_conn->pi_support = true;
2208 return TARGET_PROT_ALL;
2209 }
2210 }
2211
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002212 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002213 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002214
2215 return TARGET_PROT_NORMAL;
2216}
2217
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002218static int
2219isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2220 bool nopout_response)
2221{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002222 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002223 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002224 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2225
2226 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2227 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2228 &isert_cmd->tx_desc.iscsi_header,
2229 nopout_response);
2230 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002231 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002232
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002233 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002234
2235 return isert_post_response(isert_conn, isert_cmd);
2236}
2237
2238static int
2239isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2240{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002241 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002242 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002243 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2244
2245 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2246 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2247 &isert_cmd->tx_desc.iscsi_header);
2248 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002249 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002250
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002251 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002252
2253 return isert_post_response(isert_conn, isert_cmd);
2254}
2255
2256static int
2257isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2258{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002259 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002260 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002261 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2262
2263 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2264 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2265 &isert_cmd->tx_desc.iscsi_header);
2266 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002267 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002268
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002269 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002270
2271 return isert_post_response(isert_conn, isert_cmd);
2272}
2273
2274static int
2275isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
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;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002280 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002281 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002282 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2283 struct iscsi_reject *hdr =
2284 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002285
2286 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002287 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002288 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002289
2290 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002291 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002292 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2293 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002294 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2295 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002296 tx_dsg->length = ISCSI_HDR_LEN;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002297 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002298 isert_cmd->tx_desc.num_sge = 2;
2299
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002300 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002301
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002302 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002303
2304 return isert_post_response(isert_conn, isert_cmd);
2305}
2306
2307static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002308isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2309{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002310 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002311 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002312 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2313 struct iscsi_text_rsp *hdr =
2314 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2315 u32 txt_rsp_len;
2316 int rc;
2317
2318 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002319 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002320 if (rc < 0)
2321 return rc;
2322
2323 txt_rsp_len = rc;
2324 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2325
2326 if (txt_rsp_len) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002327 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002328 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002329 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2330 void *txt_rsp_buf = cmd->buf_ptr;
2331
2332 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2333 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2334
2335 isert_cmd->pdu_buf_len = txt_rsp_len;
2336 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2337 tx_dsg->length = txt_rsp_len;
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002338 tx_dsg->lkey = device->pd->local_dma_lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002339 isert_cmd->tx_desc.num_sge = 2;
2340 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002341 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002342
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002343 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002344
2345 return isert_post_response(isert_conn, isert_cmd);
2346}
2347
2348static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002350 struct ib_sge *ib_sge, struct ib_rdma_wr *rdma_wr,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002351 u32 data_left, u32 offset)
2352{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002353 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002354 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002355 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002356 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357 u32 sg_off, page_off;
2358 int i = 0, sg_nents;
2359
2360 sg_off = offset / PAGE_SIZE;
2361 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2362 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2363 page_off = offset % PAGE_SIZE;
2364
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002365 rdma_wr->wr.sg_list = ib_sge;
2366 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002367 /*
2368 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2369 */
2370 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002371 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2372 "page_off: %u\n",
2373 (unsigned long long)tmp_sg->dma_address,
2374 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002375
2376 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2377 ib_sge->length = min_t(u32, data_left,
2378 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002379 ib_sge->lkey = device->pd->local_dma_lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002380
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002381 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2382 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002383 page_off = 0;
2384 data_left -= ib_sge->length;
Sagi Grimberg9253e662015-06-04 19:49:19 +03002385 if (!data_left)
2386 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002387 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002388 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002389 }
2390
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002391 rdma_wr->wr.num_sge = ++i;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002392 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002393 rdma_wr->wr.sg_list, rdma_wr->wr.num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002394
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002395 return rdma_wr->wr.num_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002396}
2397
2398static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002399isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2400 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002401{
2402 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002403 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002404 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002405 struct isert_data_buf *data = &wr->data;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002406 struct ib_rdma_wr *rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002407 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002408 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2409 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002410
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002411 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002413 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2414 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2415 se_cmd->t_data_nents, se_cmd->data_length,
2416 offset, wr->iser_ib_op, &wr->data);
2417 if (ret)
2418 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002419
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002420 data_left = data->len;
2421 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002423 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002424 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002425 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002427 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002428 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002429 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002431 wr->rdma_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2432 wr->rdma_wr = kzalloc(sizeof(struct ib_rdma_wr) * wr->rdma_wr_num,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002433 GFP_KERNEL);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002434 if (!wr->rdma_wr) {
2435 isert_dbg("Unable to allocate wr->rdma_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002437 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002439
2440 wr->isert_cmd = isert_cmd;
2441 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002443 for (i = 0; i < wr->rdma_wr_num; i++) {
2444 rdma_wr = &isert_cmd->rdma_wr.rdma_wr[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002445 data_len = min(data_left, rdma_write_max);
2446
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002447 rdma_wr->wr.send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002448 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002449 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2450 rdma_wr->remote_addr = isert_cmd->read_va + offset;
2451 rdma_wr->rkey = isert_cmd->read_stag;
2452 if (i + 1 == wr->rdma_wr_num)
2453 rdma_wr->wr.next = &isert_cmd->tx_desc.send_wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002454 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002455 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002456 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002457 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2458 rdma_wr->remote_addr = isert_cmd->write_va + va_offset;
2459 rdma_wr->rkey = isert_cmd->write_stag;
2460 if (i + 1 == wr->rdma_wr_num)
2461 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002462 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002463 rdma_wr->wr.next = &wr->rdma_wr[i + 1].wr;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002464 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002465
2466 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002467 rdma_wr, data_len, offset);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002468 ib_sge += ib_sge_cnt;
2469
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002470 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002471 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002472 data_left -= data_len;
2473 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002474
2475 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002476unmap_cmd:
2477 isert_unmap_data_buf(isert_conn, data);
2478
Vu Pham90ecc6e2013-08-28 23:23:33 +03002479 return ret;
2480}
2481
Sagi Grimberg10633c32014-12-07 13:12:04 +02002482static inline void
2483isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2484{
2485 u32 rkey;
2486
2487 memset(inv_wr, 0, sizeof(*inv_wr));
2488 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2489 inv_wr->opcode = IB_WR_LOCAL_INV;
2490 inv_wr->ex.invalidate_rkey = mr->rkey;
2491
2492 /* Bump the key */
2493 rkey = ib_inc_rkey(mr->rkey);
2494 ib_update_fast_reg_key(mr, rkey);
2495}
2496
Vu Pham59464ef2013-08-28 23:23:35 +03002497static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002498isert_fast_reg_mr(struct isert_conn *isert_conn,
2499 struct fast_reg_descriptor *fr_desc,
2500 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002501 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002502 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002503{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002504 struct isert_device *device = isert_conn->device;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002505 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002506 struct ib_mr *mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002507 struct ib_reg_wr reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002508 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002509 int ret, n;
Vu Pham59464ef2013-08-28 23:23:35 +03002510
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002511 if (mem->dma_nents == 1) {
Jason Gunthorpe34efc7d2015-07-30 17:22:21 -06002512 sge->lkey = device->pd->local_dma_lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002513 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2514 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002515 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2516 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002517 return 0;
2518 }
2519
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002520 if (ind == ISERT_DATA_KEY_VALID)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002521 /* Registering data buffer */
2522 mr = fr_desc->data_mr;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002523 else
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002524 /* Registering protection buffer */
2525 mr = fr_desc->pi_ctx->prot_mr;
Vu Pham59464ef2013-08-28 23:23:35 +03002526
Sagi Grimberg10633c32014-12-07 13:12:04 +02002527 if (!(fr_desc->ind & ind)) {
2528 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002529 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002530 }
2531
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002532 n = ib_map_mr_sg(mr, mem->sg, mem->nents, PAGE_SIZE);
2533 if (unlikely(n != mem->nents)) {
2534 isert_err("failed to map mr sg (%d/%d)\n",
2535 n, mem->nents);
2536 return n < 0 ? n : -EINVAL;
2537 }
2538
2539 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
2540 fr_desc, mem->nents, mem->offset);
2541
2542 reg_wr.wr.next = NULL;
2543 reg_wr.wr.opcode = IB_WR_REG_MR;
2544 reg_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2545 reg_wr.wr.send_flags = 0;
2546 reg_wr.wr.num_sge = 0;
2547 reg_wr.mr = mr;
2548 reg_wr.key = mr->lkey;
2549 reg_wr.access = IB_ACCESS_LOCAL_WRITE;
Vu Pham59464ef2013-08-28 23:23:35 +03002550
2551 if (!wr)
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002552 wr = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002553 else
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002554 wr->next = &reg_wr.wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002555
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002556 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Vu Pham59464ef2013-08-28 23:23:35 +03002557 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002558 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002559 return ret;
2560 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002561 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002562
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002563 sge->lkey = mr->lkey;
Sagi Grimberg16c2d702015-10-13 19:11:34 +03002564 sge->addr = mr->iova;
2565 sge->length = mr->length;
Vu Pham59464ef2013-08-28 23:23:35 +03002566
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002567 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2568 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002569
2570 return ret;
2571}
2572
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002573static inline void
2574isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2575 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002576{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002577 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002578 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2579 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2580 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002581 /*
2582 * At the moment we hard code those, but if in the future
2583 * the target core would like to use it, we will take it
2584 * from se_cmd.
2585 */
2586 domain->sig.dif.apptag_check_mask = 0xffff;
2587 domain->sig.dif.app_escape = true;
2588 domain->sig.dif.ref_escape = true;
2589 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2590 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2591 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002592};
2593
2594static int
2595isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2596{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002597 switch (se_cmd->prot_op) {
2598 case TARGET_PROT_DIN_INSERT:
2599 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002600 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002601 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002602 break;
2603 case TARGET_PROT_DOUT_INSERT:
2604 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002605 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002606 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002607 break;
2608 case TARGET_PROT_DIN_PASS:
2609 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002610 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2611 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002612 break;
2613 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002614 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002615 return -EINVAL;
2616 }
2617
2618 return 0;
2619}
2620
2621static inline u8
2622isert_set_prot_checks(u8 prot_checks)
2623{
2624 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2625 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2626 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2627}
2628
2629static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002630isert_reg_sig_mr(struct isert_conn *isert_conn,
2631 struct se_cmd *se_cmd,
2632 struct isert_rdma_wr *rdma_wr,
2633 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002634{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002635 struct ib_sig_handover_wr sig_wr;
2636 struct ib_send_wr inv_wr, *bad_wr, *wr = NULL;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002637 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2638 struct ib_sig_attrs sig_attrs;
2639 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002640
2641 memset(&sig_attrs, 0, sizeof(sig_attrs));
2642 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2643 if (ret)
2644 goto err;
2645
2646 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2647
2648 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002649 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002650 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002651 }
2652
2653 memset(&sig_wr, 0, sizeof(sig_wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002654 sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
2655 sig_wr.wr.wr_id = ISER_FASTREG_LI_WRID;
2656 sig_wr.wr.sg_list = &rdma_wr->ib_sg[DATA];
2657 sig_wr.wr.num_sge = 1;
2658 sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
2659 sig_wr.sig_attrs = &sig_attrs;
2660 sig_wr.sig_mr = pi_ctx->sig_mr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002661 if (se_cmd->t_prot_sg)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002662 sig_wr.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002663
2664 if (!wr)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002665 wr = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002666 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002667 wr->next = &sig_wr.wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002668
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002669 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002670 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002671 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002672 goto err;
2673 }
2674 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2675
Sagi Grimberg570db172014-12-02 16:57:31 +02002676 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2677 rdma_wr->ib_sg[SIG].addr = 0;
2678 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002679 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2680 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2681 /*
2682 * We have protection guards on the wire
2683 * so we need to set a larget transfer
2684 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002685 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002686
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002687 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002688 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2689 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002690err:
2691 return ret;
2692}
2693
Vu Pham59464ef2013-08-28 23:23:35 +03002694static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002695isert_handle_prot_cmd(struct isert_conn *isert_conn,
2696 struct isert_cmd *isert_cmd,
2697 struct isert_rdma_wr *wr)
2698{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002699 struct isert_device *device = isert_conn->device;
Sagi Grimberg570db172014-12-02 16:57:31 +02002700 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2701 int ret;
2702
2703 if (!wr->fr_desc->pi_ctx) {
2704 ret = isert_create_pi_ctx(wr->fr_desc,
2705 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002706 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002707 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002708 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002709 isert_conn);
2710 return ret;
2711 }
2712 }
2713
2714 if (se_cmd->t_prot_sg) {
2715 ret = isert_map_data_buf(isert_conn, isert_cmd,
2716 se_cmd->t_prot_sg,
2717 se_cmd->t_prot_nents,
2718 se_cmd->prot_length,
2719 0, wr->iser_ib_op, &wr->prot);
2720 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002721 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002722 isert_conn);
2723 return ret;
2724 }
2725
2726 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2727 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2728 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2729 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002730 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002731 isert_conn);
2732 goto unmap_prot_cmd;
2733 }
2734 }
2735
2736 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2737 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002738 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002739 isert_conn);
2740 goto unmap_prot_cmd;
2741 }
2742 wr->fr_desc->ind |= ISERT_PROTECTED;
2743
2744 return 0;
2745
2746unmap_prot_cmd:
2747 if (se_cmd->t_prot_sg)
2748 isert_unmap_data_buf(isert_conn, &wr->prot);
2749
2750 return ret;
2751}
2752
2753static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002754isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2755 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002756{
2757 struct se_cmd *se_cmd = &cmd->se_cmd;
2758 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002759 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002760 struct fast_reg_descriptor *fr_desc = NULL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002761 struct ib_rdma_wr *rdma_wr;
Sagi Grimberg570db172014-12-02 16:57:31 +02002762 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002763 u32 offset;
2764 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002765 unsigned long flags;
2766
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002767 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2768
2769 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2770 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2771 se_cmd->t_data_nents, se_cmd->data_length,
2772 offset, wr->iser_ib_op, &wr->data);
2773 if (ret)
2774 return ret;
2775
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002776 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002777 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2778 fr_desc = list_first_entry(&isert_conn->fr_pool,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002779 struct fast_reg_descriptor, list);
2780 list_del(&fr_desc->list);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002781 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002782 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002783 }
2784
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002785 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002786 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002787 if (ret)
2788 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002789
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002790 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002791 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002792 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002793 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002794
Sagi Grimberg570db172014-12-02 16:57:31 +02002795 ib_sg = &wr->ib_sg[SIG];
2796 } else {
2797 ib_sg = &wr->ib_sg[DATA];
2798 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002799
Sagi Grimberg570db172014-12-02 16:57:31 +02002800 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002801 wr->ib_sge = &wr->s_ib_sge;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002802 wr->rdma_wr_num = 1;
2803 memset(&wr->s_rdma_wr, 0, sizeof(wr->s_rdma_wr));
2804 wr->rdma_wr = &wr->s_rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002805 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002806
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002807 rdma_wr = &isert_cmd->rdma_wr.s_rdma_wr;
2808 rdma_wr->wr.sg_list = &wr->s_ib_sge;
2809 rdma_wr->wr.num_sge = 1;
2810 rdma_wr->wr.wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002811 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002812 rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
2813 rdma_wr->remote_addr = isert_cmd->read_va;
2814 rdma_wr->rkey = isert_cmd->read_stag;
2815 rdma_wr->wr.send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002816 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002817 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002818 rdma_wr->wr.opcode = IB_WR_RDMA_READ;
2819 rdma_wr->remote_addr = isert_cmd->write_va;
2820 rdma_wr->rkey = isert_cmd->write_stag;
2821 rdma_wr->wr.send_flags = IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002822 }
2823
Vu Pham59464ef2013-08-28 23:23:35 +03002824 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002825
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002826unmap_cmd:
2827 if (fr_desc) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002828 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2829 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2830 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002831 }
2832 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002833
Vu Pham59464ef2013-08-28 23:23:35 +03002834 return ret;
2835}
2836
2837static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002838isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2839{
2840 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002841 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002842 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002843 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002844 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002845 struct ib_send_wr *wr_failed;
2846 int rc;
2847
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002848 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002849 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002850
Vu Pham90ecc6e2013-08-28 23:23:33 +03002851 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002852 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002853 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002854 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002855 return rc;
2856 }
2857
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002858 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002859 /*
2860 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2861 */
2862 isert_create_send_desc(isert_conn, isert_cmd,
2863 &isert_cmd->tx_desc);
2864 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2865 &isert_cmd->tx_desc.iscsi_header);
2866 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2867 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002868 &isert_cmd->tx_desc.send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002869 isert_cmd->rdma_wr.s_rdma_wr.wr.next = &isert_cmd->tx_desc.send_wr;
2870 wr->rdma_wr_num += 1;
Jenny Derzhavetz4366b192015-09-06 14:52:25 +03002871
2872 rc = isert_post_recv(isert_conn, isert_cmd->rx_desc);
2873 if (rc) {
2874 isert_err("ib_post_recv failed with %d\n", rc);
2875 return rc;
2876 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002877 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002878
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002879 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002880 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002881 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002882
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002883 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002884 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002885 "READ\n", isert_cmd);
2886 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002887 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002888 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002889
Vu Pham90ecc6e2013-08-28 23:23:33 +03002890 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002891}
2892
2893static int
2894isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2895{
2896 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002897 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002898 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002899 struct isert_conn *isert_conn = conn->context;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03002900 struct isert_device *device = isert_conn->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002901 struct ib_send_wr *wr_failed;
2902 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002903
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002904 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002905 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002906 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002907 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002908 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002909 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002910 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002911 }
2912
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002913 rc = ib_post_send(isert_conn->qp, &wr->rdma_wr->wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002914 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002915 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002916
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002917 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002918 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002919
Vu Pham90ecc6e2013-08-28 23:23:33 +03002920 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002921}
2922
2923static int
2924isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2925{
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002926 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2927 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002928
2929 switch (state) {
Jenny Derzhavetza4c15cd2015-09-06 14:52:20 +03002930 case ISTATE_REMOVE:
2931 spin_lock_bh(&conn->cmd_lock);
2932 list_del_init(&cmd->i_conn_node);
2933 spin_unlock_bh(&conn->cmd_lock);
2934 isert_put_cmd(isert_cmd, true);
2935 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002936 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2937 ret = isert_put_nopin(cmd, conn, false);
2938 break;
2939 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002940 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002941 ret = -EINVAL;
2942 break;
2943 }
2944
2945 return ret;
2946}
2947
2948static int
2949isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2950{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002951 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002952 int ret;
2953
2954 switch (state) {
2955 case ISTATE_SEND_LOGOUTRSP:
2956 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002957 if (!ret)
2958 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002959 break;
2960 case ISTATE_SEND_NOPIN:
2961 ret = isert_put_nopin(cmd, conn, true);
2962 break;
2963 case ISTATE_SEND_TASKMGTRSP:
2964 ret = isert_put_tm_rsp(cmd, conn);
2965 break;
2966 case ISTATE_SEND_REJECT:
2967 ret = isert_put_reject(cmd, conn);
2968 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002969 case ISTATE_SEND_TEXTRSP:
2970 ret = isert_put_text_rsp(cmd, conn);
2971 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002972 case ISTATE_SEND_STATUS:
2973 /*
2974 * Special case for sending non GOOD SCSI status from TX thread
2975 * context during pre se_cmd excecution failure.
2976 */
2977 ret = isert_put_response(conn, cmd);
2978 break;
2979 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002980 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002981 ret = -EINVAL;
2982 break;
2983 }
2984
2985 return ret;
2986}
2987
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002988struct rdma_cm_id *
2989isert_setup_id(struct isert_np *isert_np)
2990{
2991 struct iscsi_np *np = isert_np->np;
2992 struct rdma_cm_id *id;
2993 struct sockaddr *sa;
2994 int ret;
2995
2996 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002997 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002998
Guy Shapirofa201052015-10-22 15:20:10 +03002999 id = rdma_create_id(&init_net, isert_cma_handler, isert_np,
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003000 RDMA_PS_TCP, IB_QPT_RC);
3001 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003002 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003003 ret = PTR_ERR(id);
3004 goto out;
3005 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003006 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003007
3008 ret = rdma_bind_addr(id, sa);
3009 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003010 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003011 goto out_id;
3012 }
3013
Sagi Grimberg992607e2015-03-29 15:52:18 +03003014 ret = rdma_listen(id, 0);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003015 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003016 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003017 goto out_id;
3018 }
3019
3020 return id;
3021out_id:
3022 rdma_destroy_id(id);
3023out:
3024 return ERR_PTR(ret);
3025}
3026
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003027static int
3028isert_setup_np(struct iscsi_np *np,
Andy Grover13a3cf02015-08-24 10:26:06 -07003029 struct sockaddr_storage *ksockaddr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003030{
3031 struct isert_np *isert_np;
3032 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003033 int ret;
3034
3035 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3036 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003037 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003038 return -ENOMEM;
3039 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003040 sema_init(&isert_np->sem, 0);
3041 mutex_init(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003042 INIT_LIST_HEAD(&isert_np->accepted);
3043 INIT_LIST_HEAD(&isert_np->pending);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003044 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003045
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003046 /*
3047 * Setup the np->np_sockaddr from the passed sockaddr setup
3048 * in iscsi_target_configfs.c code..
3049 */
3050 memcpy(&np->np_sockaddr, ksockaddr,
Andy Grover13a3cf02015-08-24 10:26:06 -07003051 sizeof(struct sockaddr_storage));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003052
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003053 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003054 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003055 ret = PTR_ERR(isert_lid);
3056 goto out;
3057 }
3058
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003059 isert_np->cm_id = isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003060 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003061
3062 return 0;
3063
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003064out:
3065 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003066
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003067 return ret;
3068}
3069
3070static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003071isert_rdma_accept(struct isert_conn *isert_conn)
3072{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003073 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003074 struct rdma_conn_param cp;
3075 int ret;
3076
3077 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003078 cp.initiator_depth = isert_conn->initiator_depth;
3079 cp.retry_count = 7;
3080 cp.rnr_retry_count = 7;
3081
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003082 ret = rdma_accept(cm_id, &cp);
3083 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003084 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003085 return ret;
3086 }
3087
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003088 return 0;
3089}
3090
3091static int
3092isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3093{
Sagi Grimberg67004252015-03-29 15:52:09 +03003094 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003095 int ret;
3096
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003097 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003098 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3099 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003100 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003101 isert_conn);
3102 return ret;
3103 }
3104 reinit_completion(&isert_conn->login_req_comp);
3105
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003106 /*
3107 * For login requests after the first PDU, isert_rx_login_req() will
3108 * kick schedule_delayed_work(&conn->login_work) as the packet is
3109 * received, which turns this callback from iscsi_target_do_login_rx()
3110 * into a NOP.
3111 */
3112 if (!login->first_request)
3113 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003114
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003115 isert_rx_login_req(isert_conn);
3116
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003117 isert_info("before login_comp conn: %p\n", conn);
3118 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003119 if (ret)
3120 return ret;
3121
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003122 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003123
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124 return 0;
3125}
3126
3127static void
3128isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3129 struct isert_conn *isert_conn)
3130{
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003131 struct rdma_cm_id *cm_id = isert_conn->cm_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003132 struct rdma_route *cm_route = &cm_id->route;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003133
3134 conn->login_family = np->np_sockaddr.ss_family;
3135
Andy Groverdc58f762015-08-24 10:26:05 -07003136 conn->login_sockaddr = cm_route->addr.dst_addr;
3137 conn->local_sockaddr = cm_route->addr.src_addr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003138}
3139
3140static int
3141isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3142{
Sagi Grimberg67004252015-03-29 15:52:09 +03003143 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003144 struct isert_conn *isert_conn;
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003145 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003146
3147accept_wait:
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003148 ret = down_interruptible(&isert_np->sem);
Sagi Grimbergc6b8e912015-03-29 15:52:16 +03003149 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003150 return -ENODEV;
3151
3152 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003153 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003154 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003155 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003156 np->np_thread_state);
3157 /**
3158 * No point in stalling here when np_thread
3159 * is in state RESET/SHUTDOWN/EXIT - bail
3160 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003161 return -ENODEV;
3162 }
3163 spin_unlock_bh(&np->np_thread_lock);
3164
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003165 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003166 if (list_empty(&isert_np->pending)) {
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003167 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003168 goto accept_wait;
3169 }
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003170 isert_conn = list_first_entry(&isert_np->pending,
3171 struct isert_conn, node);
3172 list_del_init(&isert_conn->node);
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003173 mutex_unlock(&isert_np->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003174
3175 conn->context = isert_conn;
3176 isert_conn->conn = conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003177
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003178 isert_set_conn_info(np, conn, isert_conn);
3179
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003180 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003181
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003182 return 0;
3183}
3184
3185static void
3186isert_free_np(struct iscsi_np *np)
3187{
Sagi Grimberg67004252015-03-29 15:52:09 +03003188 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003189 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003190
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003191 if (isert_np->cm_id)
3192 rdma_destroy_id(isert_np->cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003193
Sagi Grimberg268e6812014-12-02 16:57:36 +02003194 /*
3195 * FIXME: At this point we don't have a good way to insure
3196 * that at this point we don't have hanging connections that
3197 * completed RDMA establishment but didn't start iscsi login
3198 * process. So work-around this by cleaning up what ever piled
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003199 * up in accepted and pending lists.
Sagi Grimberg268e6812014-12-02 16:57:36 +02003200 */
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003201 mutex_lock(&isert_np->mutex);
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003202 if (!list_empty(&isert_np->pending)) {
3203 isert_info("Still have isert pending connections\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003204 list_for_each_entry_safe(isert_conn, n,
Jenny Derzhavetzbd379222015-09-06 14:52:24 +03003205 &isert_np->pending,
3206 node) {
3207 isert_info("cleaning isert_conn %p state (%d)\n",
3208 isert_conn, isert_conn->state);
3209 isert_connect_release(isert_conn);
3210 }
3211 }
3212
3213 if (!list_empty(&isert_np->accepted)) {
3214 isert_info("Still have isert accepted connections\n");
3215 list_for_each_entry_safe(isert_conn, n,
3216 &isert_np->accepted,
3217 node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003218 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003219 isert_conn, isert_conn->state);
3220 isert_connect_release(isert_conn);
3221 }
3222 }
Jenny Derzhavetzed8cb0a2015-09-06 14:52:23 +03003223 mutex_unlock(&isert_np->mutex);
Sagi Grimberg268e6812014-12-02 16:57:36 +02003224
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003225 np->np_context = NULL;
3226 kfree(isert_np);
3227}
3228
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003229static void isert_release_work(struct work_struct *work)
3230{
3231 struct isert_conn *isert_conn = container_of(work,
3232 struct isert_conn,
3233 release_work);
3234
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003235 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003236
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003237 wait_for_completion(&isert_conn->wait);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003238
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003239 mutex_lock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003240 isert_conn->state = ISER_CONN_DOWN;
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003241 mutex_unlock(&isert_conn->mutex);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003242
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003243 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003244 isert_put_conn(isert_conn);
3245}
3246
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003247static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003248isert_wait4logout(struct isert_conn *isert_conn)
3249{
3250 struct iscsi_conn *conn = isert_conn->conn;
3251
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003252 isert_info("conn %p\n", isert_conn);
3253
Sagi Grimberg991bb762014-12-07 13:12:01 +02003254 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003255 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003256 wait_for_completion_timeout(&conn->conn_logout_comp,
3257 SECONDS_FOR_LOGOUT_COMP * HZ);
3258 }
3259}
3260
3261static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003262isert_wait4cmds(struct iscsi_conn *conn)
3263{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003264 isert_info("iscsi_conn %p\n", conn);
3265
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003266 if (conn->sess) {
3267 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3268 target_wait_for_sess_cmds(conn->sess->se_sess);
3269 }
3270}
3271
3272static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003273isert_wait4flush(struct isert_conn *isert_conn)
3274{
3275 struct ib_recv_wr *bad_wr;
3276
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003277 isert_info("conn %p\n", isert_conn);
3278
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003279 init_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003280 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3281 /* post an indication that all flush errors were consumed */
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003282 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003283 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003284 return;
3285 }
3286
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003287 wait_for_completion(&isert_conn->wait_comp_err);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003288}
3289
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003290/**
3291 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3292 * unsolicitate dataout
3293 * @conn: iscsi connection
3294 *
3295 * We might still have commands that are waiting for unsolicited
3296 * dataouts messages. We must put the extra reference on those
3297 * before blocking on the target_wait_for_session_cmds
3298 */
3299static void
3300isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3301{
3302 struct iscsi_cmd *cmd, *tmp;
3303 static LIST_HEAD(drop_cmd_list);
3304
3305 spin_lock_bh(&conn->cmd_lock);
3306 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3307 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3308 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3309 (cmd->write_data_done < cmd->se_cmd.data_length))
3310 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3311 }
3312 spin_unlock_bh(&conn->cmd_lock);
3313
3314 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3315 list_del_init(&cmd->i_conn_node);
3316 if (cmd->i_state != ISTATE_REMOVE) {
3317 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3318
3319 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3320 isert_put_cmd(isert_cmd, true);
3321 }
3322 }
3323}
3324
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003325static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003326{
3327 struct isert_conn *isert_conn = conn->context;
3328
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003329 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003330
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003331 mutex_lock(&isert_conn->mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003332 /*
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003333 * Only wait for wait_comp_err if the isert_conn made it
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003334 * into full feature phase..
3335 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003336 if (isert_conn->state == ISER_CONN_INIT) {
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003337 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003338 return;
3339 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003340 isert_conn_terminate(isert_conn);
Sagi Grimbergdac6ab32015-03-29 15:52:19 +03003341 mutex_unlock(&isert_conn->mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003342
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003343 isert_wait4flush(isert_conn);
Jenny Derzhavetz3e03c4b2015-09-06 14:52:21 +03003344 isert_put_unsol_pending_cmds(conn);
3345 isert_wait4cmds(conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003346 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003347
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003348 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003349}
3350
3351static void isert_free_conn(struct iscsi_conn *conn)
3352{
3353 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003354
Sagi Grimberg524630d2015-06-04 19:49:21 +03003355 isert_wait4flush(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003356 isert_put_conn(isert_conn);
3357}
3358
3359static struct iscsit_transport iser_target_transport = {
3360 .name = "IB/iSER",
3361 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003362 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003363 .owner = THIS_MODULE,
3364 .iscsit_setup_np = isert_setup_np,
3365 .iscsit_accept_np = isert_accept_np,
3366 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003367 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003368 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003369 .iscsit_get_login_rx = isert_get_login_rx,
3370 .iscsit_put_login_tx = isert_put_login_tx,
3371 .iscsit_immediate_queue = isert_immediate_queue,
3372 .iscsit_response_queue = isert_response_queue,
3373 .iscsit_get_dataout = isert_get_dataout,
3374 .iscsit_queue_data_in = isert_put_datain,
3375 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003376 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003377 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003378};
3379
3380static int __init isert_init(void)
3381{
3382 int ret;
3383
Sagi Grimberg631af552015-01-25 19:09:50 +02003384 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3385 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003386 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003387 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003388 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003389 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003390 }
3391
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003392 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3393 WQ_UNBOUND_MAX_ACTIVE);
3394 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003395 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003396 ret = -ENOMEM;
3397 goto destroy_comp_wq;
3398 }
3399
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003400 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003401 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003402
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003403 return 0;
3404
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003405destroy_comp_wq:
3406 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003407
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003408 return ret;
3409}
3410
3411static void __exit isert_exit(void)
3412{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003413 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003414 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003415 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003416 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003417 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003418}
3419
3420MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
Sagi Grimberg9e35eff2015-03-29 15:52:20 +03003421MODULE_VERSION("1.0");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003422MODULE_AUTHOR("nab@Linux-iSCSI.org");
3423MODULE_LICENSE("GPL");
3424
3425module_init(isert_init);
3426module_exit(isert_exit);