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