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