OSDN Git Service

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