blob: adec4d7438612fa871aee92dc7e133b1d2cb978e [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);
Sagi Grimberg8c18ed02015-08-06 18:32:55 +0300247 if (IS_ERR(ib_conn->fmr.pool)) {
248 ret = PTR_ERR(ib_conn->fmr.pool);
249 iser_err("FMR allocation failed, err %d\n", ret);
250 goto err;
251 }
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300252
Sagi Grimberg8c18ed02015-08-06 18:32:55 +0300253 return 0;
254
255err:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300256 kfree(ib_conn->fmr.page_vec);
257 ib_conn->fmr.page_vec = NULL;
Sagi Grimberg8c18ed02015-08-06 18:32:55 +0300258 return ret;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300259}
260
261/**
262 * iser_free_fmr_pool - releases the FMR pool and page vec
263 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300264void iser_free_fmr_pool(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300265{
266 iser_info("freeing conn %p fmr pool %p\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300267 ib_conn, ib_conn->fmr.pool);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300268
Sagi Grimberg8c18ed02015-08-06 18:32:55 +0300269 ib_destroy_fmr_pool(ib_conn->fmr.pool);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300270 ib_conn->fmr.pool = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300271
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300272 kfree(ib_conn->fmr.page_vec);
273 ib_conn->fmr.page_vec = NULL;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300274}
275
Sagi Grimberg310b3472014-03-05 19:43:41 +0200276static int
Sagi Grimbergd711d812015-08-06 18:32:53 +0300277iser_alloc_reg_res(struct ib_device *ib_device, struct ib_pd *pd,
278 struct iser_reg_resources *res)
279{
280 int ret;
281
282 res->frpl = ib_alloc_fast_reg_page_list(ib_device,
283 ISCSI_ISER_SG_TABLESIZE + 1);
284 if (IS_ERR(res->frpl)) {
285 ret = PTR_ERR(res->frpl);
286 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
287 ret);
288 return PTR_ERR(res->frpl);
289 }
290
291 res->mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
292 ISCSI_ISER_SG_TABLESIZE + 1);
293 if (IS_ERR(res->mr)) {
294 ret = PTR_ERR(res->mr);
295 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
296 goto fast_reg_mr_failure;
297 }
298 res->mr_valid = 1;
299
300 return 0;
301
302fast_reg_mr_failure:
303 ib_free_fast_reg_page_list(res->frpl);
304
305 return ret;
306}
307
308static void
309iser_free_reg_res(struct iser_reg_resources *rsc)
310{
311 ib_dereg_mr(rsc->mr);
312 ib_free_fast_reg_page_list(rsc->frpl);
313}
314
315static int
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300316iser_alloc_pi_ctx(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300317 struct iser_fr_desc *desc)
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300318{
319 struct iser_pi_context *pi_ctx = NULL;
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300320 int ret;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300321
322 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
323 if (!desc->pi_ctx)
324 return -ENOMEM;
325
326 pi_ctx = desc->pi_ctx;
327
Sagi Grimbergd711d812015-08-06 18:32:53 +0300328 ret = iser_alloc_reg_res(ib_device, pd, &pi_ctx->rsc);
329 if (ret) {
330 iser_err("failed to allocate reg_resources\n");
331 goto alloc_reg_res_err;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300332 }
333
Sagi Grimberg9bee1782015-07-30 10:32:35 +0300334 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300335 if (IS_ERR(pi_ctx->sig_mr)) {
336 ret = PTR_ERR(pi_ctx->sig_mr);
337 goto sig_mr_failure;
338 }
Sagi Grimbergd711d812015-08-06 18:32:53 +0300339 pi_ctx->sig_mr_valid = 1;
340 desc->pi_ctx->sig_protected = 0;
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300341
342 return 0;
343
344sig_mr_failure:
Sagi Grimbergd711d812015-08-06 18:32:53 +0300345 iser_free_reg_res(&pi_ctx->rsc);
346alloc_reg_res_err:
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300347 kfree(desc->pi_ctx);
348
349 return ret;
350}
351
352static void
353iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
354{
Sagi Grimbergd711d812015-08-06 18:32:53 +0300355 iser_free_reg_res(&pi_ctx->rsc);
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +0300356 ib_dereg_mr(pi_ctx->sig_mr);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300357 kfree(pi_ctx);
358}
359
360static int
Sagi Grimberg310b3472014-03-05 19:43:41 +0200361iser_create_fastreg_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300362 bool pi_enable, struct iser_fr_desc *desc)
Sagi Grimberg310b3472014-03-05 19:43:41 +0200363{
364 int ret;
365
Sagi Grimbergd711d812015-08-06 18:32:53 +0300366 ret = iser_alloc_reg_res(ib_device, pd, &desc->rsc);
367 if (ret) {
368 iser_err("failed to allocate reg_resources\n");
369 return ret;
Sagi Grimberg310b3472014-03-05 19:43:41 +0200370 }
371
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200372 if (pi_enable) {
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300373 ret = iser_alloc_pi_ctx(ib_device, pd, desc);
374 if (ret)
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200375 goto pi_ctx_alloc_failure;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200376 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200377
378 return 0;
Sagi Grimbergd711d812015-08-06 18:32:53 +0300379
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200380pi_ctx_alloc_failure:
Sagi Grimbergd711d812015-08-06 18:32:53 +0300381 iser_free_reg_res(&desc->rsc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200382
383 return ret;
384}
385
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300386/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200387 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300388 * for fast registration work requests.
389 * returns 0 on success, or errno code on failure
390 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300391int iser_create_fastreg_pool(struct ib_conn *ib_conn, unsigned cmds_max)
Sagi Grimberg55878562013-07-28 12:35:42 +0300392{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300393 struct iser_device *device = ib_conn->device;
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300394 struct iser_fr_desc *desc;
Sagi Grimberg55878562013-07-28 12:35:42 +0300395 int i, ret;
396
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300397 INIT_LIST_HEAD(&ib_conn->fastreg.pool);
398 ib_conn->fastreg.pool_size = 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300399 for (i = 0; i < cmds_max; i++) {
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200400 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
Sagi Grimberg55878562013-07-28 12:35:42 +0300401 if (!desc) {
402 iser_err("Failed to allocate a new fast_reg descriptor\n");
403 ret = -ENOMEM;
404 goto err;
405 }
406
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200407 ret = iser_create_fastreg_desc(device->ib_device, device->pd,
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300408 ib_conn->pi_support, desc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200409 if (ret) {
410 iser_err("Failed to create fastreg descriptor err=%d\n",
411 ret);
412 kfree(desc);
413 goto err;
Sagi Grimberg55878562013-07-28 12:35:42 +0300414 }
415
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300416 list_add_tail(&desc->list, &ib_conn->fastreg.pool);
417 ib_conn->fastreg.pool_size++;
Sagi Grimberg55878562013-07-28 12:35:42 +0300418 }
419
420 return 0;
Roi Dayan27ae2d12013-08-19 16:41:51 +0300421
Sagi Grimberg55878562013-07-28 12:35:42 +0300422err:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300423 iser_free_fastreg_pool(ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300424 return ret;
425}
426
427/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200428 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300429 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300430void iser_free_fastreg_pool(struct ib_conn *ib_conn)
Sagi Grimberg55878562013-07-28 12:35:42 +0300431{
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300432 struct iser_fr_desc *desc, *tmp;
Sagi Grimberg55878562013-07-28 12:35:42 +0300433 int i = 0;
434
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300435 if (list_empty(&ib_conn->fastreg.pool))
Sagi Grimberg55878562013-07-28 12:35:42 +0300436 return;
437
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300438 iser_info("freeing conn %p fr pool\n", ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300439
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300440 list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.pool, list) {
Sagi Grimberg55878562013-07-28 12:35:42 +0300441 list_del(&desc->list);
Sagi Grimbergd711d812015-08-06 18:32:53 +0300442 iser_free_reg_res(&desc->rsc);
Sagi Grimberg4dec2a22015-04-14 18:08:22 +0300443 if (desc->pi_ctx)
444 iser_free_pi_ctx(desc->pi_ctx);
Sagi Grimberg55878562013-07-28 12:35:42 +0300445 kfree(desc);
446 ++i;
447 }
448
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300449 if (i < ib_conn->fastreg.pool_size)
Sagi Grimberg55878562013-07-28 12:35:42 +0300450 iser_warn("pool still has %d regions registered\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300451 ib_conn->fastreg.pool_size - i);
Sagi Grimberg55878562013-07-28 12:35:42 +0300452}
453
454/**
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300455 * iser_create_ib_conn_res - Queue-Pair (QP)
456 *
457 * returns 0 on success, -1 on failure
458 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300459static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300460{
Minh Tranf4641ef2014-12-07 16:09:52 +0200461 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
462 ib_conn);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300463 struct iser_device *device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200464 struct ib_device_attr *dev_attr;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300465 struct ib_qp_init_attr init_attr;
466 int ret = -ENOMEM;
467 int index, min_index = 0;
468
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300469 BUG_ON(ib_conn->device == NULL);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300470
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300471 device = ib_conn->device;
Minh Tranf4641ef2014-12-07 16:09:52 +0200472 dev_attr = &device->dev_attr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300473
474 memset(&init_attr, 0, sizeof init_attr);
475
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000476 mutex_lock(&ig.connlist_mutex);
477 /* select the CQ with the minimal number of usages */
Sagi Grimbergbf175542014-10-01 14:02:07 +0300478 for (index = 0; index < device->comps_used; index++) {
479 if (device->comps[index].active_qps <
480 device->comps[min_index].active_qps)
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000481 min_index = index;
Sagi Grimbergbf175542014-10-01 14:02:07 +0300482 }
483 ib_conn->comp = &device->comps[min_index];
484 ib_conn->comp->active_qps++;
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000485 mutex_unlock(&ig.connlist_mutex);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300486 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000487
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300488 init_attr.event_handler = iser_qp_event_callback;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300489 init_attr.qp_context = (void *)ib_conn;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300490 init_attr.send_cq = ib_conn->comp->cq;
491 init_attr.recv_cq = ib_conn->comp->cq;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300492 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000493 init_attr.cap.max_send_sge = 2;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000494 init_attr.cap.max_recv_sge = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300495 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
496 init_attr.qp_type = IB_QPT_RC;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300497 if (ib_conn->pi_support) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300498 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200499 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Minh Tranf4641ef2014-12-07 16:09:52 +0200500 iser_conn->max_cmds =
501 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200502 } else {
Minh Tranf4641ef2014-12-07 16:09:52 +0200503 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
504 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
505 iser_conn->max_cmds =
506 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
507 } else {
508 init_attr.cap.max_send_wr = dev_attr->max_qp_wr;
509 iser_conn->max_cmds =
510 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr);
511 iser_dbg("device %s supports max_send_wr %d\n",
512 device->ib_device->name, dev_attr->max_qp_wr);
513 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200514 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300515
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300516 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300517 if (ret)
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300518 goto out_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300519
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300520 ib_conn->qp = ib_conn->cma_id->qp;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300521 iser_info("setting conn %p cma_id %p qp %p\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300522 ib_conn, ib_conn->cma_id,
523 ib_conn->cma_id->qp);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300524 return ret;
525
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300526out_err:
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200527 mutex_lock(&ig.connlist_mutex);
528 ib_conn->comp->active_qps--;
529 mutex_unlock(&ig.connlist_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300530 iser_err("unable to alloc mem or create resource, err %d\n", ret);
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200531
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300532 return ret;
533}
534
535/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300536 * based on the resolved device node GUID see if there already allocated
537 * device for this device. If there's no such, create one.
538 */
539static
540struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
541{
Arne Redlich9a378272008-03-04 14:07:22 +0200542 struct iser_device *device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300543
544 mutex_lock(&ig.device_list_mutex);
545
Arne Redlich9a378272008-03-04 14:07:22 +0200546 list_for_each_entry(device, &ig.device_list, ig_list)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300547 /* find if there's a match using the node GUID */
548 if (device->ib_device->node_guid == cma_id->device->node_guid)
Arne Redlichd33ed422008-03-04 14:11:54 +0200549 goto inc_refcnt;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300550
Arne Redlich9a378272008-03-04 14:07:22 +0200551 device = kzalloc(sizeof *device, GFP_KERNEL);
552 if (device == NULL)
553 goto out;
554
555 /* assign this device to the device */
556 device->ib_device = cma_id->device;
557 /* init the device and link it into ig device list */
558 if (iser_create_device_ib_res(device)) {
559 kfree(device);
560 device = NULL;
561 goto out;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300562 }
Arne Redlich9a378272008-03-04 14:07:22 +0200563 list_add(&device->ig_list, &ig.device_list);
564
Arne Redlichd33ed422008-03-04 14:11:54 +0200565inc_refcnt:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300566 device->refcount++;
Arne Redlichd33ed422008-03-04 14:11:54 +0200567out:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300568 mutex_unlock(&ig.device_list_mutex);
569 return device;
570}
571
572/* if there's no demand for this device, release it */
573static void iser_device_try_release(struct iser_device *device)
574{
575 mutex_lock(&ig.device_list_mutex);
576 device->refcount--;
Roi Dayan4f363882013-05-01 13:25:25 +0000577 iser_info("device %p refcount %d\n", device, device->refcount);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300578 if (!device->refcount) {
579 iser_free_device_ib_res(device);
580 list_del(&device->ig_list);
581 kfree(device);
582 }
583 mutex_unlock(&ig.device_list_mutex);
584}
585
Ariel Nahum504130c2014-07-31 13:27:49 +0300586/**
587 * Called with state mutex held
588 **/
Sagi Grimberg5716af62014-10-01 14:01:57 +0300589static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
590 enum iser_conn_state comp,
591 enum iser_conn_state exch)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300592{
593 int ret;
594
Sagi Grimberg5716af62014-10-01 14:01:57 +0300595 ret = (iser_conn->state == comp);
596 if (ret)
597 iser_conn->state = exch;
598
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300599 return ret;
600}
601
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300602void iser_release_work(struct work_struct *work)
603{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300604 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300605
Sagi Grimberg5716af62014-10-01 14:01:57 +0300606 iser_conn = container_of(work, struct iser_conn, release_work);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300607
Sagi Grimbergc107a6c2014-10-01 14:02:02 +0300608 /* Wait for conn_stop to complete */
609 wait_for_completion(&iser_conn->stop_completion);
610 /* Wait for IB resouces cleanup to complete */
611 wait_for_completion(&iser_conn->ib_completion);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300612
Sagi Grimberg5716af62014-10-01 14:01:57 +0300613 mutex_lock(&iser_conn->state_mutex);
614 iser_conn->state = ISER_CONN_DOWN;
615 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300616
Sagi Grimberg5716af62014-10-01 14:01:57 +0300617 iser_conn_release(iser_conn);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300618}
619
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300620/**
Sagi Grimberg96f15192014-10-01 14:02:00 +0300621 * iser_free_ib_conn_res - release IB related resources
622 * @iser_conn: iser connection struct
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200623 * @destroy: indicator if we need to try to release the
624 * iser device and memory regoins pool (only iscsi
625 * shutdown and DEVICE_REMOVAL will use this).
Sagi Grimberg96f15192014-10-01 14:02:00 +0300626 *
627 * This routine is called with the iser state mutex held
628 * so the cm_id removal is out of here. It is Safe to
629 * be invoked multiple times.
630 */
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300631static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200632 bool destroy)
Sagi Grimberg96f15192014-10-01 14:02:00 +0300633{
634 struct ib_conn *ib_conn = &iser_conn->ib_conn;
635 struct iser_device *device = ib_conn->device;
636
637 iser_info("freeing conn %p cma_id %p qp %p\n",
638 iser_conn, ib_conn->cma_id, ib_conn->qp);
639
Sagi Grimberg96f15192014-10-01 14:02:00 +0300640 if (ib_conn->qp != NULL) {
Sagi Grimbergbf175542014-10-01 14:02:07 +0300641 ib_conn->comp->active_qps--;
Sagi Grimberg96f15192014-10-01 14:02:00 +0300642 rdma_destroy_qp(ib_conn->cma_id);
643 ib_conn->qp = NULL;
644 }
645
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200646 if (destroy) {
647 if (iser_conn->rx_descs)
648 iser_free_rx_descriptors(iser_conn);
649
650 if (device != NULL) {
651 iser_device_try_release(device);
652 ib_conn->device = NULL;
653 }
Sagi Grimberg96f15192014-10-01 14:02:00 +0300654 }
655}
656
657/**
Roland Dreier41179e22007-07-17 18:37:42 -0700658 * Frees all conn objects and deallocs conn descriptor
659 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300660void iser_conn_release(struct iser_conn *iser_conn)
Roland Dreier41179e22007-07-17 18:37:42 -0700661{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300662 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Roland Dreier41179e22007-07-17 18:37:42 -0700663
Roland Dreier41179e22007-07-17 18:37:42 -0700664 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300665 list_del(&iser_conn->conn_list);
Roland Dreier41179e22007-07-17 18:37:42 -0700666 mutex_unlock(&ig.connlist_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300667
Sagi Grimberg5716af62014-10-01 14:01:57 +0300668 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200669 /* In case we endup here without ep_disconnect being invoked. */
Ariel Nahum5426b172014-12-07 16:09:54 +0200670 if (iser_conn->state != ISER_CONN_DOWN) {
Ariel Nahumaea8f4d2014-10-01 14:02:06 +0300671 iser_warn("iser conn %p state %d, expected state down.\n",
672 iser_conn, iser_conn->state);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200673 iscsi_destroy_endpoint(iser_conn->ep);
Ariel Nahum5426b172014-12-07 16:09:54 +0200674 iser_conn->state = ISER_CONN_DOWN;
675 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300676 /*
677 * In case we never got to bind stage, we still need to
678 * release IB resources (which is safe to call more than once).
679 */
680 iser_free_ib_conn_res(iser_conn, true);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300681 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300682
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300683 if (ib_conn->cma_id != NULL) {
684 rdma_destroy_id(ib_conn->cma_id);
685 ib_conn->cma_id = NULL;
Roi Dayan5b61ff42013-05-08 12:21:17 +0000686 }
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300687
Sagi Grimberg5716af62014-10-01 14:01:57 +0300688 kfree(iser_conn);
Roland Dreier41179e22007-07-17 18:37:42 -0700689}
690
691/**
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300692 * triggers start of the disconnect procedures and wait for them to be done
693 * Called with state mutex held
694 */
695int iser_conn_terminate(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300696{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300697 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300698 struct ib_send_wr *bad_wr;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300699 int err = 0;
700
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300701 /* terminate the iser conn only if the conn state is UP */
702 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
703 ISER_CONN_TERMINATING))
704 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300705
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300706 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
707
708 /* suspend queuing of new iscsi commands */
709 if (iser_conn->iscsi_conn)
710 iscsi_suspend_queue(iser_conn->iscsi_conn);
711
712 /*
713 * In case we didn't already clean up the cma_id (peer initiated
714 * a disconnection), we need to Cause the CMA to change the QP
715 * state to ERROR.
716 */
717 if (ib_conn->cma_id) {
718 err = rdma_disconnect(ib_conn->cma_id);
719 if (err)
720 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
721 iser_conn, err);
722
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300723 /* post an indication that all flush errors were consumed */
724 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200725 if (err) {
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300726 iser_err("conn %p failed to post beacon", ib_conn);
Sagi Grimberg16df2a22014-12-07 16:09:53 +0200727 return 1;
728 }
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300729
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300730 wait_for_completion(&ib_conn->flush_comp);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300731 }
732
733 return 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300734}
735
Ariel Nahum504130c2014-07-31 13:27:49 +0300736/**
737 * Called with state mutex held
738 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300739static void iser_connect_error(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300740{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300741 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300742
Sagi Grimberg5716af62014-10-01 14:01:57 +0300743 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimbergc4de4662015-04-14 18:08:11 +0300744 iser_conn->state = ISER_CONN_TERMINATING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300745}
746
Ariel Nahum504130c2014-07-31 13:27:49 +0300747/**
748 * Called with state mutex held
749 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300750static void iser_addr_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300751{
752 struct iser_device *device;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300753 struct iser_conn *iser_conn;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300754 struct ib_conn *ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300755 int ret;
756
Sagi Grimberg5716af62014-10-01 14:01:57 +0300757 iser_conn = (struct iser_conn *)cma_id->context;
758 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300759 /* bailout */
760 return;
761
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300762 ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300763 device = iser_device_find_by_ib_device(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200764 if (!device) {
765 iser_err("device lookup/creation failed\n");
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300766 iser_connect_error(cma_id);
767 return;
Arne Redlichd33ed422008-03-04 14:11:54 +0200768 }
769
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300770 ib_conn->device = device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300771
Alex Tabachnik7f733842014-03-05 19:43:46 +0200772 /* connection T10-PI support */
773 if (iser_pi_enable) {
774 if (!(device->dev_attr.device_cap_flags &
775 IB_DEVICE_SIGNATURE_HANDOVER)) {
776 iser_warn("T10-PI requested but not supported on %s, "
777 "continue without T10-PI\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300778 ib_conn->device->ib_device->name);
779 ib_conn->pi_support = false;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200780 } else {
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300781 ib_conn->pi_support = true;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200782 }
783 }
784
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300785 ret = rdma_resolve_route(cma_id, 1000);
786 if (ret) {
787 iser_err("resolve route failed: %d\n", ret);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300788 iser_connect_error(cma_id);
789 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300790 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300791}
792
Ariel Nahum504130c2014-07-31 13:27:49 +0300793/**
794 * Called with state mutex held
795 **/
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300796static void iser_route_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300797{
798 struct rdma_conn_param conn_param;
799 int ret;
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000800 struct iser_cm_hdr req_hdr;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300801 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300802 struct ib_conn *ib_conn = &iser_conn->ib_conn;
803 struct iser_device *device = ib_conn->device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300804
Sagi Grimberg5716af62014-10-01 14:01:57 +0300805 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300806 /* bailout */
807 return;
808
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300809 ret = iser_create_ib_conn_res(ib_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300810 if (ret)
811 goto failure;
812
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300813 memset(&conn_param, 0, sizeof conn_param);
Sagi Grimberg2ea32932014-07-31 13:27:46 +0300814 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300815 conn_param.initiator_depth = 1;
816 conn_param.retry_count = 7;
817 conn_param.rnr_retry_count = 6;
818
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000819 memset(&req_hdr, 0, sizeof(req_hdr));
820 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
821 ISER_SEND_W_INV_NOT_SUPPORTED);
822 conn_param.private_data = (void *)&req_hdr;
823 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
824
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300825 ret = rdma_connect(cma_id, &conn_param);
826 if (ret) {
827 iser_err("failure connecting: %d\n", ret);
828 goto failure;
829 }
830
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300831 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300832failure:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300833 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300834}
835
836static void iser_connected_handler(struct rdma_cm_id *cma_id)
837{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300838 struct iser_conn *iser_conn;
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300839 struct ib_qp_attr attr;
840 struct ib_qp_init_attr init_attr;
841
Sagi Grimberg5716af62014-10-01 14:01:57 +0300842 iser_conn = (struct iser_conn *)cma_id->context;
843 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300844 /* bailout */
845 return;
846
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300847 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
848 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 +0300849
Sagi Grimberg5716af62014-10-01 14:01:57 +0300850 iser_conn->state = ISER_CONN_UP;
851 complete(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300852}
853
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300854static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300855{
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300856 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300857
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300858 if (iser_conn_terminate(iser_conn)) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300859 if (iser_conn->iscsi_conn)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300860 iscsi_conn_failure(iser_conn->iscsi_conn,
861 ISCSI_ERR_CONN_FAILED);
Roi Dayan7d9eacf2014-02-04 16:54:54 +0200862 else
863 iser_err("iscsi_iser connection isn't bound\n");
864 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300865}
866
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300867static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200868 bool destroy)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300869{
870 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
871
872 /*
873 * We are not guaranteed that we visited disconnected_handler
874 * by now, call it here to be safe that we handle CM drep
875 * and flush errors.
876 */
877 iser_disconnected_handler(cma_id);
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200878 iser_free_ib_conn_res(iser_conn, destroy);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300879 complete(&iser_conn->ib_completion);
880};
881
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300882static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
883{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300884 struct iser_conn *iser_conn;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300885 int ret = 0;
Ariel Nahum504130c2014-07-31 13:27:49 +0300886
Sagi Grimberg5716af62014-10-01 14:01:57 +0300887 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300888 iser_info("%s (%d): status %d conn %p id %p\n",
889 rdma_event_msg(event->event), event->event,
890 event->status, cma_id->context, cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300891
Sagi Grimberg5716af62014-10-01 14:01:57 +0300892 mutex_lock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300893 switch (event->event) {
894 case RDMA_CM_EVENT_ADDR_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300895 iser_addr_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300896 break;
897 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300898 iser_route_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300899 break;
900 case RDMA_CM_EVENT_ESTABLISHED:
901 iser_connected_handler(cma_id);
902 break;
903 case RDMA_CM_EVENT_ADDR_ERROR:
904 case RDMA_CM_EVENT_ROUTE_ERROR:
905 case RDMA_CM_EVENT_CONNECT_ERROR:
906 case RDMA_CM_EVENT_UNREACHABLE:
907 case RDMA_CM_EVENT_REJECTED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300908 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300909 break;
910 case RDMA_CM_EVENT_DISCONNECTED:
Or Gerlitz2f5de152008-07-22 14:16:21 -0700911 case RDMA_CM_EVENT_ADDR_CHANGE:
Ariel Nahum5426b172014-12-07 16:09:54 +0200912 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
913 iser_cleanup_handler(cma_id, false);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300914 break;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300915 case RDMA_CM_EVENT_DEVICE_REMOVAL:
916 /*
917 * we *must* destroy the device as we cannot rely
918 * on iscsid to be around to initiate error handling.
Ariel Nahum5426b172014-12-07 16:09:54 +0200919 * also if we are not in state DOWN implicitly destroy
920 * the cma_id.
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300921 */
922 iser_cleanup_handler(cma_id, true);
Ariel Nahum5426b172014-12-07 16:09:54 +0200923 if (iser_conn->state != ISER_CONN_DOWN) {
924 iser_conn->ib_conn.cma_id = NULL;
925 ret = 1;
926 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300927 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300928 default:
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300929 iser_err("Unexpected RDMA CM event: %s (%d)\n",
930 rdma_event_msg(event->event), event->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300931 break;
932 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300933 mutex_unlock(&iser_conn->state_mutex);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300934
935 return ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300936}
937
Sagi Grimberg5716af62014-10-01 14:01:57 +0300938void iser_conn_init(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300939{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300940 iser_conn->state = ISER_CONN_INIT;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300941 iser_conn->ib_conn.post_recv_buf_count = 0;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +0300942 init_completion(&iser_conn->ib_conn.flush_comp);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300943 init_completion(&iser_conn->stop_completion);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300944 init_completion(&iser_conn->ib_completion);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300945 init_completion(&iser_conn->up_completion);
946 INIT_LIST_HEAD(&iser_conn->conn_list);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300947 spin_lock_init(&iser_conn->ib_conn.lock);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300948 mutex_init(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300949}
950
951 /**
952 * starts the process of connecting to the target
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200953 * sleeps until the connection is established or rejected
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300954 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300955int iser_connect(struct iser_conn *iser_conn,
Roi Dayan96ed02d2014-07-31 13:27:44 +0300956 struct sockaddr *src_addr,
957 struct sockaddr *dst_addr,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300958 int non_blocking)
959{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300960 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300961 int err = 0;
962
Sagi Grimberg5716af62014-10-01 14:01:57 +0300963 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300964
Sagi Grimberg5716af62014-10-01 14:01:57 +0300965 sprintf(iser_conn->name, "%pISp", dst_addr);
Roi Dayan96ed02d2014-07-31 13:27:44 +0300966
Sagi Grimberg5716af62014-10-01 14:01:57 +0300967 iser_info("connecting to: %s\n", iser_conn->name);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300968
969 /* the device is known only --after-- address resolution */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300970 ib_conn->device = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300971
Sagi Grimberg5716af62014-10-01 14:01:57 +0300972 iser_conn->state = ISER_CONN_PENDING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300973
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300974 ib_conn->beacon.wr_id = ISER_BEACON_WRID;
975 ib_conn->beacon.opcode = IB_WR_SEND;
976
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300977 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
978 (void *)iser_conn,
979 RDMA_PS_TCP, IB_QPT_RC);
980 if (IS_ERR(ib_conn->cma_id)) {
981 err = PTR_ERR(ib_conn->cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300982 iser_err("rdma_create_id failed: %d\n", err);
983 goto id_failure;
984 }
985
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300986 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300987 if (err) {
988 iser_err("rdma_resolve_addr failed: %d\n", err);
989 goto addr_failure;
990 }
991
992 if (!non_blocking) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300993 wait_for_completion_interruptible(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300994
Sagi Grimberg5716af62014-10-01 14:01:57 +0300995 if (iser_conn->state != ISER_CONN_UP) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300996 err = -EIO;
997 goto connect_failure;
998 }
999 }
Sagi Grimberg5716af62014-10-01 14:01:57 +03001000 mutex_unlock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001001
1002 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +03001003 list_add(&iser_conn->conn_list, &ig.connlist);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001004 mutex_unlock(&ig.connlist_mutex);
1005 return 0;
1006
1007id_failure:
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001008 ib_conn->cma_id = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001009addr_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001010 iser_conn->state = ISER_CONN_DOWN;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001011connect_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +03001012 mutex_unlock(&iser_conn->state_mutex);
1013 iser_conn_release(iser_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001014 return err;
1015}
1016
Sagi Grimberg5716af62014-10-01 14:01:57 +03001017int iser_post_recvl(struct iser_conn *iser_conn)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001018{
1019 struct ib_recv_wr rx_wr, *rx_wr_failed;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001020 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001021 struct ib_sge sge;
1022 int ib_ret;
1023
Sagi Grimberg5716af62014-10-01 14:01:57 +03001024 sge.addr = iser_conn->login_resp_dma;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001025 sge.length = ISER_RX_LOGIN_SIZE;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001026 sge.lkey = ib_conn->device->mr->lkey;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001027
Sagi Grimberg49df2782014-12-07 16:09:57 +02001028 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001029 rx_wr.sg_list = &sge;
1030 rx_wr.num_sge = 1;
1031 rx_wr.next = NULL;
1032
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001033 ib_conn->post_recv_buf_count++;
1034 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001035 if (ib_ret) {
1036 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001037 ib_conn->post_recv_buf_count--;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001038 }
1039 return ib_ret;
1040}
1041
Sagi Grimberg5716af62014-10-01 14:01:57 +03001042int iser_post_recvm(struct iser_conn *iser_conn, int count)
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001043{
1044 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1045 int i, ib_ret;
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001046 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001047 unsigned int my_rx_head = iser_conn->rx_desc_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001048 struct iser_rx_desc *rx_desc;
1049
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001050 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
Sagi Grimberg5716af62014-10-01 14:01:57 +03001051 rx_desc = &iser_conn->rx_descs[my_rx_head];
Sagi Grimberg49df2782014-12-07 16:09:57 +02001052 rx_wr->wr_id = (uintptr_t)rx_desc;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001053 rx_wr->sg_list = &rx_desc->rx_sg;
1054 rx_wr->num_sge = 1;
1055 rx_wr->next = rx_wr + 1;
Sagi Grimberg5716af62014-10-01 14:01:57 +03001056 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001057 }
1058
1059 rx_wr--;
1060 rx_wr->next = NULL; /* mark end of work requests list */
1061
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001062 ib_conn->post_recv_buf_count += count;
1063 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001064 if (ib_ret) {
1065 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001066 ib_conn->post_recv_buf_count -= count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001067 } else
Sagi Grimberg5716af62014-10-01 14:01:57 +03001068 iser_conn->rx_desc_head = my_rx_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +00001069 return ib_ret;
1070}
1071
1072
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001073/**
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001074 * iser_start_send - Initiate a Send DTO operation
1075 *
1076 * returns 0 on success, -1 on failure
1077 */
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001078int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
1079 bool signal)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001080{
Or Gerlitzf19624a2010-02-08 13:19:56 +00001081 int ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001082 struct ib_send_wr send_wr, *send_wr_failed;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001083
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001084 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
Sagi Grimberg5716af62014-10-01 14:01:57 +03001085 tx_desc->dma_addr, ISER_HEADERS_LEN,
1086 DMA_TO_DEVICE);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001087
1088 send_wr.next = NULL;
Sagi Grimberg49df2782014-12-07 16:09:57 +02001089 send_wr.wr_id = (uintptr_t)tx_desc;
Or Gerlitzf19624a2010-02-08 13:19:56 +00001090 send_wr.sg_list = tx_desc->tx_sg;
1091 send_wr.num_sge = tx_desc->num_sge;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001092 send_wr.opcode = IB_WR_SEND;
Sagi Grimberg6df5a122014-10-01 14:02:12 +03001093 send_wr.send_flags = signal ? IB_SEND_SIGNALED : 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001094
Sagi Grimberga4ee3532014-10-01 14:01:58 +03001095 ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001096 if (ib_ret)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001097 iser_err("ib_post_send failed, ret:%d\n", ib_ret);
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001098
Or Gerlitzf19624a2010-02-08 13:19:56 +00001099 return ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001100}
1101
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001102/**
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001103 * is_iser_tx_desc - Indicate if the completion wr_id
1104 * is a TX descriptor or not.
1105 * @iser_conn: iser connection
1106 * @wr_id: completion WR identifier
1107 *
1108 * Since we cannot rely on wc opcode in FLUSH errors
1109 * we must work around it by checking if the wr_id address
1110 * falls in the iser connection rx_descs buffer. If so
1111 * it is an RX descriptor, otherwize it is a TX.
1112 */
1113static inline bool
1114is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id)
1115{
1116 void *start = iser_conn->rx_descs;
1117 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs);
1118
1119 if (wr_id >= start && wr_id < start + len)
1120 return false;
1121
1122 return true;
1123}
1124
1125/**
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001126 * iser_handle_comp_error() - Handle error completion
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001127 * @ib_conn: connection RDMA resources
1128 * @wc: work completion
1129 *
1130 * Notes: We may handle a FLUSH error completion and in this case
1131 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1132 * error completion we should also notify iscsi layer that
1133 * connection is failed (in case we passed bind stage).
1134 */
1135static void
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001136iser_handle_comp_error(struct ib_conn *ib_conn,
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001137 struct ib_wc *wc)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001138{
Sagi Grimberg49df2782014-12-07 16:09:57 +02001139 void *wr_id = (void *)(uintptr_t)wc->wr_id;
Sagi Grimberg8c204e62014-10-01 14:02:05 +03001140 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
1141 ib_conn);
1142
1143 if (wc->status != IB_WC_WR_FLUSH_ERR)
1144 if (iser_conn->iscsi_conn)
1145 iscsi_conn_failure(iser_conn->iscsi_conn,
1146 ISCSI_ERR_CONN_FAILED);
1147
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001148 if (wc->wr_id == ISER_FASTREG_LI_WRID)
1149 return;
1150
Sagi Grimberg49df2782014-12-07 16:09:57 +02001151 if (is_iser_tx_desc(iser_conn, wr_id)) {
1152 struct iser_tx_desc *desc = wr_id;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001153
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001154 if (desc->type == ISCSI_TX_DATAOUT)
1155 kmem_cache_free(ig.desc_cache, desc);
1156 } else {
1157 ib_conn->post_recv_buf_count--;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001158 }
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001159}
1160
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001161/**
1162 * iser_handle_wc - handle a single work completion
1163 * @wc: work completion
1164 *
1165 * Soft-IRQ context, work completion can be either
1166 * SEND or RECV, and can turn out successful or
1167 * with error (or flush error).
1168 */
1169static void iser_handle_wc(struct ib_wc *wc)
1170{
1171 struct ib_conn *ib_conn;
1172 struct iser_tx_desc *tx_desc;
1173 struct iser_rx_desc *rx_desc;
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001174
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001175 ib_conn = wc->qp->qp_context;
Sagi Grimberg06c7fb62014-12-07 16:10:05 +02001176 if (likely(wc->status == IB_WC_SUCCESS)) {
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001177 if (wc->opcode == IB_WC_RECV) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001178 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001179 iser_rcv_completion(rx_desc, wc->byte_len,
1180 ib_conn);
1181 } else
1182 if (wc->opcode == IB_WC_SEND) {
Sagi Grimberg49df2782014-12-07 16:09:57 +02001183 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001184 iser_snd_completion(tx_desc, ib_conn);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001185 } else {
1186 iser_err("Unknown wc opcode %d\n", wc->opcode);
1187 }
1188 } else {
1189 if (wc->status != IB_WC_WR_FLUSH_ERR)
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001190 iser_err("%s (%d): wr id %llx vend_err %x\n",
1191 ib_wc_status_msg(wc->status), wc->status,
1192 wc->wr_id, wc->vendor_err);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001193 else
Sagi Grimberg871e00a2015-05-18 13:40:30 +03001194 iser_dbg("%s (%d): wr id %llx\n",
1195 ib_wc_status_msg(wc->status), wc->status,
1196 wc->wr_id);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001197
Sagi Grimbergff3dd522014-10-01 14:02:10 +03001198 if (wc->wr_id == ISER_BEACON_WRID)
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001199 /* all flush errors were consumed */
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001200 complete(&ib_conn->flush_comp);
Sagi Grimberg30bf1d52015-04-14 18:08:12 +03001201 else
1202 iser_handle_comp_error(ib_conn, wc);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001203 }
1204}
1205
1206/**
1207 * iser_cq_tasklet_fn - iSER completion polling loop
1208 * @data: iSER completion context
1209 *
1210 * Soft-IRQ context, polling connection CQ until
1211 * either CQ was empty or we exausted polling budget
1212 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001213static void iser_cq_tasklet_fn(unsigned long data)
1214{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001215 struct iser_comp *comp = (struct iser_comp *)data;
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001216 struct ib_cq *cq = comp->cq;
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001217 struct ib_wc *const wcs = comp->wcs;
1218 int i, n, completed = 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001219
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001220 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
1221 for (i = 0; i < n; i++)
1222 iser_handle_wc(&wcs[i]);
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001223
Sagi Grimberg6e6fe2f2014-10-01 14:02:11 +03001224 completed += n;
1225 if (completed >= iser_cq_poll_limit)
Sagi Grimberg183cfa42014-10-01 14:02:08 +03001226 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001227 }
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001228
1229 /*
1230 * It is assumed here that arming CQ only once its empty
1231 * would not cause interrupts to be missed.
1232 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001233 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
Or Gerlitz78ad0a32010-02-08 13:19:21 +00001234
Sagi Grimberg6aabfa72014-10-01 14:02:09 +03001235 iser_dbg("got %d completions\n", completed);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001236}
1237
1238static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
1239{
Sagi Grimbergbf175542014-10-01 14:02:07 +03001240 struct iser_comp *comp = cq_context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001241
Sagi Grimbergbf175542014-10-01 14:02:07 +03001242 tasklet_schedule(&comp->tasklet);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001243}
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001244
1245u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
1246 enum iser_data_dir cmd_dir, sector_t *sector)
1247{
Sagi Grimbergb130ede2015-04-14 18:08:19 +03001248 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimberg5190cc22015-08-06 18:32:54 +03001249 struct iser_fr_desc *desc = reg->mem_h;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001250 unsigned long sector_size = iser_task->sc->device->sector_size;
1251 struct ib_mr_status mr_status;
1252 int ret;
1253
Sagi Grimbergd711d812015-08-06 18:32:53 +03001254 if (desc && desc->pi_ctx->sig_protected) {
1255 desc->pi_ctx->sig_protected = 0;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001256 ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
1257 IB_MR_CHECK_SIG_STATUS, &mr_status);
1258 if (ret) {
1259 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1260 goto err;
1261 }
1262
1263 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1264 sector_t sector_off = mr_status.sig_err.sig_err_offset;
1265
1266 do_div(sector_off, sector_size + 8);
1267 *sector = scsi_get_lba(iser_task->sc) + sector_off;
1268
Randy Dunlap39c978c2014-03-28 14:03:40 -07001269 pr_err("PI error found type %d at sector %llx "
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001270 "expected %x vs actual %x\n",
Randy Dunlap39c978c2014-03-28 14:03:40 -07001271 mr_status.sig_err.err_type,
1272 (unsigned long long)*sector,
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +02001273 mr_status.sig_err.expected,
1274 mr_status.sig_err.actual);
1275
1276 switch (mr_status.sig_err.err_type) {
1277 case IB_SIG_BAD_GUARD:
1278 return 0x1;
1279 case IB_SIG_BAD_REFTAG:
1280 return 0x3;
1281 case IB_SIG_BAD_APPTAG:
1282 return 0x2;
1283 }
1284 }
1285 }
1286
1287 return 0;
1288err:
1289 /* Not alot we can do here, return ambiguous guard error */
1290 return 0x1;
1291}