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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
39 #include "iscsi_iser.h"
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 + \
47 static int iser_cq_poll_limit
= 512;
49 static void iser_cq_tasklet_fn(unsigned long data
);
50 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
);
52 static void iser_cq_event_callback(struct ib_event
*cause
, void *context
)
54 iser_err("cq event %s (%d)\n",
55 ib_event_msg(cause
->event
), cause
->event
);
58 static void iser_qp_event_callback(struct ib_event
*cause
, void *context
)
60 iser_err("qp event %s (%d)\n",
61 ib_event_msg(cause
->event
), cause
->event
);
64 static void iser_event_handler(struct ib_event_handler
*handler
,
65 struct ib_event
*event
)
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
);
73 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
74 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
77 * returns 0 on success, -1 on failure
79 static int iser_create_device_ib_res(struct iser_device
*device
)
81 struct ib_device_attr
*dev_attr
= &device
->dev_attr
;
84 ret
= ib_query_device(device
->ib_device
, dev_attr
);
86 pr_warn("Query device failed for %s\n", device
->ib_device
->name
);
90 ret
= iser_assign_reg_ops(device
);
94 device
->comps_used
= min_t(int, num_online_cpus(),
95 device
->ib_device
->num_comp_vectors
);
97 device
->comps
= kcalloc(device
->comps_used
, sizeof(*device
->comps
),
102 max_cqe
= min(ISER_MAX_CQ_LEN
, dev_attr
->max_cqe
);
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
);
108 device
->pd
= ib_alloc_pd(device
->ib_device
);
109 if (IS_ERR(device
->pd
))
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
];
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
,
121 iser_cq_event_callback
,
124 if (IS_ERR(comp
->cq
)) {
129 if (ib_req_notify_cq(comp
->cq
, IB_CQ_NEXT_COMP
))
132 tasklet_init(&comp
->tasklet
, iser_cq_tasklet_fn
,
133 (unsigned long)comp
);
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
))
142 INIT_IB_EVENT_HANDLER(&device
->event_handler
, device
->ib_device
,
144 if (ib_register_event_handler(&device
->event_handler
))
150 ib_dereg_mr(device
->mr
);
152 for (i
= 0; i
< device
->comps_used
; i
++)
153 tasklet_kill(&device
->comps
[i
].tasklet
);
155 for (i
= 0; i
< device
->comps_used
; i
++) {
156 struct iser_comp
*comp
= &device
->comps
[i
];
159 ib_destroy_cq(comp
->cq
);
161 ib_dealloc_pd(device
->pd
);
163 kfree(device
->comps
);
165 iser_err("failed to allocate an IB resource\n");
170 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
171 * CQ and PD created with the device associated with the adapator.
173 static void iser_free_device_ib_res(struct iser_device
*device
)
176 BUG_ON(device
->mr
== NULL
);
178 for (i
= 0; i
< device
->comps_used
; i
++) {
179 struct iser_comp
*comp
= &device
->comps
[i
];
181 tasklet_kill(&comp
->tasklet
);
182 ib_destroy_cq(comp
->cq
);
186 (void)ib_unregister_event_handler(&device
->event_handler
);
187 (void)ib_dereg_mr(device
->mr
);
188 (void)ib_dealloc_pd(device
->pd
);
190 kfree(device
->comps
);
191 device
->comps
= NULL
;
198 * iser_alloc_fmr_pool - Creates FMR pool and page_vector
200 * returns 0 on success, or errno code on failure
202 int iser_alloc_fmr_pool(struct ib_conn
*ib_conn
,
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
;
214 INIT_LIST_HEAD(&fr_pool
->list
);
215 spin_lock_init(&fr_pool
->lock
);
217 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
221 page_vec
= kmalloc(sizeof(*page_vec
) + (sizeof(u64
) * size
),
228 page_vec
->pages
= (u64
*)(page_vec
+ 1);
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
;
237 params
.flush_function
= NULL
;
238 params
.access
= (IB_ACCESS_LOCAL_WRITE
|
239 IB_ACCESS_REMOTE_WRITE
|
240 IB_ACCESS_REMOTE_READ
);
242 fmr_pool
= ib_create_fmr_pool(device
->pd
, ¶ms
);
243 if (IS_ERR(fmr_pool
)) {
244 ret
= PTR_ERR(fmr_pool
);
245 iser_err("FMR allocation failed, err %d\n", ret
);
249 desc
->rsc
.page_vec
= page_vec
;
250 desc
->rsc
.fmr_pool
= fmr_pool
;
251 list_add(&desc
->list
, &fr_pool
->list
);
264 * iser_free_fmr_pool - releases the FMR pool and page vec
266 void iser_free_fmr_pool(struct ib_conn
*ib_conn
)
268 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
269 struct iser_fr_desc
*desc
;
271 desc
= list_first_entry(&fr_pool
->list
,
272 struct iser_fr_desc
, list
);
273 list_del(&desc
->list
);
275 iser_info("freeing conn %p fmr pool %p\n",
276 ib_conn
, desc
->rsc
.fmr_pool
);
278 ib_destroy_fmr_pool(desc
->rsc
.fmr_pool
);
279 kfree(desc
->rsc
.page_vec
);
284 iser_alloc_reg_res(struct ib_device
*ib_device
,
286 struct iser_reg_resources
*res
,
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",
296 return PTR_ERR(res
->frpl
);
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
;
310 ib_free_fast_reg_page_list(res
->frpl
);
316 iser_free_reg_res(struct iser_reg_resources
*rsc
)
318 ib_dereg_mr(rsc
->mr
);
319 ib_free_fast_reg_page_list(rsc
->frpl
);
323 iser_alloc_pi_ctx(struct ib_device
*ib_device
,
325 struct iser_fr_desc
*desc
,
328 struct iser_pi_context
*pi_ctx
= NULL
;
331 desc
->pi_ctx
= kzalloc(sizeof(*desc
->pi_ctx
), GFP_KERNEL
);
335 pi_ctx
= desc
->pi_ctx
;
337 ret
= iser_alloc_reg_res(ib_device
, pd
, &pi_ctx
->rsc
, size
);
339 iser_err("failed to allocate reg_resources\n");
340 goto alloc_reg_res_err
;
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
);
348 pi_ctx
->sig_mr_valid
= 1;
349 desc
->pi_ctx
->sig_protected
= 0;
354 iser_free_reg_res(&pi_ctx
->rsc
);
362 iser_free_pi_ctx(struct iser_pi_context
*pi_ctx
)
364 iser_free_reg_res(&pi_ctx
->rsc
);
365 ib_dereg_mr(pi_ctx
->sig_mr
);
369 static struct iser_fr_desc
*
370 iser_create_fastreg_desc(struct ib_device
*ib_device
,
375 struct iser_fr_desc
*desc
;
378 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
380 return ERR_PTR(-ENOMEM
);
382 ret
= iser_alloc_reg_res(ib_device
, pd
, &desc
->rsc
, size
);
384 goto reg_res_alloc_failure
;
387 ret
= iser_alloc_pi_ctx(ib_device
, pd
, desc
, size
);
389 goto pi_ctx_alloc_failure
;
394 pi_ctx_alloc_failure
:
395 iser_free_reg_res(&desc
->rsc
);
396 reg_res_alloc_failure
:
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
407 int iser_alloc_fastreg_pool(struct ib_conn
*ib_conn
,
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
;
416 INIT_LIST_HEAD(&fr_pool
->list
);
417 spin_lock_init(&fr_pool
->lock
);
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
);
427 list_add_tail(&desc
->list
, &fr_pool
->list
);
434 iser_free_fastreg_pool(ib_conn
);
439 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
441 void iser_free_fastreg_pool(struct ib_conn
*ib_conn
)
443 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
444 struct iser_fr_desc
*desc
, *tmp
;
447 if (list_empty(&fr_pool
->list
))
450 iser_info("freeing conn %p fr pool\n", ib_conn
);
452 list_for_each_entry_safe(desc
, tmp
, &fr_pool
->list
, list
) {
453 list_del(&desc
->list
);
454 iser_free_reg_res(&desc
->rsc
);
456 iser_free_pi_ctx(desc
->pi_ctx
);
461 if (i
< fr_pool
->size
)
462 iser_warn("pool still has %d regions registered\n",
467 * iser_create_ib_conn_res - Queue-Pair (QP)
469 * returns 0 on success, -1 on failure
471 static int iser_create_ib_conn_res(struct ib_conn
*ib_conn
)
473 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
475 struct iser_device
*device
;
476 struct ib_device_attr
*dev_attr
;
477 struct ib_qp_init_attr init_attr
;
479 int index
, min_index
= 0;
481 BUG_ON(ib_conn
->device
== NULL
);
483 device
= ib_conn
->device
;
484 dev_attr
= &device
->dev_attr
;
486 memset(&init_attr
, 0, sizeof init_attr
);
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
)
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
);
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
);
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
);
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
);
528 ret
= rdma_create_qp(ib_conn
->cma_id
, device
->pd
, &init_attr
);
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
);
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
);
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.
552 struct iser_device
*iser_device_find_by_ib_device(struct rdma_cm_id
*cma_id
)
554 struct iser_device
*device
;
556 mutex_lock(&ig
.device_list_mutex
);
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
)
563 device
= kzalloc(sizeof *device
, GFP_KERNEL
);
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
)) {
575 list_add(&device
->ig_list
, &ig
.device_list
);
580 mutex_unlock(&ig
.device_list_mutex
);
584 /* if there's no demand for this device, release it */
585 static void iser_device_try_release(struct iser_device
*device
)
587 mutex_lock(&ig
.device_list_mutex
);
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
);
595 mutex_unlock(&ig
.device_list_mutex
);
599 * Called with state mutex held
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
)
607 ret
= (iser_conn
->state
== comp
);
609 iser_conn
->state
= exch
;
614 void iser_release_work(struct work_struct
*work
)
616 struct iser_conn
*iser_conn
;
618 iser_conn
= container_of(work
, struct iser_conn
, release_work
);
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
);
625 mutex_lock(&iser_conn
->state_mutex
);
626 iser_conn
->state
= ISER_CONN_DOWN
;
627 mutex_unlock(&iser_conn
->state_mutex
);
629 iser_conn_release(iser_conn
);
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).
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.
643 static void iser_free_ib_conn_res(struct iser_conn
*iser_conn
,
646 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
647 struct iser_device
*device
= ib_conn
->device
;
649 iser_info("freeing conn %p cma_id %p qp %p\n",
650 iser_conn
, ib_conn
->cma_id
, ib_conn
->qp
);
652 if (ib_conn
->qp
!= NULL
) {
653 ib_conn
->comp
->active_qps
--;
654 rdma_destroy_qp(ib_conn
->cma_id
);
659 if (iser_conn
->rx_descs
)
660 iser_free_rx_descriptors(iser_conn
);
662 if (device
!= NULL
) {
663 iser_device_try_release(device
);
664 ib_conn
->device
= NULL
;
670 * Frees all conn objects and deallocs conn descriptor
672 void iser_conn_release(struct iser_conn
*iser_conn
)
674 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
676 mutex_lock(&ig
.connlist_mutex
);
677 list_del(&iser_conn
->conn_list
);
678 mutex_unlock(&ig
.connlist_mutex
);
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
;
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).
692 iser_free_ib_conn_res(iser_conn
, true);
693 mutex_unlock(&iser_conn
->state_mutex
);
695 if (ib_conn
->cma_id
!= NULL
) {
696 rdma_destroy_id(ib_conn
->cma_id
);
697 ib_conn
->cma_id
= NULL
;
704 * triggers start of the disconnect procedures and wait for them to be done
705 * Called with state mutex held
707 int iser_conn_terminate(struct iser_conn
*iser_conn
)
709 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
710 struct ib_send_wr
*bad_wr
;
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
))
718 iser_info("iser_conn %p state %d\n", iser_conn
, iser_conn
->state
);
720 /* suspend queuing of new iscsi commands */
721 if (iser_conn
->iscsi_conn
)
722 iscsi_suspend_queue(iser_conn
->iscsi_conn
);
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
729 if (ib_conn
->cma_id
) {
730 err
= rdma_disconnect(ib_conn
->cma_id
);
732 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
735 /* post an indication that all flush errors were consumed */
736 err
= ib_post_send(ib_conn
->qp
, &ib_conn
->beacon
, &bad_wr
);
738 iser_err("conn %p failed to post beacon", ib_conn
);
742 wait_for_completion(&ib_conn
->flush_comp
);
749 * Called with state mutex held
751 static void iser_connect_error(struct rdma_cm_id
*cma_id
)
753 struct iser_conn
*iser_conn
;
755 iser_conn
= (struct iser_conn
*)cma_id
->context
;
756 iser_conn
->state
= ISER_CONN_TERMINATING
;
760 iser_calc_scsi_params(struct iser_conn
*iser_conn
,
761 unsigned int max_sectors
)
763 struct iser_device
*device
= iser_conn
->ib_conn
.device
;
764 unsigned short sg_tablesize
, sup_sg_tablesize
;
766 sg_tablesize
= DIV_ROUND_UP(max_sectors
* 512, SIZE_4K
);
767 sup_sg_tablesize
= min_t(unsigned, ISCSI_ISER_MAX_SG_TABLESIZE
,
768 device
->dev_attr
.max_fast_reg_page_list_len
);
770 if (sg_tablesize
> sup_sg_tablesize
) {
771 sg_tablesize
= sup_sg_tablesize
;
772 iser_conn
->scsi_max_sectors
= sg_tablesize
* SIZE_4K
/ 512;
774 iser_conn
->scsi_max_sectors
= max_sectors
;
777 iser_conn
->scsi_sg_tablesize
= sg_tablesize
;
779 iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
780 iser_conn
, iser_conn
->scsi_sg_tablesize
,
781 iser_conn
->scsi_max_sectors
);
785 * Called with state mutex held
787 static void iser_addr_handler(struct rdma_cm_id
*cma_id
)
789 struct iser_device
*device
;
790 struct iser_conn
*iser_conn
;
791 struct ib_conn
*ib_conn
;
794 iser_conn
= (struct iser_conn
*)cma_id
->context
;
795 if (iser_conn
->state
!= ISER_CONN_PENDING
)
799 ib_conn
= &iser_conn
->ib_conn
;
800 device
= iser_device_find_by_ib_device(cma_id
);
802 iser_err("device lookup/creation failed\n");
803 iser_connect_error(cma_id
);
807 ib_conn
->device
= device
;
809 /* connection T10-PI support */
810 if (iser_pi_enable
) {
811 if (!(device
->dev_attr
.device_cap_flags
&
812 IB_DEVICE_SIGNATURE_HANDOVER
)) {
813 iser_warn("T10-PI requested but not supported on %s, "
814 "continue without T10-PI\n",
815 ib_conn
->device
->ib_device
->name
);
816 ib_conn
->pi_support
= false;
818 ib_conn
->pi_support
= true;
822 iser_calc_scsi_params(iser_conn
, iser_max_sectors
);
824 ret
= rdma_resolve_route(cma_id
, 1000);
826 iser_err("resolve route failed: %d\n", ret
);
827 iser_connect_error(cma_id
);
833 * Called with state mutex held
835 static void iser_route_handler(struct rdma_cm_id
*cma_id
)
837 struct rdma_conn_param conn_param
;
839 struct iser_cm_hdr req_hdr
;
840 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
841 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
842 struct iser_device
*device
= ib_conn
->device
;
844 if (iser_conn
->state
!= ISER_CONN_PENDING
)
848 ret
= iser_create_ib_conn_res(ib_conn
);
852 memset(&conn_param
, 0, sizeof conn_param
);
853 conn_param
.responder_resources
= device
->dev_attr
.max_qp_rd_atom
;
854 conn_param
.initiator_depth
= 1;
855 conn_param
.retry_count
= 7;
856 conn_param
.rnr_retry_count
= 6;
858 memset(&req_hdr
, 0, sizeof(req_hdr
));
859 req_hdr
.flags
= (ISER_ZBVA_NOT_SUPPORTED
|
860 ISER_SEND_W_INV_NOT_SUPPORTED
);
861 conn_param
.private_data
= (void *)&req_hdr
;
862 conn_param
.private_data_len
= sizeof(struct iser_cm_hdr
);
864 ret
= rdma_connect(cma_id
, &conn_param
);
866 iser_err("failure connecting: %d\n", ret
);
872 iser_connect_error(cma_id
);
875 static void iser_connected_handler(struct rdma_cm_id
*cma_id
)
877 struct iser_conn
*iser_conn
;
878 struct ib_qp_attr attr
;
879 struct ib_qp_init_attr init_attr
;
881 iser_conn
= (struct iser_conn
*)cma_id
->context
;
882 if (iser_conn
->state
!= ISER_CONN_PENDING
)
886 (void)ib_query_qp(cma_id
->qp
, &attr
, ~0, &init_attr
);
887 iser_info("remote qpn:%x my qpn:%x\n", attr
.dest_qp_num
, cma_id
->qp
->qp_num
);
889 iser_conn
->state
= ISER_CONN_UP
;
890 complete(&iser_conn
->up_completion
);
893 static void iser_disconnected_handler(struct rdma_cm_id
*cma_id
)
895 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
897 if (iser_conn_terminate(iser_conn
)) {
898 if (iser_conn
->iscsi_conn
)
899 iscsi_conn_failure(iser_conn
->iscsi_conn
,
900 ISCSI_ERR_CONN_FAILED
);
902 iser_err("iscsi_iser connection isn't bound\n");
906 static void iser_cleanup_handler(struct rdma_cm_id
*cma_id
,
909 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
912 * We are not guaranteed that we visited disconnected_handler
913 * by now, call it here to be safe that we handle CM drep
916 iser_disconnected_handler(cma_id
);
917 iser_free_ib_conn_res(iser_conn
, destroy
);
918 complete(&iser_conn
->ib_completion
);
921 static int iser_cma_handler(struct rdma_cm_id
*cma_id
, struct rdma_cm_event
*event
)
923 struct iser_conn
*iser_conn
;
926 iser_conn
= (struct iser_conn
*)cma_id
->context
;
927 iser_info("%s (%d): status %d conn %p id %p\n",
928 rdma_event_msg(event
->event
), event
->event
,
929 event
->status
, cma_id
->context
, cma_id
);
931 mutex_lock(&iser_conn
->state_mutex
);
932 switch (event
->event
) {
933 case RDMA_CM_EVENT_ADDR_RESOLVED
:
934 iser_addr_handler(cma_id
);
936 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
937 iser_route_handler(cma_id
);
939 case RDMA_CM_EVENT_ESTABLISHED
:
940 iser_connected_handler(cma_id
);
942 case RDMA_CM_EVENT_ADDR_ERROR
:
943 case RDMA_CM_EVENT_ROUTE_ERROR
:
944 case RDMA_CM_EVENT_CONNECT_ERROR
:
945 case RDMA_CM_EVENT_UNREACHABLE
:
946 case RDMA_CM_EVENT_REJECTED
:
947 iser_connect_error(cma_id
);
949 case RDMA_CM_EVENT_DISCONNECTED
:
950 case RDMA_CM_EVENT_ADDR_CHANGE
:
951 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
952 iser_cleanup_handler(cma_id
, false);
954 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
956 * we *must* destroy the device as we cannot rely
957 * on iscsid to be around to initiate error handling.
958 * also if we are not in state DOWN implicitly destroy
961 iser_cleanup_handler(cma_id
, true);
962 if (iser_conn
->state
!= ISER_CONN_DOWN
) {
963 iser_conn
->ib_conn
.cma_id
= NULL
;
968 iser_err("Unexpected RDMA CM event: %s (%d)\n",
969 rdma_event_msg(event
->event
), event
->event
);
972 mutex_unlock(&iser_conn
->state_mutex
);
977 void iser_conn_init(struct iser_conn
*iser_conn
)
979 iser_conn
->state
= ISER_CONN_INIT
;
980 iser_conn
->ib_conn
.post_recv_buf_count
= 0;
981 init_completion(&iser_conn
->ib_conn
.flush_comp
);
982 init_completion(&iser_conn
->stop_completion
);
983 init_completion(&iser_conn
->ib_completion
);
984 init_completion(&iser_conn
->up_completion
);
985 INIT_LIST_HEAD(&iser_conn
->conn_list
);
986 mutex_init(&iser_conn
->state_mutex
);
990 * starts the process of connecting to the target
991 * sleeps until the connection is established or rejected
993 int iser_connect(struct iser_conn
*iser_conn
,
994 struct sockaddr
*src_addr
,
995 struct sockaddr
*dst_addr
,
998 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1001 mutex_lock(&iser_conn
->state_mutex
);
1003 sprintf(iser_conn
->name
, "%pISp", dst_addr
);
1005 iser_info("connecting to: %s\n", iser_conn
->name
);
1007 /* the device is known only --after-- address resolution */
1008 ib_conn
->device
= NULL
;
1010 iser_conn
->state
= ISER_CONN_PENDING
;
1012 ib_conn
->beacon
.wr_id
= ISER_BEACON_WRID
;
1013 ib_conn
->beacon
.opcode
= IB_WR_SEND
;
1015 ib_conn
->cma_id
= rdma_create_id(iser_cma_handler
,
1017 RDMA_PS_TCP
, IB_QPT_RC
);
1018 if (IS_ERR(ib_conn
->cma_id
)) {
1019 err
= PTR_ERR(ib_conn
->cma_id
);
1020 iser_err("rdma_create_id failed: %d\n", err
);
1024 err
= rdma_resolve_addr(ib_conn
->cma_id
, src_addr
, dst_addr
, 1000);
1026 iser_err("rdma_resolve_addr failed: %d\n", err
);
1030 if (!non_blocking
) {
1031 wait_for_completion_interruptible(&iser_conn
->up_completion
);
1033 if (iser_conn
->state
!= ISER_CONN_UP
) {
1035 goto connect_failure
;
1038 mutex_unlock(&iser_conn
->state_mutex
);
1040 mutex_lock(&ig
.connlist_mutex
);
1041 list_add(&iser_conn
->conn_list
, &ig
.connlist
);
1042 mutex_unlock(&ig
.connlist_mutex
);
1046 ib_conn
->cma_id
= NULL
;
1048 iser_conn
->state
= ISER_CONN_DOWN
;
1050 mutex_unlock(&iser_conn
->state_mutex
);
1051 iser_conn_release(iser_conn
);
1055 int iser_post_recvl(struct iser_conn
*iser_conn
)
1057 struct ib_recv_wr rx_wr
, *rx_wr_failed
;
1058 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1062 sge
.addr
= iser_conn
->login_resp_dma
;
1063 sge
.length
= ISER_RX_LOGIN_SIZE
;
1064 sge
.lkey
= ib_conn
->device
->pd
->local_dma_lkey
;
1066 rx_wr
.wr_id
= (uintptr_t)iser_conn
->login_resp_buf
;
1067 rx_wr
.sg_list
= &sge
;
1071 ib_conn
->post_recv_buf_count
++;
1072 ib_ret
= ib_post_recv(ib_conn
->qp
, &rx_wr
, &rx_wr_failed
);
1074 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1075 ib_conn
->post_recv_buf_count
--;
1080 int iser_post_recvm(struct iser_conn
*iser_conn
, int count
)
1082 struct ib_recv_wr
*rx_wr
, *rx_wr_failed
;
1084 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1085 unsigned int my_rx_head
= iser_conn
->rx_desc_head
;
1086 struct iser_rx_desc
*rx_desc
;
1088 for (rx_wr
= ib_conn
->rx_wr
, i
= 0; i
< count
; i
++, rx_wr
++) {
1089 rx_desc
= &iser_conn
->rx_descs
[my_rx_head
];
1090 rx_wr
->wr_id
= (uintptr_t)rx_desc
;
1091 rx_wr
->sg_list
= &rx_desc
->rx_sg
;
1093 rx_wr
->next
= rx_wr
+ 1;
1094 my_rx_head
= (my_rx_head
+ 1) & iser_conn
->qp_max_recv_dtos_mask
;
1098 rx_wr
->next
= NULL
; /* mark end of work requests list */
1100 ib_conn
->post_recv_buf_count
+= count
;
1101 ib_ret
= ib_post_recv(ib_conn
->qp
, ib_conn
->rx_wr
, &rx_wr_failed
);
1103 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1104 ib_conn
->post_recv_buf_count
-= count
;
1106 iser_conn
->rx_desc_head
= my_rx_head
;
1112 * iser_start_send - Initiate a Send DTO operation
1114 * returns 0 on success, -1 on failure
1116 int iser_post_send(struct ib_conn
*ib_conn
, struct iser_tx_desc
*tx_desc
,
1119 struct ib_send_wr
*bad_wr
, *wr
= iser_tx_next_wr(tx_desc
);
1122 ib_dma_sync_single_for_device(ib_conn
->device
->ib_device
,
1123 tx_desc
->dma_addr
, ISER_HEADERS_LEN
,
1127 wr
->wr_id
= (uintptr_t)tx_desc
;
1128 wr
->sg_list
= tx_desc
->tx_sg
;
1129 wr
->num_sge
= tx_desc
->num_sge
;
1130 wr
->opcode
= IB_WR_SEND
;
1131 wr
->send_flags
= signal
? IB_SEND_SIGNALED
: 0;
1133 ib_ret
= ib_post_send(ib_conn
->qp
, &tx_desc
->wrs
[0], &bad_wr
);
1135 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
1136 ib_ret
, bad_wr
->opcode
);
1142 * is_iser_tx_desc - Indicate if the completion wr_id
1143 * is a TX descriptor or not.
1144 * @iser_conn: iser connection
1145 * @wr_id: completion WR identifier
1147 * Since we cannot rely on wc opcode in FLUSH errors
1148 * we must work around it by checking if the wr_id address
1149 * falls in the iser connection rx_descs buffer. If so
1150 * it is an RX descriptor, otherwize it is a TX.
1153 is_iser_tx_desc(struct iser_conn
*iser_conn
, void *wr_id
)
1155 void *start
= iser_conn
->rx_descs
;
1156 int len
= iser_conn
->num_rx_descs
* sizeof(*iser_conn
->rx_descs
);
1158 if (wr_id
>= start
&& wr_id
< start
+ len
)
1165 * iser_handle_comp_error() - Handle error completion
1166 * @ib_conn: connection RDMA resources
1167 * @wc: work completion
1169 * Notes: We may handle a FLUSH error completion and in this case
1170 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1171 * error completion we should also notify iscsi layer that
1172 * connection is failed (in case we passed bind stage).
1175 iser_handle_comp_error(struct ib_conn
*ib_conn
,
1178 void *wr_id
= (void *)(uintptr_t)wc
->wr_id
;
1179 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
1182 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1183 if (iser_conn
->iscsi_conn
)
1184 iscsi_conn_failure(iser_conn
->iscsi_conn
,
1185 ISCSI_ERR_CONN_FAILED
);
1187 if (wc
->wr_id
== ISER_FASTREG_LI_WRID
)
1190 if (is_iser_tx_desc(iser_conn
, wr_id
)) {
1191 struct iser_tx_desc
*desc
= wr_id
;
1193 if (desc
->type
== ISCSI_TX_DATAOUT
)
1194 kmem_cache_free(ig
.desc_cache
, desc
);
1196 ib_conn
->post_recv_buf_count
--;
1201 * iser_handle_wc - handle a single work completion
1202 * @wc: work completion
1204 * Soft-IRQ context, work completion can be either
1205 * SEND or RECV, and can turn out successful or
1206 * with error (or flush error).
1208 static void iser_handle_wc(struct ib_wc
*wc
)
1210 struct ib_conn
*ib_conn
;
1211 struct iser_tx_desc
*tx_desc
;
1212 struct iser_rx_desc
*rx_desc
;
1214 ib_conn
= wc
->qp
->qp_context
;
1215 if (likely(wc
->status
== IB_WC_SUCCESS
)) {
1216 if (wc
->opcode
== IB_WC_RECV
) {
1217 rx_desc
= (struct iser_rx_desc
*)(uintptr_t)wc
->wr_id
;
1218 iser_rcv_completion(rx_desc
, wc
->byte_len
,
1221 if (wc
->opcode
== IB_WC_SEND
) {
1222 tx_desc
= (struct iser_tx_desc
*)(uintptr_t)wc
->wr_id
;
1223 iser_snd_completion(tx_desc
, ib_conn
);
1225 iser_err("Unknown wc opcode %d\n", wc
->opcode
);
1228 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1229 iser_err("%s (%d): wr id %llx vend_err %x\n",
1230 ib_wc_status_msg(wc
->status
), wc
->status
,
1231 wc
->wr_id
, wc
->vendor_err
);
1233 iser_dbg("%s (%d): wr id %llx\n",
1234 ib_wc_status_msg(wc
->status
), wc
->status
,
1237 if (wc
->wr_id
== ISER_BEACON_WRID
)
1238 /* all flush errors were consumed */
1239 complete(&ib_conn
->flush_comp
);
1241 iser_handle_comp_error(ib_conn
, wc
);
1246 * iser_cq_tasklet_fn - iSER completion polling loop
1247 * @data: iSER completion context
1249 * Soft-IRQ context, polling connection CQ until
1250 * either CQ was empty or we exausted polling budget
1252 static void iser_cq_tasklet_fn(unsigned long data
)
1254 struct iser_comp
*comp
= (struct iser_comp
*)data
;
1255 struct ib_cq
*cq
= comp
->cq
;
1256 struct ib_wc
*const wcs
= comp
->wcs
;
1257 int i
, n
, completed
= 0;
1259 while ((n
= ib_poll_cq(cq
, ARRAY_SIZE(comp
->wcs
), wcs
)) > 0) {
1260 for (i
= 0; i
< n
; i
++)
1261 iser_handle_wc(&wcs
[i
]);
1264 if (completed
>= iser_cq_poll_limit
)
1269 * It is assumed here that arming CQ only once its empty
1270 * would not cause interrupts to be missed.
1272 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1274 iser_dbg("got %d completions\n", completed
);
1277 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
)
1279 struct iser_comp
*comp
= cq_context
;
1281 tasklet_schedule(&comp
->tasklet
);
1284 u8
iser_check_task_pi_status(struct iscsi_iser_task
*iser_task
,
1285 enum iser_data_dir cmd_dir
, sector_t
*sector
)
1287 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
1288 struct iser_fr_desc
*desc
= reg
->mem_h
;
1289 unsigned long sector_size
= iser_task
->sc
->device
->sector_size
;
1290 struct ib_mr_status mr_status
;
1293 if (desc
&& desc
->pi_ctx
->sig_protected
) {
1294 desc
->pi_ctx
->sig_protected
= 0;
1295 ret
= ib_check_mr_status(desc
->pi_ctx
->sig_mr
,
1296 IB_MR_CHECK_SIG_STATUS
, &mr_status
);
1298 pr_err("ib_check_mr_status failed, ret %d\n", ret
);
1302 if (mr_status
.fail_status
& IB_MR_CHECK_SIG_STATUS
) {
1303 sector_t sector_off
= mr_status
.sig_err
.sig_err_offset
;
1305 do_div(sector_off
, sector_size
+ 8);
1306 *sector
= scsi_get_lba(iser_task
->sc
) + sector_off
;
1308 pr_err("PI error found type %d at sector %llx "
1309 "expected %x vs actual %x\n",
1310 mr_status
.sig_err
.err_type
,
1311 (unsigned long long)*sector
,
1312 mr_status
.sig_err
.expected
,
1313 mr_status
.sig_err
.actual
);
1315 switch (mr_status
.sig_err
.err_type
) {
1316 case IB_SIG_BAD_GUARD
:
1318 case IB_SIG_BAD_REFTAG
:
1320 case IB_SIG_BAD_APPTAG
:
1328 /* Not alot we can do here, return ambiguous guard error */