blob: 8f24728f105250692f7a46a516674deac844b4f4 [file] [log] [blame]
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
Or Gerlitz3ee07d22014-04-01 16:28:41 +03004 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030033 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030034#include <linux/kernel.h>
35#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030037#include <linux/delay.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030038
39#include "iscsi_iser.h"
40
41#define ISCSI_ISER_MAX_CONN 8
Sagi Grimberg6aabfa72014-10-01 14:02:09 +030042#define ISER_MAX_RX_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
43#define ISER_MAX_TX_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
Sagi Grimbergff3dd522014-10-01 14:02:10 +030044#define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
45 ISCSI_ISER_MAX_CONN)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030046
Sagi Grimberg183cfa42014-10-01 14:02:08 +030047static int iser_cq_poll_limit = 512;
48
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030049static void iser_cq_tasklet_fn(unsigned long data);
50static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030051
52static void iser_cq_event_callback(struct ib_event *cause, void *context)
53{
Sagi Grimberg871e00a2015-05-18 13:40:30 +030054 iser_err("cq event %s (%d)\n",
55 ib_event_msg(cause->event), cause->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030056}
57
58static void iser_qp_event_callback(struct ib_event *cause, void *context)
59{
Sagi Grimberg871e00a2015-05-18 13:40:30 +030060 iser_err("qp event %s (%d)\n",
61 ib_event_msg(cause->event), cause->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030062}
63
Or Gerlitz2110f9b2010-05-05 17:30:10 +030064static void iser_event_handler(struct ib_event_handler *handler,
65 struct ib_event *event)
66{
Sagi Grimberg871e00a2015-05-18 13:40:30 +030067 iser_err("async event %s (%d) on device %s port %d\n",
68 ib_event_msg(event->event), event->event,
69 event->device->name, event->element.port_num);
Or Gerlitz2110f9b2010-05-05 17:30:10 +030070}
71
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030072/**
73 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
74 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
75 * the adapator.
76 *
77 * returns 0 on success, -1 on failure
78 */
79static int iser_create_device_ib_res(struct iser_device *device)
80{
Sagi Grimberg65198d62014-03-05 19:43:42 +020081 struct ib_device_attr *dev_attr = &device->dev_attr;
Minh Tranf4641ef2014-12-07 16:09:52 +020082 int ret, i, max_cqe;
Alex Tabachnik5a33a662012-09-23 15:17:44 +000083
Sagi Grimberg65198d62014-03-05 19:43:42 +020084 ret = ib_query_device(device->ib_device, dev_attr);
85 if (ret) {
Sagi Grimberg55878562013-07-28 12:35:42 +030086 pr_warn("Query device failed for %s\n", device->ib_device->name);
Sagi Grimberg65198d62014-03-05 19:43:42 +020087 return ret;
Sagi Grimberg55878562013-07-28 12:35:42 +030088 }
89
90 /* Assign function handles - based on FMR support */
91 if (device->ib_device->alloc_fmr && device->ib_device->dealloc_fmr &&
92 device->ib_device->map_phys_fmr && device->ib_device->unmap_fmr) {
93 iser_info("FMR supported, using FMR for registration\n");
94 device->iser_alloc_rdma_reg_res = iser_create_fmr_pool;
95 device->iser_free_rdma_reg_res = iser_free_fmr_pool;
96 device->iser_reg_rdma_mem = iser_reg_rdma_mem_fmr;
97 device->iser_unreg_rdma_mem = iser_unreg_mem_fmr;
98 } else
99 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200100 iser_info("FastReg supported, using FastReg for registration\n");
101 device->iser_alloc_rdma_reg_res = iser_create_fastreg_pool;
102 device->iser_free_rdma_reg_res = iser_free_fastreg_pool;
103 device->iser_reg_rdma_mem = iser_reg_rdma_mem_fastreg;
104 device->iser_unreg_rdma_mem = iser_unreg_mem_fastreg;
Sagi Grimberg55878562013-07-28 12:35:42 +0300105 } else {
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200106 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
Sagi Grimberg65198d62014-03-05 19:43:42 +0200107 return -1;
Sagi Grimberg55878562013-07-28 12:35:42 +0300108 }
Sagi Grimbergb4e155f2013-07-28 12:35:39 +0300109
Sagi Grimbergda64bdb2014-12-07 16:10:03 +0200110 device->comps_used = min_t(int, num_online_cpus(),
Sagi Grimbergbf175542014-10-01 14:02:07 +0300111 device->ib_device->num_comp_vectors);
Minh Tranf4641ef2014-12-07 16:09:52 +0200112
Sagi Grimbergda64bdb2014-12-07 16:10:03 +0200113 device->comps = kcalloc(device->comps_used, sizeof(*device->comps),
114 GFP_KERNEL);
115 if (!device->comps)
116 goto comps_err;
117
Minh Tranf4641ef2014-12-07 16:09:52 +0200118 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
119
120 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n",
Sagi Grimbergbf175542014-10-01 14:02:07 +0300121 device->comps_used, device->ib_device->name,
Minh Tranf4641ef2014-12-07 16:09:52 +0200122 device->ib_device->num_comp_vectors, max_cqe);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000123
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300124 device->pd = ib_alloc_pd(device->ib_device);
125 if (IS_ERR(device->pd))
126 goto pd_err;
127
Sagi Grimbergbf175542014-10-01 14:02:07 +0300128 for (i = 0; i < device->comps_used; i++) {
Matan Barak8e372102015-06-11 16:35:21 +0300129 struct ib_cq_init_attr cq_attr = {};
Sagi Grimbergbf175542014-10-01 14:02:07 +0300130 struct iser_comp *comp = &device->comps[i];
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300131
Sagi Grimbergbf175542014-10-01 14:02:07 +0300132 comp->device = device;
Matan Barak8e372102015-06-11 16:35:21 +0300133 cq_attr.cqe = max_cqe;
134 cq_attr.comp_vector = i;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300135 comp->cq = ib_create_cq(device->ib_device,
136 iser_cq_callback,
137 iser_cq_event_callback,
138 (void *)comp,
Matan Barak8e372102015-06-11 16:35:21 +0300139 &cq_attr);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300140 if (IS_ERR(comp->cq)) {
141 comp->cq = NULL;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000142 goto cq_err;
Roi Dayanc33b15f2014-09-02 17:08:41 +0300143 }
Or Gerlitz78ad0a32010-02-08 13:19:21 +0000144
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300145 if (ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP))
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000146 goto cq_err;
147
Sagi Grimbergbf175542014-10-01 14:02:07 +0300148 tasklet_init(&comp->tasklet, iser_cq_tasklet_fn,
149 (unsigned long)comp);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000150 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300151
Erez Zilberd8111022006-09-11 12:26:33 +0300152 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
153 IB_ACCESS_REMOTE_WRITE |
154 IB_ACCESS_REMOTE_READ);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300155 if (IS_ERR(device->mr))
156 goto dma_mr_err;
157
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300158 INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
159 iser_event_handler);
160 if (ib_register_event_handler(&device->event_handler))
161 goto handler_err;
162
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300163 return 0;
164
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300165handler_err:
166 ib_dereg_mr(device->mr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300167dma_mr_err:
Sagi Grimbergbf175542014-10-01 14:02:07 +0300168 for (i = 0; i < device->comps_used; i++)
169 tasklet_kill(&device->comps[i].tasklet);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000170cq_err:
Sagi Grimbergbf175542014-10-01 14:02:07 +0300171 for (i = 0; i < device->comps_used; i++) {
172 struct iser_comp *comp = &device->comps[i];
173
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300174 if (comp->cq)
175 ib_destroy_cq(comp->cq);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000176 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300177 ib_dealloc_pd(device->pd);
178pd_err:
Sagi Grimbergda64bdb2014-12-07 16:10:03 +0200179 kfree(device->comps);
180comps_err:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300181 iser_err("failed to allocate an IB resource\n");
182 return -1;
183}
184
185/**
Oliver Pinter38dc7322008-01-25 14:15:32 -0800186 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300187 * CQ and PD created with the device associated with the adapator.
188 */
189static void iser_free_device_ib_res(struct iser_device *device)
190{
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000191 int i;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300192 BUG_ON(device->mr == NULL);
193
Sagi Grimbergbf175542014-10-01 14:02:07 +0300194 for (i = 0; i < device->comps_used; i++) {
195 struct iser_comp *comp = &device->comps[i];
196
197 tasklet_kill(&comp->tasklet);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300198 ib_destroy_cq(comp->cq);
199 comp->cq = NULL;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000200 }
201
Or Gerlitz2110f9b2010-05-05 17:30:10 +0300202 (void)ib_unregister_event_handler(&device->event_handler);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300203 (void)ib_dereg_mr(device->mr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300204 (void)ib_dealloc_pd(device->pd);
205
Sagi Grimbergda64bdb2014-12-07 16:10:03 +0200206 kfree(device->comps);
207 device->comps = NULL;
208
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300209 device->mr = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300210 device->pd = NULL;
211}
212
213/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300214 * iser_create_fmr_pool - Creates FMR pool and page_vector
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300215 *
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300216 * returns 0 on success, or errno code on failure
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300217 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300218int iser_create_fmr_pool(struct ib_conn *ib_conn, unsigned cmds_max)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300219{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300220 struct iser_device *device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300221 struct ib_fmr_pool_param params;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300222 int ret = -ENOMEM;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000223
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300224 ib_conn->fmr.page_vec = kmalloc(sizeof(*ib_conn->fmr.page_vec) +
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200225 (sizeof(u64)*(ISCSI_ISER_SG_TABLESIZE + 1)),
226 GFP_KERNEL);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300227 if (!ib_conn->fmr.page_vec)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300228 return ret;
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300229
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300230 ib_conn->fmr.page_vec->pages = (u64 *)(ib_conn->fmr.page_vec + 1);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300231
Erez Zilber8dfa0872006-09-11 12:22:30 +0300232 params.page_shift = SHIFT_4K;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300233 /* when the first/last SG element are not start/end *
234 * page aligned, the map whould be of N+1 pages */
235 params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1;
236 /* make the pool size twice the max number of SCSI commands *
237 * the ML is expected to queue, watermark for unmap at 50% */
Shlomo Pongratzb7f04512013-07-28 12:35:38 +0300238 params.pool_size = cmds_max * 2;
239 params.dirty_watermark = cmds_max;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300240 params.cache = 0;
241 params.flush_function = NULL;
242 params.access = (IB_ACCESS_LOCAL_WRITE |
243 IB_ACCESS_REMOTE_WRITE |
244 IB_ACCESS_REMOTE_READ);
245
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300246 ib_conn->fmr.pool = ib_create_fmr_pool(device->pd, &params);
247 if (!IS_ERR(ib_conn->fmr.pool))
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300248 return 0;
249
250 /* no FMR => no need for page_vec */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300251 kfree(ib_conn->fmr.page_vec);
252 ib_conn->fmr.page_vec = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300253
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300254 ret = PTR_ERR(ib_conn->fmr.pool);
255 ib_conn->fmr.pool = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300256 if (ret != -ENOSYS) {
257 iser_err("FMR allocation failed, err %d\n", ret);
258 return ret;
259 } else {
Or Gerlitz5525d212013-02-21 14:50:10 +0000260 iser_warn("FMRs are not supported, using unaligned mode\n");
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300261 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300262 }
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300263}
264
265/**
266 * iser_free_fmr_pool - releases the FMR pool and page vec
267 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300268void iser_free_fmr_pool(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300269{
270 iser_info("freeing conn %p fmr pool %p\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300271 ib_conn, ib_conn->fmr.pool);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300272
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300273 if (ib_conn->fmr.pool != NULL)
274 ib_destroy_fmr_pool(ib_conn->fmr.pool);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300275
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300276 ib_conn->fmr.pool = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300277
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300278 kfree(ib_conn->fmr.page_vec);
279 ib_conn->fmr.page_vec = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300280}
281
Sagi Grimberg310b3472014-03-05 19:43:41 +0200282static int
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300283iser_alloc_pi_ctx(struct ib_device *ib_device, struct ib_pd *pd,
284 struct fast_reg_descriptor *desc)
285{
286 struct iser_pi_context *pi_ctx = NULL;
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300287 int ret;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300288
289 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
290 if (!desc->pi_ctx)
291 return -ENOMEM;
292
293 pi_ctx = desc->pi_ctx;
294
295 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
296 ISCSI_ISER_SG_TABLESIZE);
297 if (IS_ERR(pi_ctx->prot_frpl)) {
298 ret = PTR_ERR(pi_ctx->prot_frpl);
299 goto prot_frpl_failure;
300 }
301
Sagi Grimberg34780f02015-07-30 10:32:36 +0300302 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
303 ISCSI_ISER_SG_TABLESIZE + 1);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300304 if (IS_ERR(pi_ctx->prot_mr)) {
305 ret = PTR_ERR(pi_ctx->prot_mr);
306 goto prot_mr_failure;
307 }
308 desc->reg_indicators |= ISER_PROT_KEY_VALID;
309
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300310 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300311 if (IS_ERR(pi_ctx->sig_mr)) {
312 ret = PTR_ERR(pi_ctx->sig_mr);
313 goto sig_mr_failure;
314 }
315 desc->reg_indicators |= ISER_SIG_KEY_VALID;
316 desc->reg_indicators &= ~ISER_FASTREG_PROTECTED;
317
318 return 0;
319
320sig_mr_failure:
321 ib_dereg_mr(desc->pi_ctx->prot_mr);
322prot_mr_failure:
323 ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
324prot_frpl_failure:
325 kfree(desc->pi_ctx);
326
327 return ret;
328}
329
330static void
331iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
332{
333 ib_free_fast_reg_page_list(pi_ctx->prot_frpl);
334 ib_dereg_mr(pi_ctx->prot_mr);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300335 ib_dereg_mr(pi_ctx->sig_mr);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300336 kfree(pi_ctx);
337}
338
339static int
Sagi Grimberg310b3472014-03-05 19:43:41 +0200340iser_create_fastreg_desc(struct ib_device *ib_device, struct ib_pd *pd,
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200341 bool pi_enable, struct fast_reg_descriptor *desc)
Sagi Grimberg310b3472014-03-05 19:43:41 +0200342{
343 int ret;
344
345 desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
346 ISCSI_ISER_SG_TABLESIZE + 1);
347 if (IS_ERR(desc->data_frpl)) {
348 ret = PTR_ERR(desc->data_frpl);
349 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
350 ret);
351 return PTR_ERR(desc->data_frpl);
352 }
353
Sagi Grimberg34780f02015-07-30 10:32:36 +0300354 desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
355 ISCSI_ISER_SG_TABLESIZE + 1);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200356 if (IS_ERR(desc->data_mr)) {
357 ret = PTR_ERR(desc->data_mr);
358 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
359 goto fast_reg_mr_failure;
360 }
Sagi Grimberg73bc06b2014-03-05 19:43:43 +0200361 desc->reg_indicators |= ISER_DATA_KEY_VALID;
Sagi Grimberg310b3472014-03-05 19:43:41 +0200362
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200363 if (pi_enable) {
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300364 ret = iser_alloc_pi_ctx(ib_device, pd, desc);
365 if (ret)
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200366 goto pi_ctx_alloc_failure;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200367 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200368
369 return 0;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200370pi_ctx_alloc_failure:
371 ib_dereg_mr(desc->data_mr);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200372fast_reg_mr_failure:
373 ib_free_fast_reg_page_list(desc->data_frpl);
374
375 return ret;
376}
377
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300378/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200379 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300380 * for fast registration work requests.
381 * returns 0 on success, or errno code on failure
382 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300383int iser_create_fastreg_pool(struct ib_conn *ib_conn, unsigned cmds_max)
Sagi Grimberg55878562013-07-28 12:35:42 +0300384{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300385 struct iser_device *device = ib_conn->device;
386 struct fast_reg_descriptor *desc;
Sagi Grimberg55878562013-07-28 12:35:42 +0300387 int i, ret;
388
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300389 INIT_LIST_HEAD(&ib_conn->fastreg.pool);
390 ib_conn->fastreg.pool_size = 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300391 for (i = 0; i < cmds_max; i++) {
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200392 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
Sagi Grimberg55878562013-07-28 12:35:42 +0300393 if (!desc) {
394 iser_err("Failed to allocate a new fast_reg descriptor\n");
395 ret = -ENOMEM;
396 goto err;
397 }
398
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200399 ret = iser_create_fastreg_desc(device->ib_device, device->pd,
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300400 ib_conn->pi_support, desc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200401 if (ret) {
402 iser_err("Failed to create fastreg descriptor err=%d\n",
403 ret);
404 kfree(desc);
405 goto err;
Sagi Grimberg55878562013-07-28 12:35:42 +0300406 }
407
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300408 list_add_tail(&desc->list, &ib_conn->fastreg.pool);
409 ib_conn->fastreg.pool_size++;
Sagi Grimberg55878562013-07-28 12:35:42 +0300410 }
411
412 return 0;
Roi Dayan27ae2d12013-08-19 16:41:51 +0300413
Sagi Grimberg55878562013-07-28 12:35:42 +0300414err:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300415 iser_free_fastreg_pool(ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300416 return ret;
417}
418
419/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200420 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300421 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300422void iser_free_fastreg_pool(struct ib_conn *ib_conn)
Sagi Grimberg55878562013-07-28 12:35:42 +0300423{
424 struct fast_reg_descriptor *desc, *tmp;
425 int i = 0;
426
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300427 if (list_empty(&ib_conn->fastreg.pool))
Sagi Grimberg55878562013-07-28 12:35:42 +0300428 return;
429
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300430 iser_info("freeing conn %p fr pool\n", ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300431
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300432 list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.pool, list) {
Sagi Grimberg55878562013-07-28 12:35:42 +0300433 list_del(&desc->list);
434 ib_free_fast_reg_page_list(desc->data_frpl);
435 ib_dereg_mr(desc->data_mr);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300436 if (desc->pi_ctx)
437 iser_free_pi_ctx(desc->pi_ctx);
Sagi Grimberg55878562013-07-28 12:35:42 +0300438 kfree(desc);
439 ++i;
440 }
441
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300442 if (i < ib_conn->fastreg.pool_size)
Sagi Grimberg55878562013-07-28 12:35:42 +0300443 iser_warn("pool still has %d regions registered\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300444 ib_conn->fastreg.pool_size - i);
Sagi Grimberg55878562013-07-28 12:35:42 +0300445}
446
447/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300448 * iser_create_ib_conn_res - Queue-Pair (QP)
449 *
450 * returns 0 on success, -1 on failure
451 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300452static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300453{
Minh Tranf4641ef2014-12-07 16:09:52 +0200454 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
455 ib_conn);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300456 struct iser_device *device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200457 struct ib_device_attr *dev_attr;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300458 struct ib_qp_init_attr init_attr;
459 int ret = -ENOMEM;
460 int index, min_index = 0;
461
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300462 BUG_ON(ib_conn->device == NULL);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300463
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300464 device = ib_conn->device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200465 dev_attr = &device->dev_attr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300466
467 memset(&init_attr, 0, sizeof init_attr);
468
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000469 mutex_lock(&ig.connlist_mutex);
470 /* select the CQ with the minimal number of usages */
Sagi Grimbergbf175542014-10-01 14:02:07 +0300471 for (index = 0; index < device->comps_used; index++) {
472 if (device->comps[index].active_qps <
473 device->comps[min_index].active_qps)
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000474 min_index = index;
Sagi Grimbergbf175542014-10-01 14:02:07 +0300475 }
476 ib_conn->comp = &device->comps[min_index];
477 ib_conn->comp->active_qps++;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000478 mutex_unlock(&ig.connlist_mutex);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300479 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000480
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300481 init_attr.event_handler = iser_qp_event_callback;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300482 init_attr.qp_context = (void *)ib_conn;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300483 init_attr.send_cq = ib_conn->comp->cq;
484 init_attr.recv_cq = ib_conn->comp->cq;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300485 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000486 init_attr.cap.max_send_sge = 2;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000487 init_attr.cap.max_recv_sge = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300488 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
489 init_attr.qp_type = IB_QPT_RC;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300490 if (ib_conn->pi_support) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300491 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200492 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Minh Tranf4641ef2014-12-07 16:09:52 +0200493 iser_conn->max_cmds =
494 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200495 } else {
Minh Tranf4641ef2014-12-07 16:09:52 +0200496 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
497 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
498 iser_conn->max_cmds =
499 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
500 } else {
501 init_attr.cap.max_send_wr = dev_attr->max_qp_wr;
502 iser_conn->max_cmds =
503 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr);
504 iser_dbg("device %s supports max_send_wr %d\n",
505 device->ib_device->name, dev_attr->max_qp_wr);
506 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200507 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300508
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300509 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300510 if (ret)
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300511 goto out_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300512
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300513 ib_conn->qp = ib_conn->cma_id->qp;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300514 iser_info("setting conn %p cma_id %p qp %p\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300515 ib_conn, ib_conn->cma_id,
516 ib_conn->cma_id->qp);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300517 return ret;
518
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300519out_err:
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200520 mutex_lock(&ig.connlist_mutex);
521 ib_conn->comp->active_qps--;
522 mutex_unlock(&ig.connlist_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300523 iser_err("unable to alloc mem or create resource, err %d\n", ret);
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200524
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300525 return ret;
526}
527
528/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300529 * based on the resolved device node GUID see if there already allocated
530 * device for this device. If there's no such, create one.
531 */
532static
533struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
534{
Arne Redlich9a378272008-03-04 14:07:22 +0200535 struct iser_device *device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300536
537 mutex_lock(&ig.device_list_mutex);
538
Arne Redlich9a378272008-03-04 14:07:22 +0200539 list_for_each_entry(device, &ig.device_list, ig_list)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300540 /* find if there's a match using the node GUID */
541 if (device->ib_device->node_guid == cma_id->device->node_guid)
Arne Redlichd33ed422008-03-04 14:11:54 +0200542 goto inc_refcnt;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300543
Arne Redlich9a378272008-03-04 14:07:22 +0200544 device = kzalloc(sizeof *device, GFP_KERNEL);
545 if (device == NULL)
546 goto out;
547
548 /* assign this device to the device */
549 device->ib_device = cma_id->device;
550 /* init the device and link it into ig device list */
551 if (iser_create_device_ib_res(device)) {
552 kfree(device);
553 device = NULL;
554 goto out;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300555 }
Arne Redlich9a378272008-03-04 14:07:22 +0200556 list_add(&device->ig_list, &ig.device_list);
557
Arne Redlichd33ed422008-03-04 14:11:54 +0200558inc_refcnt:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300559 device->refcount++;
Arne Redlichd33ed422008-03-04 14:11:54 +0200560out:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300561 mutex_unlock(&ig.device_list_mutex);
562 return device;
563}
564
565/* if there's no demand for this device, release it */
566static void iser_device_try_release(struct iser_device *device)
567{
568 mutex_lock(&ig.device_list_mutex);
569 device->refcount--;
Roi Dayan4f363882013-05-01 13:25:25 +0000570 iser_info("device %p refcount %d\n", device, device->refcount);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300571 if (!device->refcount) {
572 iser_free_device_ib_res(device);
573 list_del(&device->ig_list);
574 kfree(device);
575 }
576 mutex_unlock(&ig.device_list_mutex);
577}
578
Ariel Nahum504130c2014-07-31 13:27:49 +0300579/**
580 * Called with state mutex held
581 **/
Sagi Grimberg5716af62014-10-01 14:01:57 +0300582static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
583 enum iser_conn_state comp,
584 enum iser_conn_state exch)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300585{
586 int ret;
587
Sagi Grimberg5716af62014-10-01 14:01:57 +0300588 ret = (iser_conn->state == comp);
589 if (ret)
590 iser_conn->state = exch;
591
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300592 return ret;
593}
594
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300595void iser_release_work(struct work_struct *work)
596{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300597 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300598
Sagi Grimberg5716af62014-10-01 14:01:57 +0300599 iser_conn = container_of(work, struct iser_conn, release_work);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300600
Sagi Grimbergc107a6c2014-10-01 14:02:02 +0300601 /* Wait for conn_stop to complete */
602 wait_for_completion(&iser_conn->stop_completion);
603 /* Wait for IB resouces cleanup to complete */
604 wait_for_completion(&iser_conn->ib_completion);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300605
Sagi Grimberg5716af62014-10-01 14:01:57 +0300606 mutex_lock(&iser_conn->state_mutex);
607 iser_conn->state = ISER_CONN_DOWN;
608 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300609
Sagi Grimberg5716af62014-10-01 14:01:57 +0300610 iser_conn_release(iser_conn);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300611}
612
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300613/**
Sagi Grimberg96f15192014-10-01 14:02:00 +0300614 * iser_free_ib_conn_res - release IB related resources
615 * @iser_conn: iser connection struct
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200616 * @destroy: indicator if we need to try to release the
617 * iser device and memory regoins pool (only iscsi
618 * shutdown and DEVICE_REMOVAL will use this).
Sagi Grimberg96f15192014-10-01 14:02:00 +0300619 *
620 * This routine is called with the iser state mutex held
621 * so the cm_id removal is out of here. It is Safe to
622 * be invoked multiple times.
623 */
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300624static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200625 bool destroy)
Sagi Grimberg96f15192014-10-01 14:02:00 +0300626{
627 struct ib_conn *ib_conn = &iser_conn->ib_conn;
628 struct iser_device *device = ib_conn->device;
629
630 iser_info("freeing conn %p cma_id %p qp %p\n",
631 iser_conn, ib_conn->cma_id, ib_conn->qp);
632
Sagi Grimberg96f15192014-10-01 14:02:00 +0300633 if (ib_conn->qp != NULL) {
Sagi Grimbergbf175542014-10-01 14:02:07 +0300634 ib_conn->comp->active_qps--;
Sagi Grimberg96f15192014-10-01 14:02:00 +0300635 rdma_destroy_qp(ib_conn->cma_id);
636 ib_conn->qp = NULL;
637 }
638
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200639 if (destroy) {
640 if (iser_conn->rx_descs)
641 iser_free_rx_descriptors(iser_conn);
642
643 if (device != NULL) {
644 iser_device_try_release(device);
645 ib_conn->device = NULL;
646 }
Sagi Grimberg96f15192014-10-01 14:02:00 +0300647 }
648}
649
650/**
Roland Dreier41179e22007-07-17 18:37:42 -0700651 * Frees all conn objects and deallocs conn descriptor
652 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300653void iser_conn_release(struct iser_conn *iser_conn)
Roland Dreier41179e22007-07-17 18:37:42 -0700654{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300655 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Roland Dreier41179e22007-07-17 18:37:42 -0700656
Roland Dreier41179e22007-07-17 18:37:42 -0700657 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300658 list_del(&iser_conn->conn_list);
Roland Dreier41179e22007-07-17 18:37:42 -0700659 mutex_unlock(&ig.connlist_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300660
Sagi Grimberg5716af62014-10-01 14:01:57 +0300661 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200662 /* In case we endup here without ep_disconnect being invoked. */
Ariel Nahum5426b172014-12-07 16:09:54 +0200663 if (iser_conn->state != ISER_CONN_DOWN) {
Ariel Nahumaea8f4d2014-10-01 14:02:06 +0300664 iser_warn("iser conn %p state %d, expected state down.\n",
665 iser_conn, iser_conn->state);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200666 iscsi_destroy_endpoint(iser_conn->ep);
Ariel Nahum5426b172014-12-07 16:09:54 +0200667 iser_conn->state = ISER_CONN_DOWN;
668 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300669 /*
670 * In case we never got to bind stage, we still need to
671 * release IB resources (which is safe to call more than once).
672 */
673 iser_free_ib_conn_res(iser_conn, true);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300674 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300675
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300676 if (ib_conn->cma_id != NULL) {
677 rdma_destroy_id(ib_conn->cma_id);
678 ib_conn->cma_id = NULL;
Roi Dayan5b61ff42013-05-08 12:21:17 +0000679 }
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300680
Sagi Grimberg5716af62014-10-01 14:01:57 +0300681 kfree(iser_conn);
Roland Dreier41179e22007-07-17 18:37:42 -0700682}
683
684/**
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300685 * triggers start of the disconnect procedures and wait for them to be done
686 * Called with state mutex held
687 */
688int iser_conn_terminate(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300689{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300690 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300691 struct ib_send_wr *bad_wr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300692 int err = 0;
693
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300694 /* terminate the iser conn only if the conn state is UP */
695 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
696 ISER_CONN_TERMINATING))
697 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300698
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300699 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
700
701 /* suspend queuing of new iscsi commands */
702 if (iser_conn->iscsi_conn)
703 iscsi_suspend_queue(iser_conn->iscsi_conn);
704
705 /*
706 * In case we didn't already clean up the cma_id (peer initiated
707 * a disconnection), we need to Cause the CMA to change the QP
708 * state to ERROR.
709 */
710 if (ib_conn->cma_id) {
711 err = rdma_disconnect(ib_conn->cma_id);
712 if (err)
713 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
714 iser_conn, err);
715
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300716 /* post an indication that all flush errors were consumed */
717 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200718 if (err) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300719 iser_err("conn %p failed to post beacon", ib_conn);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200720 return 1;
721 }
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300722
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300723 wait_for_completion(&ib_conn->flush_comp);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300724 }
725
726 return 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300727}
728
Ariel Nahum504130c2014-07-31 13:27:49 +0300729/**
730 * Called with state mutex held
731 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300732static void iser_connect_error(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300733{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300734 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300735
Sagi Grimberg5716af62014-10-01 14:01:57 +0300736 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimbergc4de4662015-04-14 18:08:11 +0300737 iser_conn->state = ISER_CONN_TERMINATING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300738}
739
Ariel Nahum504130c2014-07-31 13:27:49 +0300740/**
741 * Called with state mutex held
742 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300743static void iser_addr_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300744{
745 struct iser_device *device;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300746 struct iser_conn *iser_conn;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300747 struct ib_conn *ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300748 int ret;
749
Sagi Grimberg5716af62014-10-01 14:01:57 +0300750 iser_conn = (struct iser_conn *)cma_id->context;
751 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300752 /* bailout */
753 return;
754
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300755 ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300756 device = iser_device_find_by_ib_device(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200757 if (!device) {
758 iser_err("device lookup/creation failed\n");
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300759 iser_connect_error(cma_id);
760 return;
Arne Redlichd33ed422008-03-04 14:11:54 +0200761 }
762
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300763 ib_conn->device = device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300764
Alex Tabachnik7f733842014-03-05 19:43:46 +0200765 /* connection T10-PI support */
766 if (iser_pi_enable) {
767 if (!(device->dev_attr.device_cap_flags &
768 IB_DEVICE_SIGNATURE_HANDOVER)) {
769 iser_warn("T10-PI requested but not supported on %s, "
770 "continue without T10-PI\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300771 ib_conn->device->ib_device->name);
772 ib_conn->pi_support = false;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200773 } else {
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300774 ib_conn->pi_support = true;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200775 }
776 }
777
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300778 ret = rdma_resolve_route(cma_id, 1000);
779 if (ret) {
780 iser_err("resolve route failed: %d\n", ret);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300781 iser_connect_error(cma_id);
782 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300783 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300784}
785
Ariel Nahum504130c2014-07-31 13:27:49 +0300786/**
787 * Called with state mutex held
788 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300789static void iser_route_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300790{
791 struct rdma_conn_param conn_param;
792 int ret;
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000793 struct iser_cm_hdr req_hdr;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300794 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300795 struct ib_conn *ib_conn = &iser_conn->ib_conn;
796 struct iser_device *device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300797
Sagi Grimberg5716af62014-10-01 14:01:57 +0300798 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300799 /* bailout */
800 return;
801
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300802 ret = iser_create_ib_conn_res(ib_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300803 if (ret)
804 goto failure;
805
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300806 memset(&conn_param, 0, sizeof conn_param);
Sagi Grimberg2ea32932014-07-31 13:27:46 +0300807 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300808 conn_param.initiator_depth = 1;
809 conn_param.retry_count = 7;
810 conn_param.rnr_retry_count = 6;
811
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000812 memset(&req_hdr, 0, sizeof(req_hdr));
813 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
814 ISER_SEND_W_INV_NOT_SUPPORTED);
815 conn_param.private_data = (void *)&req_hdr;
816 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
817
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300818 ret = rdma_connect(cma_id, &conn_param);
819 if (ret) {
820 iser_err("failure connecting: %d\n", ret);
821 goto failure;
822 }
823
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300824 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300825failure:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300826 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300827}
828
829static void iser_connected_handler(struct rdma_cm_id *cma_id)
830{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300831 struct iser_conn *iser_conn;
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300832 struct ib_qp_attr attr;
833 struct ib_qp_init_attr init_attr;
834
Sagi Grimberg5716af62014-10-01 14:01:57 +0300835 iser_conn = (struct iser_conn *)cma_id->context;
836 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300837 /* bailout */
838 return;
839
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300840 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
841 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300842
Sagi Grimberg5716af62014-10-01 14:01:57 +0300843 iser_conn->state = ISER_CONN_UP;
844 complete(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300845}
846
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300847static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300848{
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300849 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300850
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300851 if (iser_conn_terminate(iser_conn)) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300852 if (iser_conn->iscsi_conn)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300853 iscsi_conn_failure(iser_conn->iscsi_conn,
854 ISCSI_ERR_CONN_FAILED);
Roi Dayan7d9eacf2014-02-04 16:54:54 +0200855 else
856 iser_err("iscsi_iser connection isn't bound\n");
857 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300858}
859
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300860static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200861 bool destroy)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300862{
863 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
864
865 /*
866 * We are not guaranteed that we visited disconnected_handler
867 * by now, call it here to be safe that we handle CM drep
868 * and flush errors.
869 */
870 iser_disconnected_handler(cma_id);
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200871 iser_free_ib_conn_res(iser_conn, destroy);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300872 complete(&iser_conn->ib_completion);
873};
874
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300875static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
876{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300877 struct iser_conn *iser_conn;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300878 int ret = 0;
Ariel Nahum504130c2014-07-31 13:27:49 +0300879
Sagi Grimberg5716af62014-10-01 14:01:57 +0300880 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300881 iser_info("%s (%d): status %d conn %p id %p\n",
882 rdma_event_msg(event->event), event->event,
883 event->status, cma_id->context, cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300884
Sagi Grimberg5716af62014-10-01 14:01:57 +0300885 mutex_lock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300886 switch (event->event) {
887 case RDMA_CM_EVENT_ADDR_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300888 iser_addr_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300889 break;
890 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300891 iser_route_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300892 break;
893 case RDMA_CM_EVENT_ESTABLISHED:
894 iser_connected_handler(cma_id);
895 break;
896 case RDMA_CM_EVENT_ADDR_ERROR:
897 case RDMA_CM_EVENT_ROUTE_ERROR:
898 case RDMA_CM_EVENT_CONNECT_ERROR:
899 case RDMA_CM_EVENT_UNREACHABLE:
900 case RDMA_CM_EVENT_REJECTED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300901 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300902 break;
903 case RDMA_CM_EVENT_DISCONNECTED:
Or Gerlitz2f5de152008-07-22 14:16:21 -0700904 case RDMA_CM_EVENT_ADDR_CHANGE:
Ariel Nahum5426b172014-12-07 16:09:54 +0200905 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
906 iser_cleanup_handler(cma_id, false);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300907 break;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300908 case RDMA_CM_EVENT_DEVICE_REMOVAL:
909 /*
910 * we *must* destroy the device as we cannot rely
911 * on iscsid to be around to initiate error handling.
Ariel Nahum5426b172014-12-07 16:09:54 +0200912 * also if we are not in state DOWN implicitly destroy
913 * the cma_id.
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300914 */
915 iser_cleanup_handler(cma_id, true);
Ariel Nahum5426b172014-12-07 16:09:54 +0200916 if (iser_conn->state != ISER_CONN_DOWN) {
917 iser_conn->ib_conn.cma_id = NULL;
918 ret = 1;
919 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300920 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300921 default:
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300922 iser_err("Unexpected RDMA CM event: %s (%d)\n",
923 rdma_event_msg(event->event), event->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300924 break;
925 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300926 mutex_unlock(&iser_conn->state_mutex);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300927
928 return ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300929}
930
Sagi Grimberg5716af62014-10-01 14:01:57 +0300931void iser_conn_init(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300932{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300933 iser_conn->state = ISER_CONN_INIT;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300934 iser_conn->ib_conn.post_recv_buf_count = 0;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300935 init_completion(&iser_conn->ib_conn.flush_comp);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300936 init_completion(&iser_conn->stop_completion);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300937 init_completion(&iser_conn->ib_completion);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300938 init_completion(&iser_conn->up_completion);
939 INIT_LIST_HEAD(&iser_conn->conn_list);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300940 spin_lock_init(&iser_conn->ib_conn.lock);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300941 mutex_init(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300942}
943
944 /**
945 * starts the process of connecting to the target
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200946 * sleeps until the connection is established or rejected
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300947 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300948int iser_connect(struct iser_conn *iser_conn,
Roi Dayan96ed02d2014-07-31 13:27:44 +0300949 struct sockaddr *src_addr,
950 struct sockaddr *dst_addr,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300951 int non_blocking)
952{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300953 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300954 int err = 0;
955
Sagi Grimberg5716af62014-10-01 14:01:57 +0300956 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300957
Sagi Grimberg5716af62014-10-01 14:01:57 +0300958 sprintf(iser_conn->name, "%pISp", dst_addr);
Roi Dayan96ed02d2014-07-31 13:27:44 +0300959
Sagi Grimberg5716af62014-10-01 14:01:57 +0300960 iser_info("connecting to: %s\n", iser_conn->name);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300961
962 /* the device is known only --after-- address resolution */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300963 ib_conn->device = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300964
Sagi Grimberg5716af62014-10-01 14:01:57 +0300965 iser_conn->state = ISER_CONN_PENDING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300966
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300967 ib_conn->beacon.wr_id = ISER_BEACON_WRID;
968 ib_conn->beacon.opcode = IB_WR_SEND;
969
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300970 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
971 (void *)iser_conn,
972 RDMA_PS_TCP, IB_QPT_RC);
973 if (IS_ERR(ib_conn->cma_id)) {
974 err = PTR_ERR(ib_conn->cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300975 iser_err("rdma_create_id failed: %d\n", err);
976 goto id_failure;
977 }
978
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300979 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300980 if (err) {
981 iser_err("rdma_resolve_addr failed: %d\n", err);
982 goto addr_failure;
983 }
984
985 if (!non_blocking) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300986 wait_for_completion_interruptible(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300987
Sagi Grimberg5716af62014-10-01 14:01:57 +0300988 if (iser_conn->state != ISER_CONN_UP) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300989 err = -EIO;
990 goto connect_failure;
991 }
992 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300993 mutex_unlock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300994
995 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300996 list_add(&iser_conn->conn_list, &ig.connlist);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300997 mutex_unlock(&ig.connlist_mutex);
998 return 0;
999
1000id_failure:
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001001 ib_conn->cma_id = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001002addr_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001003 iser_conn->state = ISER_CONN_DOWN;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001004connect_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001005 mutex_unlock(&iser_conn->state_mutex);
1006 iser_conn_release(iser_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001007 return err;
1008}
1009
Sagi Grimberg5716af62014-10-01 14:01:57 +03001010int iser_post_recvl(struct iser_conn *iser_conn)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001011{
1012 struct ib_recv_wr rx_wr, *rx_wr_failed;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001013 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001014 struct ib_sge sge;
1015 int ib_ret;
1016
Sagi Grimberg5716af62014-10-01 14:01:57 +03001017 sge.addr = iser_conn->login_resp_dma;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001018 sge.length = ISER_RX_LOGIN_SIZE;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001019 sge.lkey = ib_conn->device->mr->lkey;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001020
Sagi Grimberg49df2782014-12-07 16:09:57 +02001021 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001022 rx_wr.sg_list = &sge;
1023 rx_wr.num_sge = 1;
1024 rx_wr.next = NULL;
1025
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001026 ib_conn->post_recv_buf_count++;
1027 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001028 if (ib_ret) {
1029 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001030 ib_conn->post_recv_buf_count--;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001031 }
1032 return ib_ret;
1033}
1034
Sagi Grimberg5716af62014-10-01 14:01:57 +03001035int iser_post_recvm(struct iser_conn *iser_conn, int count)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001036{
1037 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1038 int i, ib_ret;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001039 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001040 unsigned int my_rx_head = iser_conn->rx_desc_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001041 struct iser_rx_desc *rx_desc;
1042
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001043 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Sagi Grimberg5716af62014-10-01 14:01:57 +03001044 rx_desc = &iser_conn->rx_descs[my_rx_head];
Sagi Grimberg49df2782014-12-07 16:09:57 +02001045 rx_wr->wr_id = (uintptr_t)rx_desc;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001046 rx_wr->sg_list = &rx_desc->rx_sg;
1047 rx_wr->num_sge = 1;
1048 rx_wr->next = rx_wr + 1;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001049 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001050 }
1051
1052 rx_wr--;
1053 rx_wr->next = NULL; /* mark end of work requests list */
1054
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001055 ib_conn->post_recv_buf_count += count;
1056 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001057 if (ib_ret) {
1058 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001059 ib_conn->post_recv_buf_count -= count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001060 } else
Sagi Grimberg5716af62014-10-01 14:01:57 +03001061 iser_conn->rx_desc_head = my_rx_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001062 return ib_ret;
1063}
1064
1065
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001066/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001067 * iser_start_send - Initiate a Send DTO operation
1068 *
1069 * returns 0 on success, -1 on failure
1070 */
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001071int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
1072 bool signal)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001073{
Or Gerlitzf19624a2010-02-08 13:19:56 +00001074 int ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001075 struct ib_send_wr send_wr, *send_wr_failed;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001076
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001077 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
Sagi Grimberg5716af62014-10-01 14:01:57 +03001078 tx_desc->dma_addr, ISER_HEADERS_LEN,
1079 DMA_TO_DEVICE);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001080
1081 send_wr.next = NULL;
Sagi Grimberg49df2782014-12-07 16:09:57 +02001082 send_wr.wr_id = (uintptr_t)tx_desc;
Or Gerlitzf19624a2010-02-08 13:19:56 +00001083 send_wr.sg_list = tx_desc->tx_sg;
1084 send_wr.num_sge = tx_desc->num_sge;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001085 send_wr.opcode = IB_WR_SEND;
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001086 send_wr.send_flags = signal ? IB_SEND_SIGNALED : 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001087
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001088 ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001089 if (ib_ret)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001090 iser_err("ib_post_send failed, ret:%d\n", ib_ret);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001091
Or Gerlitzf19624a2010-02-08 13:19:56 +00001092 return ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001093}
1094
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001095/**
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001096 * is_iser_tx_desc - Indicate if the completion wr_id
1097 * is a TX descriptor or not.
1098 * @iser_conn: iser connection
1099 * @wr_id: completion WR identifier
1100 *
1101 * Since we cannot rely on wc opcode in FLUSH errors
1102 * we must work around it by checking if the wr_id address
1103 * falls in the iser connection rx_descs buffer. If so
1104 * it is an RX descriptor, otherwize it is a TX.
1105 */
1106static inline bool
1107is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id)
1108{
1109 void *start = iser_conn->rx_descs;
1110 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs);
1111
1112 if (wr_id >= start && wr_id < start + len)
1113 return false;
1114
1115 return true;
1116}
1117
1118/**
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001119 * iser_handle_comp_error() - Handle error completion
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001120 * @ib_conn: connection RDMA resources
1121 * @wc: work completion
1122 *
1123 * Notes: We may handle a FLUSH error completion and in this case
1124 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1125 * error completion we should also notify iscsi layer that
1126 * connection is failed (in case we passed bind stage).
1127 */
1128static void
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001129iser_handle_comp_error(struct ib_conn *ib_conn,
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001130 struct ib_wc *wc)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001131{
Sagi Grimberg49df2782014-12-07 16:09:57 +02001132 void *wr_id = (void *)(uintptr_t)wc->wr_id;
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001133 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
1134 ib_conn);
1135
1136 if (wc->status != IB_WC_WR_FLUSH_ERR)
1137 if (iser_conn->iscsi_conn)
1138 iscsi_conn_failure(iser_conn->iscsi_conn,
1139 ISCSI_ERR_CONN_FAILED);
1140
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001141 if (wc->wr_id == ISER_FASTREG_LI_WRID)
1142 return;
1143
Sagi Grimberg49df2782014-12-07 16:09:57 +02001144 if (is_iser_tx_desc(iser_conn, wr_id)) {
1145 struct iser_tx_desc *desc = wr_id;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001146
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001147 if (desc->type == ISCSI_TX_DATAOUT)
1148 kmem_cache_free(ig.desc_cache, desc);
1149 } else {
1150 ib_conn->post_recv_buf_count--;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001151 }
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001152}
1153
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001154/**
1155 * iser_handle_wc - handle a single work completion
1156 * @wc: work completion
1157 *
1158 * Soft-IRQ context, work completion can be either
1159 * SEND or RECV, and can turn out successful or
1160 * with error (or flush error).
1161 */
1162static void iser_handle_wc(struct ib_wc *wc)
1163{
1164 struct ib_conn *ib_conn;
1165 struct iser_tx_desc *tx_desc;
1166 struct iser_rx_desc *rx_desc;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001167
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001168 ib_conn = wc->qp->qp_context;
Sagi Grimberg06c7fb62014-12-07 16:10:05 +02001169 if (likely(wc->status == IB_WC_SUCCESS)) {
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001170 if (wc->opcode == IB_WC_RECV) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001171 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001172 iser_rcv_completion(rx_desc, wc->byte_len,
1173 ib_conn);
1174 } else
1175 if (wc->opcode == IB_WC_SEND) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001176 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001177 iser_snd_completion(tx_desc, ib_conn);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001178 } else {
1179 iser_err("Unknown wc opcode %d\n", wc->opcode);
1180 }
1181 } else {
1182 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001183 iser_err("%s (%d): wr id %llx vend_err %x\n",
1184 ib_wc_status_msg(wc->status), wc->status,
1185 wc->wr_id, wc->vendor_err);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001186 else
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001187 iser_dbg("%s (%d): wr id %llx\n",
1188 ib_wc_status_msg(wc->status), wc->status,
1189 wc->wr_id);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001190
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001191 if (wc->wr_id == ISER_BEACON_WRID)
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001192 /* all flush errors were consumed */
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001193 complete(&ib_conn->flush_comp);
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001194 else
1195 iser_handle_comp_error(ib_conn, wc);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001196 }
1197}
1198
1199/**
1200 * iser_cq_tasklet_fn - iSER completion polling loop
1201 * @data: iSER completion context
1202 *
1203 * Soft-IRQ context, polling connection CQ until
1204 * either CQ was empty or we exausted polling budget
1205 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001206static void iser_cq_tasklet_fn(unsigned long data)
1207{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001208 struct iser_comp *comp = (struct iser_comp *)data;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001209 struct ib_cq *cq = comp->cq;
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001210 struct ib_wc *const wcs = comp->wcs;
1211 int i, n, completed = 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001212
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001213 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
1214 for (i = 0; i < n; i++)
1215 iser_handle_wc(&wcs[i]);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001216
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001217 completed += n;
1218 if (completed >= iser_cq_poll_limit)
Sagi Grimberg183cfa42014-10-01 14:02:08 +03001219 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001220 }
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001221
1222 /*
1223 * It is assumed here that arming CQ only once its empty
1224 * would not cause interrupts to be missed.
1225 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001226 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001227
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001228 iser_dbg("got %d completions\n", completed);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001229}
1230
1231static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
1232{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001233 struct iser_comp *comp = cq_context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001234
Sagi Grimbergbf175542014-10-01 14:02:07 +03001235 tasklet_schedule(&comp->tasklet);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001236}
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001237
1238u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
1239 enum iser_data_dir cmd_dir, sector_t *sector)
1240{
Sagi Grimbergb130ede2015-04-14 18:08:19 +03001241 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001242 struct fast_reg_descriptor *desc = reg->mem_h;
1243 unsigned long sector_size = iser_task->sc->device->sector_size;
1244 struct ib_mr_status mr_status;
1245 int ret;
1246
1247 if (desc && desc->reg_indicators & ISER_FASTREG_PROTECTED) {
1248 desc->reg_indicators &= ~ISER_FASTREG_PROTECTED;
1249 ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
1250 IB_MR_CHECK_SIG_STATUS, &mr_status);
1251 if (ret) {
1252 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1253 goto err;
1254 }
1255
1256 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1257 sector_t sector_off = mr_status.sig_err.sig_err_offset;
1258
1259 do_div(sector_off, sector_size + 8);
1260 *sector = scsi_get_lba(iser_task->sc) + sector_off;
1261
Randy Dunlap39c978c2014-03-28 14:03:40 -07001262 pr_err("PI error found type %d at sector %llx "
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001263 "expected %x vs actual %x\n",
Randy Dunlap39c978c2014-03-28 14:03:40 -07001264 mr_status.sig_err.err_type,
1265 (unsigned long long)*sector,
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001266 mr_status.sig_err.expected,
1267 mr_status.sig_err.actual);
1268
1269 switch (mr_status.sig_err.err_type) {
1270 case IB_SIG_BAD_GUARD:
1271 return 0x1;
1272 case IB_SIG_BAD_REFTAG:
1273 return 0x3;
1274 case IB_SIG_BAD_APPTAG:
1275 return 0x2;
1276 }
1277 }
1278 }
1279
1280 return 0;
1281err:
1282 /* Not alot we can do here, return ambiguous guard error */
1283 return 0x1;
1284}