blob: e4f89676248c48b70bca819febe1385ac81ecca4 [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 Grimbergd711d812015-08-06 18:32:53 +0300283iser_alloc_reg_res(struct ib_device *ib_device, struct ib_pd *pd,
284 struct iser_reg_resources *res)
285{
286 int ret;
287
288 res->frpl = ib_alloc_fast_reg_page_list(ib_device,
289 ISCSI_ISER_SG_TABLESIZE + 1);
290 if (IS_ERR(res->frpl)) {
291 ret = PTR_ERR(res->frpl);
292 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
293 ret);
294 return PTR_ERR(res->frpl);
295 }
296
297 res->mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
298 ISCSI_ISER_SG_TABLESIZE + 1);
299 if (IS_ERR(res->mr)) {
300 ret = PTR_ERR(res->mr);
301 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
302 goto fast_reg_mr_failure;
303 }
304 res->mr_valid = 1;
305
306 return 0;
307
308fast_reg_mr_failure:
309 ib_free_fast_reg_page_list(res->frpl);
310
311 return ret;
312}
313
314static void
315iser_free_reg_res(struct iser_reg_resources *rsc)
316{
317 ib_dereg_mr(rsc->mr);
318 ib_free_fast_reg_page_list(rsc->frpl);
319}
320
321static int
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300322iser_alloc_pi_ctx(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300323 struct iser_fr_desc *desc)
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300324{
325 struct iser_pi_context *pi_ctx = NULL;
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300326 int ret;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300327
328 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
329 if (!desc->pi_ctx)
330 return -ENOMEM;
331
332 pi_ctx = desc->pi_ctx;
333
Sagi Grimbergd711d812015-08-06 18:32:53 +0300334 ret = iser_alloc_reg_res(ib_device, pd, &pi_ctx->rsc);
335 if (ret) {
336 iser_err("failed to allocate reg_resources\n");
337 goto alloc_reg_res_err;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300338 }
339
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300340 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300341 if (IS_ERR(pi_ctx->sig_mr)) {
342 ret = PTR_ERR(pi_ctx->sig_mr);
343 goto sig_mr_failure;
344 }
Sagi Grimbergd711d812015-08-06 18:32:53 +0300345 pi_ctx->sig_mr_valid = 1;
346 desc->pi_ctx->sig_protected = 0;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300347
348 return 0;
349
350sig_mr_failure:
Sagi Grimbergd711d812015-08-06 18:32:53 +0300351 iser_free_reg_res(&pi_ctx->rsc);
352alloc_reg_res_err:
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300353 kfree(desc->pi_ctx);
354
355 return ret;
356}
357
358static void
359iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
360{
Sagi Grimbergd711d812015-08-06 18:32:53 +0300361 iser_free_reg_res(&pi_ctx->rsc);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300362 ib_dereg_mr(pi_ctx->sig_mr);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300363 kfree(pi_ctx);
364}
365
366static int
Sagi Grimberg310b3472014-03-05 19:43:41 +0200367iser_create_fastreg_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300368 bool pi_enable, struct iser_fr_desc *desc)
Sagi Grimberg310b3472014-03-05 19:43:41 +0200369{
370 int ret;
371
Sagi Grimbergd711d812015-08-06 18:32:53 +0300372 ret = iser_alloc_reg_res(ib_device, pd, &desc->rsc);
373 if (ret) {
374 iser_err("failed to allocate reg_resources\n");
375 return ret;
Sagi Grimberg310b3472014-03-05 19:43:41 +0200376 }
377
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200378 if (pi_enable) {
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300379 ret = iser_alloc_pi_ctx(ib_device, pd, desc);
380 if (ret)
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200381 goto pi_ctx_alloc_failure;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200382 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200383
384 return 0;
Sagi Grimbergd711d812015-08-06 18:32:53 +0300385
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200386pi_ctx_alloc_failure:
Sagi Grimbergd711d812015-08-06 18:32:53 +0300387 iser_free_reg_res(&desc->rsc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200388
389 return ret;
390}
391
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300392/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200393 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300394 * for fast registration work requests.
395 * returns 0 on success, or errno code on failure
396 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300397int iser_create_fastreg_pool(struct ib_conn *ib_conn, unsigned cmds_max)
Sagi Grimberg55878562013-07-28 12:35:42 +0300398{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300399 struct iser_device *device = ib_conn->device;
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300400 struct iser_fr_desc *desc;
Sagi Grimberg55878562013-07-28 12:35:42 +0300401 int i, ret;
402
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300403 INIT_LIST_HEAD(&ib_conn->fastreg.pool);
404 ib_conn->fastreg.pool_size = 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300405 for (i = 0; i < cmds_max; i++) {
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200406 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
Sagi Grimberg55878562013-07-28 12:35:42 +0300407 if (!desc) {
408 iser_err("Failed to allocate a new fast_reg descriptor\n");
409 ret = -ENOMEM;
410 goto err;
411 }
412
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200413 ret = iser_create_fastreg_desc(device->ib_device, device->pd,
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300414 ib_conn->pi_support, desc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200415 if (ret) {
416 iser_err("Failed to create fastreg descriptor err=%d\n",
417 ret);
418 kfree(desc);
419 goto err;
Sagi Grimberg55878562013-07-28 12:35:42 +0300420 }
421
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300422 list_add_tail(&desc->list, &ib_conn->fastreg.pool);
423 ib_conn->fastreg.pool_size++;
Sagi Grimberg55878562013-07-28 12:35:42 +0300424 }
425
426 return 0;
Roi Dayan27ae2d12013-08-19 16:41:51 +0300427
Sagi Grimberg55878562013-07-28 12:35:42 +0300428err:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300429 iser_free_fastreg_pool(ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300430 return ret;
431}
432
433/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200434 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300435 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300436void iser_free_fastreg_pool(struct ib_conn *ib_conn)
Sagi Grimberg55878562013-07-28 12:35:42 +0300437{
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300438 struct iser_fr_desc *desc, *tmp;
Sagi Grimberg55878562013-07-28 12:35:42 +0300439 int i = 0;
440
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300441 if (list_empty(&ib_conn->fastreg.pool))
Sagi Grimberg55878562013-07-28 12:35:42 +0300442 return;
443
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300444 iser_info("freeing conn %p fr pool\n", ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300445
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300446 list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.pool, list) {
Sagi Grimberg55878562013-07-28 12:35:42 +0300447 list_del(&desc->list);
Sagi Grimbergd711d812015-08-06 18:32:53 +0300448 iser_free_reg_res(&desc->rsc);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300449 if (desc->pi_ctx)
450 iser_free_pi_ctx(desc->pi_ctx);
Sagi Grimberg55878562013-07-28 12:35:42 +0300451 kfree(desc);
452 ++i;
453 }
454
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300455 if (i < ib_conn->fastreg.pool_size)
Sagi Grimberg55878562013-07-28 12:35:42 +0300456 iser_warn("pool still has %d regions registered\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300457 ib_conn->fastreg.pool_size - i);
Sagi Grimberg55878562013-07-28 12:35:42 +0300458}
459
460/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300461 * iser_create_ib_conn_res - Queue-Pair (QP)
462 *
463 * returns 0 on success, -1 on failure
464 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300465static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300466{
Minh Tranf4641ef2014-12-07 16:09:52 +0200467 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
468 ib_conn);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300469 struct iser_device *device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200470 struct ib_device_attr *dev_attr;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300471 struct ib_qp_init_attr init_attr;
472 int ret = -ENOMEM;
473 int index, min_index = 0;
474
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300475 BUG_ON(ib_conn->device == NULL);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300476
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300477 device = ib_conn->device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200478 dev_attr = &device->dev_attr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300479
480 memset(&init_attr, 0, sizeof init_attr);
481
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000482 mutex_lock(&ig.connlist_mutex);
483 /* select the CQ with the minimal number of usages */
Sagi Grimbergbf175542014-10-01 14:02:07 +0300484 for (index = 0; index < device->comps_used; index++) {
485 if (device->comps[index].active_qps <
486 device->comps[min_index].active_qps)
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000487 min_index = index;
Sagi Grimbergbf175542014-10-01 14:02:07 +0300488 }
489 ib_conn->comp = &device->comps[min_index];
490 ib_conn->comp->active_qps++;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000491 mutex_unlock(&ig.connlist_mutex);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300492 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000493
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300494 init_attr.event_handler = iser_qp_event_callback;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300495 init_attr.qp_context = (void *)ib_conn;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300496 init_attr.send_cq = ib_conn->comp->cq;
497 init_attr.recv_cq = ib_conn->comp->cq;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300498 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000499 init_attr.cap.max_send_sge = 2;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000500 init_attr.cap.max_recv_sge = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300501 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
502 init_attr.qp_type = IB_QPT_RC;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300503 if (ib_conn->pi_support) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300504 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200505 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Minh Tranf4641ef2014-12-07 16:09:52 +0200506 iser_conn->max_cmds =
507 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200508 } else {
Minh Tranf4641ef2014-12-07 16:09:52 +0200509 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
510 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
511 iser_conn->max_cmds =
512 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
513 } else {
514 init_attr.cap.max_send_wr = dev_attr->max_qp_wr;
515 iser_conn->max_cmds =
516 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr);
517 iser_dbg("device %s supports max_send_wr %d\n",
518 device->ib_device->name, dev_attr->max_qp_wr);
519 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200520 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300521
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300522 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300523 if (ret)
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300524 goto out_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300525
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300526 ib_conn->qp = ib_conn->cma_id->qp;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300527 iser_info("setting conn %p cma_id %p qp %p\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300528 ib_conn, ib_conn->cma_id,
529 ib_conn->cma_id->qp);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300530 return ret;
531
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300532out_err:
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200533 mutex_lock(&ig.connlist_mutex);
534 ib_conn->comp->active_qps--;
535 mutex_unlock(&ig.connlist_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300536 iser_err("unable to alloc mem or create resource, err %d\n", ret);
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200537
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300538 return ret;
539}
540
541/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300542 * based on the resolved device node GUID see if there already allocated
543 * device for this device. If there's no such, create one.
544 */
545static
546struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
547{
Arne Redlich9a378272008-03-04 14:07:22 +0200548 struct iser_device *device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300549
550 mutex_lock(&ig.device_list_mutex);
551
Arne Redlich9a378272008-03-04 14:07:22 +0200552 list_for_each_entry(device, &ig.device_list, ig_list)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300553 /* find if there's a match using the node GUID */
554 if (device->ib_device->node_guid == cma_id->device->node_guid)
Arne Redlichd33ed422008-03-04 14:11:54 +0200555 goto inc_refcnt;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300556
Arne Redlich9a378272008-03-04 14:07:22 +0200557 device = kzalloc(sizeof *device, GFP_KERNEL);
558 if (device == NULL)
559 goto out;
560
561 /* assign this device to the device */
562 device->ib_device = cma_id->device;
563 /* init the device and link it into ig device list */
564 if (iser_create_device_ib_res(device)) {
565 kfree(device);
566 device = NULL;
567 goto out;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300568 }
Arne Redlich9a378272008-03-04 14:07:22 +0200569 list_add(&device->ig_list, &ig.device_list);
570
Arne Redlichd33ed422008-03-04 14:11:54 +0200571inc_refcnt:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300572 device->refcount++;
Arne Redlichd33ed422008-03-04 14:11:54 +0200573out:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300574 mutex_unlock(&ig.device_list_mutex);
575 return device;
576}
577
578/* if there's no demand for this device, release it */
579static void iser_device_try_release(struct iser_device *device)
580{
581 mutex_lock(&ig.device_list_mutex);
582 device->refcount--;
Roi Dayan4f363882013-05-01 13:25:25 +0000583 iser_info("device %p refcount %d\n", device, device->refcount);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300584 if (!device->refcount) {
585 iser_free_device_ib_res(device);
586 list_del(&device->ig_list);
587 kfree(device);
588 }
589 mutex_unlock(&ig.device_list_mutex);
590}
591
Ariel Nahum504130c2014-07-31 13:27:49 +0300592/**
593 * Called with state mutex held
594 **/
Sagi Grimberg5716af62014-10-01 14:01:57 +0300595static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
596 enum iser_conn_state comp,
597 enum iser_conn_state exch)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300598{
599 int ret;
600
Sagi Grimberg5716af62014-10-01 14:01:57 +0300601 ret = (iser_conn->state == comp);
602 if (ret)
603 iser_conn->state = exch;
604
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300605 return ret;
606}
607
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300608void iser_release_work(struct work_struct *work)
609{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300610 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300611
Sagi Grimberg5716af62014-10-01 14:01:57 +0300612 iser_conn = container_of(work, struct iser_conn, release_work);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300613
Sagi Grimbergc107a6c2014-10-01 14:02:02 +0300614 /* Wait for conn_stop to complete */
615 wait_for_completion(&iser_conn->stop_completion);
616 /* Wait for IB resouces cleanup to complete */
617 wait_for_completion(&iser_conn->ib_completion);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300618
Sagi Grimberg5716af62014-10-01 14:01:57 +0300619 mutex_lock(&iser_conn->state_mutex);
620 iser_conn->state = ISER_CONN_DOWN;
621 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300622
Sagi Grimberg5716af62014-10-01 14:01:57 +0300623 iser_conn_release(iser_conn);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300624}
625
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300626/**
Sagi Grimberg96f15192014-10-01 14:02:00 +0300627 * iser_free_ib_conn_res - release IB related resources
628 * @iser_conn: iser connection struct
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200629 * @destroy: indicator if we need to try to release the
630 * iser device and memory regoins pool (only iscsi
631 * shutdown and DEVICE_REMOVAL will use this).
Sagi Grimberg96f15192014-10-01 14:02:00 +0300632 *
633 * This routine is called with the iser state mutex held
634 * so the cm_id removal is out of here. It is Safe to
635 * be invoked multiple times.
636 */
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300637static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200638 bool destroy)
Sagi Grimberg96f15192014-10-01 14:02:00 +0300639{
640 struct ib_conn *ib_conn = &iser_conn->ib_conn;
641 struct iser_device *device = ib_conn->device;
642
643 iser_info("freeing conn %p cma_id %p qp %p\n",
644 iser_conn, ib_conn->cma_id, ib_conn->qp);
645
Sagi Grimberg96f15192014-10-01 14:02:00 +0300646 if (ib_conn->qp != NULL) {
Sagi Grimbergbf175542014-10-01 14:02:07 +0300647 ib_conn->comp->active_qps--;
Sagi Grimberg96f15192014-10-01 14:02:00 +0300648 rdma_destroy_qp(ib_conn->cma_id);
649 ib_conn->qp = NULL;
650 }
651
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200652 if (destroy) {
653 if (iser_conn->rx_descs)
654 iser_free_rx_descriptors(iser_conn);
655
656 if (device != NULL) {
657 iser_device_try_release(device);
658 ib_conn->device = NULL;
659 }
Sagi Grimberg96f15192014-10-01 14:02:00 +0300660 }
661}
662
663/**
Roland Dreier41179e22007-07-17 18:37:42 -0700664 * Frees all conn objects and deallocs conn descriptor
665 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300666void iser_conn_release(struct iser_conn *iser_conn)
Roland Dreier41179e22007-07-17 18:37:42 -0700667{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300668 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Roland Dreier41179e22007-07-17 18:37:42 -0700669
Roland Dreier41179e22007-07-17 18:37:42 -0700670 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300671 list_del(&iser_conn->conn_list);
Roland Dreier41179e22007-07-17 18:37:42 -0700672 mutex_unlock(&ig.connlist_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300673
Sagi Grimberg5716af62014-10-01 14:01:57 +0300674 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200675 /* In case we endup here without ep_disconnect being invoked. */
Ariel Nahum5426b172014-12-07 16:09:54 +0200676 if (iser_conn->state != ISER_CONN_DOWN) {
Ariel Nahumaea8f4d2014-10-01 14:02:06 +0300677 iser_warn("iser conn %p state %d, expected state down.\n",
678 iser_conn, iser_conn->state);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200679 iscsi_destroy_endpoint(iser_conn->ep);
Ariel Nahum5426b172014-12-07 16:09:54 +0200680 iser_conn->state = ISER_CONN_DOWN;
681 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300682 /*
683 * In case we never got to bind stage, we still need to
684 * release IB resources (which is safe to call more than once).
685 */
686 iser_free_ib_conn_res(iser_conn, true);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300687 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300688
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300689 if (ib_conn->cma_id != NULL) {
690 rdma_destroy_id(ib_conn->cma_id);
691 ib_conn->cma_id = NULL;
Roi Dayan5b61ff42013-05-08 12:21:17 +0000692 }
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300693
Sagi Grimberg5716af62014-10-01 14:01:57 +0300694 kfree(iser_conn);
Roland Dreier41179e22007-07-17 18:37:42 -0700695}
696
697/**
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300698 * triggers start of the disconnect procedures and wait for them to be done
699 * Called with state mutex held
700 */
701int iser_conn_terminate(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300702{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300703 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300704 struct ib_send_wr *bad_wr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300705 int err = 0;
706
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300707 /* terminate the iser conn only if the conn state is UP */
708 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
709 ISER_CONN_TERMINATING))
710 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300711
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300712 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
713
714 /* suspend queuing of new iscsi commands */
715 if (iser_conn->iscsi_conn)
716 iscsi_suspend_queue(iser_conn->iscsi_conn);
717
718 /*
719 * In case we didn't already clean up the cma_id (peer initiated
720 * a disconnection), we need to Cause the CMA to change the QP
721 * state to ERROR.
722 */
723 if (ib_conn->cma_id) {
724 err = rdma_disconnect(ib_conn->cma_id);
725 if (err)
726 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
727 iser_conn, err);
728
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300729 /* post an indication that all flush errors were consumed */
730 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200731 if (err) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300732 iser_err("conn %p failed to post beacon", ib_conn);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200733 return 1;
734 }
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300735
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300736 wait_for_completion(&ib_conn->flush_comp);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300737 }
738
739 return 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300740}
741
Ariel Nahum504130c2014-07-31 13:27:49 +0300742/**
743 * Called with state mutex held
744 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300745static void iser_connect_error(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300746{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300747 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300748
Sagi Grimberg5716af62014-10-01 14:01:57 +0300749 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimbergc4de4662015-04-14 18:08:11 +0300750 iser_conn->state = ISER_CONN_TERMINATING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300751}
752
Ariel Nahum504130c2014-07-31 13:27:49 +0300753/**
754 * Called with state mutex held
755 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300756static void iser_addr_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300757{
758 struct iser_device *device;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300759 struct iser_conn *iser_conn;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300760 struct ib_conn *ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300761 int ret;
762
Sagi Grimberg5716af62014-10-01 14:01:57 +0300763 iser_conn = (struct iser_conn *)cma_id->context;
764 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300765 /* bailout */
766 return;
767
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300768 ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300769 device = iser_device_find_by_ib_device(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200770 if (!device) {
771 iser_err("device lookup/creation failed\n");
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300772 iser_connect_error(cma_id);
773 return;
Arne Redlichd33ed422008-03-04 14:11:54 +0200774 }
775
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300776 ib_conn->device = device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300777
Alex Tabachnik7f733842014-03-05 19:43:46 +0200778 /* connection T10-PI support */
779 if (iser_pi_enable) {
780 if (!(device->dev_attr.device_cap_flags &
781 IB_DEVICE_SIGNATURE_HANDOVER)) {
782 iser_warn("T10-PI requested but not supported on %s, "
783 "continue without T10-PI\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300784 ib_conn->device->ib_device->name);
785 ib_conn->pi_support = false;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200786 } else {
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300787 ib_conn->pi_support = true;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200788 }
789 }
790
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300791 ret = rdma_resolve_route(cma_id, 1000);
792 if (ret) {
793 iser_err("resolve route failed: %d\n", ret);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300794 iser_connect_error(cma_id);
795 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300796 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300797}
798
Ariel Nahum504130c2014-07-31 13:27:49 +0300799/**
800 * Called with state mutex held
801 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300802static void iser_route_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300803{
804 struct rdma_conn_param conn_param;
805 int ret;
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000806 struct iser_cm_hdr req_hdr;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300807 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300808 struct ib_conn *ib_conn = &iser_conn->ib_conn;
809 struct iser_device *device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300810
Sagi Grimberg5716af62014-10-01 14:01:57 +0300811 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300812 /* bailout */
813 return;
814
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300815 ret = iser_create_ib_conn_res(ib_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300816 if (ret)
817 goto failure;
818
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300819 memset(&conn_param, 0, sizeof conn_param);
Sagi Grimberg2ea32932014-07-31 13:27:46 +0300820 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300821 conn_param.initiator_depth = 1;
822 conn_param.retry_count = 7;
823 conn_param.rnr_retry_count = 6;
824
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000825 memset(&req_hdr, 0, sizeof(req_hdr));
826 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
827 ISER_SEND_W_INV_NOT_SUPPORTED);
828 conn_param.private_data = (void *)&req_hdr;
829 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
830
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300831 ret = rdma_connect(cma_id, &conn_param);
832 if (ret) {
833 iser_err("failure connecting: %d\n", ret);
834 goto failure;
835 }
836
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300837 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300838failure:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300839 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300840}
841
842static void iser_connected_handler(struct rdma_cm_id *cma_id)
843{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300844 struct iser_conn *iser_conn;
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300845 struct ib_qp_attr attr;
846 struct ib_qp_init_attr init_attr;
847
Sagi Grimberg5716af62014-10-01 14:01:57 +0300848 iser_conn = (struct iser_conn *)cma_id->context;
849 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300850 /* bailout */
851 return;
852
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300853 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
854 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 +0300855
Sagi Grimberg5716af62014-10-01 14:01:57 +0300856 iser_conn->state = ISER_CONN_UP;
857 complete(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300858}
859
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300860static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300861{
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300862 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300863
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300864 if (iser_conn_terminate(iser_conn)) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300865 if (iser_conn->iscsi_conn)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300866 iscsi_conn_failure(iser_conn->iscsi_conn,
867 ISCSI_ERR_CONN_FAILED);
Roi Dayan7d9eacf2014-02-04 16:54:54 +0200868 else
869 iser_err("iscsi_iser connection isn't bound\n");
870 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300871}
872
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300873static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200874 bool destroy)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300875{
876 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
877
878 /*
879 * We are not guaranteed that we visited disconnected_handler
880 * by now, call it here to be safe that we handle CM drep
881 * and flush errors.
882 */
883 iser_disconnected_handler(cma_id);
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200884 iser_free_ib_conn_res(iser_conn, destroy);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300885 complete(&iser_conn->ib_completion);
886};
887
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300888static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
889{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300890 struct iser_conn *iser_conn;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300891 int ret = 0;
Ariel Nahum504130c2014-07-31 13:27:49 +0300892
Sagi Grimberg5716af62014-10-01 14:01:57 +0300893 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300894 iser_info("%s (%d): status %d conn %p id %p\n",
895 rdma_event_msg(event->event), event->event,
896 event->status, cma_id->context, cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300897
Sagi Grimberg5716af62014-10-01 14:01:57 +0300898 mutex_lock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300899 switch (event->event) {
900 case RDMA_CM_EVENT_ADDR_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300901 iser_addr_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300902 break;
903 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300904 iser_route_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300905 break;
906 case RDMA_CM_EVENT_ESTABLISHED:
907 iser_connected_handler(cma_id);
908 break;
909 case RDMA_CM_EVENT_ADDR_ERROR:
910 case RDMA_CM_EVENT_ROUTE_ERROR:
911 case RDMA_CM_EVENT_CONNECT_ERROR:
912 case RDMA_CM_EVENT_UNREACHABLE:
913 case RDMA_CM_EVENT_REJECTED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300914 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300915 break;
916 case RDMA_CM_EVENT_DISCONNECTED:
Or Gerlitz2f5de152008-07-22 14:16:21 -0700917 case RDMA_CM_EVENT_ADDR_CHANGE:
Ariel Nahum5426b172014-12-07 16:09:54 +0200918 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
919 iser_cleanup_handler(cma_id, false);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300920 break;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300921 case RDMA_CM_EVENT_DEVICE_REMOVAL:
922 /*
923 * we *must* destroy the device as we cannot rely
924 * on iscsid to be around to initiate error handling.
Ariel Nahum5426b172014-12-07 16:09:54 +0200925 * also if we are not in state DOWN implicitly destroy
926 * the cma_id.
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300927 */
928 iser_cleanup_handler(cma_id, true);
Ariel Nahum5426b172014-12-07 16:09:54 +0200929 if (iser_conn->state != ISER_CONN_DOWN) {
930 iser_conn->ib_conn.cma_id = NULL;
931 ret = 1;
932 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300933 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300934 default:
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300935 iser_err("Unexpected RDMA CM event: %s (%d)\n",
936 rdma_event_msg(event->event), event->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300937 break;
938 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300939 mutex_unlock(&iser_conn->state_mutex);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300940
941 return ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300942}
943
Sagi Grimberg5716af62014-10-01 14:01:57 +0300944void iser_conn_init(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300945{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300946 iser_conn->state = ISER_CONN_INIT;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300947 iser_conn->ib_conn.post_recv_buf_count = 0;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300948 init_completion(&iser_conn->ib_conn.flush_comp);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300949 init_completion(&iser_conn->stop_completion);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300950 init_completion(&iser_conn->ib_completion);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300951 init_completion(&iser_conn->up_completion);
952 INIT_LIST_HEAD(&iser_conn->conn_list);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300953 spin_lock_init(&iser_conn->ib_conn.lock);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300954 mutex_init(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300955}
956
957 /**
958 * starts the process of connecting to the target
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200959 * sleeps until the connection is established or rejected
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300960 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300961int iser_connect(struct iser_conn *iser_conn,
Roi Dayan96ed02d2014-07-31 13:27:44 +0300962 struct sockaddr *src_addr,
963 struct sockaddr *dst_addr,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300964 int non_blocking)
965{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300966 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300967 int err = 0;
968
Sagi Grimberg5716af62014-10-01 14:01:57 +0300969 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300970
Sagi Grimberg5716af62014-10-01 14:01:57 +0300971 sprintf(iser_conn->name, "%pISp", dst_addr);
Roi Dayan96ed02d2014-07-31 13:27:44 +0300972
Sagi Grimberg5716af62014-10-01 14:01:57 +0300973 iser_info("connecting to: %s\n", iser_conn->name);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300974
975 /* the device is known only --after-- address resolution */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300976 ib_conn->device = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300977
Sagi Grimberg5716af62014-10-01 14:01:57 +0300978 iser_conn->state = ISER_CONN_PENDING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300979
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300980 ib_conn->beacon.wr_id = ISER_BEACON_WRID;
981 ib_conn->beacon.opcode = IB_WR_SEND;
982
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300983 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
984 (void *)iser_conn,
985 RDMA_PS_TCP, IB_QPT_RC);
986 if (IS_ERR(ib_conn->cma_id)) {
987 err = PTR_ERR(ib_conn->cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300988 iser_err("rdma_create_id failed: %d\n", err);
989 goto id_failure;
990 }
991
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300992 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300993 if (err) {
994 iser_err("rdma_resolve_addr failed: %d\n", err);
995 goto addr_failure;
996 }
997
998 if (!non_blocking) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300999 wait_for_completion_interruptible(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001000
Sagi Grimberg5716af62014-10-01 14:01:57 +03001001 if (iser_conn->state != ISER_CONN_UP) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001002 err = -EIO;
1003 goto connect_failure;
1004 }
1005 }
Sagi Grimberg5716af62014-10-01 14:01:57 +03001006 mutex_unlock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001007
1008 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +03001009 list_add(&iser_conn->conn_list, &ig.connlist);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001010 mutex_unlock(&ig.connlist_mutex);
1011 return 0;
1012
1013id_failure:
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001014 ib_conn->cma_id = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001015addr_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001016 iser_conn->state = ISER_CONN_DOWN;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001017connect_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001018 mutex_unlock(&iser_conn->state_mutex);
1019 iser_conn_release(iser_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001020 return err;
1021}
1022
Sagi Grimberg5716af62014-10-01 14:01:57 +03001023int iser_post_recvl(struct iser_conn *iser_conn)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001024{
1025 struct ib_recv_wr rx_wr, *rx_wr_failed;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001026 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001027 struct ib_sge sge;
1028 int ib_ret;
1029
Sagi Grimberg5716af62014-10-01 14:01:57 +03001030 sge.addr = iser_conn->login_resp_dma;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001031 sge.length = ISER_RX_LOGIN_SIZE;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001032 sge.lkey = ib_conn->device->mr->lkey;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001033
Sagi Grimberg49df2782014-12-07 16:09:57 +02001034 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001035 rx_wr.sg_list = &sge;
1036 rx_wr.num_sge = 1;
1037 rx_wr.next = NULL;
1038
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001039 ib_conn->post_recv_buf_count++;
1040 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001041 if (ib_ret) {
1042 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001043 ib_conn->post_recv_buf_count--;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001044 }
1045 return ib_ret;
1046}
1047
Sagi Grimberg5716af62014-10-01 14:01:57 +03001048int iser_post_recvm(struct iser_conn *iser_conn, int count)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001049{
1050 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1051 int i, ib_ret;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001052 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001053 unsigned int my_rx_head = iser_conn->rx_desc_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001054 struct iser_rx_desc *rx_desc;
1055
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001056 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Sagi Grimberg5716af62014-10-01 14:01:57 +03001057 rx_desc = &iser_conn->rx_descs[my_rx_head];
Sagi Grimberg49df2782014-12-07 16:09:57 +02001058 rx_wr->wr_id = (uintptr_t)rx_desc;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001059 rx_wr->sg_list = &rx_desc->rx_sg;
1060 rx_wr->num_sge = 1;
1061 rx_wr->next = rx_wr + 1;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001062 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001063 }
1064
1065 rx_wr--;
1066 rx_wr->next = NULL; /* mark end of work requests list */
1067
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001068 ib_conn->post_recv_buf_count += count;
1069 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001070 if (ib_ret) {
1071 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001072 ib_conn->post_recv_buf_count -= count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001073 } else
Sagi Grimberg5716af62014-10-01 14:01:57 +03001074 iser_conn->rx_desc_head = my_rx_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001075 return ib_ret;
1076}
1077
1078
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001079/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001080 * iser_start_send - Initiate a Send DTO operation
1081 *
1082 * returns 0 on success, -1 on failure
1083 */
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001084int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
1085 bool signal)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001086{
Or Gerlitzf19624a2010-02-08 13:19:56 +00001087 int ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001088 struct ib_send_wr send_wr, *send_wr_failed;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001089
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001090 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
Sagi Grimberg5716af62014-10-01 14:01:57 +03001091 tx_desc->dma_addr, ISER_HEADERS_LEN,
1092 DMA_TO_DEVICE);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001093
1094 send_wr.next = NULL;
Sagi Grimberg49df2782014-12-07 16:09:57 +02001095 send_wr.wr_id = (uintptr_t)tx_desc;
Or Gerlitzf19624a2010-02-08 13:19:56 +00001096 send_wr.sg_list = tx_desc->tx_sg;
1097 send_wr.num_sge = tx_desc->num_sge;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001098 send_wr.opcode = IB_WR_SEND;
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001099 send_wr.send_flags = signal ? IB_SEND_SIGNALED : 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001100
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001101 ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001102 if (ib_ret)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001103 iser_err("ib_post_send failed, ret:%d\n", ib_ret);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001104
Or Gerlitzf19624a2010-02-08 13:19:56 +00001105 return ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001106}
1107
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001108/**
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001109 * is_iser_tx_desc - Indicate if the completion wr_id
1110 * is a TX descriptor or not.
1111 * @iser_conn: iser connection
1112 * @wr_id: completion WR identifier
1113 *
1114 * Since we cannot rely on wc opcode in FLUSH errors
1115 * we must work around it by checking if the wr_id address
1116 * falls in the iser connection rx_descs buffer. If so
1117 * it is an RX descriptor, otherwize it is a TX.
1118 */
1119static inline bool
1120is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id)
1121{
1122 void *start = iser_conn->rx_descs;
1123 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs);
1124
1125 if (wr_id >= start && wr_id < start + len)
1126 return false;
1127
1128 return true;
1129}
1130
1131/**
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001132 * iser_handle_comp_error() - Handle error completion
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001133 * @ib_conn: connection RDMA resources
1134 * @wc: work completion
1135 *
1136 * Notes: We may handle a FLUSH error completion and in this case
1137 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1138 * error completion we should also notify iscsi layer that
1139 * connection is failed (in case we passed bind stage).
1140 */
1141static void
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001142iser_handle_comp_error(struct ib_conn *ib_conn,
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001143 struct ib_wc *wc)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001144{
Sagi Grimberg49df2782014-12-07 16:09:57 +02001145 void *wr_id = (void *)(uintptr_t)wc->wr_id;
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001146 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
1147 ib_conn);
1148
1149 if (wc->status != IB_WC_WR_FLUSH_ERR)
1150 if (iser_conn->iscsi_conn)
1151 iscsi_conn_failure(iser_conn->iscsi_conn,
1152 ISCSI_ERR_CONN_FAILED);
1153
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001154 if (wc->wr_id == ISER_FASTREG_LI_WRID)
1155 return;
1156
Sagi Grimberg49df2782014-12-07 16:09:57 +02001157 if (is_iser_tx_desc(iser_conn, wr_id)) {
1158 struct iser_tx_desc *desc = wr_id;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001159
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001160 if (desc->type == ISCSI_TX_DATAOUT)
1161 kmem_cache_free(ig.desc_cache, desc);
1162 } else {
1163 ib_conn->post_recv_buf_count--;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001164 }
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001165}
1166
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001167/**
1168 * iser_handle_wc - handle a single work completion
1169 * @wc: work completion
1170 *
1171 * Soft-IRQ context, work completion can be either
1172 * SEND or RECV, and can turn out successful or
1173 * with error (or flush error).
1174 */
1175static void iser_handle_wc(struct ib_wc *wc)
1176{
1177 struct ib_conn *ib_conn;
1178 struct iser_tx_desc *tx_desc;
1179 struct iser_rx_desc *rx_desc;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001180
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001181 ib_conn = wc->qp->qp_context;
Sagi Grimberg06c7fb62014-12-07 16:10:05 +02001182 if (likely(wc->status == IB_WC_SUCCESS)) {
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001183 if (wc->opcode == IB_WC_RECV) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001184 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001185 iser_rcv_completion(rx_desc, wc->byte_len,
1186 ib_conn);
1187 } else
1188 if (wc->opcode == IB_WC_SEND) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001189 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001190 iser_snd_completion(tx_desc, ib_conn);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001191 } else {
1192 iser_err("Unknown wc opcode %d\n", wc->opcode);
1193 }
1194 } else {
1195 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001196 iser_err("%s (%d): wr id %llx vend_err %x\n",
1197 ib_wc_status_msg(wc->status), wc->status,
1198 wc->wr_id, wc->vendor_err);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001199 else
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001200 iser_dbg("%s (%d): wr id %llx\n",
1201 ib_wc_status_msg(wc->status), wc->status,
1202 wc->wr_id);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001203
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001204 if (wc->wr_id == ISER_BEACON_WRID)
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001205 /* all flush errors were consumed */
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001206 complete(&ib_conn->flush_comp);
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001207 else
1208 iser_handle_comp_error(ib_conn, wc);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001209 }
1210}
1211
1212/**
1213 * iser_cq_tasklet_fn - iSER completion polling loop
1214 * @data: iSER completion context
1215 *
1216 * Soft-IRQ context, polling connection CQ until
1217 * either CQ was empty or we exausted polling budget
1218 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001219static void iser_cq_tasklet_fn(unsigned long data)
1220{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001221 struct iser_comp *comp = (struct iser_comp *)data;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001222 struct ib_cq *cq = comp->cq;
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001223 struct ib_wc *const wcs = comp->wcs;
1224 int i, n, completed = 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001225
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001226 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
1227 for (i = 0; i < n; i++)
1228 iser_handle_wc(&wcs[i]);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001229
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001230 completed += n;
1231 if (completed >= iser_cq_poll_limit)
Sagi Grimberg183cfa42014-10-01 14:02:08 +03001232 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001233 }
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001234
1235 /*
1236 * It is assumed here that arming CQ only once its empty
1237 * would not cause interrupts to be missed.
1238 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001239 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001240
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001241 iser_dbg("got %d completions\n", completed);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001242}
1243
1244static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
1245{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001246 struct iser_comp *comp = cq_context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001247
Sagi Grimbergbf175542014-10-01 14:02:07 +03001248 tasklet_schedule(&comp->tasklet);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001249}
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001250
1251u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
1252 enum iser_data_dir cmd_dir, sector_t *sector)
1253{
Sagi Grimbergb130ede2015-04-14 18:08:19 +03001254 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimberg5190cc22015-08-06 18:32:54 +03001255 struct iser_fr_desc *desc = reg->mem_h;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001256 unsigned long sector_size = iser_task->sc->device->sector_size;
1257 struct ib_mr_status mr_status;
1258 int ret;
1259
Sagi Grimbergd711d812015-08-06 18:32:53 +03001260 if (desc && desc->pi_ctx->sig_protected) {
1261 desc->pi_ctx->sig_protected = 0;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001262 ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
1263 IB_MR_CHECK_SIG_STATUS, &mr_status);
1264 if (ret) {
1265 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1266 goto err;
1267 }
1268
1269 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1270 sector_t sector_off = mr_status.sig_err.sig_err_offset;
1271
1272 do_div(sector_off, sector_size + 8);
1273 *sector = scsi_get_lba(iser_task->sc) + sector_off;
1274
Randy Dunlap39c978c2014-03-28 14:03:40 -07001275 pr_err("PI error found type %d at sector %llx "
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001276 "expected %x vs actual %x\n",
Randy Dunlap39c978c2014-03-28 14:03:40 -07001277 mr_status.sig_err.err_type,
1278 (unsigned long long)*sector,
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001279 mr_status.sig_err.expected,
1280 mr_status.sig_err.actual);
1281
1282 switch (mr_status.sig_err.err_type) {
1283 case IB_SIG_BAD_GUARD:
1284 return 0x1;
1285 case IB_SIG_BAD_REFTAG:
1286 return 0x3;
1287 case IB_SIG_BAD_APPTAG:
1288 return 0x2;
1289 }
1290 }
1291 }
1292
1293 return 0;
1294err:
1295 /* Not alot we can do here, return ambiguous guard error */
1296 return 0x1;
1297}