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