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