IB/iser: Pass registration pool a size parameter
[linux-2.6-block.git] / drivers / infiniband / ulp / iser / iser_verbs.c
CommitLineData
1cfa0a75
OG
1/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
3ee07d27 4 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
1cfa0a75
OG
5 *
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.
1cfa0a75 33 */
1cfa0a75
OG
34#include <linux/kernel.h>
35#include <linux/module.h>
5a0e3ad6 36#include <linux/slab.h>
1cfa0a75 37#include <linux/delay.h>
1cfa0a75
OG
38
39#include "iscsi_iser.h"
40
41#define ISCSI_ISER_MAX_CONN 8
6aabfa76
SG
42#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)
ff3dd52d
SG
44#define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
45 ISCSI_ISER_MAX_CONN)
1cfa0a75 46
183cfa43
SG
47static int iser_cq_poll_limit = 512;
48
1cfa0a75
OG
49static void iser_cq_tasklet_fn(unsigned long data);
50static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
1cfa0a75
OG
51
52static void iser_cq_event_callback(struct ib_event *cause, void *context)
53{
871e00af
SG
54 iser_err("cq event %s (%d)\n",
55 ib_event_msg(cause->event), cause->event);
1cfa0a75
OG
56}
57
58static void iser_qp_event_callback(struct ib_event *cause, void *context)
59{
871e00af
SG
60 iser_err("qp event %s (%d)\n",
61 ib_event_msg(cause->event), cause->event);
1cfa0a75
OG
62}
63
2110f9bf
OG
64static void iser_event_handler(struct ib_event_handler *handler,
65 struct ib_event *event)
66{
871e00af
SG
67 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);
2110f9bf
OG
70}
71
1cfa0a75
OG
72/**
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{
65198d6b 81 struct ib_device_attr *dev_attr = &device->dev_attr;
f4641ef7 82 int ret, i, max_cqe;
5a33a669 83
65198d6b
SG
84 ret = ib_query_device(device->ib_device, dev_attr);
85 if (ret) {
5587856c 86 pr_warn("Query device failed for %s\n", device->ib_device->name);
65198d6b 87 return ret;
5587856c
SG
88 }
89
48afbff6
SG
90 ret = iser_assign_reg_ops(device);
91 if (ret)
92 return ret;
b4e155ff 93
da64bdb2 94 device->comps_used = min_t(int, num_online_cpus(),
bf175540 95 device->ib_device->num_comp_vectors);
f4641ef7 96
da64bdb2
SG
97 device->comps = kcalloc(device->comps_used, sizeof(*device->comps),
98 GFP_KERNEL);
99 if (!device->comps)
100 goto comps_err;
101
f4641ef7
MT
102 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
103
104 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n",
bf175540 105 device->comps_used, device->ib_device->name,
f4641ef7 106 device->ib_device->num_comp_vectors, max_cqe);
5a33a669 107
1cfa0a75
OG
108 device->pd = ib_alloc_pd(device->ib_device);
109 if (IS_ERR(device->pd))
110 goto pd_err;
111
bf175540 112 for (i = 0; i < device->comps_used; i++) {
8e37210b 113 struct ib_cq_init_attr cq_attr = {};
bf175540
SG
114 struct iser_comp *comp = &device->comps[i];
115
116 comp->device = device;
8e37210b
MB
117 cq_attr.cqe = max_cqe;
118 cq_attr.comp_vector = i;
6aabfa76
SG
119 comp->cq = ib_create_cq(device->ib_device,
120 iser_cq_callback,
121 iser_cq_event_callback,
122 (void *)comp,
8e37210b 123 &cq_attr);
6aabfa76
SG
124 if (IS_ERR(comp->cq)) {
125 comp->cq = NULL;
5a33a669 126 goto cq_err;
c33b15f0 127 }
1cfa0a75 128
6aabfa76 129 if (ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP))
5a33a669 130 goto cq_err;
1cfa0a75 131
bf175540
SG
132 tasklet_init(&comp->tasklet, iser_cq_tasklet_fn,
133 (unsigned long)comp);
5a33a669 134 }
1cfa0a75 135
d8111028
EZ
136 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
137 IB_ACCESS_REMOTE_WRITE |
138 IB_ACCESS_REMOTE_READ);
1cfa0a75
OG
139 if (IS_ERR(device->mr))
140 goto dma_mr_err;
141
2110f9bf
OG
142 INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
143 iser_event_handler);
144 if (ib_register_event_handler(&device->event_handler))
145 goto handler_err;
146
1cfa0a75
OG
147 return 0;
148
2110f9bf
OG
149handler_err:
150 ib_dereg_mr(device->mr);
1cfa0a75 151dma_mr_err:
bf175540
SG
152 for (i = 0; i < device->comps_used; i++)
153 tasklet_kill(&device->comps[i].tasklet);
5a33a669 154cq_err:
bf175540
SG
155 for (i = 0; i < device->comps_used; i++) {
156 struct iser_comp *comp = &device->comps[i];
157
6aabfa76
SG
158 if (comp->cq)
159 ib_destroy_cq(comp->cq);
5a33a669 160 }
1cfa0a75
OG
161 ib_dealloc_pd(device->pd);
162pd_err:
da64bdb2
SG
163 kfree(device->comps);
164comps_err:
1cfa0a75
OG
165 iser_err("failed to allocate an IB resource\n");
166 return -1;
167}
168
169/**
38dc732f 170 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
1cfa0a75
OG
171 * CQ and PD created with the device associated with the adapator.
172 */
173static void iser_free_device_ib_res(struct iser_device *device)
174{
5a33a669 175 int i;
1cfa0a75
OG
176 BUG_ON(device->mr == NULL);
177
bf175540
SG
178 for (i = 0; i < device->comps_used; i++) {
179 struct iser_comp *comp = &device->comps[i];
180
181 tasklet_kill(&comp->tasklet);
6aabfa76
SG
182 ib_destroy_cq(comp->cq);
183 comp->cq = NULL;
5a33a669
AT
184 }
185
2110f9bf 186 (void)ib_unregister_event_handler(&device->event_handler);
1cfa0a75 187 (void)ib_dereg_mr(device->mr);
1cfa0a75
OG
188 (void)ib_dealloc_pd(device->pd);
189
da64bdb2
SG
190 kfree(device->comps);
191 device->comps = NULL;
192
1cfa0a75 193 device->mr = NULL;
1cfa0a75
OG
194 device->pd = NULL;
195}
196
197/**
48afbff6 198 * iser_alloc_fmr_pool - Creates FMR pool and page_vector
1cfa0a75 199 *
986db0d6 200 * returns 0 on success, or errno code on failure
1cfa0a75 201 */
f8db651d
SG
202int iser_alloc_fmr_pool(struct ib_conn *ib_conn,
203 unsigned cmds_max,
204 unsigned int size)
1cfa0a75 205{
a4ee3539 206 struct iser_device *device = ib_conn->device;
385ad87d
SG
207 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
208 struct iser_page_vec *page_vec;
2b3bf958 209 struct iser_fr_desc *desc;
385ad87d 210 struct ib_fmr_pool *fmr_pool;
1cfa0a75 211 struct ib_fmr_pool_param params;
2b3bf958 212 int ret;
bcc60c38 213
2b3bf958 214 INIT_LIST_HEAD(&fr_pool->list);
385ad87d
SG
215 spin_lock_init(&fr_pool->lock);
216
2b3bf958
AL
217 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
218 if (!desc)
219 return -ENOMEM;
220
f8db651d 221 page_vec = kmalloc(sizeof(*page_vec) + (sizeof(u64) * size),
385ad87d 222 GFP_KERNEL);
2b3bf958
AL
223 if (!page_vec) {
224 ret = -ENOMEM;
225 goto err_frpl;
226 }
9fda1ac5 227
385ad87d 228 page_vec->pages = (u64 *)(page_vec + 1);
1cfa0a75 229
8dfa0876 230 params.page_shift = SHIFT_4K;
f8db651d 231 params.max_pages_per_fmr = size;
1cfa0a75
OG
232 /* make the pool size twice the max number of SCSI commands *
233 * the ML is expected to queue, watermark for unmap at 50% */
b7f04513
SP
234 params.pool_size = cmds_max * 2;
235 params.dirty_watermark = cmds_max;
1cfa0a75
OG
236 params.cache = 0;
237 params.flush_function = NULL;
238 params.access = (IB_ACCESS_LOCAL_WRITE |
239 IB_ACCESS_REMOTE_WRITE |
240 IB_ACCESS_REMOTE_READ);
241
385ad87d
SG
242 fmr_pool = ib_create_fmr_pool(device->pd, &params);
243 if (IS_ERR(fmr_pool)) {
244 ret = PTR_ERR(fmr_pool);
8c18ed03 245 iser_err("FMR allocation failed, err %d\n", ret);
2b3bf958 246 goto err_fmr;
8c18ed03
SG
247 }
248
2b3bf958
AL
249 desc->rsc.page_vec = page_vec;
250 desc->rsc.fmr_pool = fmr_pool;
251 list_add(&desc->list, &fr_pool->list);
385ad87d 252
8c18ed03 253 return 0;
986db0d6 254
2b3bf958 255err_fmr:
385ad87d 256 kfree(page_vec);
2b3bf958
AL
257err_frpl:
258 kfree(desc);
259
8c18ed03 260 return ret;
986db0d6
SP
261}
262
263/**
264 * iser_free_fmr_pool - releases the FMR pool and page vec
265 */
a4ee3539 266void iser_free_fmr_pool(struct ib_conn *ib_conn)
986db0d6 267{
385ad87d 268 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
2b3bf958
AL
269 struct iser_fr_desc *desc;
270
271 desc = list_first_entry(&fr_pool->list,
272 struct iser_fr_desc, list);
273 list_del(&desc->list);
986db0d6 274
385ad87d 275 iser_info("freeing conn %p fmr pool %p\n",
2b3bf958 276 ib_conn, desc->rsc.fmr_pool);
986db0d6 277
2b3bf958
AL
278 ib_destroy_fmr_pool(desc->rsc.fmr_pool);
279 kfree(desc->rsc.page_vec);
280 kfree(desc);
986db0d6
SP
281}
282
d711d81d 283static int
f8db651d
SG
284iser_alloc_reg_res(struct ib_device *ib_device,
285 struct ib_pd *pd,
286 struct iser_reg_resources *res,
287 unsigned int size)
d711d81d
SG
288{
289 int ret;
290
f8db651d 291 res->frpl = ib_alloc_fast_reg_page_list(ib_device, size);
d711d81d
SG
292 if (IS_ERR(res->frpl)) {
293 ret = PTR_ERR(res->frpl);
294 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
295 ret);
296 return PTR_ERR(res->frpl);
297 }
298
f8db651d 299 res->mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, size);
d711d81d
SG
300 if (IS_ERR(res->mr)) {
301 ret = PTR_ERR(res->mr);
302 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
303 goto fast_reg_mr_failure;
304 }
305 res->mr_valid = 1;
306
307 return 0;
308
309fast_reg_mr_failure:
310 ib_free_fast_reg_page_list(res->frpl);
311
312 return ret;
313}
314
315static void
316iser_free_reg_res(struct iser_reg_resources *rsc)
317{
318 ib_dereg_mr(rsc->mr);
319 ib_free_fast_reg_page_list(rsc->frpl);
320}
321
4dec2a27 322static int
f8db651d
SG
323iser_alloc_pi_ctx(struct ib_device *ib_device,
324 struct ib_pd *pd,
325 struct iser_fr_desc *desc,
326 unsigned int size)
4dec2a27
SG
327{
328 struct iser_pi_context *pi_ctx = NULL;
9bee178b 329 int ret;
4dec2a27
SG
330
331 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
332 if (!desc->pi_ctx)
333 return -ENOMEM;
334
335 pi_ctx = desc->pi_ctx;
336
f8db651d 337 ret = iser_alloc_reg_res(ib_device, pd, &pi_ctx->rsc, size);
d711d81d
SG
338 if (ret) {
339 iser_err("failed to allocate reg_resources\n");
340 goto alloc_reg_res_err;
4dec2a27 341 }
4dec2a27 342
9bee178b 343 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
4dec2a27
SG
344 if (IS_ERR(pi_ctx->sig_mr)) {
345 ret = PTR_ERR(pi_ctx->sig_mr);
346 goto sig_mr_failure;
347 }
d711d81d
SG
348 pi_ctx->sig_mr_valid = 1;
349 desc->pi_ctx->sig_protected = 0;
4dec2a27
SG
350
351 return 0;
352
353sig_mr_failure:
d711d81d
SG
354 iser_free_reg_res(&pi_ctx->rsc);
355alloc_reg_res_err:
4dec2a27
SG
356 kfree(desc->pi_ctx);
357
358 return ret;
359}
360
361static void
362iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
363{
d711d81d 364 iser_free_reg_res(&pi_ctx->rsc);
8b91ffc1 365 ib_dereg_mr(pi_ctx->sig_mr);
4dec2a27
SG
366 kfree(pi_ctx);
367}
368
eb6ea8c3 369static struct iser_fr_desc *
f8db651d
SG
370iser_create_fastreg_desc(struct ib_device *ib_device,
371 struct ib_pd *pd,
372 bool pi_enable,
373 unsigned int size)
310b347c 374{
eb6ea8c3 375 struct iser_fr_desc *desc;
310b347c
SG
376 int ret;
377
eb6ea8c3
SG
378 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
379 if (!desc)
380 return ERR_PTR(-ENOMEM);
381
f8db651d 382 ret = iser_alloc_reg_res(ib_device, pd, &desc->rsc, size);
eb6ea8c3
SG
383 if (ret)
384 goto reg_res_alloc_failure;
6b5a8fb0
AT
385
386 if (pi_enable) {
f8db651d 387 ret = iser_alloc_pi_ctx(ib_device, pd, desc, size);
4dec2a27 388 if (ret)
6b5a8fb0 389 goto pi_ctx_alloc_failure;
6b5a8fb0 390 }
310b347c 391
eb6ea8c3 392 return desc;
d711d81d 393
6b5a8fb0 394pi_ctx_alloc_failure:
d711d81d 395 iser_free_reg_res(&desc->rsc);
eb6ea8c3
SG
396reg_res_alloc_failure:
397 kfree(desc);
310b347c 398
eb6ea8c3 399 return ERR_PTR(ret);
310b347c
SG
400}
401
5587856c 402/**
48afbff6 403 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
5587856c
SG
404 * for fast registration work requests.
405 * returns 0 on success, or errno code on failure
406 */
f8db651d
SG
407int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
408 unsigned cmds_max,
409 unsigned int size)
5587856c 410{
a4ee3539 411 struct iser_device *device = ib_conn->device;
385ad87d 412 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
5190cc26 413 struct iser_fr_desc *desc;
5587856c
SG
414 int i, ret;
415
2b3bf958 416 INIT_LIST_HEAD(&fr_pool->list);
385ad87d 417 spin_lock_init(&fr_pool->lock);
2b3bf958 418 fr_pool->size = 0;
5587856c 419 for (i = 0; i < cmds_max; i++) {
eb6ea8c3 420 desc = iser_create_fastreg_desc(device->ib_device, device->pd,
f8db651d 421 ib_conn->pi_support, size);
eb6ea8c3
SG
422 if (IS_ERR(desc)) {
423 ret = PTR_ERR(desc);
310b347c 424 goto err;
5587856c
SG
425 }
426
2b3bf958
AL
427 list_add_tail(&desc->list, &fr_pool->list);
428 fr_pool->size++;
5587856c
SG
429 }
430
431 return 0;
27ae2d1e 432
5587856c 433err:
a4ee3539 434 iser_free_fastreg_pool(ib_conn);
5587856c
SG
435 return ret;
436}
437
438/**
7306b8fa 439 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
5587856c 440 */
a4ee3539 441void iser_free_fastreg_pool(struct ib_conn *ib_conn)
5587856c 442{
385ad87d 443 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
5190cc26 444 struct iser_fr_desc *desc, *tmp;
5587856c
SG
445 int i = 0;
446
2b3bf958 447 if (list_empty(&fr_pool->list))
5587856c
SG
448 return;
449
a4ee3539 450 iser_info("freeing conn %p fr pool\n", ib_conn);
5587856c 451
2b3bf958 452 list_for_each_entry_safe(desc, tmp, &fr_pool->list, list) {
5587856c 453 list_del(&desc->list);
d711d81d 454 iser_free_reg_res(&desc->rsc);
4dec2a27
SG
455 if (desc->pi_ctx)
456 iser_free_pi_ctx(desc->pi_ctx);
5587856c
SG
457 kfree(desc);
458 ++i;
459 }
460
2b3bf958 461 if (i < fr_pool->size)
5587856c 462 iser_warn("pool still has %d regions registered\n",
2b3bf958 463 fr_pool->size - i);
5587856c
SG
464}
465
986db0d6
SP
466/**
467 * iser_create_ib_conn_res - Queue-Pair (QP)
468 *
469 * returns 0 on success, -1 on failure
470 */
a4ee3539 471static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
986db0d6 472{
f4641ef7
MT
473 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
474 ib_conn);
986db0d6 475 struct iser_device *device;
f4641ef7 476 struct ib_device_attr *dev_attr;
986db0d6
SP
477 struct ib_qp_init_attr init_attr;
478 int ret = -ENOMEM;
479 int index, min_index = 0;
480
a4ee3539 481 BUG_ON(ib_conn->device == NULL);
986db0d6 482
a4ee3539 483 device = ib_conn->device;
f4641ef7 484 dev_attr = &device->dev_attr;
1cfa0a75
OG
485
486 memset(&init_attr, 0, sizeof init_attr);
487
5a33a669
AT
488 mutex_lock(&ig.connlist_mutex);
489 /* select the CQ with the minimal number of usages */
bf175540
SG
490 for (index = 0; index < device->comps_used; index++) {
491 if (device->comps[index].active_qps <
492 device->comps[min_index].active_qps)
5a33a669 493 min_index = index;
bf175540
SG
494 }
495 ib_conn->comp = &device->comps[min_index];
496 ib_conn->comp->active_qps++;
5a33a669 497 mutex_unlock(&ig.connlist_mutex);
a4ee3539 498 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
5a33a669 499
1cfa0a75 500 init_attr.event_handler = iser_qp_event_callback;
a4ee3539 501 init_attr.qp_context = (void *)ib_conn;
6aabfa76
SG
502 init_attr.send_cq = ib_conn->comp->cq;
503 init_attr.recv_cq = ib_conn->comp->cq;
1cfa0a75 504 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
f19624aa 505 init_attr.cap.max_send_sge = 2;
bcc60c38 506 init_attr.cap.max_recv_sge = 1;
1cfa0a75
OG
507 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
508 init_attr.qp_type = IB_QPT_RC;
a4ee3539 509 if (ib_conn->pi_support) {
ff3dd52d 510 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
6b5a8fb0 511 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
f4641ef7
MT
512 iser_conn->max_cmds =
513 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
6b5a8fb0 514 } else {
f4641ef7
MT
515 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
516 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
517 iser_conn->max_cmds =
518 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
519 } else {
520 init_attr.cap.max_send_wr = dev_attr->max_qp_wr;
521 iser_conn->max_cmds =
522 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr);
523 iser_dbg("device %s supports max_send_wr %d\n",
524 device->ib_device->name, dev_attr->max_qp_wr);
525 }
6b5a8fb0 526 }
1cfa0a75 527
a4ee3539 528 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
1cfa0a75 529 if (ret)
9fda1ac5 530 goto out_err;
1cfa0a75 531
a4ee3539 532 ib_conn->qp = ib_conn->cma_id->qp;
986db0d6 533 iser_info("setting conn %p cma_id %p qp %p\n",
a4ee3539
SG
534 ib_conn, ib_conn->cma_id,
535 ib_conn->cma_id->qp);
1cfa0a75
OG
536 return ret;
537
9fda1ac5 538out_err:
93acb7bb
SG
539 mutex_lock(&ig.connlist_mutex);
540 ib_conn->comp->active_qps--;
541 mutex_unlock(&ig.connlist_mutex);
1cfa0a75 542 iser_err("unable to alloc mem or create resource, err %d\n", ret);
93acb7bb 543
1cfa0a75
OG
544 return ret;
545}
546
1cfa0a75
OG
547/**
548 * based on the resolved device node GUID see if there already allocated
549 * device for this device. If there's no such, create one.
550 */
551static
552struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
553{
9a378270 554 struct iser_device *device;
1cfa0a75
OG
555
556 mutex_lock(&ig.device_list_mutex);
557
9a378270 558 list_for_each_entry(device, &ig.device_list, ig_list)
1cfa0a75
OG
559 /* find if there's a match using the node GUID */
560 if (device->ib_device->node_guid == cma_id->device->node_guid)
d33ed425 561 goto inc_refcnt;
9a378270
AR
562
563 device = kzalloc(sizeof *device, GFP_KERNEL);
564 if (device == NULL)
565 goto out;
566
567 /* assign this device to the device */
568 device->ib_device = cma_id->device;
569 /* init the device and link it into ig device list */
570 if (iser_create_device_ib_res(device)) {
571 kfree(device);
572 device = NULL;
573 goto out;
1cfa0a75 574 }
9a378270
AR
575 list_add(&device->ig_list, &ig.device_list);
576
d33ed425 577inc_refcnt:
1cfa0a75 578 device->refcount++;
d33ed425 579out:
1cfa0a75
OG
580 mutex_unlock(&ig.device_list_mutex);
581 return device;
582}
583
584/* if there's no demand for this device, release it */
585static void iser_device_try_release(struct iser_device *device)
586{
587 mutex_lock(&ig.device_list_mutex);
588 device->refcount--;
4f363882 589 iser_info("device %p refcount %d\n", device, device->refcount);
1cfa0a75
OG
590 if (!device->refcount) {
591 iser_free_device_ib_res(device);
592 list_del(&device->ig_list);
593 kfree(device);
594 }
595 mutex_unlock(&ig.device_list_mutex);
596}
597
504130c0
AN
598/**
599 * Called with state mutex held
600 **/
5716af6e
SG
601static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
602 enum iser_conn_state comp,
603 enum iser_conn_state exch)
1cfa0a75
OG
604{
605 int ret;
606
5716af6e
SG
607 ret = (iser_conn->state == comp);
608 if (ret)
609 iser_conn->state = exch;
610
1cfa0a75
OG
611 return ret;
612}
613
b73c3ada
AN
614void iser_release_work(struct work_struct *work)
615{
5716af6e 616 struct iser_conn *iser_conn;
b73c3ada 617
5716af6e 618 iser_conn = container_of(work, struct iser_conn, release_work);
b73c3ada 619
c107a6c0
SG
620 /* Wait for conn_stop to complete */
621 wait_for_completion(&iser_conn->stop_completion);
622 /* Wait for IB resouces cleanup to complete */
623 wait_for_completion(&iser_conn->ib_completion);
b73c3ada 624
5716af6e
SG
625 mutex_lock(&iser_conn->state_mutex);
626 iser_conn->state = ISER_CONN_DOWN;
627 mutex_unlock(&iser_conn->state_mutex);
504130c0 628
5716af6e 629 iser_conn_release(iser_conn);
b73c3ada
AN
630}
631
96f15198
SG
632/**
633 * iser_free_ib_conn_res - release IB related resources
634 * @iser_conn: iser connection struct
6606e6a2
SG
635 * @destroy: indicator if we need to try to release the
636 * iser device and memory regoins pool (only iscsi
637 * shutdown and DEVICE_REMOVAL will use this).
96f15198
SG
638 *
639 * This routine is called with the iser state mutex held
640 * so the cm_id removal is out of here. It is Safe to
641 * be invoked multiple times.
642 */
c47a3c9e 643static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
6606e6a2 644 bool destroy)
96f15198
SG
645{
646 struct ib_conn *ib_conn = &iser_conn->ib_conn;
647 struct iser_device *device = ib_conn->device;
648
649 iser_info("freeing conn %p cma_id %p qp %p\n",
650 iser_conn, ib_conn->cma_id, ib_conn->qp);
651
96f15198 652 if (ib_conn->qp != NULL) {
bf175540 653 ib_conn->comp->active_qps--;
96f15198
SG
654 rdma_destroy_qp(ib_conn->cma_id);
655 ib_conn->qp = NULL;
656 }
657
6606e6a2
SG
658 if (destroy) {
659 if (iser_conn->rx_descs)
660 iser_free_rx_descriptors(iser_conn);
661
662 if (device != NULL) {
663 iser_device_try_release(device);
664 ib_conn->device = NULL;
665 }
96f15198
SG
666 }
667}
668
41179e2d
RD
669/**
670 * Frees all conn objects and deallocs conn descriptor
671 */
5716af6e 672void iser_conn_release(struct iser_conn *iser_conn)
41179e2d 673{
a4ee3539 674 struct ib_conn *ib_conn = &iser_conn->ib_conn;
41179e2d 675
41179e2d 676 mutex_lock(&ig.connlist_mutex);
5716af6e 677 list_del(&iser_conn->conn_list);
41179e2d 678 mutex_unlock(&ig.connlist_mutex);
504130c0 679
5716af6e 680 mutex_lock(&iser_conn->state_mutex);
9a3119e4 681 /* In case we endup here without ep_disconnect being invoked. */
5426b171 682 if (iser_conn->state != ISER_CONN_DOWN) {
aea8f4df
AN
683 iser_warn("iser conn %p state %d, expected state down.\n",
684 iser_conn, iser_conn->state);
9a3119e4 685 iscsi_destroy_endpoint(iser_conn->ep);
5426b171
AN
686 iser_conn->state = ISER_CONN_DOWN;
687 }
c47a3c9e
SG
688 /*
689 * In case we never got to bind stage, we still need to
690 * release IB resources (which is safe to call more than once).
691 */
692 iser_free_ib_conn_res(iser_conn, true);
5716af6e 693 mutex_unlock(&iser_conn->state_mutex);
504130c0 694
a4ee3539
SG
695 if (ib_conn->cma_id != NULL) {
696 rdma_destroy_id(ib_conn->cma_id);
697 ib_conn->cma_id = NULL;
5b61ff43 698 }
a4ee3539 699
5716af6e 700 kfree(iser_conn);
41179e2d
RD
701}
702
1cfa0a75
OG
703/**
704 * triggers start of the disconnect procedures and wait for them to be done
c47a3c9e 705 * Called with state mutex held
1cfa0a75 706 */
c47a3c9e 707int iser_conn_terminate(struct iser_conn *iser_conn)
1cfa0a75 708{
a4ee3539 709 struct ib_conn *ib_conn = &iser_conn->ib_conn;
ff3dd52d 710 struct ib_send_wr *bad_wr;
1cfa0a75
OG
711 int err = 0;
712
c47a3c9e
SG
713 /* terminate the iser conn only if the conn state is UP */
714 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
715 ISER_CONN_TERMINATING))
716 return 0;
717
718 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
719
720 /* suspend queuing of new iscsi commands */
721 if (iser_conn->iscsi_conn)
722 iscsi_suspend_queue(iser_conn->iscsi_conn);
723
724 /*
725 * In case we didn't already clean up the cma_id (peer initiated
726 * a disconnection), we need to Cause the CMA to change the QP
727 * state to ERROR.
1cfa0a75 728 */
c47a3c9e
SG
729 if (ib_conn->cma_id) {
730 err = rdma_disconnect(ib_conn->cma_id);
731 if (err)
732 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
733 iser_conn, err);
734
ff3dd52d
SG
735 /* post an indication that all flush errors were consumed */
736 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr);
16df2a26 737 if (err) {
ff3dd52d 738 iser_err("conn %p failed to post beacon", ib_conn);
16df2a26
SG
739 return 1;
740 }
ff3dd52d 741
6aabfa76 742 wait_for_completion(&ib_conn->flush_comp);
c47a3c9e 743 }
1cfa0a75 744
c47a3c9e 745 return 1;
1cfa0a75
OG
746}
747
504130c0
AN
748/**
749 * Called with state mutex held
750 **/
b73c3ada 751static void iser_connect_error(struct rdma_cm_id *cma_id)
1cfa0a75 752{
5716af6e 753 struct iser_conn *iser_conn;
b73c3ada 754
5716af6e 755 iser_conn = (struct iser_conn *)cma_id->context;
c4de4663 756 iser_conn->state = ISER_CONN_TERMINATING;
1cfa0a75
OG
757}
758
504130c0
AN
759/**
760 * Called with state mutex held
761 **/
b73c3ada 762static void iser_addr_handler(struct rdma_cm_id *cma_id)
1cfa0a75
OG
763{
764 struct iser_device *device;
5716af6e 765 struct iser_conn *iser_conn;
a4ee3539 766 struct ib_conn *ib_conn;
1cfa0a75
OG
767 int ret;
768
5716af6e
SG
769 iser_conn = (struct iser_conn *)cma_id->context;
770 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
771 /* bailout */
772 return;
773
a4ee3539 774 ib_conn = &iser_conn->ib_conn;
1cfa0a75 775 device = iser_device_find_by_ib_device(cma_id);
d33ed425
AR
776 if (!device) {
777 iser_err("device lookup/creation failed\n");
b73c3ada
AN
778 iser_connect_error(cma_id);
779 return;
d33ed425
AR
780 }
781
a4ee3539 782 ib_conn->device = device;
1cfa0a75 783
7f733847
AT
784 /* connection T10-PI support */
785 if (iser_pi_enable) {
786 if (!(device->dev_attr.device_cap_flags &
787 IB_DEVICE_SIGNATURE_HANDOVER)) {
788 iser_warn("T10-PI requested but not supported on %s, "
789 "continue without T10-PI\n",
a4ee3539
SG
790 ib_conn->device->ib_device->name);
791 ib_conn->pi_support = false;
7f733847 792 } else {
a4ee3539 793 ib_conn->pi_support = true;
7f733847
AT
794 }
795 }
796
1cfa0a75
OG
797 ret = rdma_resolve_route(cma_id, 1000);
798 if (ret) {
799 iser_err("resolve route failed: %d\n", ret);
b73c3ada
AN
800 iser_connect_error(cma_id);
801 return;
1cfa0a75 802 }
1cfa0a75
OG
803}
804
504130c0
AN
805/**
806 * Called with state mutex held
807 **/
b73c3ada 808static void iser_route_handler(struct rdma_cm_id *cma_id)
1cfa0a75
OG
809{
810 struct rdma_conn_param conn_param;
811 int ret;
8d8399de 812 struct iser_cm_hdr req_hdr;
5716af6e 813 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
a4ee3539
SG
814 struct ib_conn *ib_conn = &iser_conn->ib_conn;
815 struct iser_device *device = ib_conn->device;
1cfa0a75 816
5716af6e 817 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
818 /* bailout */
819 return;
820
a4ee3539 821 ret = iser_create_ib_conn_res(ib_conn);
1cfa0a75
OG
822 if (ret)
823 goto failure;
824
1cfa0a75 825 memset(&conn_param, 0, sizeof conn_param);
2ea32938 826 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
1cfa0a75
OG
827 conn_param.initiator_depth = 1;
828 conn_param.retry_count = 7;
829 conn_param.rnr_retry_count = 6;
830
8d8399de
OG
831 memset(&req_hdr, 0, sizeof(req_hdr));
832 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
833 ISER_SEND_W_INV_NOT_SUPPORTED);
834 conn_param.private_data = (void *)&req_hdr;
835 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
836
1cfa0a75
OG
837 ret = rdma_connect(cma_id, &conn_param);
838 if (ret) {
839 iser_err("failure connecting: %d\n", ret);
840 goto failure;
841 }
842
b73c3ada 843 return;
1cfa0a75 844failure:
b73c3ada 845 iser_connect_error(cma_id);
1cfa0a75
OG
846}
847
848static void iser_connected_handler(struct rdma_cm_id *cma_id)
849{
5716af6e 850 struct iser_conn *iser_conn;
4f9208ad
OG
851 struct ib_qp_attr attr;
852 struct ib_qp_init_attr init_attr;
853
5716af6e
SG
854 iser_conn = (struct iser_conn *)cma_id->context;
855 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
856 /* bailout */
857 return;
858
4f9208ad
OG
859 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
860 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
1cfa0a75 861
5716af6e
SG
862 iser_conn->state = ISER_CONN_UP;
863 complete(&iser_conn->up_completion);
1cfa0a75
OG
864}
865
b73c3ada 866static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
1cfa0a75 867{
c47a3c9e 868 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
1cfa0a75 869
c47a3c9e 870 if (iser_conn_terminate(iser_conn)) {
5716af6e 871 if (iser_conn->iscsi_conn)
c47a3c9e
SG
872 iscsi_conn_failure(iser_conn->iscsi_conn,
873 ISCSI_ERR_CONN_FAILED);
7d9eacf9
RD
874 else
875 iser_err("iscsi_iser connection isn't bound\n");
876 }
c47a3c9e
SG
877}
878
879static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
6606e6a2 880 bool destroy)
c47a3c9e
SG
881{
882 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
1cfa0a75 883
c47a3c9e
SG
884 /*
885 * We are not guaranteed that we visited disconnected_handler
886 * by now, call it here to be safe that we handle CM drep
887 * and flush errors.
8d4aca7f 888 */
c47a3c9e 889 iser_disconnected_handler(cma_id);
6606e6a2 890 iser_free_ib_conn_res(iser_conn, destroy);
c47a3c9e
SG
891 complete(&iser_conn->ib_completion);
892};
1cfa0a75
OG
893
894static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
895{
5716af6e 896 struct iser_conn *iser_conn;
c47a3c9e 897 int ret = 0;
504130c0 898
5716af6e 899 iser_conn = (struct iser_conn *)cma_id->context;
871e00af
SG
900 iser_info("%s (%d): status %d conn %p id %p\n",
901 rdma_event_msg(event->event), event->event,
902 event->status, cma_id->context, cma_id);
1cfa0a75 903
5716af6e 904 mutex_lock(&iser_conn->state_mutex);
1cfa0a75
OG
905 switch (event->event) {
906 case RDMA_CM_EVENT_ADDR_RESOLVED:
b73c3ada 907 iser_addr_handler(cma_id);
1cfa0a75
OG
908 break;
909 case RDMA_CM_EVENT_ROUTE_RESOLVED:
b73c3ada 910 iser_route_handler(cma_id);
1cfa0a75
OG
911 break;
912 case RDMA_CM_EVENT_ESTABLISHED:
913 iser_connected_handler(cma_id);
914 break;
915 case RDMA_CM_EVENT_ADDR_ERROR:
916 case RDMA_CM_EVENT_ROUTE_ERROR:
917 case RDMA_CM_EVENT_CONNECT_ERROR:
918 case RDMA_CM_EVENT_UNREACHABLE:
919 case RDMA_CM_EVENT_REJECTED:
b73c3ada 920 iser_connect_error(cma_id);
1cfa0a75
OG
921 break;
922 case RDMA_CM_EVENT_DISCONNECTED:
2f5de151 923 case RDMA_CM_EVENT_ADDR_CHANGE:
5426b171
AN
924 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
925 iser_cleanup_handler(cma_id, false);
1cfa0a75 926 break;
c47a3c9e
SG
927 case RDMA_CM_EVENT_DEVICE_REMOVAL:
928 /*
929 * we *must* destroy the device as we cannot rely
930 * on iscsid to be around to initiate error handling.
5426b171
AN
931 * also if we are not in state DOWN implicitly destroy
932 * the cma_id.
c47a3c9e
SG
933 */
934 iser_cleanup_handler(cma_id, true);
5426b171
AN
935 if (iser_conn->state != ISER_CONN_DOWN) {
936 iser_conn->ib_conn.cma_id = NULL;
937 ret = 1;
938 }
c47a3c9e 939 break;
1cfa0a75 940 default:
871e00af
SG
941 iser_err("Unexpected RDMA CM event: %s (%d)\n",
942 rdma_event_msg(event->event), event->event);
1cfa0a75
OG
943 break;
944 }
5716af6e 945 mutex_unlock(&iser_conn->state_mutex);
c47a3c9e
SG
946
947 return ret;
1cfa0a75
OG
948}
949
5716af6e 950void iser_conn_init(struct iser_conn *iser_conn)
1cfa0a75 951{
5716af6e 952 iser_conn->state = ISER_CONN_INIT;
a4ee3539 953 iser_conn->ib_conn.post_recv_buf_count = 0;
6aabfa76 954 init_completion(&iser_conn->ib_conn.flush_comp);
5716af6e 955 init_completion(&iser_conn->stop_completion);
c47a3c9e 956 init_completion(&iser_conn->ib_completion);
5716af6e
SG
957 init_completion(&iser_conn->up_completion);
958 INIT_LIST_HEAD(&iser_conn->conn_list);
5716af6e 959 mutex_init(&iser_conn->state_mutex);
1cfa0a75
OG
960}
961
962 /**
963 * starts the process of connecting to the target
94e2bd68 964 * sleeps until the connection is established or rejected
1cfa0a75 965 */
5716af6e 966int iser_connect(struct iser_conn *iser_conn,
96ed02d4
RD
967 struct sockaddr *src_addr,
968 struct sockaddr *dst_addr,
1cfa0a75
OG
969 int non_blocking)
970{
a4ee3539 971 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1cfa0a75
OG
972 int err = 0;
973
5716af6e 974 mutex_lock(&iser_conn->state_mutex);
504130c0 975
5716af6e 976 sprintf(iser_conn->name, "%pISp", dst_addr);
96ed02d4 977
5716af6e 978 iser_info("connecting to: %s\n", iser_conn->name);
1cfa0a75
OG
979
980 /* the device is known only --after-- address resolution */
a4ee3539 981 ib_conn->device = NULL;
1cfa0a75 982
5716af6e 983 iser_conn->state = ISER_CONN_PENDING;
1cfa0a75 984
ff3dd52d
SG
985 ib_conn->beacon.wr_id = ISER_BEACON_WRID;
986 ib_conn->beacon.opcode = IB_WR_SEND;
987
a4ee3539
SG
988 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
989 (void *)iser_conn,
990 RDMA_PS_TCP, IB_QPT_RC);
991 if (IS_ERR(ib_conn->cma_id)) {
992 err = PTR_ERR(ib_conn->cma_id);
1cfa0a75
OG
993 iser_err("rdma_create_id failed: %d\n", err);
994 goto id_failure;
995 }
996
a4ee3539 997 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
1cfa0a75
OG
998 if (err) {
999 iser_err("rdma_resolve_addr failed: %d\n", err);
1000 goto addr_failure;
1001 }
1002
1003 if (!non_blocking) {
5716af6e 1004 wait_for_completion_interruptible(&iser_conn->up_completion);
1cfa0a75 1005
5716af6e 1006 if (iser_conn->state != ISER_CONN_UP) {
1cfa0a75
OG
1007 err = -EIO;
1008 goto connect_failure;
1009 }
1010 }
5716af6e 1011 mutex_unlock(&iser_conn->state_mutex);
1cfa0a75
OG
1012
1013 mutex_lock(&ig.connlist_mutex);
5716af6e 1014 list_add(&iser_conn->conn_list, &ig.connlist);
1cfa0a75
OG
1015 mutex_unlock(&ig.connlist_mutex);
1016 return 0;
1017
1018id_failure:
a4ee3539 1019 ib_conn->cma_id = NULL;
1cfa0a75 1020addr_failure:
5716af6e 1021 iser_conn->state = ISER_CONN_DOWN;
1cfa0a75 1022connect_failure:
5716af6e
SG
1023 mutex_unlock(&iser_conn->state_mutex);
1024 iser_conn_release(iser_conn);
1cfa0a75
OG
1025 return err;
1026}
1027
5716af6e 1028int iser_post_recvl(struct iser_conn *iser_conn)
bcc60c38
OG
1029{
1030 struct ib_recv_wr rx_wr, *rx_wr_failed;
a4ee3539 1031 struct ib_conn *ib_conn = &iser_conn->ib_conn;
bcc60c38
OG
1032 struct ib_sge sge;
1033 int ib_ret;
1034
5716af6e 1035 sge.addr = iser_conn->login_resp_dma;
bcc60c38 1036 sge.length = ISER_RX_LOGIN_SIZE;
a4ee3539 1037 sge.lkey = ib_conn->device->mr->lkey;
bcc60c38 1038
49df2781 1039 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf;
bcc60c38
OG
1040 rx_wr.sg_list = &sge;
1041 rx_wr.num_sge = 1;
1042 rx_wr.next = NULL;
1043
a4ee3539
SG
1044 ib_conn->post_recv_buf_count++;
1045 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
bcc60c38
OG
1046 if (ib_ret) {
1047 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
a4ee3539 1048 ib_conn->post_recv_buf_count--;
bcc60c38
OG
1049 }
1050 return ib_ret;
1051}
1052
5716af6e 1053int iser_post_recvm(struct iser_conn *iser_conn, int count)
bcc60c38
OG
1054{
1055 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1056 int i, ib_ret;
a4ee3539 1057 struct ib_conn *ib_conn = &iser_conn->ib_conn;
5716af6e 1058 unsigned int my_rx_head = iser_conn->rx_desc_head;
bcc60c38
OG
1059 struct iser_rx_desc *rx_desc;
1060
a4ee3539 1061 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
5716af6e 1062 rx_desc = &iser_conn->rx_descs[my_rx_head];
49df2781 1063 rx_wr->wr_id = (uintptr_t)rx_desc;
bcc60c38
OG
1064 rx_wr->sg_list = &rx_desc->rx_sg;
1065 rx_wr->num_sge = 1;
1066 rx_wr->next = rx_wr + 1;
5716af6e 1067 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
bcc60c38
OG
1068 }
1069
1070 rx_wr--;
1071 rx_wr->next = NULL; /* mark end of work requests list */
1072
a4ee3539
SG
1073 ib_conn->post_recv_buf_count += count;
1074 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
bcc60c38
OG
1075 if (ib_ret) {
1076 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
a4ee3539 1077 ib_conn->post_recv_buf_count -= count;
bcc60c38 1078 } else
5716af6e 1079 iser_conn->rx_desc_head = my_rx_head;
bcc60c38
OG
1080 return ib_ret;
1081}
1082
1083
1cfa0a75
OG
1084/**
1085 * iser_start_send - Initiate a Send DTO operation
1086 *
1087 * returns 0 on success, -1 on failure
1088 */
6df5a128
SG
1089int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
1090 bool signal)
1cfa0a75 1091{
f19624aa 1092 int ib_ret;
1cfa0a75 1093 struct ib_send_wr send_wr, *send_wr_failed;
1cfa0a75 1094
a4ee3539 1095 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
5716af6e
SG
1096 tx_desc->dma_addr, ISER_HEADERS_LEN,
1097 DMA_TO_DEVICE);
1cfa0a75
OG
1098
1099 send_wr.next = NULL;
49df2781 1100 send_wr.wr_id = (uintptr_t)tx_desc;
f19624aa
OG
1101 send_wr.sg_list = tx_desc->tx_sg;
1102 send_wr.num_sge = tx_desc->num_sge;
1cfa0a75 1103 send_wr.opcode = IB_WR_SEND;
6df5a128 1104 send_wr.send_flags = signal ? IB_SEND_SIGNALED : 0;
1cfa0a75 1105
a4ee3539 1106 ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
ff3dd52d 1107 if (ib_ret)
1cfa0a75 1108 iser_err("ib_post_send failed, ret:%d\n", ib_ret);
ff3dd52d 1109
f19624aa 1110 return ib_ret;
1cfa0a75
OG
1111}
1112
6aabfa76
SG
1113/**
1114 * is_iser_tx_desc - Indicate if the completion wr_id
1115 * is a TX descriptor or not.
1116 * @iser_conn: iser connection
1117 * @wr_id: completion WR identifier
1118 *
1119 * Since we cannot rely on wc opcode in FLUSH errors
1120 * we must work around it by checking if the wr_id address
1121 * falls in the iser connection rx_descs buffer. If so
1122 * it is an RX descriptor, otherwize it is a TX.
1123 */
1124static inline bool
1125is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id)
1126{
1127 void *start = iser_conn->rx_descs;
1128 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs);
1129
1130 if (wr_id >= start && wr_id < start + len)
1131 return false;
1132
1133 return true;
1134}
1135
8c204e69
SG
1136/**
1137 * iser_handle_comp_error() - Handle error completion
8c204e69
SG
1138 * @ib_conn: connection RDMA resources
1139 * @wc: work completion
1140 *
1141 * Notes: We may handle a FLUSH error completion and in this case
1142 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1143 * error completion we should also notify iscsi layer that
1144 * connection is failed (in case we passed bind stage).
1145 */
1146static void
6aabfa76 1147iser_handle_comp_error(struct ib_conn *ib_conn,
8c204e69 1148 struct ib_wc *wc)
1cfa0a75 1149{
49df2781 1150 void *wr_id = (void *)(uintptr_t)wc->wr_id;
8c204e69
SG
1151 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
1152 ib_conn);
1153
1154 if (wc->status != IB_WC_WR_FLUSH_ERR)
1155 if (iser_conn->iscsi_conn)
1156 iscsi_conn_failure(iser_conn->iscsi_conn,
1157 ISCSI_ERR_CONN_FAILED);
1158
30bf1d58
SG
1159 if (wc->wr_id == ISER_FASTREG_LI_WRID)
1160 return;
1161
49df2781
SG
1162 if (is_iser_tx_desc(iser_conn, wr_id)) {
1163 struct iser_tx_desc *desc = wr_id;
6aabfa76 1164
6aabfa76
SG
1165 if (desc->type == ISCSI_TX_DATAOUT)
1166 kmem_cache_free(ig.desc_cache, desc);
1167 } else {
1168 ib_conn->post_recv_buf_count--;
1169 }
1cfa0a75
OG
1170}
1171
6aabfa76
SG
1172/**
1173 * iser_handle_wc - handle a single work completion
1174 * @wc: work completion
1175 *
1176 * Soft-IRQ context, work completion can be either
1177 * SEND or RECV, and can turn out successful or
1178 * with error (or flush error).
1179 */
1180static void iser_handle_wc(struct ib_wc *wc)
78ad0a34 1181{
a4ee3539 1182 struct ib_conn *ib_conn;
6aabfa76
SG
1183 struct iser_tx_desc *tx_desc;
1184 struct iser_rx_desc *rx_desc;
78ad0a34 1185
6aabfa76 1186 ib_conn = wc->qp->qp_context;
06c7fb67 1187 if (likely(wc->status == IB_WC_SUCCESS)) {
6aabfa76 1188 if (wc->opcode == IB_WC_RECV) {
49df2781 1189 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
6aabfa76
SG
1190 iser_rcv_completion(rx_desc, wc->byte_len,
1191 ib_conn);
1192 } else
1193 if (wc->opcode == IB_WC_SEND) {
49df2781 1194 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
6aabfa76 1195 iser_snd_completion(tx_desc, ib_conn);
78ad0a34 1196 } else {
6aabfa76 1197 iser_err("Unknown wc opcode %d\n", wc->opcode);
78ad0a34 1198 }
6aabfa76
SG
1199 } else {
1200 if (wc->status != IB_WC_WR_FLUSH_ERR)
871e00af
SG
1201 iser_err("%s (%d): wr id %llx vend_err %x\n",
1202 ib_wc_status_msg(wc->status), wc->status,
1203 wc->wr_id, wc->vendor_err);
6aabfa76 1204 else
871e00af
SG
1205 iser_dbg("%s (%d): wr id %llx\n",
1206 ib_wc_status_msg(wc->status), wc->status,
1207 wc->wr_id);
6aabfa76 1208
ff3dd52d 1209 if (wc->wr_id == ISER_BEACON_WRID)
30bf1d58 1210 /* all flush errors were consumed */
6aabfa76 1211 complete(&ib_conn->flush_comp);
30bf1d58
SG
1212 else
1213 iser_handle_comp_error(ib_conn, wc);
78ad0a34 1214 }
78ad0a34
OG
1215}
1216
6aabfa76
SG
1217/**
1218 * iser_cq_tasklet_fn - iSER completion polling loop
1219 * @data: iSER completion context
1220 *
1221 * Soft-IRQ context, polling connection CQ until
1222 * either CQ was empty or we exausted polling budget
1223 */
1cfa0a75
OG
1224static void iser_cq_tasklet_fn(unsigned long data)
1225{
bf175540 1226 struct iser_comp *comp = (struct iser_comp *)data;
6aabfa76 1227 struct ib_cq *cq = comp->cq;
6e6fe2fb
SG
1228 struct ib_wc *const wcs = comp->wcs;
1229 int i, n, completed = 0;
1cfa0a75 1230
6e6fe2fb
SG
1231 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
1232 for (i = 0; i < n; i++)
1233 iser_handle_wc(&wcs[i]);
6aabfa76 1234
6e6fe2fb
SG
1235 completed += n;
1236 if (completed >= iser_cq_poll_limit)
183cfa43 1237 break;
1cfa0a75 1238 }
6aabfa76
SG
1239
1240 /*
1241 * It is assumed here that arming CQ only once its empty
1242 * would not cause interrupts to be missed.
1243 */
1cfa0a75 1244 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
78ad0a34 1245
6aabfa76 1246 iser_dbg("got %d completions\n", completed);
1cfa0a75
OG
1247}
1248
1249static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
1250{
bf175540 1251 struct iser_comp *comp = cq_context;
1cfa0a75 1252
bf175540 1253 tasklet_schedule(&comp->tasklet);
1cfa0a75 1254}
0a7a08ad
SG
1255
1256u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
1257 enum iser_data_dir cmd_dir, sector_t *sector)
1258{
b130eded 1259 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
5190cc26 1260 struct iser_fr_desc *desc = reg->mem_h;
0a7a08ad
SG
1261 unsigned long sector_size = iser_task->sc->device->sector_size;
1262 struct ib_mr_status mr_status;
1263 int ret;
1264
d711d81d
SG
1265 if (desc && desc->pi_ctx->sig_protected) {
1266 desc->pi_ctx->sig_protected = 0;
0a7a08ad
SG
1267 ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
1268 IB_MR_CHECK_SIG_STATUS, &mr_status);
1269 if (ret) {
1270 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1271 goto err;
1272 }
1273
1274 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1275 sector_t sector_off = mr_status.sig_err.sig_err_offset;
1276
1277 do_div(sector_off, sector_size + 8);
1278 *sector = scsi_get_lba(iser_task->sc) + sector_off;
1279
39c978cd 1280 pr_err("PI error found type %d at sector %llx "
0a7a08ad 1281 "expected %x vs actual %x\n",
39c978cd
RD
1282 mr_status.sig_err.err_type,
1283 (unsigned long long)*sector,
0a7a08ad
SG
1284 mr_status.sig_err.expected,
1285 mr_status.sig_err.actual);
1286
1287 switch (mr_status.sig_err.err_type) {
1288 case IB_SIG_BAD_GUARD:
1289 return 0x1;
1290 case IB_SIG_BAD_REFTAG:
1291 return 0x3;
1292 case IB_SIG_BAD_APPTAG:
1293 return 0x2;
1294 }
1295 }
1296 }
1297
1298 return 0;
1299err:
1300 /* Not alot we can do here, return ambiguous guard error */
1301 return 0x1;
1302}