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