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 /* Assign function handles - based on FMR support */
91 if (device
->ib_device
->alloc_fmr
&& device
->ib_device
->dealloc_fmr
&&
92 device
->ib_device
->map_phys_fmr
&& device
->ib_device
->unmap_fmr
) {
93 iser_info("FMR supported, using FMR for registration\n");
94 device
->iser_alloc_rdma_reg_res
= iser_create_fmr_pool
;
95 device
->iser_free_rdma_reg_res
= iser_free_fmr_pool
;
96 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fmr
;
97 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fmr
;
99 if (dev_attr
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
100 iser_info("FastReg supported, using FastReg for registration\n");
101 device
->iser_alloc_rdma_reg_res
= iser_create_fastreg_pool
;
102 device
->iser_free_rdma_reg_res
= iser_free_fastreg_pool
;
103 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fastreg
;
104 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fastreg
;
106 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
110 device
->comps_used
= min_t(int, num_online_cpus(),
111 device
->ib_device
->num_comp_vectors
);
113 device
->comps
= kcalloc(device
->comps_used
, sizeof(*device
->comps
),
118 max_cqe
= min(ISER_MAX_CQ_LEN
, dev_attr
->max_cqe
);
120 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n",
121 device
->comps_used
, device
->ib_device
->name
,
122 device
->ib_device
->num_comp_vectors
, max_cqe
);
124 device
->pd
= ib_alloc_pd(device
->ib_device
);
125 if (IS_ERR(device
->pd
))
128 for (i
= 0; i
< device
->comps_used
; i
++) {
129 struct ib_cq_init_attr cq_attr
= {};
130 struct iser_comp
*comp
= &device
->comps
[i
];
132 comp
->device
= device
;
133 cq_attr
.cqe
= max_cqe
;
134 cq_attr
.comp_vector
= i
;
135 comp
->cq
= ib_create_cq(device
->ib_device
,
137 iser_cq_event_callback
,
140 if (IS_ERR(comp
->cq
)) {
145 if (ib_req_notify_cq(comp
->cq
, IB_CQ_NEXT_COMP
))
148 tasklet_init(&comp
->tasklet
, iser_cq_tasklet_fn
,
149 (unsigned long)comp
);
152 device
->mr
= ib_get_dma_mr(device
->pd
, IB_ACCESS_LOCAL_WRITE
|
153 IB_ACCESS_REMOTE_WRITE
|
154 IB_ACCESS_REMOTE_READ
);
155 if (IS_ERR(device
->mr
))
158 INIT_IB_EVENT_HANDLER(&device
->event_handler
, device
->ib_device
,
160 if (ib_register_event_handler(&device
->event_handler
))
166 ib_dereg_mr(device
->mr
);
168 for (i
= 0; i
< device
->comps_used
; i
++)
169 tasklet_kill(&device
->comps
[i
].tasklet
);
171 for (i
= 0; i
< device
->comps_used
; i
++) {
172 struct iser_comp
*comp
= &device
->comps
[i
];
175 ib_destroy_cq(comp
->cq
);
177 ib_dealloc_pd(device
->pd
);
179 kfree(device
->comps
);
181 iser_err("failed to allocate an IB resource\n");
186 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
187 * CQ and PD created with the device associated with the adapator.
189 static void iser_free_device_ib_res(struct iser_device
*device
)
192 BUG_ON(device
->mr
== NULL
);
194 for (i
= 0; i
< device
->comps_used
; i
++) {
195 struct iser_comp
*comp
= &device
->comps
[i
];
197 tasklet_kill(&comp
->tasklet
);
198 ib_destroy_cq(comp
->cq
);
202 (void)ib_unregister_event_handler(&device
->event_handler
);
203 (void)ib_dereg_mr(device
->mr
);
204 (void)ib_dealloc_pd(device
->pd
);
206 kfree(device
->comps
);
207 device
->comps
= NULL
;
214 * iser_create_fmr_pool - Creates FMR pool and page_vector
216 * returns 0 on success, or errno code on failure
218 int iser_create_fmr_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
220 struct iser_device
*device
= ib_conn
->device
;
221 struct ib_fmr_pool_param params
;
224 ib_conn
->fmr
.page_vec
= kmalloc(sizeof(*ib_conn
->fmr
.page_vec
) +
225 (sizeof(u64
)*(ISCSI_ISER_SG_TABLESIZE
+ 1)),
227 if (!ib_conn
->fmr
.page_vec
)
230 ib_conn
->fmr
.page_vec
->pages
= (u64
*)(ib_conn
->fmr
.page_vec
+ 1);
232 params
.page_shift
= SHIFT_4K
;
233 /* when the first/last SG element are not start/end *
234 * page aligned, the map whould be of N+1 pages */
235 params
.max_pages_per_fmr
= ISCSI_ISER_SG_TABLESIZE
+ 1;
236 /* make the pool size twice the max number of SCSI commands *
237 * the ML is expected to queue, watermark for unmap at 50% */
238 params
.pool_size
= cmds_max
* 2;
239 params
.dirty_watermark
= cmds_max
;
241 params
.flush_function
= NULL
;
242 params
.access
= (IB_ACCESS_LOCAL_WRITE
|
243 IB_ACCESS_REMOTE_WRITE
|
244 IB_ACCESS_REMOTE_READ
);
246 ib_conn
->fmr
.pool
= ib_create_fmr_pool(device
->pd
, ¶ms
);
247 if (IS_ERR(ib_conn
->fmr
.pool
)) {
248 ret
= PTR_ERR(ib_conn
->fmr
.pool
);
249 iser_err("FMR allocation failed, err %d\n", ret
);
256 kfree(ib_conn
->fmr
.page_vec
);
257 ib_conn
->fmr
.page_vec
= NULL
;
262 * iser_free_fmr_pool - releases the FMR pool and page vec
264 void iser_free_fmr_pool(struct ib_conn
*ib_conn
)
266 iser_info("freeing conn %p fmr pool %p\n",
267 ib_conn
, ib_conn
->fmr
.pool
);
269 ib_destroy_fmr_pool(ib_conn
->fmr
.pool
);
270 ib_conn
->fmr
.pool
= NULL
;
272 kfree(ib_conn
->fmr
.page_vec
);
273 ib_conn
->fmr
.page_vec
= NULL
;
277 iser_alloc_reg_res(struct ib_device
*ib_device
, struct ib_pd
*pd
,
278 struct iser_reg_resources
*res
)
282 res
->frpl
= ib_alloc_fast_reg_page_list(ib_device
,
283 ISCSI_ISER_SG_TABLESIZE
+ 1);
284 if (IS_ERR(res
->frpl
)) {
285 ret
= PTR_ERR(res
->frpl
);
286 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
288 return PTR_ERR(res
->frpl
);
291 res
->mr
= ib_alloc_mr(pd
, IB_MR_TYPE_MEM_REG
,
292 ISCSI_ISER_SG_TABLESIZE
+ 1);
293 if (IS_ERR(res
->mr
)) {
294 ret
= PTR_ERR(res
->mr
);
295 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret
);
296 goto fast_reg_mr_failure
;
303 ib_free_fast_reg_page_list(res
->frpl
);
309 iser_free_reg_res(struct iser_reg_resources
*rsc
)
311 ib_dereg_mr(rsc
->mr
);
312 ib_free_fast_reg_page_list(rsc
->frpl
);
316 iser_alloc_pi_ctx(struct ib_device
*ib_device
, struct ib_pd
*pd
,
317 struct iser_fr_desc
*desc
)
319 struct iser_pi_context
*pi_ctx
= NULL
;
322 desc
->pi_ctx
= kzalloc(sizeof(*desc
->pi_ctx
), GFP_KERNEL
);
326 pi_ctx
= desc
->pi_ctx
;
328 ret
= iser_alloc_reg_res(ib_device
, pd
, &pi_ctx
->rsc
);
330 iser_err("failed to allocate reg_resources\n");
331 goto alloc_reg_res_err
;
334 pi_ctx
->sig_mr
= ib_alloc_mr(pd
, IB_MR_TYPE_SIGNATURE
, 2);
335 if (IS_ERR(pi_ctx
->sig_mr
)) {
336 ret
= PTR_ERR(pi_ctx
->sig_mr
);
339 pi_ctx
->sig_mr_valid
= 1;
340 desc
->pi_ctx
->sig_protected
= 0;
345 iser_free_reg_res(&pi_ctx
->rsc
);
353 iser_free_pi_ctx(struct iser_pi_context
*pi_ctx
)
355 iser_free_reg_res(&pi_ctx
->rsc
);
356 ib_dereg_mr(pi_ctx
->sig_mr
);
361 iser_create_fastreg_desc(struct ib_device
*ib_device
, struct ib_pd
*pd
,
362 bool pi_enable
, struct iser_fr_desc
*desc
)
366 ret
= iser_alloc_reg_res(ib_device
, pd
, &desc
->rsc
);
368 iser_err("failed to allocate reg_resources\n");
373 ret
= iser_alloc_pi_ctx(ib_device
, pd
, desc
);
375 goto pi_ctx_alloc_failure
;
380 pi_ctx_alloc_failure
:
381 iser_free_reg_res(&desc
->rsc
);
387 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
388 * for fast registration work requests.
389 * returns 0 on success, or errno code on failure
391 int iser_create_fastreg_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
393 struct iser_device
*device
= ib_conn
->device
;
394 struct iser_fr_desc
*desc
;
397 INIT_LIST_HEAD(&ib_conn
->fastreg
.pool
);
398 ib_conn
->fastreg
.pool_size
= 0;
399 for (i
= 0; i
< cmds_max
; i
++) {
400 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
402 iser_err("Failed to allocate a new fast_reg descriptor\n");
407 ret
= iser_create_fastreg_desc(device
->ib_device
, device
->pd
,
408 ib_conn
->pi_support
, desc
);
410 iser_err("Failed to create fastreg descriptor err=%d\n",
416 list_add_tail(&desc
->list
, &ib_conn
->fastreg
.pool
);
417 ib_conn
->fastreg
.pool_size
++;
423 iser_free_fastreg_pool(ib_conn
);
428 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
430 void iser_free_fastreg_pool(struct ib_conn
*ib_conn
)
432 struct iser_fr_desc
*desc
, *tmp
;
435 if (list_empty(&ib_conn
->fastreg
.pool
))
438 iser_info("freeing conn %p fr pool\n", ib_conn
);
440 list_for_each_entry_safe(desc
, tmp
, &ib_conn
->fastreg
.pool
, list
) {
441 list_del(&desc
->list
);
442 iser_free_reg_res(&desc
->rsc
);
444 iser_free_pi_ctx(desc
->pi_ctx
);
449 if (i
< ib_conn
->fastreg
.pool_size
)
450 iser_warn("pool still has %d regions registered\n",
451 ib_conn
->fastreg
.pool_size
- i
);
455 * iser_create_ib_conn_res - Queue-Pair (QP)
457 * returns 0 on success, -1 on failure
459 static int iser_create_ib_conn_res(struct ib_conn
*ib_conn
)
461 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
463 struct iser_device
*device
;
464 struct ib_device_attr
*dev_attr
;
465 struct ib_qp_init_attr init_attr
;
467 int index
, min_index
= 0;
469 BUG_ON(ib_conn
->device
== NULL
);
471 device
= ib_conn
->device
;
472 dev_attr
= &device
->dev_attr
;
474 memset(&init_attr
, 0, sizeof init_attr
);
476 mutex_lock(&ig
.connlist_mutex
);
477 /* select the CQ with the minimal number of usages */
478 for (index
= 0; index
< device
->comps_used
; index
++) {
479 if (device
->comps
[index
].active_qps
<
480 device
->comps
[min_index
].active_qps
)
483 ib_conn
->comp
= &device
->comps
[min_index
];
484 ib_conn
->comp
->active_qps
++;
485 mutex_unlock(&ig
.connlist_mutex
);
486 iser_info("cq index %d used for ib_conn %p\n", min_index
, ib_conn
);
488 init_attr
.event_handler
= iser_qp_event_callback
;
489 init_attr
.qp_context
= (void *)ib_conn
;
490 init_attr
.send_cq
= ib_conn
->comp
->cq
;
491 init_attr
.recv_cq
= ib_conn
->comp
->cq
;
492 init_attr
.cap
.max_recv_wr
= ISER_QP_MAX_RECV_DTOS
;
493 init_attr
.cap
.max_send_sge
= 2;
494 init_attr
.cap
.max_recv_sge
= 1;
495 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
496 init_attr
.qp_type
= IB_QPT_RC
;
497 if (ib_conn
->pi_support
) {
498 init_attr
.cap
.max_send_wr
= ISER_QP_SIG_MAX_REQ_DTOS
+ 1;
499 init_attr
.create_flags
|= IB_QP_CREATE_SIGNATURE_EN
;
500 iser_conn
->max_cmds
=
501 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS
);
503 if (dev_attr
->max_qp_wr
> ISER_QP_MAX_REQ_DTOS
) {
504 init_attr
.cap
.max_send_wr
= ISER_QP_MAX_REQ_DTOS
+ 1;
505 iser_conn
->max_cmds
=
506 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS
);
508 init_attr
.cap
.max_send_wr
= dev_attr
->max_qp_wr
;
509 iser_conn
->max_cmds
=
510 ISER_GET_MAX_XMIT_CMDS(dev_attr
->max_qp_wr
);
511 iser_dbg("device %s supports max_send_wr %d\n",
512 device
->ib_device
->name
, dev_attr
->max_qp_wr
);
516 ret
= rdma_create_qp(ib_conn
->cma_id
, device
->pd
, &init_attr
);
520 ib_conn
->qp
= ib_conn
->cma_id
->qp
;
521 iser_info("setting conn %p cma_id %p qp %p\n",
522 ib_conn
, ib_conn
->cma_id
,
523 ib_conn
->cma_id
->qp
);
527 mutex_lock(&ig
.connlist_mutex
);
528 ib_conn
->comp
->active_qps
--;
529 mutex_unlock(&ig
.connlist_mutex
);
530 iser_err("unable to alloc mem or create resource, err %d\n", ret
);
536 * based on the resolved device node GUID see if there already allocated
537 * device for this device. If there's no such, create one.
540 struct iser_device
*iser_device_find_by_ib_device(struct rdma_cm_id
*cma_id
)
542 struct iser_device
*device
;
544 mutex_lock(&ig
.device_list_mutex
);
546 list_for_each_entry(device
, &ig
.device_list
, ig_list
)
547 /* find if there's a match using the node GUID */
548 if (device
->ib_device
->node_guid
== cma_id
->device
->node_guid
)
551 device
= kzalloc(sizeof *device
, GFP_KERNEL
);
555 /* assign this device to the device */
556 device
->ib_device
= cma_id
->device
;
557 /* init the device and link it into ig device list */
558 if (iser_create_device_ib_res(device
)) {
563 list_add(&device
->ig_list
, &ig
.device_list
);
568 mutex_unlock(&ig
.device_list_mutex
);
572 /* if there's no demand for this device, release it */
573 static void iser_device_try_release(struct iser_device
*device
)
575 mutex_lock(&ig
.device_list_mutex
);
577 iser_info("device %p refcount %d\n", device
, device
->refcount
);
578 if (!device
->refcount
) {
579 iser_free_device_ib_res(device
);
580 list_del(&device
->ig_list
);
583 mutex_unlock(&ig
.device_list_mutex
);
587 * Called with state mutex held
589 static int iser_conn_state_comp_exch(struct iser_conn
*iser_conn
,
590 enum iser_conn_state comp
,
591 enum iser_conn_state exch
)
595 ret
= (iser_conn
->state
== comp
);
597 iser_conn
->state
= exch
;
602 void iser_release_work(struct work_struct
*work
)
604 struct iser_conn
*iser_conn
;
606 iser_conn
= container_of(work
, struct iser_conn
, release_work
);
608 /* Wait for conn_stop to complete */
609 wait_for_completion(&iser_conn
->stop_completion
);
610 /* Wait for IB resouces cleanup to complete */
611 wait_for_completion(&iser_conn
->ib_completion
);
613 mutex_lock(&iser_conn
->state_mutex
);
614 iser_conn
->state
= ISER_CONN_DOWN
;
615 mutex_unlock(&iser_conn
->state_mutex
);
617 iser_conn_release(iser_conn
);
621 * iser_free_ib_conn_res - release IB related resources
622 * @iser_conn: iser connection struct
623 * @destroy: indicator if we need to try to release the
624 * iser device and memory regoins pool (only iscsi
625 * shutdown and DEVICE_REMOVAL will use this).
627 * This routine is called with the iser state mutex held
628 * so the cm_id removal is out of here. It is Safe to
629 * be invoked multiple times.
631 static void iser_free_ib_conn_res(struct iser_conn
*iser_conn
,
634 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
635 struct iser_device
*device
= ib_conn
->device
;
637 iser_info("freeing conn %p cma_id %p qp %p\n",
638 iser_conn
, ib_conn
->cma_id
, ib_conn
->qp
);
640 if (ib_conn
->qp
!= NULL
) {
641 ib_conn
->comp
->active_qps
--;
642 rdma_destroy_qp(ib_conn
->cma_id
);
647 if (iser_conn
->rx_descs
)
648 iser_free_rx_descriptors(iser_conn
);
650 if (device
!= NULL
) {
651 iser_device_try_release(device
);
652 ib_conn
->device
= NULL
;
658 * Frees all conn objects and deallocs conn descriptor
660 void iser_conn_release(struct iser_conn
*iser_conn
)
662 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
664 mutex_lock(&ig
.connlist_mutex
);
665 list_del(&iser_conn
->conn_list
);
666 mutex_unlock(&ig
.connlist_mutex
);
668 mutex_lock(&iser_conn
->state_mutex
);
669 /* In case we endup here without ep_disconnect being invoked. */
670 if (iser_conn
->state
!= ISER_CONN_DOWN
) {
671 iser_warn("iser conn %p state %d, expected state down.\n",
672 iser_conn
, iser_conn
->state
);
673 iscsi_destroy_endpoint(iser_conn
->ep
);
674 iser_conn
->state
= ISER_CONN_DOWN
;
677 * In case we never got to bind stage, we still need to
678 * release IB resources (which is safe to call more than once).
680 iser_free_ib_conn_res(iser_conn
, true);
681 mutex_unlock(&iser_conn
->state_mutex
);
683 if (ib_conn
->cma_id
!= NULL
) {
684 rdma_destroy_id(ib_conn
->cma_id
);
685 ib_conn
->cma_id
= NULL
;
692 * triggers start of the disconnect procedures and wait for them to be done
693 * Called with state mutex held
695 int iser_conn_terminate(struct iser_conn
*iser_conn
)
697 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
698 struct ib_send_wr
*bad_wr
;
701 /* terminate the iser conn only if the conn state is UP */
702 if (!iser_conn_state_comp_exch(iser_conn
, ISER_CONN_UP
,
703 ISER_CONN_TERMINATING
))
706 iser_info("iser_conn %p state %d\n", iser_conn
, iser_conn
->state
);
708 /* suspend queuing of new iscsi commands */
709 if (iser_conn
->iscsi_conn
)
710 iscsi_suspend_queue(iser_conn
->iscsi_conn
);
713 * In case we didn't already clean up the cma_id (peer initiated
714 * a disconnection), we need to Cause the CMA to change the QP
717 if (ib_conn
->cma_id
) {
718 err
= rdma_disconnect(ib_conn
->cma_id
);
720 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
723 /* post an indication that all flush errors were consumed */
724 err
= ib_post_send(ib_conn
->qp
, &ib_conn
->beacon
, &bad_wr
);
726 iser_err("conn %p failed to post beacon", ib_conn
);
730 wait_for_completion(&ib_conn
->flush_comp
);
737 * Called with state mutex held
739 static void iser_connect_error(struct rdma_cm_id
*cma_id
)
741 struct iser_conn
*iser_conn
;
743 iser_conn
= (struct iser_conn
*)cma_id
->context
;
744 iser_conn
->state
= ISER_CONN_TERMINATING
;
748 * Called with state mutex held
750 static void iser_addr_handler(struct rdma_cm_id
*cma_id
)
752 struct iser_device
*device
;
753 struct iser_conn
*iser_conn
;
754 struct ib_conn
*ib_conn
;
757 iser_conn
= (struct iser_conn
*)cma_id
->context
;
758 if (iser_conn
->state
!= ISER_CONN_PENDING
)
762 ib_conn
= &iser_conn
->ib_conn
;
763 device
= iser_device_find_by_ib_device(cma_id
);
765 iser_err("device lookup/creation failed\n");
766 iser_connect_error(cma_id
);
770 ib_conn
->device
= device
;
772 /* connection T10-PI support */
773 if (iser_pi_enable
) {
774 if (!(device
->dev_attr
.device_cap_flags
&
775 IB_DEVICE_SIGNATURE_HANDOVER
)) {
776 iser_warn("T10-PI requested but not supported on %s, "
777 "continue without T10-PI\n",
778 ib_conn
->device
->ib_device
->name
);
779 ib_conn
->pi_support
= false;
781 ib_conn
->pi_support
= true;
785 ret
= rdma_resolve_route(cma_id
, 1000);
787 iser_err("resolve route failed: %d\n", ret
);
788 iser_connect_error(cma_id
);
794 * Called with state mutex held
796 static void iser_route_handler(struct rdma_cm_id
*cma_id
)
798 struct rdma_conn_param conn_param
;
800 struct iser_cm_hdr req_hdr
;
801 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
802 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
803 struct iser_device
*device
= ib_conn
->device
;
805 if (iser_conn
->state
!= ISER_CONN_PENDING
)
809 ret
= iser_create_ib_conn_res(ib_conn
);
813 memset(&conn_param
, 0, sizeof conn_param
);
814 conn_param
.responder_resources
= device
->dev_attr
.max_qp_rd_atom
;
815 conn_param
.initiator_depth
= 1;
816 conn_param
.retry_count
= 7;
817 conn_param
.rnr_retry_count
= 6;
819 memset(&req_hdr
, 0, sizeof(req_hdr
));
820 req_hdr
.flags
= (ISER_ZBVA_NOT_SUPPORTED
|
821 ISER_SEND_W_INV_NOT_SUPPORTED
);
822 conn_param
.private_data
= (void *)&req_hdr
;
823 conn_param
.private_data_len
= sizeof(struct iser_cm_hdr
);
825 ret
= rdma_connect(cma_id
, &conn_param
);
827 iser_err("failure connecting: %d\n", ret
);
833 iser_connect_error(cma_id
);
836 static void iser_connected_handler(struct rdma_cm_id
*cma_id
)
838 struct iser_conn
*iser_conn
;
839 struct ib_qp_attr attr
;
840 struct ib_qp_init_attr init_attr
;
842 iser_conn
= (struct iser_conn
*)cma_id
->context
;
843 if (iser_conn
->state
!= ISER_CONN_PENDING
)
847 (void)ib_query_qp(cma_id
->qp
, &attr
, ~0, &init_attr
);
848 iser_info("remote qpn:%x my qpn:%x\n", attr
.dest_qp_num
, cma_id
->qp
->qp_num
);
850 iser_conn
->state
= ISER_CONN_UP
;
851 complete(&iser_conn
->up_completion
);
854 static void iser_disconnected_handler(struct rdma_cm_id
*cma_id
)
856 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
858 if (iser_conn_terminate(iser_conn
)) {
859 if (iser_conn
->iscsi_conn
)
860 iscsi_conn_failure(iser_conn
->iscsi_conn
,
861 ISCSI_ERR_CONN_FAILED
);
863 iser_err("iscsi_iser connection isn't bound\n");
867 static void iser_cleanup_handler(struct rdma_cm_id
*cma_id
,
870 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
873 * We are not guaranteed that we visited disconnected_handler
874 * by now, call it here to be safe that we handle CM drep
877 iser_disconnected_handler(cma_id
);
878 iser_free_ib_conn_res(iser_conn
, destroy
);
879 complete(&iser_conn
->ib_completion
);
882 static int iser_cma_handler(struct rdma_cm_id
*cma_id
, struct rdma_cm_event
*event
)
884 struct iser_conn
*iser_conn
;
887 iser_conn
= (struct iser_conn
*)cma_id
->context
;
888 iser_info("%s (%d): status %d conn %p id %p\n",
889 rdma_event_msg(event
->event
), event
->event
,
890 event
->status
, cma_id
->context
, cma_id
);
892 mutex_lock(&iser_conn
->state_mutex
);
893 switch (event
->event
) {
894 case RDMA_CM_EVENT_ADDR_RESOLVED
:
895 iser_addr_handler(cma_id
);
897 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
898 iser_route_handler(cma_id
);
900 case RDMA_CM_EVENT_ESTABLISHED
:
901 iser_connected_handler(cma_id
);
903 case RDMA_CM_EVENT_ADDR_ERROR
:
904 case RDMA_CM_EVENT_ROUTE_ERROR
:
905 case RDMA_CM_EVENT_CONNECT_ERROR
:
906 case RDMA_CM_EVENT_UNREACHABLE
:
907 case RDMA_CM_EVENT_REJECTED
:
908 iser_connect_error(cma_id
);
910 case RDMA_CM_EVENT_DISCONNECTED
:
911 case RDMA_CM_EVENT_ADDR_CHANGE
:
912 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
913 iser_cleanup_handler(cma_id
, false);
915 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
917 * we *must* destroy the device as we cannot rely
918 * on iscsid to be around to initiate error handling.
919 * also if we are not in state DOWN implicitly destroy
922 iser_cleanup_handler(cma_id
, true);
923 if (iser_conn
->state
!= ISER_CONN_DOWN
) {
924 iser_conn
->ib_conn
.cma_id
= NULL
;
929 iser_err("Unexpected RDMA CM event: %s (%d)\n",
930 rdma_event_msg(event
->event
), event
->event
);
933 mutex_unlock(&iser_conn
->state_mutex
);
938 void iser_conn_init(struct iser_conn
*iser_conn
)
940 iser_conn
->state
= ISER_CONN_INIT
;
941 iser_conn
->ib_conn
.post_recv_buf_count
= 0;
942 init_completion(&iser_conn
->ib_conn
.flush_comp
);
943 init_completion(&iser_conn
->stop_completion
);
944 init_completion(&iser_conn
->ib_completion
);
945 init_completion(&iser_conn
->up_completion
);
946 INIT_LIST_HEAD(&iser_conn
->conn_list
);
947 spin_lock_init(&iser_conn
->ib_conn
.lock
);
948 mutex_init(&iser_conn
->state_mutex
);
952 * starts the process of connecting to the target
953 * sleeps until the connection is established or rejected
955 int iser_connect(struct iser_conn
*iser_conn
,
956 struct sockaddr
*src_addr
,
957 struct sockaddr
*dst_addr
,
960 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
963 mutex_lock(&iser_conn
->state_mutex
);
965 sprintf(iser_conn
->name
, "%pISp", dst_addr
);
967 iser_info("connecting to: %s\n", iser_conn
->name
);
969 /* the device is known only --after-- address resolution */
970 ib_conn
->device
= NULL
;
972 iser_conn
->state
= ISER_CONN_PENDING
;
974 ib_conn
->beacon
.wr_id
= ISER_BEACON_WRID
;
975 ib_conn
->beacon
.opcode
= IB_WR_SEND
;
977 ib_conn
->cma_id
= rdma_create_id(iser_cma_handler
,
979 RDMA_PS_TCP
, IB_QPT_RC
);
980 if (IS_ERR(ib_conn
->cma_id
)) {
981 err
= PTR_ERR(ib_conn
->cma_id
);
982 iser_err("rdma_create_id failed: %d\n", err
);
986 err
= rdma_resolve_addr(ib_conn
->cma_id
, src_addr
, dst_addr
, 1000);
988 iser_err("rdma_resolve_addr failed: %d\n", err
);
993 wait_for_completion_interruptible(&iser_conn
->up_completion
);
995 if (iser_conn
->state
!= ISER_CONN_UP
) {
997 goto connect_failure
;
1000 mutex_unlock(&iser_conn
->state_mutex
);
1002 mutex_lock(&ig
.connlist_mutex
);
1003 list_add(&iser_conn
->conn_list
, &ig
.connlist
);
1004 mutex_unlock(&ig
.connlist_mutex
);
1008 ib_conn
->cma_id
= NULL
;
1010 iser_conn
->state
= ISER_CONN_DOWN
;
1012 mutex_unlock(&iser_conn
->state_mutex
);
1013 iser_conn_release(iser_conn
);
1017 int iser_post_recvl(struct iser_conn
*iser_conn
)
1019 struct ib_recv_wr rx_wr
, *rx_wr_failed
;
1020 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1024 sge
.addr
= iser_conn
->login_resp_dma
;
1025 sge
.length
= ISER_RX_LOGIN_SIZE
;
1026 sge
.lkey
= ib_conn
->device
->mr
->lkey
;
1028 rx_wr
.wr_id
= (uintptr_t)iser_conn
->login_resp_buf
;
1029 rx_wr
.sg_list
= &sge
;
1033 ib_conn
->post_recv_buf_count
++;
1034 ib_ret
= ib_post_recv(ib_conn
->qp
, &rx_wr
, &rx_wr_failed
);
1036 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1037 ib_conn
->post_recv_buf_count
--;
1042 int iser_post_recvm(struct iser_conn
*iser_conn
, int count
)
1044 struct ib_recv_wr
*rx_wr
, *rx_wr_failed
;
1046 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1047 unsigned int my_rx_head
= iser_conn
->rx_desc_head
;
1048 struct iser_rx_desc
*rx_desc
;
1050 for (rx_wr
= ib_conn
->rx_wr
, i
= 0; i
< count
; i
++, rx_wr
++) {
1051 rx_desc
= &iser_conn
->rx_descs
[my_rx_head
];
1052 rx_wr
->wr_id
= (uintptr_t)rx_desc
;
1053 rx_wr
->sg_list
= &rx_desc
->rx_sg
;
1055 rx_wr
->next
= rx_wr
+ 1;
1056 my_rx_head
= (my_rx_head
+ 1) & iser_conn
->qp_max_recv_dtos_mask
;
1060 rx_wr
->next
= NULL
; /* mark end of work requests list */
1062 ib_conn
->post_recv_buf_count
+= count
;
1063 ib_ret
= ib_post_recv(ib_conn
->qp
, ib_conn
->rx_wr
, &rx_wr_failed
);
1065 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1066 ib_conn
->post_recv_buf_count
-= count
;
1068 iser_conn
->rx_desc_head
= my_rx_head
;
1074 * iser_start_send - Initiate a Send DTO operation
1076 * returns 0 on success, -1 on failure
1078 int iser_post_send(struct ib_conn
*ib_conn
, struct iser_tx_desc
*tx_desc
,
1082 struct ib_send_wr send_wr
, *send_wr_failed
;
1084 ib_dma_sync_single_for_device(ib_conn
->device
->ib_device
,
1085 tx_desc
->dma_addr
, ISER_HEADERS_LEN
,
1088 send_wr
.next
= NULL
;
1089 send_wr
.wr_id
= (uintptr_t)tx_desc
;
1090 send_wr
.sg_list
= tx_desc
->tx_sg
;
1091 send_wr
.num_sge
= tx_desc
->num_sge
;
1092 send_wr
.opcode
= IB_WR_SEND
;
1093 send_wr
.send_flags
= signal
? IB_SEND_SIGNALED
: 0;
1095 ib_ret
= ib_post_send(ib_conn
->qp
, &send_wr
, &send_wr_failed
);
1097 iser_err("ib_post_send failed, ret:%d\n", ib_ret
);
1103 * is_iser_tx_desc - Indicate if the completion wr_id
1104 * is a TX descriptor or not.
1105 * @iser_conn: iser connection
1106 * @wr_id: completion WR identifier
1108 * Since we cannot rely on wc opcode in FLUSH errors
1109 * we must work around it by checking if the wr_id address
1110 * falls in the iser connection rx_descs buffer. If so
1111 * it is an RX descriptor, otherwize it is a TX.
1114 is_iser_tx_desc(struct iser_conn
*iser_conn
, void *wr_id
)
1116 void *start
= iser_conn
->rx_descs
;
1117 int len
= iser_conn
->num_rx_descs
* sizeof(*iser_conn
->rx_descs
);
1119 if (wr_id
>= start
&& wr_id
< start
+ len
)
1126 * iser_handle_comp_error() - Handle error completion
1127 * @ib_conn: connection RDMA resources
1128 * @wc: work completion
1130 * Notes: We may handle a FLUSH error completion and in this case
1131 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1132 * error completion we should also notify iscsi layer that
1133 * connection is failed (in case we passed bind stage).
1136 iser_handle_comp_error(struct ib_conn
*ib_conn
,
1139 void *wr_id
= (void *)(uintptr_t)wc
->wr_id
;
1140 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
1143 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1144 if (iser_conn
->iscsi_conn
)
1145 iscsi_conn_failure(iser_conn
->iscsi_conn
,
1146 ISCSI_ERR_CONN_FAILED
);
1148 if (wc
->wr_id
== ISER_FASTREG_LI_WRID
)
1151 if (is_iser_tx_desc(iser_conn
, wr_id
)) {
1152 struct iser_tx_desc
*desc
= wr_id
;
1154 if (desc
->type
== ISCSI_TX_DATAOUT
)
1155 kmem_cache_free(ig
.desc_cache
, desc
);
1157 ib_conn
->post_recv_buf_count
--;
1162 * iser_handle_wc - handle a single work completion
1163 * @wc: work completion
1165 * Soft-IRQ context, work completion can be either
1166 * SEND or RECV, and can turn out successful or
1167 * with error (or flush error).
1169 static void iser_handle_wc(struct ib_wc
*wc
)
1171 struct ib_conn
*ib_conn
;
1172 struct iser_tx_desc
*tx_desc
;
1173 struct iser_rx_desc
*rx_desc
;
1175 ib_conn
= wc
->qp
->qp_context
;
1176 if (likely(wc
->status
== IB_WC_SUCCESS
)) {
1177 if (wc
->opcode
== IB_WC_RECV
) {
1178 rx_desc
= (struct iser_rx_desc
*)(uintptr_t)wc
->wr_id
;
1179 iser_rcv_completion(rx_desc
, wc
->byte_len
,
1182 if (wc
->opcode
== IB_WC_SEND
) {
1183 tx_desc
= (struct iser_tx_desc
*)(uintptr_t)wc
->wr_id
;
1184 iser_snd_completion(tx_desc
, ib_conn
);
1186 iser_err("Unknown wc opcode %d\n", wc
->opcode
);
1189 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1190 iser_err("%s (%d): wr id %llx vend_err %x\n",
1191 ib_wc_status_msg(wc
->status
), wc
->status
,
1192 wc
->wr_id
, wc
->vendor_err
);
1194 iser_dbg("%s (%d): wr id %llx\n",
1195 ib_wc_status_msg(wc
->status
), wc
->status
,
1198 if (wc
->wr_id
== ISER_BEACON_WRID
)
1199 /* all flush errors were consumed */
1200 complete(&ib_conn
->flush_comp
);
1202 iser_handle_comp_error(ib_conn
, wc
);
1207 * iser_cq_tasklet_fn - iSER completion polling loop
1208 * @data: iSER completion context
1210 * Soft-IRQ context, polling connection CQ until
1211 * either CQ was empty or we exausted polling budget
1213 static void iser_cq_tasklet_fn(unsigned long data
)
1215 struct iser_comp
*comp
= (struct iser_comp
*)data
;
1216 struct ib_cq
*cq
= comp
->cq
;
1217 struct ib_wc
*const wcs
= comp
->wcs
;
1218 int i
, n
, completed
= 0;
1220 while ((n
= ib_poll_cq(cq
, ARRAY_SIZE(comp
->wcs
), wcs
)) > 0) {
1221 for (i
= 0; i
< n
; i
++)
1222 iser_handle_wc(&wcs
[i
]);
1225 if (completed
>= iser_cq_poll_limit
)
1230 * It is assumed here that arming CQ only once its empty
1231 * would not cause interrupts to be missed.
1233 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1235 iser_dbg("got %d completions\n", completed
);
1238 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
)
1240 struct iser_comp
*comp
= cq_context
;
1242 tasklet_schedule(&comp
->tasklet
);
1245 u8
iser_check_task_pi_status(struct iscsi_iser_task
*iser_task
,
1246 enum iser_data_dir cmd_dir
, sector_t
*sector
)
1248 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
1249 struct iser_fr_desc
*desc
= reg
->mem_h
;
1250 unsigned long sector_size
= iser_task
->sc
->device
->sector_size
;
1251 struct ib_mr_status mr_status
;
1254 if (desc
&& desc
->pi_ctx
->sig_protected
) {
1255 desc
->pi_ctx
->sig_protected
= 0;
1256 ret
= ib_check_mr_status(desc
->pi_ctx
->sig_mr
,
1257 IB_MR_CHECK_SIG_STATUS
, &mr_status
);
1259 pr_err("ib_check_mr_status failed, ret %d\n", ret
);
1263 if (mr_status
.fail_status
& IB_MR_CHECK_SIG_STATUS
) {
1264 sector_t sector_off
= mr_status
.sig_err
.sig_err_offset
;
1266 do_div(sector_off
, sector_size
+ 8);
1267 *sector
= scsi_get_lba(iser_task
->sc
) + sector_off
;
1269 pr_err("PI error found type %d at sector %llx "
1270 "expected %x vs actual %x\n",
1271 mr_status
.sig_err
.err_type
,
1272 (unsigned long long)*sector
,
1273 mr_status
.sig_err
.expected
,
1274 mr_status
.sig_err
.actual
);
1276 switch (mr_status
.sig_err
.err_type
) {
1277 case IB_SIG_BAD_GUARD
:
1279 case IB_SIG_BAD_REFTAG
:
1281 case IB_SIG_BAD_APPTAG
:
1289 /* Not alot we can do here, return ambiguous guard error */