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