blob: 2a4a435fc51612bd6011935e088caac2c773d33d [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
Sagi Grimbergbdf20e72014-12-02 16:57:43 +020038#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080040
Sagi Grimberg45678b62015-01-25 19:11:19 +020041static int isert_debug_level;
Sagi Grimberg24f412d2014-12-07 13:12:02 +020042module_param_named(debug_level, isert_debug_level, int, 0644);
43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
44
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080045static DEFINE_MUTEX(device_list_mutex);
46static LIST_HEAD(device_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080047static struct workqueue_struct *isert_comp_wq;
Sagi Grimbergb02efbf2014-12-02 16:57:29 +020048static struct workqueue_struct *isert_release_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080049
50static void
Vu Phamd40945d2013-08-28 23:23:34 +030051isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52static int
53isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030055static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020056isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030057static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020058isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
59 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020060static int
61isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +020062static int
63isert_rdma_post_recvl(struct isert_conn *isert_conn);
64static int
65isert_rdma_accept(struct isert_conn *isert_conn);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +020066struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
Vu Phamd40945d2013-08-28 23:23:34 +030067
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020068static inline bool
69isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
70{
Sagi Grimberg23a548e2014-12-02 16:57:35 +020071 return (conn->pi_support &&
Sagi Grimberg302cc7c2014-12-02 16:57:34 +020072 cmd->prot_op != TARGET_PROT_NORMAL);
73}
74
75
Vu Phamd40945d2013-08-28 23:23:34 +030076static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080077isert_qp_event_callback(struct ib_event *e, void *context)
78{
Sagi Grimberg67004252015-03-29 15:52:09 +030079 struct isert_conn *isert_conn = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080080
Sagi Grimberg4c22e072014-12-07 13:12:03 +020081 isert_err("conn %p event: %d\n", isert_conn, e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080082 switch (e->event) {
83 case IB_EVENT_COMM_EST:
84 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
85 break;
86 case IB_EVENT_QP_LAST_WQE_REACHED:
Sagi Grimberg4c22e072014-12-07 13:12:03 +020087 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080088 break;
89 default:
90 break;
91 }
92}
93
94static int
95isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
96{
97 int ret;
98
99 ret = ib_query_device(ib_dev, devattr);
100 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200101 isert_err("ib_query_device() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800102 return ret;
103 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200104 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
105 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800106
107 return 0;
108}
109
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300110static struct isert_comp *
111isert_comp_get(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800112{
113 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200114 struct isert_comp *comp;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300115 int i, min = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800116
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800117 mutex_lock(&device_list_mutex);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200118 for (i = 0; i < device->comps_used; i++)
119 if (device->comps[i].active_qps <
120 device->comps[min].active_qps)
121 min = i;
122 comp = &device->comps[min];
123 comp->active_qps++;
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300124 mutex_unlock(&device_list_mutex);
125
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200126 isert_info("conn %p, using comp %p min_index: %d\n",
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200127 isert_conn, comp, min);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300128
129 return comp;
130}
131
132static void
133isert_comp_put(struct isert_comp *comp)
134{
135 mutex_lock(&device_list_mutex);
136 comp->active_qps--;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800137 mutex_unlock(&device_list_mutex);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300138}
139
140static struct ib_qp *
141isert_create_qp(struct isert_conn *isert_conn,
142 struct isert_comp *comp,
143 struct rdma_cm_id *cma_id)
144{
145 struct isert_device *device = isert_conn->conn_device;
146 struct ib_qp_init_attr attr;
147 int ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800148
149 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
150 attr.event_handler = isert_qp_event_callback;
151 attr.qp_context = isert_conn;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200152 attr.send_cq = comp->cq;
153 attr.recv_cq = comp->cq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800154 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +0200155 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800156 /*
157 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700158 * work-around for RDMA_READs with ConnectX-2.
159 *
160 * Also, still make sure to have at least two SGEs for
161 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800162 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700163 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800164 isert_conn->max_sge = attr.cap.max_send_sge;
165
166 attr.cap.max_recv_sge = 1;
167 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
168 attr.qp_type = IB_QPT_RC;
Sagi Grimberg570db172014-12-02 16:57:31 +0200169 if (device->pi_capable)
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200170 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800171
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300172 ret = rdma_create_qp(cma_id, device->pd, &attr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800173 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200174 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300175 return ERR_PTR(ret);
176 }
177
178 return cma_id->qp;
179}
180
181static int
182isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
183{
184 struct isert_comp *comp;
185 int ret;
186
187 comp = isert_comp_get(isert_conn);
188 isert_conn->conn_qp = isert_create_qp(isert_conn, comp, cma_id);
189 if (IS_ERR(isert_conn->conn_qp)) {
190 ret = PTR_ERR(isert_conn->conn_qp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200191 goto err;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800192 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800193
194 return 0;
Sagi Grimberg19e20902014-12-02 16:57:26 +0200195err:
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300196 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200197 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800198}
199
200static void
201isert_cq_event_callback(struct ib_event *e, void *context)
202{
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200203 isert_dbg("event: %d\n", e->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800204}
205
206static int
207isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
208{
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300209 struct isert_device *device = isert_conn->conn_device;
210 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800211 struct iser_rx_desc *rx_desc;
212 struct ib_sge *rx_sg;
213 u64 dma_addr;
214 int i, j;
215
216 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
217 sizeof(struct iser_rx_desc), GFP_KERNEL);
218 if (!isert_conn->conn_rx_descs)
219 goto fail;
220
221 rx_desc = isert_conn->conn_rx_descs;
222
223 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
224 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
225 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
226 if (ib_dma_mapping_error(ib_dev, dma_addr))
227 goto dma_map_fail;
228
229 rx_desc->dma_addr = dma_addr;
230
231 rx_sg = &rx_desc->rx_sg;
232 rx_sg->addr = rx_desc->dma_addr;
233 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300234 rx_sg->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800235 }
236
237 isert_conn->conn_rx_desc_head = 0;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200238
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 return 0;
240
241dma_map_fail:
242 rx_desc = isert_conn->conn_rx_descs;
243 for (j = 0; j < i; j++, rx_desc++) {
244 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
245 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
246 }
247 kfree(isert_conn->conn_rx_descs);
248 isert_conn->conn_rx_descs = NULL;
249fail:
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200250 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
251
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800252 return -ENOMEM;
253}
254
255static void
256isert_free_rx_descriptors(struct isert_conn *isert_conn)
257{
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300258 struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800259 struct iser_rx_desc *rx_desc;
260 int i;
261
262 if (!isert_conn->conn_rx_descs)
263 return;
264
265 rx_desc = isert_conn->conn_rx_descs;
266 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
267 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
268 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
269 }
270
271 kfree(isert_conn->conn_rx_descs);
272 isert_conn->conn_rx_descs = NULL;
273}
274
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200275static void isert_cq_work(struct work_struct *);
276static void isert_cq_callback(struct ib_cq *, void *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277
278static int
279isert_create_device_ib_res(struct isert_device *device)
280{
281 struct ib_device *ib_dev = device->ib_device;
Vu Pham59464ef2013-08-28 23:23:35 +0300282 struct ib_device_attr *dev_attr;
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200283 int ret = 0, i;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200284 int max_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800285
Vu Pham59464ef2013-08-28 23:23:35 +0300286 dev_attr = &device->dev_attr;
287 ret = isert_query_device(ib_dev, dev_attr);
288 if (ret)
289 return ret;
290
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200291 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000292
Vu Phamd40945d2013-08-28 23:23:34 +0300293 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200294 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
295 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200296 device->use_fastreg = 1;
297 device->reg_rdma_mem = isert_reg_rdma;
298 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300299 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200300 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300301 device->reg_rdma_mem = isert_map_rdma;
302 device->unreg_rdma_mem = isert_unmap_cmd;
303 }
Vu Phamd40945d2013-08-28 23:23:34 +0300304
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200305 /* Check signature cap */
306 device->pi_capable = dev_attr->device_cap_flags &
307 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
308
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200309 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
310 device->ib_device->num_comp_vectors));
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200311 isert_info("Using %d CQs, %s supports %d vectors support "
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200312 "Fast registration %d pi_capable %d\n",
313 device->comps_used, device->ib_device->name,
314 device->ib_device->num_comp_vectors, device->use_fastreg,
315 device->pi_capable);
316
317 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
318 GFP_KERNEL);
319 if (!device->comps) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200320 isert_err("Unable to allocate completion contexts\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800321 return -ENOMEM;
322 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800323
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200324 for (i = 0; i < device->comps_used; i++) {
325 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800326
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200327 comp->device = device;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200328 INIT_WORK(&comp->work, isert_cq_work);
329 comp->cq = ib_create_cq(device->ib_device,
330 isert_cq_callback,
331 isert_cq_event_callback,
332 (void *)comp,
333 max_cqe, i);
334 if (IS_ERR(comp->cq)) {
335 ret = PTR_ERR(comp->cq);
336 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800337 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800338 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800339
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200340 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Wei Yongjun94a71112013-10-29 09:56:34 +0800341 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800342 goto out_cq;
343 }
344
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300345 device->pd = ib_alloc_pd(device->ib_device);
346 if (IS_ERR(device->pd)) {
347 ret = PTR_ERR(device->pd);
348 isert_err("failed to allocate pd, device %p, ret=%d\n",
349 device, ret);
350 goto out_cq;
351 }
352
353 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE);
354 if (IS_ERR(device->mr)) {
355 ret = PTR_ERR(device->mr);
356 isert_err("failed to create dma mr, device %p, ret=%d\n",
357 device, ret);
358 goto out_mr;
359 }
360
361
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800362 return 0;
363
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300364out_mr:
365 ib_dealloc_pd(device->pd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800366out_cq:
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200367 for (i = 0; i < device->comps_used; i++) {
368 struct isert_comp *comp = &device->comps[i];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800369
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200370 if (comp->cq) {
371 cancel_work_sync(&comp->work);
372 ib_destroy_cq(comp->cq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800373 }
374 }
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200375 kfree(device->comps);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800376
377 return ret;
378}
379
380static void
381isert_free_device_ib_res(struct isert_device *device)
382{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800383 int i;
384
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200385 isert_info("device %p\n", device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800386
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300387 ib_dereg_mr(device->mr);
388 ib_dealloc_pd(device->pd);
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200389 for (i = 0; i < device->comps_used; i++) {
390 struct isert_comp *comp = &device->comps[i];
391
Sagi Grimberg6f0fae32014-12-02 16:57:41 +0200392 cancel_work_sync(&comp->work);
393 ib_destroy_cq(comp->cq);
394 comp->cq = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800395 }
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200396 kfree(device->comps);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800397}
398
399static void
400isert_device_try_release(struct isert_device *device)
401{
402 mutex_lock(&device_list_mutex);
403 device->refcount--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200404 isert_info("device %p refcount %d\n", device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800405 if (!device->refcount) {
406 isert_free_device_ib_res(device);
407 list_del(&device->dev_node);
408 kfree(device);
409 }
410 mutex_unlock(&device_list_mutex);
411}
412
413static struct isert_device *
414isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
415{
416 struct isert_device *device;
417 int ret;
418
419 mutex_lock(&device_list_mutex);
420 list_for_each_entry(device, &device_list, dev_node) {
421 if (device->ib_device->node_guid == cma_id->device->node_guid) {
422 device->refcount++;
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200423 isert_info("Found iser device %p refcount %d\n",
424 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800425 mutex_unlock(&device_list_mutex);
426 return device;
427 }
428 }
429
430 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
431 if (!device) {
432 mutex_unlock(&device_list_mutex);
433 return ERR_PTR(-ENOMEM);
434 }
435
436 INIT_LIST_HEAD(&device->dev_node);
437
438 device->ib_device = cma_id->device;
439 ret = isert_create_device_ib_res(device);
440 if (ret) {
441 kfree(device);
442 mutex_unlock(&device_list_mutex);
443 return ERR_PTR(ret);
444 }
445
446 device->refcount++;
447 list_add_tail(&device->dev_node, &device_list);
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200448 isert_info("Created a new iser device %p refcount %d\n",
449 device, device->refcount);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800450 mutex_unlock(&device_list_mutex);
451
452 return device;
453}
454
Vu Pham59464ef2013-08-28 23:23:35 +0300455static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200456isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300457{
458 struct fast_reg_descriptor *fr_desc, *tmp;
459 int i = 0;
460
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200461 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300462 return;
463
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200464 isert_info("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300465
466 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200467 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300468 list_del(&fr_desc->list);
469 ib_free_fast_reg_page_list(fr_desc->data_frpl);
470 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200471 if (fr_desc->pi_ctx) {
472 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
473 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
474 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
475 kfree(fr_desc->pi_ctx);
476 }
Vu Pham59464ef2013-08-28 23:23:35 +0300477 kfree(fr_desc);
478 ++i;
479 }
480
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200481 if (i < isert_conn->conn_fr_pool_size)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200482 isert_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200483 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300484}
485
486static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200487isert_create_pi_ctx(struct fast_reg_descriptor *desc,
488 struct ib_device *device,
489 struct ib_pd *pd)
490{
491 struct ib_mr_init_attr mr_init_attr;
492 struct pi_context *pi_ctx;
493 int ret;
494
495 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
496 if (!pi_ctx) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200497 isert_err("Failed to allocate pi context\n");
Sagi Grimberg570db172014-12-02 16:57:31 +0200498 return -ENOMEM;
499 }
500
501 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
502 ISCSI_ISER_SG_TABLESIZE);
503 if (IS_ERR(pi_ctx->prot_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200504 isert_err("Failed to allocate prot frpl err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200505 PTR_ERR(pi_ctx->prot_frpl));
506 ret = PTR_ERR(pi_ctx->prot_frpl);
507 goto err_pi_ctx;
508 }
509
510 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
511 if (IS_ERR(pi_ctx->prot_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200512 isert_err("Failed to allocate prot frmr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200513 PTR_ERR(pi_ctx->prot_mr));
514 ret = PTR_ERR(pi_ctx->prot_mr);
515 goto err_prot_frpl;
516 }
517 desc->ind |= ISERT_PROT_KEY_VALID;
518
519 memset(&mr_init_attr, 0, sizeof(mr_init_attr));
520 mr_init_attr.max_reg_descriptors = 2;
521 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
522 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
523 if (IS_ERR(pi_ctx->sig_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200524 isert_err("Failed to allocate signature enabled mr err=%ld\n",
Sagi Grimberg570db172014-12-02 16:57:31 +0200525 PTR_ERR(pi_ctx->sig_mr));
526 ret = PTR_ERR(pi_ctx->sig_mr);
527 goto err_prot_mr;
528 }
529
530 desc->pi_ctx = pi_ctx;
531 desc->ind |= ISERT_SIG_KEY_VALID;
532 desc->ind &= ~ISERT_PROTECTED;
533
534 return 0;
535
536err_prot_mr:
537 ib_dereg_mr(desc->pi_ctx->prot_mr);
538err_prot_frpl:
539 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
540err_pi_ctx:
541 kfree(desc->pi_ctx);
542
543 return ret;
544}
545
546static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200547isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg570db172014-12-02 16:57:31 +0200548 struct fast_reg_descriptor *fr_desc)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200549{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200550 int ret;
551
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200552 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
553 ISCSI_ISER_SG_TABLESIZE);
554 if (IS_ERR(fr_desc->data_frpl)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200555 isert_err("Failed to allocate data frpl err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200556 PTR_ERR(fr_desc->data_frpl));
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200557 return PTR_ERR(fr_desc->data_frpl);
558 }
559
560 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
561 if (IS_ERR(fr_desc->data_mr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200562 isert_err("Failed to allocate data frmr err=%ld\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200563 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200564 ret = PTR_ERR(fr_desc->data_mr);
565 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200566 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200567 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200568
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200569 isert_dbg("Created fr_desc %p\n", fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200570
571 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +0200572
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200573err_data_frpl:
574 ib_free_fast_reg_page_list(fr_desc->data_frpl);
575
576 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200577}
578
579static int
Sagi Grimberg570db172014-12-02 16:57:31 +0200580isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300581{
582 struct fast_reg_descriptor *fr_desc;
583 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700584 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
585 struct se_node_acl *se_nacl = se_sess->se_node_acl;
586 int i, ret, tag_num;
587 /*
588 * Setup the number of FRMRs based upon the number of tags
589 * available to session in iscsi_target_locate_portal().
590 */
591 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
592 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300593
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200594 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700595 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300596 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
597 if (!fr_desc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200598 isert_err("Failed to allocate fast_reg descriptor\n");
Vu Pham59464ef2013-08-28 23:23:35 +0300599 ret = -ENOMEM;
600 goto err;
601 }
602
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200603 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +0300604 device->pd, fr_desc);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200605 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200606 isert_err("Failed to create fastreg descriptor err=%d\n",
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200607 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800608 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300609 goto err;
610 }
611
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200612 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
613 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300614 }
615
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200616 isert_dbg("Creating conn %p fastreg pool size=%d",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200617 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300618
619 return 0;
620
621err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200622 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300623 return ret;
624}
625
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800626static int
627isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
628{
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200629 struct isert_np *isert_np = cma_id->context;
630 struct iscsi_np *np = isert_np->np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800631 struct isert_conn *isert_conn;
632 struct isert_device *device;
633 struct ib_device *ib_dev = cma_id->device;
634 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300635
636 spin_lock_bh(&np->np_thread_lock);
637 if (!np->enabled) {
638 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200639 isert_dbg("iscsi_np is not enabled, reject connect request\n");
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300640 return rdma_reject(cma_id, NULL, 0);
641 }
642 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800643
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200644 isert_dbg("cma_id: %p, portal: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800645 cma_id, cma_id->context);
646
647 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
648 if (!isert_conn) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200649 isert_err("Unable to allocate isert_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800650 return -ENOMEM;
651 }
652 isert_conn->state = ISER_CONN_INIT;
653 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
654 init_completion(&isert_conn->conn_login_comp);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200655 init_completion(&isert_conn->login_req_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800656 init_completion(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800657 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700658 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300659 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700660 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800661
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800662 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800663
664 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
665 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
666 if (!isert_conn->login_buf) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200667 isert_err("Unable to allocate isert_conn->login_buf\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800668 ret = -ENOMEM;
669 goto out;
670 }
671
672 isert_conn->login_req_buf = isert_conn->login_buf;
673 isert_conn->login_rsp_buf = isert_conn->login_buf +
674 ISCSI_DEF_MAX_RECV_SEG_LEN;
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200675 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800676 isert_conn->login_buf, isert_conn->login_req_buf,
677 isert_conn->login_rsp_buf);
678
679 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
680 (void *)isert_conn->login_req_buf,
681 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
682
683 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
684 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200685 isert_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 ret);
687 isert_conn->login_req_dma = 0;
688 goto out_login_buf;
689 }
690
691 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
692 (void *)isert_conn->login_rsp_buf,
693 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
694
695 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
696 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200697 isert_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698 ret);
699 isert_conn->login_rsp_dma = 0;
700 goto out_req_dma_map;
701 }
702
703 device = isert_device_find_by_ib_dev(cma_id);
704 if (IS_ERR(device)) {
705 ret = PTR_ERR(device);
706 goto out_rsp_dma_map;
707 }
708
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300709 /* Set max inflight RDMA READ requests */
710 isert_conn->initiator_depth = min_t(u8,
711 event->param.conn.initiator_depth,
712 device->dev_attr.max_qp_init_rd_atom);
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200713 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300714
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800715 isert_conn->conn_device = device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800716
Sagi Grimberg570db172014-12-02 16:57:31 +0200717 ret = isert_conn_setup_qp(isert_conn, cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800718 if (ret)
719 goto out_conn_dev;
720
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200721 ret = isert_rdma_post_recvl(isert_conn);
722 if (ret)
723 goto out_conn_dev;
724
725 ret = isert_rdma_accept(isert_conn);
726 if (ret)
727 goto out_conn_dev;
728
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800729 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300730 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800731 mutex_unlock(&isert_np->np_accept_mutex);
732
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200733 isert_info("np %p: Allow accept_np to continue\n", np);
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300734 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800735 return 0;
736
737out_conn_dev:
738 isert_device_try_release(device);
739out_rsp_dma_map:
740 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
741 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
742out_req_dma_map:
743 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
744 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
745out_login_buf:
746 kfree(isert_conn->login_buf);
747out:
748 kfree(isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200749 rdma_reject(cma_id, NULL, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800750 return ret;
751}
752
753static void
754isert_connect_release(struct isert_conn *isert_conn)
755{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300757 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800758
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200759 isert_dbg("conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800760
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200761 if (device && device->use_fastreg)
762 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300763
Sagi Grimberg19e20902014-12-02 16:57:26 +0200764 isert_free_rx_descriptors(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300765 if (isert_conn->conn_cm_id)
766 rdma_destroy_id(isert_conn->conn_cm_id);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200767
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800768 if (isert_conn->conn_qp) {
Sagi Grimberg4a295ba2014-12-02 16:57:40 +0200769 struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
770
Sagi Grimberg40fc0692015-03-29 15:52:10 +0300771 isert_comp_put(comp);
Sagi Grimberg19e20902014-12-02 16:57:26 +0200772 ib_destroy_qp(isert_conn->conn_qp);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800773 }
774
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800775 if (isert_conn->login_buf) {
776 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
777 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
778 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
779 ISCSI_DEF_MAX_RECV_SEG_LEN,
780 DMA_FROM_DEVICE);
781 kfree(isert_conn->login_buf);
782 }
783 kfree(isert_conn);
784
785 if (device)
786 isert_device_try_release(device);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800787}
788
789static void
790isert_connected_handler(struct rdma_cm_id *cma_id)
791{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200792 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300793
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200794 isert_info("conn %p\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200795
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200796 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200797 isert_warn("conn %p connect_release is running\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +0200798 return;
799 }
800
801 mutex_lock(&isert_conn->conn_mutex);
802 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
803 isert_conn->state = ISER_CONN_UP;
804 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800805}
806
807static void
808isert_release_conn_kref(struct kref *kref)
809{
810 struct isert_conn *isert_conn = container_of(kref,
811 struct isert_conn, conn_kref);
812
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200813 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
814 current->pid);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800815
816 isert_connect_release(isert_conn);
817}
818
819static void
820isert_put_conn(struct isert_conn *isert_conn)
821{
822 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
823}
824
Sagi Grimberg954f2372014-12-02 16:57:17 +0200825/**
826 * isert_conn_terminate() - Initiate connection termination
827 * @isert_conn: isert connection struct
828 *
829 * Notes:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200830 * In case the connection state is FULL_FEATURE, move state
Sagi Grimberg954f2372014-12-02 16:57:17 +0200831 * to TEMINATING and start teardown sequence (rdma_disconnect).
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200832 * In case the connection state is UP, complete flush as well.
Sagi Grimberg954f2372014-12-02 16:57:17 +0200833 *
834 * This routine must be called with conn_mutex held. Thus it is
835 * safe to call multiple times.
836 */
837static void
838isert_conn_terminate(struct isert_conn *isert_conn)
839{
840 int err;
841
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200842 switch (isert_conn->state) {
843 case ISER_CONN_TERMINATING:
844 break;
845 case ISER_CONN_UP:
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200846 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200847 isert_info("Terminating conn %p state %d\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200848 isert_conn, isert_conn->state);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200849 isert_conn->state = ISER_CONN_TERMINATING;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200850 err = rdma_disconnect(isert_conn->conn_cm_id);
851 if (err)
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200852 isert_warn("Failed rdma_disconnect isert_conn %p\n",
Sagi Grimberg954f2372014-12-02 16:57:17 +0200853 isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200854 break;
855 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200856 isert_warn("conn %p teminating in state %d\n",
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200857 isert_conn, isert_conn->state);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200858 }
859}
860
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700861static int
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200862isert_np_cma_handler(struct isert_np *isert_np,
863 enum rdma_cm_event_type event)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800864{
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200865 isert_dbg("isert np %p, handling event %d\n", isert_np, event);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200866
867 switch (event) {
868 case RDMA_CM_EVENT_DEVICE_REMOVAL:
869 isert_np->np_cm_id = NULL;
870 break;
871 case RDMA_CM_EVENT_ADDR_CHANGE:
872 isert_np->np_cm_id = isert_setup_id(isert_np);
873 if (IS_ERR(isert_np->np_cm_id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200874 isert_err("isert np %p setup id failed: %ld\n",
875 isert_np, PTR_ERR(isert_np->np_cm_id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200876 isert_np->np_cm_id = NULL;
877 }
878 break;
879 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200880 isert_err("isert np %p Unexpected event %d\n",
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200881 isert_np, event);
882 }
883
884 return -1;
885}
886
887static int
888isert_disconnected_handler(struct rdma_cm_id *cma_id,
889 enum rdma_cm_event_type event)
890{
891 struct isert_np *isert_np = cma_id->context;
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700892 struct isert_conn *isert_conn;
893
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200894 if (isert_np->np_cm_id == cma_id)
895 return isert_np_cma_handler(cma_id->context, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700896
Sagi Grimberg19e20902014-12-02 16:57:26 +0200897 isert_conn = cma_id->qp->qp_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800898
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200899 mutex_lock(&isert_conn->conn_mutex);
900 isert_conn_terminate(isert_conn);
901 mutex_unlock(&isert_conn->conn_mutex);
902
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200903 isert_info("conn %p completing conn_wait\n", isert_conn);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +0200904 complete(&isert_conn->conn_wait);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700905
906 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800907}
908
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300909static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200910isert_connect_error(struct rdma_cm_id *cma_id)
911{
Sagi Grimberg19e20902014-12-02 16:57:26 +0200912 struct isert_conn *isert_conn = cma_id->qp->qp_context;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200913
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300914 isert_conn->conn_cm_id = NULL;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200915 isert_put_conn(isert_conn);
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300916
917 return -1;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200918}
919
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800920static int
921isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
922{
923 int ret = 0;
924
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200925 isert_info("event %d status %d id %p np %p\n", event->event,
926 event->status, cma_id, cma_id->context);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800927
928 switch (event->event) {
929 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800930 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700931 if (ret)
Sagi Grimberg4c22e072014-12-07 13:12:03 +0200932 isert_err("failed handle connect request %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800933 break;
934 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800935 isert_connected_handler(cma_id);
936 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300937 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
938 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
939 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300940 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimbergca6c1d82014-12-02 16:57:27 +0200941 ret = isert_disconnected_handler(cma_id, event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800942 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200943 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
944 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800945 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg4a579da2015-03-29 15:52:04 +0300946 ret = isert_connect_error(cma_id);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200947 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800948 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200949 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800950 break;
951 }
952
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800953 return ret;
954}
955
956static int
957isert_post_recv(struct isert_conn *isert_conn, u32 count)
958{
959 struct ib_recv_wr *rx_wr, *rx_wr_failed;
960 int i, ret;
961 unsigned int rx_head = isert_conn->conn_rx_desc_head;
962 struct iser_rx_desc *rx_desc;
963
964 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
965 rx_desc = &isert_conn->conn_rx_descs[rx_head];
Sagi Grimbergb0a191e2014-12-02 16:57:39 +0200966 rx_wr->wr_id = (uintptr_t)rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800967 rx_wr->sg_list = &rx_desc->rx_sg;
968 rx_wr->num_sge = 1;
969 rx_wr->next = rx_wr + 1;
970 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
971 }
972
973 rx_wr--;
974 rx_wr->next = NULL; /* mark end of work requests list */
975
976 isert_conn->post_recv_buf_count += count;
977 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
978 &rx_wr_failed);
979 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +0200980 isert_err("ib_post_recv() failed with ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981 isert_conn->post_recv_buf_count -= count;
982 } else {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +0100983 isert_dbg("Posted %d RX buffers\n", count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800984 isert_conn->conn_rx_desc_head = rx_head;
985 }
986 return ret;
987}
988
989static int
990isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
991{
992 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
993 struct ib_send_wr send_wr, *send_wr_failed;
994 int ret;
995
996 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
997 ISER_HEADERS_LEN, DMA_TO_DEVICE);
998
999 send_wr.next = NULL;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001000 send_wr.wr_id = (uintptr_t)tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001001 send_wr.sg_list = tx_desc->tx_sg;
1002 send_wr.num_sge = tx_desc->num_sge;
1003 send_wr.opcode = IB_WR_SEND;
1004 send_wr.send_flags = IB_SEND_SIGNALED;
1005
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001006 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02001007 if (ret)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001008 isert_err("ib_post_send() failed, ret: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001009
1010 return ret;
1011}
1012
1013static void
1014isert_create_send_desc(struct isert_conn *isert_conn,
1015 struct isert_cmd *isert_cmd,
1016 struct iser_tx_desc *tx_desc)
1017{
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001018 struct isert_device *device = isert_conn->conn_device;
1019 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001020
1021 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1022 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1023
1024 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1025 tx_desc->iser_header.flags = ISER_VER;
1026
1027 tx_desc->num_sge = 1;
1028 tx_desc->isert_cmd = isert_cmd;
1029
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001030 if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
1031 tx_desc->tx_sg[0].lkey = device->mr->lkey;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001032 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033 }
1034}
1035
1036static int
1037isert_init_tx_hdrs(struct isert_conn *isert_conn,
1038 struct iser_tx_desc *tx_desc)
1039{
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001040 struct isert_device *device = isert_conn->conn_device;
1041 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001042 u64 dma_addr;
1043
1044 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1045 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1046 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001047 isert_err("ib_dma_mapping_error() failed\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001048 return -ENOMEM;
1049 }
1050
1051 tx_desc->dma_addr = dma_addr;
1052 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1053 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001054 tx_desc->tx_sg[0].lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001055
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001056 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1057 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1058 tx_desc->tx_sg[0].lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001059
1060 return 0;
1061}
1062
1063static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001064isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001065 struct ib_send_wr *send_wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001066{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001067 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1068
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001069 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001070 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001071 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001072 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001073 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001074 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001075}
1076
1077static int
1078isert_rdma_post_recvl(struct isert_conn *isert_conn)
1079{
1080 struct ib_recv_wr rx_wr, *rx_wr_fail;
1081 struct ib_sge sge;
1082 int ret;
1083
1084 memset(&sge, 0, sizeof(struct ib_sge));
1085 sge.addr = isert_conn->login_req_dma;
1086 sge.length = ISER_RX_LOGIN_SIZE;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001087 sge.lkey = isert_conn->conn_device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001088
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001089 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001090 sge.addr, sge.length, sge.lkey);
1091
1092 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02001093 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001094 rx_wr.sg_list = &sge;
1095 rx_wr.num_sge = 1;
1096
1097 isert_conn->post_recv_buf_count++;
1098 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1099 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001100 isert_err("ib_post_recv() failed: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001101 isert_conn->post_recv_buf_count--;
1102 }
1103
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001104 return ret;
1105}
1106
1107static int
1108isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1109 u32 length)
1110{
1111 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001112 struct isert_device *device = isert_conn->conn_device;
1113 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001114 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1115 int ret;
1116
1117 isert_create_send_desc(isert_conn, NULL, tx_desc);
1118
1119 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1120 sizeof(struct iscsi_hdr));
1121
1122 isert_init_tx_hdrs(isert_conn, tx_desc);
1123
1124 if (length > 0) {
1125 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1126
1127 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1128 length, DMA_TO_DEVICE);
1129
1130 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1131
1132 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1133 length, DMA_TO_DEVICE);
1134
1135 tx_dsg->addr = isert_conn->login_rsp_dma;
1136 tx_dsg->length = length;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03001137 tx_dsg->lkey = isert_conn->conn_device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001138 tx_desc->num_sge = 2;
1139 }
1140 if (!login->login_failed) {
1141 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001142 if (!conn->sess->sess_ops->SessionType &&
1143 isert_conn->conn_device->use_fastreg) {
Sagi Grimberg570db172014-12-02 16:57:31 +02001144 ret = isert_conn_create_fastreg_pool(isert_conn);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001145 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001146 isert_err("Conn: %p failed to create"
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001147 " fastreg pool\n", isert_conn);
1148 return ret;
1149 }
1150 }
1151
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001152 ret = isert_alloc_rx_descriptors(isert_conn);
1153 if (ret)
1154 return ret;
1155
1156 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1157 if (ret)
1158 return ret;
1159
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001160 /* Now we are in FULL_FEATURE phase */
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001161 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg128e9cc2014-12-02 16:57:20 +02001162 isert_conn->state = ISER_CONN_FULL_FEATURE;
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001163 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001164 goto post_send;
1165 }
1166
1167 ret = isert_rdma_post_recvl(isert_conn);
1168 if (ret)
1169 return ret;
1170 }
1171post_send:
1172 ret = isert_post_send(isert_conn, tx_desc);
1173 if (ret)
1174 return ret;
1175
1176 return 0;
1177}
1178
1179static void
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001180isert_rx_login_req(struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001181{
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001182 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1183 int rx_buflen = isert_conn->login_req_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001184 struct iscsi_conn *conn = isert_conn->conn;
1185 struct iscsi_login *login = conn->conn_login;
1186 int size;
1187
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001188 isert_info("conn %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001189
1190 WARN_ON_ONCE(!login);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191
1192 if (login->first_request) {
1193 struct iscsi_login_req *login_req =
1194 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1195 /*
1196 * Setup the initial iscsi_login values from the leading
1197 * login request PDU.
1198 */
1199 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1200 login->current_stage =
1201 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1202 >> 2;
1203 login->version_min = login_req->min_version;
1204 login->version_max = login_req->max_version;
1205 memcpy(login->isid, login_req->isid, 6);
1206 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1207 login->init_task_tag = login_req->itt;
1208 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1209 login->cid = be16_to_cpu(login_req->cid);
1210 login->tsih = be16_to_cpu(login_req->tsih);
1211 }
1212
1213 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1214
1215 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001216 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1217 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1218 MAX_KEY_VALUE_PAIRS);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001219 memcpy(login->req_buf, &rx_desc->data[0], size);
1220
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001221 if (login->first_request) {
1222 complete(&isert_conn->conn_login_comp);
1223 return;
1224 }
1225 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001226}
1227
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001228static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001229*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001230{
Sagi Grimberg67004252015-03-29 15:52:09 +03001231 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001232 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001233 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001234
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001235 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001236 if (!cmd) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001237 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001238 return NULL;
1239 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001240 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001241 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001242 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001243
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001244 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001245}
1246
1247static int
1248isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001249 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1250 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252 struct iscsi_conn *conn = isert_conn->conn;
1253 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1254 struct scatterlist *sg;
1255 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1256 bool dump_payload = false;
1257
1258 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1259 if (rc < 0)
1260 return rc;
1261
1262 imm_data = cmd->immediate_data;
1263 imm_data_len = cmd->first_burst_len;
1264 unsol_data = cmd->unsolicited_data;
1265
1266 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1267 if (rc < 0) {
1268 return 0;
1269 } else if (rc > 0) {
1270 dump_payload = true;
1271 goto sequence_cmd;
1272 }
1273
1274 if (!imm_data)
1275 return 0;
1276
1277 sg = &cmd->se_cmd.t_data_sg[0];
1278 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1279
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001280 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001281 sg, sg_nents, &rx_desc->data[0], imm_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001282
1283 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1284
1285 cmd->write_data_done += imm_data_len;
1286
1287 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1288 spin_lock_bh(&cmd->istate_lock);
1289 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1290 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1291 spin_unlock_bh(&cmd->istate_lock);
1292 }
1293
1294sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001295 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001296
1297 if (!rc && dump_payload == false && unsol_data)
1298 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001299 else if (dump_payload && imm_data)
1300 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001301
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001302 return 0;
1303}
1304
1305static int
1306isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1307 struct iser_rx_desc *rx_desc, unsigned char *buf)
1308{
1309 struct scatterlist *sg_start;
1310 struct iscsi_conn *conn = isert_conn->conn;
1311 struct iscsi_cmd *cmd = NULL;
1312 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1313 u32 unsol_data_len = ntoh24(hdr->dlength);
1314 int rc, sg_nents, sg_off, page_off;
1315
1316 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1317 if (rc < 0)
1318 return rc;
1319 else if (!cmd)
1320 return 0;
1321 /*
1322 * FIXME: Unexpected unsolicited_data out
1323 */
1324 if (!cmd->unsolicited_data) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001325 isert_err("Received unexpected solicited data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001326 dump_stack();
1327 return -1;
1328 }
1329
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001330 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1331 "write_data_done: %u, data_length: %u\n",
1332 unsol_data_len, cmd->write_data_done,
1333 cmd->se_cmd.data_length);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001334
1335 sg_off = cmd->write_data_done / PAGE_SIZE;
1336 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1337 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1338 page_off = cmd->write_data_done % PAGE_SIZE;
1339 /*
1340 * FIXME: Non page-aligned unsolicited_data out
1341 */
1342 if (page_off) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001343 isert_err("unexpected non-page aligned data payload\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001344 dump_stack();
1345 return -1;
1346 }
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001347 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1348 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1349 sg_nents, &rx_desc->data[0], unsol_data_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001350
1351 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1352 unsol_data_len);
1353
1354 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1355 if (rc < 0)
1356 return rc;
1357
1358 return 0;
1359}
1360
1361static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001362isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001363 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1364 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001365{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001366 struct iscsi_conn *conn = isert_conn->conn;
1367 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1368 int rc;
1369
1370 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1371 if (rc < 0)
1372 return rc;
1373 /*
1374 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1375 */
1376
1377 return iscsit_process_nop_out(conn, cmd, hdr);
1378}
1379
1380static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001381isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001382 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1383 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001384{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001385 struct iscsi_conn *conn = isert_conn->conn;
1386 u32 payload_length = ntoh24(hdr->dlength);
1387 int rc;
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001388 unsigned char *text_in = NULL;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001389
1390 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1391 if (rc < 0)
1392 return rc;
1393
Sagi Grimbergb44a2b62015-01-26 12:49:08 +02001394 if (payload_length) {
1395 text_in = kzalloc(payload_length, GFP_KERNEL);
1396 if (!text_in) {
1397 isert_err("Unable to allocate text_in of payload_length: %u\n",
1398 payload_length);
1399 return -ENOMEM;
1400 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001401 }
1402 cmd->text_in_ptr = text_in;
1403
1404 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1405
1406 return iscsit_process_text_cmd(conn, cmd, hdr);
1407}
1408
1409static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001410isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1411 uint32_t read_stag, uint64_t read_va,
1412 uint32_t write_stag, uint64_t write_va)
1413{
1414 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1415 struct iscsi_conn *conn = isert_conn->conn;
1416 struct iscsi_cmd *cmd;
1417 struct isert_cmd *isert_cmd;
1418 int ret = -EINVAL;
1419 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1420
Sagi Grimbergfb140272015-03-29 15:52:08 +03001421 if (conn->sess->sess_ops->SessionType &&
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001422 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001423 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001424 " ignoring\n", opcode);
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001425 return 0;
1426 }
1427
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001428 switch (opcode) {
1429 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001430 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001431 if (!cmd)
1432 break;
1433
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001434 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001435 isert_cmd->read_stag = read_stag;
1436 isert_cmd->read_va = read_va;
1437 isert_cmd->write_stag = write_stag;
1438 isert_cmd->write_va = write_va;
1439
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001440 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001441 rx_desc, (unsigned char *)hdr);
1442 break;
1443 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001444 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001445 if (!cmd)
1446 break;
1447
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001448 isert_cmd = iscsit_priv_cmd(cmd);
1449 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001450 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001451 break;
1452 case ISCSI_OP_SCSI_DATA_OUT:
1453 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1454 (unsigned char *)hdr);
1455 break;
1456 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001457 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001458 if (!cmd)
1459 break;
1460
1461 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1462 (unsigned char *)hdr);
1463 break;
1464 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001465 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001466 if (!cmd)
1467 break;
1468
1469 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001471 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001472 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1473 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1474 if (!cmd)
1475 break;
1476 } else {
1477 cmd = isert_allocate_cmd(conn);
1478 if (!cmd)
1479 break;
1480 }
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001481
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001482 isert_cmd = iscsit_priv_cmd(cmd);
1483 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001484 rx_desc, (struct iscsi_text *)hdr);
1485 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001486 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001487 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001488 dump_stack();
1489 break;
1490 }
1491
1492 return ret;
1493}
1494
1495static void
1496isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1497{
1498 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1499 uint64_t read_va = 0, write_va = 0;
1500 uint32_t read_stag = 0, write_stag = 0;
1501 int rc;
1502
1503 switch (iser_hdr->flags & 0xF0) {
1504 case ISCSI_CTRL:
1505 if (iser_hdr->flags & ISER_RSV) {
1506 read_stag = be32_to_cpu(iser_hdr->read_stag);
1507 read_va = be64_to_cpu(iser_hdr->read_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001508 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1509 read_stag, (unsigned long long)read_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001510 }
1511 if (iser_hdr->flags & ISER_WSV) {
1512 write_stag = be32_to_cpu(iser_hdr->write_stag);
1513 write_va = be64_to_cpu(iser_hdr->write_va);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001514 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1515 write_stag, (unsigned long long)write_va);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001516 }
1517
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001518 isert_dbg("ISER ISCSI_CTRL PDU\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001519 break;
1520 case ISER_HELLO:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001521 isert_err("iSER Hello message\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001522 break;
1523 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001524 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001525 break;
1526 }
1527
1528 rc = isert_rx_opcode(isert_conn, rx_desc,
1529 read_stag, read_va, write_stag, write_va);
1530}
1531
1532static void
1533isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02001534 u32 xfer_len)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001535{
1536 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1537 struct iscsi_hdr *hdr;
1538 u64 rx_dma;
1539 int rx_buflen, outstanding;
1540
1541 if ((char *)desc == isert_conn->login_req_buf) {
1542 rx_dma = isert_conn->login_req_dma;
1543 rx_buflen = ISER_RX_LOGIN_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001544 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001545 rx_dma, rx_buflen);
1546 } else {
1547 rx_dma = desc->dma_addr;
1548 rx_buflen = ISER_RX_PAYLOAD_SIZE;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001549 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001550 rx_dma, rx_buflen);
1551 }
1552
1553 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1554
1555 hdr = &desc->iscsi_header;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001556 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001557 hdr->opcode, hdr->itt, hdr->flags,
1558 (int)(xfer_len - ISER_HEADERS_LEN));
1559
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001560 if ((char *)desc == isert_conn->login_req_buf) {
1561 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1562 if (isert_conn->conn) {
1563 struct iscsi_login *login = isert_conn->conn->conn_login;
1564
1565 if (login && !login->first_request)
1566 isert_rx_login_req(isert_conn);
1567 }
1568 mutex_lock(&isert_conn->conn_mutex);
1569 complete(&isert_conn->login_req_comp);
1570 mutex_unlock(&isert_conn->conn_mutex);
1571 } else {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001572 isert_rx_do_work(desc, isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02001573 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001574
1575 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1576 DMA_FROM_DEVICE);
1577
1578 isert_conn->post_recv_buf_count--;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001579 isert_dbg("Decremented post_recv_buf_count: %d\n",
1580 isert_conn->post_recv_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001581
1582 if ((char *)desc == isert_conn->login_req_buf)
1583 return;
1584
1585 outstanding = isert_conn->post_recv_buf_count;
1586 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1587 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1588 ISERT_MIN_POSTED_RX);
1589 err = isert_post_recv(isert_conn, count);
1590 if (err) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001591 isert_err("isert_post_recv() count: %d failed, %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001592 count, err);
1593 }
1594 }
1595}
1596
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001597static int
1598isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1599 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1600 enum iser_ib_op_code op, struct isert_data_buf *data)
1601{
1602 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1603
1604 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1605 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1606
1607 data->len = length - offset;
1608 data->offset = offset;
1609 data->sg_off = data->offset / PAGE_SIZE;
1610
1611 data->sg = &sg[data->sg_off];
1612 data->nents = min_t(unsigned int, nents - data->sg_off,
1613 ISCSI_ISER_SG_TABLESIZE);
1614 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1615 PAGE_SIZE);
1616
1617 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1618 data->dma_dir);
1619 if (unlikely(!data->dma_nents)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001620 isert_err("Cmd: unable to dma map SGs %p\n", sg);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001621 return -EINVAL;
1622 }
1623
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001624 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001625 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001626
1627 return 0;
1628}
1629
1630static void
1631isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1632{
1633 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1634
1635 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1636 memset(data, 0, sizeof(*data));
1637}
1638
1639
1640
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001641static void
1642isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1643{
1644 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001645
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001646 isert_dbg("Cmd %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001647
1648 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001649 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001650 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651 }
1652
Vu Pham90ecc6e2013-08-28 23:23:33 +03001653 if (wr->send_wr) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001654 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001655 kfree(wr->send_wr);
1656 wr->send_wr = NULL;
1657 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001658
Vu Pham90ecc6e2013-08-28 23:23:33 +03001659 if (wr->ib_sge) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001660 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001661 kfree(wr->ib_sge);
1662 wr->ib_sge = NULL;
1663 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001664}
1665
1666static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001667isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001668{
1669 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001670
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001671 isert_dbg("Cmd %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001672
1673 if (wr->fr_desc) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001674 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001675 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1676 isert_unmap_data_buf(isert_conn, &wr->prot);
1677 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1678 }
Vu Pham59464ef2013-08-28 23:23:35 +03001679 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001680 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001681 spin_unlock_bh(&isert_conn->conn_lock);
1682 wr->fr_desc = NULL;
1683 }
1684
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001685 if (wr->data.sg) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001686 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001687 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001688 }
1689
1690 wr->ib_sge = NULL;
1691 wr->send_wr = NULL;
1692}
1693
1694static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001695isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001697 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001698 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001699 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001700 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001701 struct iscsi_text_rsp *hdr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001703 isert_dbg("Cmd %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704
1705 switch (cmd->iscsi_opcode) {
1706 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001707 spin_lock_bh(&conn->cmd_lock);
1708 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001709 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001710 spin_unlock_bh(&conn->cmd_lock);
1711
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001712 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001713 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001714 /*
1715 * Check for special case during comp_err where
1716 * WRITE_PENDING has been handed off from core,
1717 * but requires an extra target_put_sess_cmd()
1718 * before transport_generic_free_cmd() below.
1719 */
1720 if (comp_err &&
1721 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1722 struct se_cmd *se_cmd = &cmd->se_cmd;
1723
1724 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1725 }
1726 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001727
Vu Phamd40945d2013-08-28 23:23:34 +03001728 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001729 transport_generic_free_cmd(&cmd->se_cmd, 0);
1730 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001731 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001732 spin_lock_bh(&conn->cmd_lock);
1733 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001734 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001735 spin_unlock_bh(&conn->cmd_lock);
1736
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001737 transport_generic_free_cmd(&cmd->se_cmd, 0);
1738 break;
1739 case ISCSI_OP_REJECT:
1740 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001741 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001742 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1743 /* If the continue bit is on, keep the command alive */
1744 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1745 break;
1746
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001747 spin_lock_bh(&conn->cmd_lock);
1748 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001749 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001750 spin_unlock_bh(&conn->cmd_lock);
1751
1752 /*
1753 * Handle special case for REJECT when iscsi_add_reject*() has
1754 * overwritten the original iscsi_opcode assignment, and the
1755 * associated cmd->se_cmd needs to be released.
1756 */
1757 if (cmd->se_cmd.se_tfo != NULL) {
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01001758 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001759 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001760 transport_generic_free_cmd(&cmd->se_cmd, 0);
1761 break;
1762 }
1763 /*
1764 * Fall-through
1765 */
1766 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001767 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001768 break;
1769 }
1770}
1771
1772static void
1773isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1774{
1775 if (tx_desc->dma_addr != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001776 isert_dbg("unmap single for tx_desc->dma_addr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001777 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1778 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1779 tx_desc->dma_addr = 0;
1780 }
1781}
1782
1783static void
1784isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001785 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001787 if (isert_cmd->pdu_buf_dma != 0) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001788 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001789 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1790 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1791 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001792 }
1793
1794 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001795 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796}
1797
Sagi Grimberg96b79732014-03-17 12:52:18 +02001798static int
1799isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1800{
1801 struct ib_mr_status mr_status;
1802 int ret;
1803
1804 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1805 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001806 isert_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001807 goto fail_mr_status;
1808 }
1809
1810 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1811 u64 sec_offset_err;
1812 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1813
1814 switch (mr_status.sig_err.err_type) {
1815 case IB_SIG_BAD_GUARD:
1816 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1817 break;
1818 case IB_SIG_BAD_REFTAG:
1819 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1820 break;
1821 case IB_SIG_BAD_APPTAG:
1822 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1823 break;
1824 }
1825 sec_offset_err = mr_status.sig_err.sig_err_offset;
1826 do_div(sec_offset_err, block_size);
1827 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1828
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001829 isert_err("PI error found type %d at sector 0x%llx "
1830 "expected 0x%x vs actual 0x%x\n",
1831 mr_status.sig_err.err_type,
1832 (unsigned long long)se_cmd->bad_sector,
1833 mr_status.sig_err.expected,
1834 mr_status.sig_err.actual);
Sagi Grimberg96b79732014-03-17 12:52:18 +02001835 ret = 1;
1836 }
1837
1838fail_mr_status:
1839 return ret;
1840}
1841
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001842static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001843isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1844 struct isert_cmd *isert_cmd)
1845{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001846 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001847 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001848 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001849 struct isert_conn *isert_conn = isert_cmd->conn;
1850 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001851 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001852
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001853 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001854 ret = isert_check_pi_status(se_cmd,
1855 wr->fr_desc->pi_ctx->sig_mr);
1856 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001857 }
1858
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001859 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001860 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001861 if (ret)
1862 transport_send_check_condition_and_sense(se_cmd,
1863 se_cmd->pi_err, 0);
1864 else
1865 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001866}
1867
1868static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001869isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1870 struct isert_cmd *isert_cmd)
1871{
1872 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001873 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001874 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001875 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001876 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001877 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001878
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001879 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001880 ret = isert_check_pi_status(se_cmd,
1881 wr->fr_desc->pi_ctx->sig_mr);
1882 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001883 }
1884
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001885 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001886 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001887 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001888 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001889
Sagi Grimberg24f412d2014-12-07 13:12:02 +02001890 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001891 spin_lock_bh(&cmd->istate_lock);
1892 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1893 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1894 spin_unlock_bh(&cmd->istate_lock);
1895
Sagi Grimberg364189f2015-03-29 15:52:03 +03001896 if (ret) {
1897 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001898 transport_send_check_condition_and_sense(se_cmd,
1899 se_cmd->pi_err, 0);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001900 } else {
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001901 target_execute_cmd(se_cmd);
Sagi Grimberg364189f2015-03-29 15:52:03 +03001902 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001903}
1904
1905static void
1906isert_do_control_comp(struct work_struct *work)
1907{
1908 struct isert_cmd *isert_cmd = container_of(work,
1909 struct isert_cmd, comp_work);
1910 struct isert_conn *isert_conn = isert_cmd->conn;
1911 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001912 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001914 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1915
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001916 switch (cmd->i_state) {
1917 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001918 iscsit_tmr_post_handler(cmd, cmd->conn);
Sagi Grimberg10633c32014-12-07 13:12:04 +02001919 case ISTATE_SEND_REJECT: /* FALLTHRU */
1920 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001921 cmd->i_state = ISTATE_SENT_STATUS;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001922 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1923 ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001924 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925 case ISTATE_SEND_LOGOUTRSP:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001926 iscsit_logout_post_handler(cmd, cmd->conn);
1927 break;
1928 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001929 isert_err("Unknown i_state %d\n", cmd->i_state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001930 dump_stack();
1931 break;
1932 }
1933}
1934
1935static void
1936isert_response_completion(struct iser_tx_desc *tx_desc,
1937 struct isert_cmd *isert_cmd,
1938 struct isert_conn *isert_conn,
1939 struct ib_device *ib_dev)
1940{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001941 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001942
1943 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001944 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001945 cmd->i_state == ISTATE_SEND_REJECT ||
1946 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001947 isert_unmap_tx_desc(tx_desc, ib_dev);
1948
1949 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1950 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1951 return;
1952 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001953
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001954 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001955 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956}
1957
1958static void
Sagi Grimberg68a86de2014-12-02 16:57:37 +02001959isert_send_completion(struct iser_tx_desc *tx_desc,
1960 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001961{
1962 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1963 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1964 struct isert_rdma_wr *wr;
1965
1966 if (!isert_cmd) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967 isert_unmap_tx_desc(tx_desc, ib_dev);
1968 return;
1969 }
1970 wr = &isert_cmd->rdma_wr;
1971
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001972 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
1973
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001974 switch (wr->iser_ib_op) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001975 case ISER_IB_SEND:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001976 isert_response_completion(tx_desc, isert_cmd,
1977 isert_conn, ib_dev);
1978 break;
1979 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001980 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001981 break;
1982 case ISER_IB_RDMA_READ:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001983 isert_completion_rdma_read(tx_desc, isert_cmd);
1984 break;
1985 default:
Sagi Grimberg4c22e072014-12-07 13:12:03 +02001986 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001987 dump_stack();
1988 break;
1989 }
1990}
1991
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02001992/**
1993 * is_isert_tx_desc() - Indicate if the completion wr_id
1994 * is a TX descriptor or not.
1995 * @isert_conn: iser connection
1996 * @wr_id: completion WR identifier
1997 *
1998 * Since we cannot rely on wc opcode in FLUSH errors
1999 * we must work around it by checking if the wr_id address
2000 * falls in the iser connection rx_descs buffer. If so
2001 * it is an RX descriptor, otherwize it is a TX.
2002 */
2003static inline bool
2004is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002005{
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002006 void *start = isert_conn->conn_rx_descs;
2007 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
2008
2009 if (wr_id >= start && wr_id < start + len)
2010 return false;
2011
2012 return true;
2013}
2014
2015static void
2016isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
2017{
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002018 if (wc->wr_id == ISER_BEACON_WRID) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002019 isert_info("conn %p completing conn_wait_comp_err\n",
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002020 isert_conn);
2021 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingered4520a2014-12-07 13:12:05 +02002022 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002023 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2024 struct isert_cmd *isert_cmd;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002025 struct iser_tx_desc *desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002027 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2028 isert_cmd = desc->isert_cmd;
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002029 if (!isert_cmd)
2030 isert_unmap_tx_desc(desc, ib_dev);
2031 else
2032 isert_completion_put(desc, isert_cmd, ib_dev, true);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002033 } else {
2034 isert_conn->post_recv_buf_count--;
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002035 if (!isert_conn->post_recv_buf_count)
2036 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
Sagi Grimbergdf43deb2014-12-02 16:57:38 +02002037 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002038}
2039
2040static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002041isert_handle_wc(struct ib_wc *wc)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002042{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043 struct isert_conn *isert_conn;
2044 struct iser_tx_desc *tx_desc;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002045 struct iser_rx_desc *rx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002046
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002047 isert_conn = wc->qp->qp_context;
2048 if (likely(wc->status == IB_WC_SUCCESS)) {
2049 if (wc->opcode == IB_WC_RECV) {
2050 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
2051 isert_rx_completion(rx_desc, isert_conn, wc->byte_len);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052 } else {
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002053 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2054 isert_send_completion(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002055 }
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002056 } else {
2057 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002058 isert_err("wr id %llx status %d vend_err %x\n",
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002059 wc->wr_id, wc->status, wc->vendor_err);
2060 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002061 isert_dbg("flush error: wr id %llx\n", wc->wr_id);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002062
2063 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2064 isert_cq_comp_err(isert_conn, wc);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002066}
2067
2068static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002069isert_cq_work(struct work_struct *work)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070{
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002071 enum { isert_poll_budget = 65536 };
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002072 struct isert_comp *comp = container_of(work, struct isert_comp,
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002073 work);
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002074 struct ib_wc *const wcs = comp->wcs;
2075 int i, n, completed = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002076
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002077 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2078 for (i = 0; i < n; i++)
2079 isert_handle_wc(&wcs[i]);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002080
Sagi Grimberg36ea63b2014-12-02 16:57:45 +02002081 completed += n;
2082 if (completed >= isert_poll_budget)
Sagi Grimberg37d9fe82014-12-02 16:57:44 +02002083 break;
2084 }
2085
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002086 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002087}
2088
2089static void
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002090isert_cq_callback(struct ib_cq *cq, void *context)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002091{
Sagi Grimberg4a295ba2014-12-02 16:57:40 +02002092 struct isert_comp *comp = context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002093
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02002094 queue_work(isert_comp_wq, &comp->work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002095}
2096
2097static int
2098isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2099{
2100 struct ib_send_wr *wr_failed;
2101 int ret;
2102
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002103 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2104 &wr_failed);
2105 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002106 isert_err("ib_post_send failed with %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002107 return ret;
2108 }
2109 return ret;
2110}
2111
2112static int
2113isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2114{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002115 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002116 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002117 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2118 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2119 &isert_cmd->tx_desc.iscsi_header;
2120
2121 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2122 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2123 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2124 /*
2125 * Attach SENSE DATA payload to iSCSI Response PDU
2126 */
2127 if (cmd->se_cmd.sense_buffer &&
2128 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2129 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002130 struct isert_device *device = isert_conn->conn_device;
2131 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002132 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002133 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002134
2135 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2136 cmd->sense_buffer);
2137 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2138
2139 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2140 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002141 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002142
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002143 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2144 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002145 DMA_TO_DEVICE);
2146
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002147 isert_cmd->pdu_buf_len = pdu_len;
2148 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2149 tx_dsg->length = pdu_len;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002150 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002151 isert_cmd->tx_desc.num_sge = 2;
2152 }
2153
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002154 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002155
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002156 isert_dbg("Posting SCSI Response\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002157
2158 return isert_post_response(isert_conn, isert_cmd);
2159}
2160
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002161static void
2162isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2163{
2164 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002165 struct isert_conn *isert_conn = conn->context;
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002166 struct isert_device *device = isert_conn->conn_device;
2167
2168 spin_lock_bh(&conn->cmd_lock);
2169 if (!list_empty(&cmd->i_conn_node))
2170 list_del_init(&cmd->i_conn_node);
2171 spin_unlock_bh(&conn->cmd_lock);
2172
2173 if (cmd->data_direction == DMA_TO_DEVICE)
2174 iscsit_stop_dataout_timer(cmd);
2175
2176 device->unreg_rdma_mem(isert_cmd, isert_conn);
2177}
2178
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002179static enum target_prot_op
2180isert_get_sup_prot_ops(struct iscsi_conn *conn)
2181{
Sagi Grimberg67004252015-03-29 15:52:09 +03002182 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002183 struct isert_device *device = isert_conn->conn_device;
2184
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002185 if (conn->tpg->tpg_attrib.t10_pi) {
2186 if (device->pi_capable) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002187 isert_info("conn %p PI offload enabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002188 isert_conn->pi_support = true;
2189 return TARGET_PROT_ALL;
2190 }
2191 }
2192
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002193 isert_info("conn %p PI offload disabled\n", isert_conn);
Sagi Grimberg23a548e2014-12-02 16:57:35 +02002194 isert_conn->pi_support = false;
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002195
2196 return TARGET_PROT_NORMAL;
2197}
2198
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002199static int
2200isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2201 bool nopout_response)
2202{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002203 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002204 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002205 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2206
2207 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2208 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2209 &isert_cmd->tx_desc.iscsi_header,
2210 nopout_response);
2211 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002212 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002213
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002214 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002215
2216 return isert_post_response(isert_conn, isert_cmd);
2217}
2218
2219static int
2220isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
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_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2228 &isert_cmd->tx_desc.iscsi_header);
2229 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002230 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002232 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002233
2234 return isert_post_response(isert_conn, isert_cmd);
2235}
2236
2237static int
2238isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2239{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002240 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002241 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002242 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2243
2244 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2245 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2246 &isert_cmd->tx_desc.iscsi_header);
2247 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002248 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002249
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002250 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002251
2252 return isert_post_response(isert_conn, isert_cmd);
2253}
2254
2255static int
2256isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2257{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002258 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002259 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002260 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002261 struct isert_device *device = isert_conn->conn_device;
2262 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002263 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2264 struct iscsi_reject *hdr =
2265 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002266
2267 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002268 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002269 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002270
2271 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002272 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002273 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2274 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002275 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2276 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002277 tx_dsg->length = ISCSI_HDR_LEN;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002278 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002279 isert_cmd->tx_desc.num_sge = 2;
2280
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002281 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002282
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002283 isert_dbg("conn %p Posting Reject\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002284
2285 return isert_post_response(isert_conn, isert_cmd);
2286}
2287
2288static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002289isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2290{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002291 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002292 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002293 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2294 struct iscsi_text_rsp *hdr =
2295 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2296 u32 txt_rsp_len;
2297 int rc;
2298
2299 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002300 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002301 if (rc < 0)
2302 return rc;
2303
2304 txt_rsp_len = rc;
2305 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2306
2307 if (txt_rsp_len) {
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002308 struct isert_device *device = isert_conn->conn_device;
2309 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002310 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2311 void *txt_rsp_buf = cmd->buf_ptr;
2312
2313 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2314 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2315
2316 isert_cmd->pdu_buf_len = txt_rsp_len;
2317 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2318 tx_dsg->length = txt_rsp_len;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002319 tx_dsg->lkey = device->mr->lkey;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002320 isert_cmd->tx_desc.num_sge = 2;
2321 }
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002322 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002323
Sagi Grimbergf64d2792015-01-25 19:11:20 +02002324 isert_dbg("conn %p Text Response\n", isert_conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002325
2326 return isert_post_response(isert_conn, isert_cmd);
2327}
2328
2329static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002330isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2331 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2332 u32 data_left, u32 offset)
2333{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002334 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002335 struct scatterlist *sg_start, *tmp_sg;
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002336 struct isert_device *device = isert_conn->conn_device;
2337 struct ib_device *ib_dev = device->ib_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002338 u32 sg_off, page_off;
2339 int i = 0, sg_nents;
2340
2341 sg_off = offset / PAGE_SIZE;
2342 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2343 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2344 page_off = offset % PAGE_SIZE;
2345
2346 send_wr->sg_list = ib_sge;
2347 send_wr->num_sge = sg_nents;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002348 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349 /*
2350 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2351 */
2352 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002353 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2354 "page_off: %u\n",
2355 (unsigned long long)tmp_sg->dma_address,
2356 tmp_sg->length, page_off);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357
2358 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2359 ib_sge->length = min_t(u32, data_left,
2360 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002361 ib_sge->lkey = device->mr->lkey;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002362
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002363 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2364 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002365 page_off = 0;
2366 data_left -= ib_sge->length;
2367 ib_sge++;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002368 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002369 }
2370
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002371 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002372 send_wr->sg_list, send_wr->num_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002373
2374 return sg_nents;
2375}
2376
2377static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002378isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2379 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002380{
2381 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002382 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberg67004252015-03-29 15:52:09 +03002383 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002384 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002385 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002386 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002387 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2388 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002389
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002390 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002391
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002392 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2393 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2394 se_cmd->t_data_nents, se_cmd->data_length,
2395 offset, wr->iser_ib_op, &wr->data);
2396 if (ret)
2397 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002399 data_left = data->len;
2400 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002401
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002402 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 if (!ib_sge) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002404 isert_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002405 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002406 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002407 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002408 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002409
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002410 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2412 GFP_KERNEL);
2413 if (!wr->send_wr) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002414 isert_dbg("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002415 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002416 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002417 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418
2419 wr->isert_cmd = isert_cmd;
2420 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002421
2422 for (i = 0; i < wr->send_wr_num; i++) {
2423 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2424 data_len = min(data_left, rdma_write_max);
2425
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002427 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2428 send_wr->opcode = IB_WR_RDMA_WRITE;
2429 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2430 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2431 if (i + 1 == wr->send_wr_num)
2432 send_wr->next = &isert_cmd->tx_desc.send_wr;
2433 else
2434 send_wr->next = &wr->send_wr[i + 1];
2435 } else {
2436 send_wr->opcode = IB_WR_RDMA_READ;
2437 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2438 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2439 if (i + 1 == wr->send_wr_num)
2440 send_wr->send_flags = IB_SEND_SIGNALED;
2441 else
2442 send_wr->next = &wr->send_wr[i + 1];
2443 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002444
2445 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2446 send_wr, data_len, offset);
2447 ib_sge += ib_sge_cnt;
2448
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002449 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002450 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451 data_left -= data_len;
2452 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002453
2454 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002455unmap_cmd:
2456 isert_unmap_data_buf(isert_conn, data);
2457
Vu Pham90ecc6e2013-08-28 23:23:33 +03002458 return ret;
2459}
2460
2461static int
Vu Pham59464ef2013-08-28 23:23:35 +03002462isert_map_fr_pagelist(struct ib_device *ib_dev,
2463 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2464{
2465 u64 start_addr, end_addr, page, chunk_start = 0;
2466 struct scatterlist *tmp_sg;
2467 int i = 0, new_chunk, last_ent, n_pages;
2468
2469 n_pages = 0;
2470 new_chunk = 1;
2471 last_ent = sg_nents - 1;
2472 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2473 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2474 if (new_chunk)
2475 chunk_start = start_addr;
2476 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2477
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002478 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2479 i, (unsigned long long)tmp_sg->dma_address,
2480 tmp_sg->length);
Vu Pham59464ef2013-08-28 23:23:35 +03002481
2482 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2483 new_chunk = 0;
2484 continue;
2485 }
2486 new_chunk = 1;
2487
2488 page = chunk_start & PAGE_MASK;
2489 do {
2490 fr_pl[n_pages++] = page;
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002491 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2492 n_pages - 1, page);
Vu Pham59464ef2013-08-28 23:23:35 +03002493 page += PAGE_SIZE;
2494 } while (page < end_addr);
2495 }
2496
2497 return n_pages;
2498}
2499
Sagi Grimberg10633c32014-12-07 13:12:04 +02002500static inline void
2501isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2502{
2503 u32 rkey;
2504
2505 memset(inv_wr, 0, sizeof(*inv_wr));
2506 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2507 inv_wr->opcode = IB_WR_LOCAL_INV;
2508 inv_wr->ex.invalidate_rkey = mr->rkey;
2509
2510 /* Bump the key */
2511 rkey = ib_inc_rkey(mr->rkey);
2512 ib_update_fast_reg_key(mr, rkey);
2513}
2514
Vu Pham59464ef2013-08-28 23:23:35 +03002515static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002516isert_fast_reg_mr(struct isert_conn *isert_conn,
2517 struct fast_reg_descriptor *fr_desc,
2518 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002519 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002520 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002521{
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002522 struct isert_device *device = isert_conn->conn_device;
2523 struct ib_device *ib_dev = device->ib_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002524 struct ib_mr *mr;
2525 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002526 struct ib_send_wr fr_wr, inv_wr;
2527 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002528 int ret, pagelist_len;
2529 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002530
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002531 if (mem->dma_nents == 1) {
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002532 sge->lkey = device->mr->lkey;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002533 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2534 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002535 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2536 sge->addr, sge->length, sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002537 return 0;
2538 }
2539
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002540 if (ind == ISERT_DATA_KEY_VALID) {
2541 /* Registering data buffer */
2542 mr = fr_desc->data_mr;
2543 frpl = fr_desc->data_frpl;
2544 } else {
2545 /* Registering protection buffer */
2546 mr = fr_desc->pi_ctx->prot_mr;
2547 frpl = fr_desc->pi_ctx->prot_frpl;
2548 }
2549
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002550 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002551
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002552 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002553 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002554
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002555 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002556 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002557
Sagi Grimberg10633c32014-12-07 13:12:04 +02002558 if (!(fr_desc->ind & ind)) {
2559 isert_inv_rkey(&inv_wr, mr);
Vu Pham59464ef2013-08-28 23:23:35 +03002560 wr = &inv_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002561 }
2562
2563 /* Prepare FASTREG WR */
2564 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002565 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002566 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002567 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2568 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002569 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2570 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002571 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002572 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002573 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2574
2575 if (!wr)
2576 wr = &fr_wr;
2577 else
2578 wr->next = &fr_wr;
2579
2580 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2581 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002582 isert_err("fast registration failed, ret:%d\n", ret);
Vu Pham59464ef2013-08-28 23:23:35 +03002583 return ret;
2584 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002585 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002586
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002587 sge->lkey = mr->lkey;
2588 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002589 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002590
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002591 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2592 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002593
2594 return ret;
2595}
2596
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002597static inline void
2598isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2599 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002600{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002601 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002602 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2603 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2604 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002605 /*
2606 * At the moment we hard code those, but if in the future
2607 * the target core would like to use it, we will take it
2608 * from se_cmd.
2609 */
2610 domain->sig.dif.apptag_check_mask = 0xffff;
2611 domain->sig.dif.app_escape = true;
2612 domain->sig.dif.ref_escape = true;
2613 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2614 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2615 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002616};
2617
2618static int
2619isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2620{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002621 switch (se_cmd->prot_op) {
2622 case TARGET_PROT_DIN_INSERT:
2623 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002624 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002625 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002626 break;
2627 case TARGET_PROT_DOUT_INSERT:
2628 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002629 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002630 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002631 break;
2632 case TARGET_PROT_DIN_PASS:
2633 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002634 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2635 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002636 break;
2637 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002638 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002639 return -EINVAL;
2640 }
2641
2642 return 0;
2643}
2644
2645static inline u8
2646isert_set_prot_checks(u8 prot_checks)
2647{
2648 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2649 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2650 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2651}
2652
2653static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002654isert_reg_sig_mr(struct isert_conn *isert_conn,
2655 struct se_cmd *se_cmd,
2656 struct isert_rdma_wr *rdma_wr,
2657 struct fast_reg_descriptor *fr_desc)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002658{
2659 struct ib_send_wr sig_wr, inv_wr;
2660 struct ib_send_wr *bad_wr, *wr = NULL;
2661 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2662 struct ib_sig_attrs sig_attrs;
2663 int ret;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002664
2665 memset(&sig_attrs, 0, sizeof(sig_attrs));
2666 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2667 if (ret)
2668 goto err;
2669
2670 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2671
2672 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
Sagi Grimberg10633c32014-12-07 13:12:04 +02002673 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002674 wr = &inv_wr;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002675 }
2676
2677 memset(&sig_wr, 0, sizeof(sig_wr));
2678 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002679 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg570db172014-12-02 16:57:31 +02002680 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002681 sig_wr.num_sge = 1;
2682 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2683 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2684 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2685 if (se_cmd->t_prot_sg)
Sagi Grimberg570db172014-12-02 16:57:31 +02002686 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002687
2688 if (!wr)
2689 wr = &sig_wr;
2690 else
2691 wr->next = &sig_wr;
2692
2693 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2694 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002695 isert_err("fast registration failed, ret:%d\n", ret);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002696 goto err;
2697 }
2698 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2699
Sagi Grimberg570db172014-12-02 16:57:31 +02002700 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2701 rdma_wr->ib_sg[SIG].addr = 0;
2702 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002703 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2704 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2705 /*
2706 * We have protection guards on the wire
2707 * so we need to set a larget transfer
2708 */
Sagi Grimberg570db172014-12-02 16:57:31 +02002709 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002710
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002711 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002712 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2713 rdma_wr->ib_sg[SIG].lkey);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002714err:
2715 return ret;
2716}
2717
Vu Pham59464ef2013-08-28 23:23:35 +03002718static int
Sagi Grimberg570db172014-12-02 16:57:31 +02002719isert_handle_prot_cmd(struct isert_conn *isert_conn,
2720 struct isert_cmd *isert_cmd,
2721 struct isert_rdma_wr *wr)
2722{
2723 struct isert_device *device = isert_conn->conn_device;
2724 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2725 int ret;
2726
2727 if (!wr->fr_desc->pi_ctx) {
2728 ret = isert_create_pi_ctx(wr->fr_desc,
2729 device->ib_device,
Sagi Grimberg67cb3942015-03-29 15:52:05 +03002730 device->pd);
Sagi Grimberg570db172014-12-02 16:57:31 +02002731 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002732 isert_err("conn %p failed to allocate pi_ctx\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002733 isert_conn);
2734 return ret;
2735 }
2736 }
2737
2738 if (se_cmd->t_prot_sg) {
2739 ret = isert_map_data_buf(isert_conn, isert_cmd,
2740 se_cmd->t_prot_sg,
2741 se_cmd->t_prot_nents,
2742 se_cmd->prot_length,
2743 0, wr->iser_ib_op, &wr->prot);
2744 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002745 isert_err("conn %p failed to map protection buffer\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002746 isert_conn);
2747 return ret;
2748 }
2749
2750 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2751 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2752 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2753 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002754 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002755 isert_conn);
2756 goto unmap_prot_cmd;
2757 }
2758 }
2759
2760 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2761 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002762 isert_err("conn %p failed to fast reg mr\n",
Sagi Grimberg570db172014-12-02 16:57:31 +02002763 isert_conn);
2764 goto unmap_prot_cmd;
2765 }
2766 wr->fr_desc->ind |= ISERT_PROTECTED;
2767
2768 return 0;
2769
2770unmap_prot_cmd:
2771 if (se_cmd->t_prot_sg)
2772 isert_unmap_data_buf(isert_conn, &wr->prot);
2773
2774 return ret;
2775}
2776
2777static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002778isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2779 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002780{
2781 struct se_cmd *se_cmd = &cmd->se_cmd;
2782 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002783 struct isert_conn *isert_conn = conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002784 struct fast_reg_descriptor *fr_desc = NULL;
Sagi Grimberg570db172014-12-02 16:57:31 +02002785 struct ib_send_wr *send_wr;
2786 struct ib_sge *ib_sg;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002787 u32 offset;
2788 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002789 unsigned long flags;
2790
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002791 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2792
2793 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2794 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2795 se_cmd->t_data_nents, se_cmd->data_length,
2796 offset, wr->iser_ib_op, &wr->data);
2797 if (ret)
2798 return ret;
2799
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002800 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002801 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2802 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2803 struct fast_reg_descriptor, list);
2804 list_del(&fr_desc->list);
2805 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2806 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002807 }
2808
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002809 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
Sagi Grimberg570db172014-12-02 16:57:31 +02002810 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002811 if (ret)
2812 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002813
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002814 if (isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg570db172014-12-02 16:57:31 +02002815 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002816 if (ret)
Sagi Grimberg570db172014-12-02 16:57:31 +02002817 goto unmap_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002818
Sagi Grimberg570db172014-12-02 16:57:31 +02002819 ib_sg = &wr->ib_sg[SIG];
2820 } else {
2821 ib_sg = &wr->ib_sg[DATA];
2822 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002823
Sagi Grimberg570db172014-12-02 16:57:31 +02002824 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002825 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002826 wr->send_wr_num = 1;
2827 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2828 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002829 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002830
2831 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002832 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002833 send_wr->num_sge = 1;
Sagi Grimbergb0a191e2014-12-02 16:57:39 +02002834 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002835 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2836 send_wr->opcode = IB_WR_RDMA_WRITE;
2837 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2838 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002839 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002840 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002841 } else {
2842 send_wr->opcode = IB_WR_RDMA_READ;
2843 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2844 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2845 send_wr->send_flags = IB_SEND_SIGNALED;
2846 }
2847
Vu Pham59464ef2013-08-28 23:23:35 +03002848 return 0;
Sagi Grimberg570db172014-12-02 16:57:31 +02002849
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002850unmap_cmd:
2851 if (fr_desc) {
2852 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2853 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2854 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2855 }
2856 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002857
Vu Pham59464ef2013-08-28 23:23:35 +03002858 return ret;
2859}
2860
2861static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002862isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2863{
2864 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002865 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002866 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002867 struct isert_conn *isert_conn = conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002868 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002869 struct ib_send_wr *wr_failed;
2870 int rc;
2871
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002872 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002873 isert_cmd, se_cmd->data_length);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02002874
Vu Pham90ecc6e2013-08-28 23:23:33 +03002875 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002876 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002877 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002878 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002879 return rc;
2880 }
2881
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002882 if (!isert_prot_cmd(isert_conn, se_cmd)) {
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002883 /*
2884 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2885 */
2886 isert_create_send_desc(isert_conn, isert_cmd,
2887 &isert_cmd->tx_desc);
2888 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2889 &isert_cmd->tx_desc.iscsi_header);
2890 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2891 isert_init_send_wr(isert_conn, isert_cmd,
Sagi Grimberg68a86de2014-12-02 16:57:37 +02002892 &isert_cmd->tx_desc.send_wr);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002893 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002894 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002895 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002897 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002898 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002899 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002900
Sagi Grimberg302cc7c2014-12-02 16:57:34 +02002901 if (!isert_prot_cmd(isert_conn, se_cmd))
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002902 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002903 "READ\n", isert_cmd);
2904 else
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002905 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002906 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002907
Vu Pham90ecc6e2013-08-28 23:23:33 +03002908 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002909}
2910
2911static int
2912isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2913{
2914 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002915 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002916 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimberg67004252015-03-29 15:52:09 +03002917 struct isert_conn *isert_conn = conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002918 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002919 struct ib_send_wr *wr_failed;
2920 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002921
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002922 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002923 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002924 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002925 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002926 if (rc) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002927 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002928 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002929 }
2930
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002931 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002932 if (rc)
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002933 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02002934
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002935 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
Vu Pham90ecc6e2013-08-28 23:23:33 +03002936 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002937
Vu Pham90ecc6e2013-08-28 23:23:33 +03002938 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002939}
2940
2941static int
2942isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2943{
2944 int ret;
2945
2946 switch (state) {
2947 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2948 ret = isert_put_nopin(cmd, conn, false);
2949 break;
2950 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002951 isert_err("Unknown immediate state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002952 ret = -EINVAL;
2953 break;
2954 }
2955
2956 return ret;
2957}
2958
2959static int
2960isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2961{
Sagi Grimberg991bb762014-12-07 13:12:01 +02002962 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002963 int ret;
2964
2965 switch (state) {
2966 case ISTATE_SEND_LOGOUTRSP:
2967 ret = isert_put_logout_rsp(cmd, conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02002968 if (!ret)
2969 isert_conn->logout_posted = true;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002970 break;
2971 case ISTATE_SEND_NOPIN:
2972 ret = isert_put_nopin(cmd, conn, true);
2973 break;
2974 case ISTATE_SEND_TASKMGTRSP:
2975 ret = isert_put_tm_rsp(cmd, conn);
2976 break;
2977 case ISTATE_SEND_REJECT:
2978 ret = isert_put_reject(cmd, conn);
2979 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002980 case ISTATE_SEND_TEXTRSP:
2981 ret = isert_put_text_rsp(cmd, conn);
2982 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002983 case ISTATE_SEND_STATUS:
2984 /*
2985 * Special case for sending non GOOD SCSI status from TX thread
2986 * context during pre se_cmd excecution failure.
2987 */
2988 ret = isert_put_response(conn, cmd);
2989 break;
2990 default:
Sagi Grimberg24f412d2014-12-07 13:12:02 +02002991 isert_err("Unknown response state: 0x%02x\n", state);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002992 ret = -EINVAL;
2993 break;
2994 }
2995
2996 return ret;
2997}
2998
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02002999struct rdma_cm_id *
3000isert_setup_id(struct isert_np *isert_np)
3001{
3002 struct iscsi_np *np = isert_np->np;
3003 struct rdma_cm_id *id;
3004 struct sockaddr *sa;
3005 int ret;
3006
3007 sa = (struct sockaddr *)&np->np_sockaddr;
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003008 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003009
3010 id = rdma_create_id(isert_cma_handler, isert_np,
3011 RDMA_PS_TCP, IB_QPT_RC);
3012 if (IS_ERR(id)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003013 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003014 ret = PTR_ERR(id);
3015 goto out;
3016 }
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003017 isert_dbg("id %p context %p\n", id, id->context);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003018
3019 ret = rdma_bind_addr(id, sa);
3020 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003021 isert_err("rdma_bind_addr() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003022 goto out_id;
3023 }
3024
3025 ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
3026 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003027 isert_err("rdma_listen() failed: %d\n", ret);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003028 goto out_id;
3029 }
3030
3031 return id;
3032out_id:
3033 rdma_destroy_id(id);
3034out:
3035 return ERR_PTR(ret);
3036}
3037
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003038static int
3039isert_setup_np(struct iscsi_np *np,
3040 struct __kernel_sockaddr_storage *ksockaddr)
3041{
3042 struct isert_np *isert_np;
3043 struct rdma_cm_id *isert_lid;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003044 int ret;
3045
3046 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3047 if (!isert_np) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003048 isert_err("Unable to allocate struct isert_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003049 return -ENOMEM;
3050 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003051 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003052 mutex_init(&isert_np->np_accept_mutex);
3053 INIT_LIST_HEAD(&isert_np->np_accept_list);
3054 init_completion(&isert_np->np_login_comp);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003055 isert_np->np = np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003056
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003057 /*
3058 * Setup the np->np_sockaddr from the passed sockaddr setup
3059 * in iscsi_target_configfs.c code..
3060 */
3061 memcpy(&np->np_sockaddr, ksockaddr,
3062 sizeof(struct __kernel_sockaddr_storage));
3063
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003064 isert_lid = isert_setup_id(isert_np);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003065 if (IS_ERR(isert_lid)) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003066 ret = PTR_ERR(isert_lid);
3067 goto out;
3068 }
3069
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003070 isert_np->np_cm_id = isert_lid;
3071 np->np_context = isert_np;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003072
3073 return 0;
3074
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003075out:
3076 kfree(isert_np);
Sagi Grimbergca6c1d82014-12-02 16:57:27 +02003077
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003078 return ret;
3079}
3080
3081static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003082isert_rdma_accept(struct isert_conn *isert_conn)
3083{
3084 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3085 struct rdma_conn_param cp;
3086 int ret;
3087
3088 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003089 cp.initiator_depth = isert_conn->initiator_depth;
3090 cp.retry_count = 7;
3091 cp.rnr_retry_count = 7;
3092
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003093 ret = rdma_accept(cm_id, &cp);
3094 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003095 isert_err("rdma_accept() failed with: %d\n", ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003096 return ret;
3097 }
3098
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003099 return 0;
3100}
3101
3102static int
3103isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3104{
Sagi Grimberg67004252015-03-29 15:52:09 +03003105 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003106 int ret;
3107
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003108 isert_info("before login_req comp conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003109 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3110 if (ret) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003111 isert_err("isert_conn %p interrupted before got login req\n",
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003112 isert_conn);
3113 return ret;
3114 }
3115 reinit_completion(&isert_conn->login_req_comp);
3116
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003117 /*
3118 * For login requests after the first PDU, isert_rx_login_req() will
3119 * kick schedule_delayed_work(&conn->login_work) as the packet is
3120 * received, which turns this callback from iscsi_target_do_login_rx()
3121 * into a NOP.
3122 */
3123 if (!login->first_request)
3124 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003125
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003126 isert_rx_login_req(isert_conn);
3127
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003128 isert_info("before conn_login_comp conn: %p\n", conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003129 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3130 if (ret)
3131 return ret;
3132
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003133 isert_info("processing login->req: %p\n", login->req);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003134
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003135 return 0;
3136}
3137
3138static void
3139isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3140 struct isert_conn *isert_conn)
3141{
3142 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3143 struct rdma_route *cm_route = &cm_id->route;
3144 struct sockaddr_in *sock_in;
3145 struct sockaddr_in6 *sock_in6;
3146
3147 conn->login_family = np->np_sockaddr.ss_family;
3148
3149 if (np->np_sockaddr.ss_family == AF_INET6) {
3150 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3151 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3152 &sock_in6->sin6_addr.in6_u);
3153 conn->login_port = ntohs(sock_in6->sin6_port);
3154
3155 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3156 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3157 &sock_in6->sin6_addr.in6_u);
3158 conn->local_port = ntohs(sock_in6->sin6_port);
3159 } else {
3160 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3161 sprintf(conn->login_ip, "%pI4",
3162 &sock_in->sin_addr.s_addr);
3163 conn->login_port = ntohs(sock_in->sin_port);
3164
3165 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3166 sprintf(conn->local_ip, "%pI4",
3167 &sock_in->sin_addr.s_addr);
3168 conn->local_port = ntohs(sock_in->sin_port);
3169 }
3170}
3171
3172static int
3173isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3174{
Sagi Grimberg67004252015-03-29 15:52:09 +03003175 struct isert_np *isert_np = np->np_context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003176 struct isert_conn *isert_conn;
3177 int max_accept = 0, ret;
3178
3179accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003180 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003181 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003182 return -ENODEV;
3183
3184 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003185 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003186 spin_unlock_bh(&np->np_thread_lock);
Rasmus Villemoes11378cd2015-02-06 01:09:05 +01003187 isert_dbg("np_thread_state %d\n",
Sagi Grimberge346ab32014-05-19 17:44:22 +03003188 np->np_thread_state);
3189 /**
3190 * No point in stalling here when np_thread
3191 * is in state RESET/SHUTDOWN/EXIT - bail
3192 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003193 return -ENODEV;
3194 }
3195 spin_unlock_bh(&np->np_thread_lock);
3196
3197 mutex_lock(&isert_np->np_accept_mutex);
3198 if (list_empty(&isert_np->np_accept_list)) {
3199 mutex_unlock(&isert_np->np_accept_mutex);
3200 max_accept++;
3201 goto accept_wait;
3202 }
3203 isert_conn = list_first_entry(&isert_np->np_accept_list,
3204 struct isert_conn, conn_accept_node);
3205 list_del_init(&isert_conn->conn_accept_node);
3206 mutex_unlock(&isert_np->np_accept_mutex);
3207
3208 conn->context = isert_conn;
3209 isert_conn->conn = conn;
3210 max_accept = 0;
3211
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003212 isert_set_conn_info(np, conn, isert_conn);
3213
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003214 isert_dbg("Processing isert_conn: %p\n", isert_conn);
Sagi Grimberg2371e5d2014-12-02 16:57:21 +02003215
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003216 return 0;
3217}
3218
3219static void
3220isert_free_np(struct iscsi_np *np)
3221{
Sagi Grimberg67004252015-03-29 15:52:09 +03003222 struct isert_np *isert_np = np->np_context;
Sagi Grimberg268e6812014-12-02 16:57:36 +02003223 struct isert_conn *isert_conn, *n;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003224
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003225 if (isert_np->np_cm_id)
3226 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003227
Sagi Grimberg268e6812014-12-02 16:57:36 +02003228 /*
3229 * FIXME: At this point we don't have a good way to insure
3230 * that at this point we don't have hanging connections that
3231 * completed RDMA establishment but didn't start iscsi login
3232 * process. So work-around this by cleaning up what ever piled
3233 * up in np_accept_list.
3234 */
3235 mutex_lock(&isert_np->np_accept_mutex);
3236 if (!list_empty(&isert_np->np_accept_list)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003237 isert_info("Still have isert connections, cleaning up...\n");
Sagi Grimberg268e6812014-12-02 16:57:36 +02003238 list_for_each_entry_safe(isert_conn, n,
3239 &isert_np->np_accept_list,
3240 conn_accept_node) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003241 isert_info("cleaning isert_conn %p state (%d)\n",
Sagi Grimberg268e6812014-12-02 16:57:36 +02003242 isert_conn, isert_conn->state);
3243 isert_connect_release(isert_conn);
3244 }
3245 }
3246 mutex_unlock(&isert_np->np_accept_mutex);
3247
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003248 np->np_context = NULL;
3249 kfree(isert_np);
3250}
3251
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003252static void isert_release_work(struct work_struct *work)
3253{
3254 struct isert_conn *isert_conn = container_of(work,
3255 struct isert_conn,
3256 release_work);
3257
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003258 isert_info("Starting release conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003259
3260 wait_for_completion(&isert_conn->conn_wait);
3261
3262 mutex_lock(&isert_conn->conn_mutex);
3263 isert_conn->state = ISER_CONN_DOWN;
3264 mutex_unlock(&isert_conn->conn_mutex);
3265
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003266 isert_info("Destroying conn %p\n", isert_conn);
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003267 isert_put_conn(isert_conn);
3268}
3269
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003270static void
Sagi Grimberg991bb762014-12-07 13:12:01 +02003271isert_wait4logout(struct isert_conn *isert_conn)
3272{
3273 struct iscsi_conn *conn = isert_conn->conn;
3274
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003275 isert_info("conn %p\n", isert_conn);
3276
Sagi Grimberg991bb762014-12-07 13:12:01 +02003277 if (isert_conn->logout_posted) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003278 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003279 wait_for_completion_timeout(&conn->conn_logout_comp,
3280 SECONDS_FOR_LOGOUT_COMP * HZ);
3281 }
3282}
3283
3284static void
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003285isert_wait4cmds(struct iscsi_conn *conn)
3286{
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003287 isert_info("iscsi_conn %p\n", conn);
3288
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003289 if (conn->sess) {
3290 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3291 target_wait_for_sess_cmds(conn->sess->se_sess);
3292 }
3293}
3294
3295static void
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003296isert_wait4flush(struct isert_conn *isert_conn)
3297{
3298 struct ib_recv_wr *bad_wr;
3299
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003300 isert_info("conn %p\n", isert_conn);
3301
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003302 init_completion(&isert_conn->conn_wait_comp_err);
3303 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3304 /* post an indication that all flush errors were consumed */
3305 if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003306 isert_err("conn %p failed to post beacon", isert_conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003307 return;
3308 }
3309
3310 wait_for_completion(&isert_conn->conn_wait_comp_err);
3311}
3312
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003313static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003314{
3315 struct isert_conn *isert_conn = conn->context;
3316
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003317 isert_info("Starting conn %p\n", isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003318
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003319 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003320 /*
3321 * Only wait for conn_wait_comp_err if the isert_conn made it
3322 * into full feature phase..
3323 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003324 if (isert_conn->state == ISER_CONN_INIT) {
3325 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003326 return;
3327 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003328 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003329 mutex_unlock(&isert_conn->conn_mutex);
3330
Sagi Grimbergc7e160e2014-12-02 16:57:46 +02003331 isert_wait4cmds(conn);
Sagi Grimbergbdf20e72014-12-02 16:57:43 +02003332 isert_wait4flush(isert_conn);
Sagi Grimberg991bb762014-12-07 13:12:01 +02003333 isert_wait4logout(isert_conn);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003334
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003335 INIT_WORK(&isert_conn->release_work, isert_release_work);
3336 queue_work(isert_release_wq, &isert_conn->release_work);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003337}
3338
3339static void isert_free_conn(struct iscsi_conn *conn)
3340{
3341 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003342
3343 isert_put_conn(isert_conn);
3344}
3345
3346static struct iscsit_transport iser_target_transport = {
3347 .name = "IB/iSER",
3348 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003349 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003350 .owner = THIS_MODULE,
3351 .iscsit_setup_np = isert_setup_np,
3352 .iscsit_accept_np = isert_accept_np,
3353 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003354 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003355 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003356 .iscsit_get_login_rx = isert_get_login_rx,
3357 .iscsit_put_login_tx = isert_put_login_tx,
3358 .iscsit_immediate_queue = isert_immediate_queue,
3359 .iscsit_response_queue = isert_response_queue,
3360 .iscsit_get_dataout = isert_get_dataout,
3361 .iscsit_queue_data_in = isert_put_datain,
3362 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003363 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003364 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003365};
3366
3367static int __init isert_init(void)
3368{
3369 int ret;
3370
Sagi Grimberg631af552015-01-25 19:09:50 +02003371 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3372 WQ_UNBOUND | WQ_HIGHPRI, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003373 if (!isert_comp_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003374 isert_err("Unable to allocate isert_comp_wq\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003375 ret = -ENOMEM;
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003376 return -ENOMEM;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003377 }
3378
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003379 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3380 WQ_UNBOUND_MAX_ACTIVE);
3381 if (!isert_release_wq) {
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003382 isert_err("Unable to allocate isert_release_wq\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003383 ret = -ENOMEM;
3384 goto destroy_comp_wq;
3385 }
3386
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003387 iscsit_register_transport(&iser_target_transport);
Sagi Grimberg24f412d2014-12-07 13:12:02 +02003388 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003389
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003390 return 0;
3391
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003392destroy_comp_wq:
3393 destroy_workqueue(isert_comp_wq);
Sagi Grimberg6f0fae32014-12-02 16:57:41 +02003394
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003395 return ret;
3396}
3397
3398static void __exit isert_exit(void)
3399{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003400 flush_scheduled_work();
Sagi Grimbergb02efbf2014-12-02 16:57:29 +02003401 destroy_workqueue(isert_release_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003402 destroy_workqueue(isert_comp_wq);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003403 iscsit_unregister_transport(&iser_target_transport);
Sagi Grimberg4c22e072014-12-07 13:12:03 +02003404 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003405}
3406
3407MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3408MODULE_VERSION("0.1");
3409MODULE_AUTHOR("nab@Linux-iSCSI.org");
3410MODULE_LICENSE("GPL");
3411
3412module_init(isert_init);
3413module_exit(isert_exit);