OSDN Git Service

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