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