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_CQ_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
43 #define ISER_MAX_TX_CQ_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
45 static void iser_cq_tasklet_fn(unsigned long data
);
46 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
);
47 static int iser_drain_tx_cq(struct iser_comp
*comp
);
49 static void iser_cq_event_callback(struct ib_event
*cause
, void *context
)
51 iser_err("got cq event %d \n", cause
->event
);
54 static void iser_qp_event_callback(struct ib_event
*cause
, void *context
)
56 iser_err("got qp event %d\n",cause
->event
);
59 static void iser_event_handler(struct ib_event_handler
*handler
,
60 struct ib_event
*event
)
62 iser_err("async event %d on device %s port %d\n", event
->event
,
63 event
->device
->name
, event
->element
.port_num
);
67 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
68 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
71 * returns 0 on success, -1 on failure
73 static int iser_create_device_ib_res(struct iser_device
*device
)
75 struct ib_device_attr
*dev_attr
= &device
->dev_attr
;
78 ret
= ib_query_device(device
->ib_device
, dev_attr
);
80 pr_warn("Query device failed for %s\n", device
->ib_device
->name
);
84 /* Assign function handles - based on FMR support */
85 if (device
->ib_device
->alloc_fmr
&& device
->ib_device
->dealloc_fmr
&&
86 device
->ib_device
->map_phys_fmr
&& device
->ib_device
->unmap_fmr
) {
87 iser_info("FMR supported, using FMR for registration\n");
88 device
->iser_alloc_rdma_reg_res
= iser_create_fmr_pool
;
89 device
->iser_free_rdma_reg_res
= iser_free_fmr_pool
;
90 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fmr
;
91 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fmr
;
93 if (dev_attr
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
94 iser_info("FastReg supported, using FastReg for registration\n");
95 device
->iser_alloc_rdma_reg_res
= iser_create_fastreg_pool
;
96 device
->iser_free_rdma_reg_res
= iser_free_fastreg_pool
;
97 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fastreg
;
98 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fastreg
;
100 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
104 device
->comps_used
= min(ISER_MAX_CQ
,
105 device
->ib_device
->num_comp_vectors
);
106 iser_info("using %d CQs, device %s supports %d vectors\n",
107 device
->comps_used
, device
->ib_device
->name
,
108 device
->ib_device
->num_comp_vectors
);
110 device
->pd
= ib_alloc_pd(device
->ib_device
);
111 if (IS_ERR(device
->pd
))
114 for (i
= 0; i
< device
->comps_used
; i
++) {
115 struct iser_comp
*comp
= &device
->comps
[i
];
117 comp
->device
= device
;
118 comp
->rx_cq
= ib_create_cq(device
->ib_device
,
120 iser_cq_event_callback
,
122 ISER_MAX_RX_CQ_LEN
, i
);
123 if (IS_ERR(comp
->rx_cq
)) {
128 comp
->tx_cq
= ib_create_cq(device
->ib_device
, NULL
,
129 iser_cq_event_callback
,
131 ISER_MAX_TX_CQ_LEN
, i
);
132 if (IS_ERR(comp
->tx_cq
)) {
137 if (ib_req_notify_cq(comp
->rx_cq
, IB_CQ_NEXT_COMP
))
140 tasklet_init(&comp
->tasklet
, iser_cq_tasklet_fn
,
141 (unsigned long)comp
);
144 device
->mr
= ib_get_dma_mr(device
->pd
, IB_ACCESS_LOCAL_WRITE
|
145 IB_ACCESS_REMOTE_WRITE
|
146 IB_ACCESS_REMOTE_READ
);
147 if (IS_ERR(device
->mr
))
150 INIT_IB_EVENT_HANDLER(&device
->event_handler
, device
->ib_device
,
152 if (ib_register_event_handler(&device
->event_handler
))
158 ib_dereg_mr(device
->mr
);
160 for (i
= 0; i
< device
->comps_used
; i
++)
161 tasklet_kill(&device
->comps
[i
].tasklet
);
163 for (i
= 0; i
< device
->comps_used
; i
++) {
164 struct iser_comp
*comp
= &device
->comps
[i
];
167 ib_destroy_cq(comp
->tx_cq
);
169 ib_destroy_cq(comp
->rx_cq
);
171 ib_dealloc_pd(device
->pd
);
173 iser_err("failed to allocate an IB resource\n");
178 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
179 * CQ and PD created with the device associated with the adapator.
181 static void iser_free_device_ib_res(struct iser_device
*device
)
184 BUG_ON(device
->mr
== NULL
);
186 for (i
= 0; i
< device
->comps_used
; i
++) {
187 struct iser_comp
*comp
= &device
->comps
[i
];
189 tasklet_kill(&comp
->tasklet
);
190 ib_destroy_cq(comp
->tx_cq
);
191 ib_destroy_cq(comp
->rx_cq
);
196 (void)ib_unregister_event_handler(&device
->event_handler
);
197 (void)ib_dereg_mr(device
->mr
);
198 (void)ib_dealloc_pd(device
->pd
);
205 * iser_create_fmr_pool - Creates FMR pool and page_vector
207 * returns 0 on success, or errno code on failure
209 int iser_create_fmr_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
211 struct iser_device
*device
= ib_conn
->device
;
212 struct ib_fmr_pool_param params
;
215 ib_conn
->fmr
.page_vec
= kmalloc(sizeof(*ib_conn
->fmr
.page_vec
) +
216 (sizeof(u64
)*(ISCSI_ISER_SG_TABLESIZE
+ 1)),
218 if (!ib_conn
->fmr
.page_vec
)
221 ib_conn
->fmr
.page_vec
->pages
= (u64
*)(ib_conn
->fmr
.page_vec
+ 1);
223 params
.page_shift
= SHIFT_4K
;
224 /* when the first/last SG element are not start/end *
225 * page aligned, the map whould be of N+1 pages */
226 params
.max_pages_per_fmr
= ISCSI_ISER_SG_TABLESIZE
+ 1;
227 /* make the pool size twice the max number of SCSI commands *
228 * the ML is expected to queue, watermark for unmap at 50% */
229 params
.pool_size
= cmds_max
* 2;
230 params
.dirty_watermark
= cmds_max
;
232 params
.flush_function
= NULL
;
233 params
.access
= (IB_ACCESS_LOCAL_WRITE
|
234 IB_ACCESS_REMOTE_WRITE
|
235 IB_ACCESS_REMOTE_READ
);
237 ib_conn
->fmr
.pool
= ib_create_fmr_pool(device
->pd
, ¶ms
);
238 if (!IS_ERR(ib_conn
->fmr
.pool
))
241 /* no FMR => no need for page_vec */
242 kfree(ib_conn
->fmr
.page_vec
);
243 ib_conn
->fmr
.page_vec
= NULL
;
245 ret
= PTR_ERR(ib_conn
->fmr
.pool
);
246 ib_conn
->fmr
.pool
= NULL
;
247 if (ret
!= -ENOSYS
) {
248 iser_err("FMR allocation failed, err %d\n", ret
);
251 iser_warn("FMRs are not supported, using unaligned mode\n");
257 * iser_free_fmr_pool - releases the FMR pool and page vec
259 void iser_free_fmr_pool(struct ib_conn
*ib_conn
)
261 iser_info("freeing conn %p fmr pool %p\n",
262 ib_conn
, ib_conn
->fmr
.pool
);
264 if (ib_conn
->fmr
.pool
!= NULL
)
265 ib_destroy_fmr_pool(ib_conn
->fmr
.pool
);
267 ib_conn
->fmr
.pool
= NULL
;
269 kfree(ib_conn
->fmr
.page_vec
);
270 ib_conn
->fmr
.page_vec
= NULL
;
274 iser_create_fastreg_desc(struct ib_device
*ib_device
, struct ib_pd
*pd
,
275 bool pi_enable
, struct fast_reg_descriptor
*desc
)
279 desc
->data_frpl
= ib_alloc_fast_reg_page_list(ib_device
,
280 ISCSI_ISER_SG_TABLESIZE
+ 1);
281 if (IS_ERR(desc
->data_frpl
)) {
282 ret
= PTR_ERR(desc
->data_frpl
);
283 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
285 return PTR_ERR(desc
->data_frpl
);
288 desc
->data_mr
= ib_alloc_fast_reg_mr(pd
, ISCSI_ISER_SG_TABLESIZE
+ 1);
289 if (IS_ERR(desc
->data_mr
)) {
290 ret
= PTR_ERR(desc
->data_mr
);
291 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret
);
292 goto fast_reg_mr_failure
;
294 desc
->reg_indicators
|= ISER_DATA_KEY_VALID
;
297 struct ib_mr_init_attr mr_init_attr
= {0};
298 struct iser_pi_context
*pi_ctx
= NULL
;
300 desc
->pi_ctx
= kzalloc(sizeof(*desc
->pi_ctx
), GFP_KERNEL
);
302 iser_err("Failed to allocate pi context\n");
304 goto pi_ctx_alloc_failure
;
306 pi_ctx
= desc
->pi_ctx
;
308 pi_ctx
->prot_frpl
= ib_alloc_fast_reg_page_list(ib_device
,
309 ISCSI_ISER_SG_TABLESIZE
);
310 if (IS_ERR(pi_ctx
->prot_frpl
)) {
311 ret
= PTR_ERR(pi_ctx
->prot_frpl
);
312 iser_err("Failed to allocate prot frpl ret=%d\n",
314 goto prot_frpl_failure
;
317 pi_ctx
->prot_mr
= ib_alloc_fast_reg_mr(pd
,
318 ISCSI_ISER_SG_TABLESIZE
+ 1);
319 if (IS_ERR(pi_ctx
->prot_mr
)) {
320 ret
= PTR_ERR(pi_ctx
->prot_mr
);
321 iser_err("Failed to allocate prot frmr ret=%d\n",
323 goto prot_mr_failure
;
325 desc
->reg_indicators
|= ISER_PROT_KEY_VALID
;
327 mr_init_attr
.max_reg_descriptors
= 2;
328 mr_init_attr
.flags
|= IB_MR_SIGNATURE_EN
;
329 pi_ctx
->sig_mr
= ib_create_mr(pd
, &mr_init_attr
);
330 if (IS_ERR(pi_ctx
->sig_mr
)) {
331 ret
= PTR_ERR(pi_ctx
->sig_mr
);
332 iser_err("Failed to allocate signature enabled mr err=%d\n",
336 desc
->reg_indicators
|= ISER_SIG_KEY_VALID
;
338 desc
->reg_indicators
&= ~ISER_FASTREG_PROTECTED
;
340 iser_dbg("Create fr_desc %p page_list %p\n",
341 desc
, desc
->data_frpl
->page_list
);
345 ib_dereg_mr(desc
->pi_ctx
->prot_mr
);
347 ib_free_fast_reg_page_list(desc
->pi_ctx
->prot_frpl
);
350 pi_ctx_alloc_failure
:
351 ib_dereg_mr(desc
->data_mr
);
353 ib_free_fast_reg_page_list(desc
->data_frpl
);
359 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
360 * for fast registration work requests.
361 * returns 0 on success, or errno code on failure
363 int iser_create_fastreg_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
365 struct iser_device
*device
= ib_conn
->device
;
366 struct fast_reg_descriptor
*desc
;
369 INIT_LIST_HEAD(&ib_conn
->fastreg
.pool
);
370 ib_conn
->fastreg
.pool_size
= 0;
371 for (i
= 0; i
< cmds_max
; i
++) {
372 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
374 iser_err("Failed to allocate a new fast_reg descriptor\n");
379 ret
= iser_create_fastreg_desc(device
->ib_device
, device
->pd
,
380 ib_conn
->pi_support
, desc
);
382 iser_err("Failed to create fastreg descriptor err=%d\n",
388 list_add_tail(&desc
->list
, &ib_conn
->fastreg
.pool
);
389 ib_conn
->fastreg
.pool_size
++;
395 iser_free_fastreg_pool(ib_conn
);
400 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
402 void iser_free_fastreg_pool(struct ib_conn
*ib_conn
)
404 struct fast_reg_descriptor
*desc
, *tmp
;
407 if (list_empty(&ib_conn
->fastreg
.pool
))
410 iser_info("freeing conn %p fr pool\n", ib_conn
);
412 list_for_each_entry_safe(desc
, tmp
, &ib_conn
->fastreg
.pool
, list
) {
413 list_del(&desc
->list
);
414 ib_free_fast_reg_page_list(desc
->data_frpl
);
415 ib_dereg_mr(desc
->data_mr
);
417 ib_free_fast_reg_page_list(desc
->pi_ctx
->prot_frpl
);
418 ib_dereg_mr(desc
->pi_ctx
->prot_mr
);
419 ib_destroy_mr(desc
->pi_ctx
->sig_mr
);
426 if (i
< ib_conn
->fastreg
.pool_size
)
427 iser_warn("pool still has %d regions registered\n",
428 ib_conn
->fastreg
.pool_size
- i
);
432 * iser_create_ib_conn_res - Queue-Pair (QP)
434 * returns 0 on success, -1 on failure
436 static int iser_create_ib_conn_res(struct ib_conn
*ib_conn
)
438 struct iser_device
*device
;
439 struct ib_qp_init_attr init_attr
;
441 int index
, min_index
= 0;
443 BUG_ON(ib_conn
->device
== NULL
);
445 device
= ib_conn
->device
;
447 memset(&init_attr
, 0, sizeof init_attr
);
449 mutex_lock(&ig
.connlist_mutex
);
450 /* select the CQ with the minimal number of usages */
451 for (index
= 0; index
< device
->comps_used
; index
++) {
452 if (device
->comps
[index
].active_qps
<
453 device
->comps
[min_index
].active_qps
)
456 ib_conn
->comp
= &device
->comps
[min_index
];
457 ib_conn
->comp
->active_qps
++;
458 mutex_unlock(&ig
.connlist_mutex
);
459 iser_info("cq index %d used for ib_conn %p\n", min_index
, ib_conn
);
461 init_attr
.event_handler
= iser_qp_event_callback
;
462 init_attr
.qp_context
= (void *)ib_conn
;
463 init_attr
.send_cq
= ib_conn
->comp
->tx_cq
;
464 init_attr
.recv_cq
= ib_conn
->comp
->rx_cq
;
465 init_attr
.cap
.max_recv_wr
= ISER_QP_MAX_RECV_DTOS
;
466 init_attr
.cap
.max_send_sge
= 2;
467 init_attr
.cap
.max_recv_sge
= 1;
468 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
469 init_attr
.qp_type
= IB_QPT_RC
;
470 if (ib_conn
->pi_support
) {
471 init_attr
.cap
.max_send_wr
= ISER_QP_SIG_MAX_REQ_DTOS
;
472 init_attr
.create_flags
|= IB_QP_CREATE_SIGNATURE_EN
;
474 init_attr
.cap
.max_send_wr
= ISER_QP_MAX_REQ_DTOS
;
477 ret
= rdma_create_qp(ib_conn
->cma_id
, device
->pd
, &init_attr
);
481 ib_conn
->qp
= ib_conn
->cma_id
->qp
;
482 iser_info("setting conn %p cma_id %p qp %p\n",
483 ib_conn
, ib_conn
->cma_id
,
484 ib_conn
->cma_id
->qp
);
488 iser_err("unable to alloc mem or create resource, err %d\n", ret
);
493 * based on the resolved device node GUID see if there already allocated
494 * device for this device. If there's no such, create one.
497 struct iser_device
*iser_device_find_by_ib_device(struct rdma_cm_id
*cma_id
)
499 struct iser_device
*device
;
501 mutex_lock(&ig
.device_list_mutex
);
503 list_for_each_entry(device
, &ig
.device_list
, ig_list
)
504 /* find if there's a match using the node GUID */
505 if (device
->ib_device
->node_guid
== cma_id
->device
->node_guid
)
508 device
= kzalloc(sizeof *device
, GFP_KERNEL
);
512 /* assign this device to the device */
513 device
->ib_device
= cma_id
->device
;
514 /* init the device and link it into ig device list */
515 if (iser_create_device_ib_res(device
)) {
520 list_add(&device
->ig_list
, &ig
.device_list
);
525 mutex_unlock(&ig
.device_list_mutex
);
529 /* if there's no demand for this device, release it */
530 static void iser_device_try_release(struct iser_device
*device
)
532 mutex_lock(&ig
.device_list_mutex
);
534 iser_info("device %p refcount %d\n", device
, device
->refcount
);
535 if (!device
->refcount
) {
536 iser_free_device_ib_res(device
);
537 list_del(&device
->ig_list
);
540 mutex_unlock(&ig
.device_list_mutex
);
544 * Called with state mutex held
546 static int iser_conn_state_comp_exch(struct iser_conn
*iser_conn
,
547 enum iser_conn_state comp
,
548 enum iser_conn_state exch
)
552 ret
= (iser_conn
->state
== comp
);
554 iser_conn
->state
= exch
;
559 void iser_release_work(struct work_struct
*work
)
561 struct iser_conn
*iser_conn
;
563 iser_conn
= container_of(work
, struct iser_conn
, release_work
);
565 /* Wait for conn_stop to complete */
566 wait_for_completion(&iser_conn
->stop_completion
);
567 /* Wait for IB resouces cleanup to complete */
568 wait_for_completion(&iser_conn
->ib_completion
);
570 mutex_lock(&iser_conn
->state_mutex
);
571 iser_conn
->state
= ISER_CONN_DOWN
;
572 mutex_unlock(&iser_conn
->state_mutex
);
574 iser_conn_release(iser_conn
);
578 * iser_free_ib_conn_res - release IB related resources
579 * @iser_conn: iser connection struct
580 * @destroy_device: indicator if we need to try to release
581 * the iser device (only iscsi shutdown and DEVICE_REMOVAL
584 * This routine is called with the iser state mutex held
585 * so the cm_id removal is out of here. It is Safe to
586 * be invoked multiple times.
588 static void iser_free_ib_conn_res(struct iser_conn
*iser_conn
,
591 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
592 struct iser_device
*device
= ib_conn
->device
;
594 iser_info("freeing conn %p cma_id %p qp %p\n",
595 iser_conn
, ib_conn
->cma_id
, ib_conn
->qp
);
597 iser_free_rx_descriptors(iser_conn
);
599 if (ib_conn
->qp
!= NULL
) {
600 ib_conn
->comp
->active_qps
--;
601 rdma_destroy_qp(ib_conn
->cma_id
);
605 if (destroy_device
&& device
!= NULL
) {
606 iser_device_try_release(device
);
607 ib_conn
->device
= NULL
;
612 * Frees all conn objects and deallocs conn descriptor
614 void iser_conn_release(struct iser_conn
*iser_conn
)
616 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
618 mutex_lock(&ig
.connlist_mutex
);
619 list_del(&iser_conn
->conn_list
);
620 mutex_unlock(&ig
.connlist_mutex
);
622 mutex_lock(&iser_conn
->state_mutex
);
623 if (iser_conn
->state
!= ISER_CONN_DOWN
)
624 iser_warn("iser conn %p state %d, expected state down.\n",
625 iser_conn
, iser_conn
->state
);
627 * In case we never got to bind stage, we still need to
628 * release IB resources (which is safe to call more than once).
630 iser_free_ib_conn_res(iser_conn
, true);
631 mutex_unlock(&iser_conn
->state_mutex
);
633 if (ib_conn
->cma_id
!= NULL
) {
634 rdma_destroy_id(ib_conn
->cma_id
);
635 ib_conn
->cma_id
= NULL
;
642 * iser_poll_for_flush_errors - Don't settle for less than all.
643 * @struct ib_conn: IB context of the connection
645 * This routine is called when the QP is in error state
646 * It polls the send CQ until all flush errors are consumed and
647 * returns when all flush errors were processed.
649 static void iser_poll_for_flush_errors(struct ib_conn
*ib_conn
)
653 while (ib_conn
->post_recv_buf_count
> 0 ||
654 atomic_read(&ib_conn
->post_send_buf_count
) > 0) {
656 if (atomic_read(&ib_conn
->post_send_buf_count
) > 0)
657 iser_drain_tx_cq(ib_conn
->comp
);
660 /* Don't flood with prints */
662 iser_dbg("post_recv %d post_send %d",
663 ib_conn
->post_recv_buf_count
,
664 atomic_read(&ib_conn
->post_send_buf_count
));
669 * triggers start of the disconnect procedures and wait for them to be done
670 * Called with state mutex held
672 int iser_conn_terminate(struct iser_conn
*iser_conn
)
674 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
677 /* terminate the iser conn only if the conn state is UP */
678 if (!iser_conn_state_comp_exch(iser_conn
, ISER_CONN_UP
,
679 ISER_CONN_TERMINATING
))
682 iser_info("iser_conn %p state %d\n", iser_conn
, iser_conn
->state
);
684 /* suspend queuing of new iscsi commands */
685 if (iser_conn
->iscsi_conn
)
686 iscsi_suspend_queue(iser_conn
->iscsi_conn
);
689 * In case we didn't already clean up the cma_id (peer initiated
690 * a disconnection), we need to Cause the CMA to change the QP
693 if (ib_conn
->cma_id
) {
694 err
= rdma_disconnect(ib_conn
->cma_id
);
696 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
699 iser_poll_for_flush_errors(ib_conn
);
706 * Called with state mutex held
708 static void iser_connect_error(struct rdma_cm_id
*cma_id
)
710 struct iser_conn
*iser_conn
;
712 iser_conn
= (struct iser_conn
*)cma_id
->context
;
713 iser_conn
->state
= ISER_CONN_DOWN
;
717 * Called with state mutex held
719 static void iser_addr_handler(struct rdma_cm_id
*cma_id
)
721 struct iser_device
*device
;
722 struct iser_conn
*iser_conn
;
723 struct ib_conn
*ib_conn
;
726 iser_conn
= (struct iser_conn
*)cma_id
->context
;
727 if (iser_conn
->state
!= ISER_CONN_PENDING
)
731 ib_conn
= &iser_conn
->ib_conn
;
732 device
= iser_device_find_by_ib_device(cma_id
);
734 iser_err("device lookup/creation failed\n");
735 iser_connect_error(cma_id
);
739 ib_conn
->device
= device
;
741 /* connection T10-PI support */
742 if (iser_pi_enable
) {
743 if (!(device
->dev_attr
.device_cap_flags
&
744 IB_DEVICE_SIGNATURE_HANDOVER
)) {
745 iser_warn("T10-PI requested but not supported on %s, "
746 "continue without T10-PI\n",
747 ib_conn
->device
->ib_device
->name
);
748 ib_conn
->pi_support
= false;
750 ib_conn
->pi_support
= true;
754 ret
= rdma_resolve_route(cma_id
, 1000);
756 iser_err("resolve route failed: %d\n", ret
);
757 iser_connect_error(cma_id
);
763 * Called with state mutex held
765 static void iser_route_handler(struct rdma_cm_id
*cma_id
)
767 struct rdma_conn_param conn_param
;
769 struct iser_cm_hdr req_hdr
;
770 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
771 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
772 struct iser_device
*device
= ib_conn
->device
;
774 if (iser_conn
->state
!= ISER_CONN_PENDING
)
778 ret
= iser_create_ib_conn_res(ib_conn
);
782 memset(&conn_param
, 0, sizeof conn_param
);
783 conn_param
.responder_resources
= device
->dev_attr
.max_qp_rd_atom
;
784 conn_param
.initiator_depth
= 1;
785 conn_param
.retry_count
= 7;
786 conn_param
.rnr_retry_count
= 6;
788 memset(&req_hdr
, 0, sizeof(req_hdr
));
789 req_hdr
.flags
= (ISER_ZBVA_NOT_SUPPORTED
|
790 ISER_SEND_W_INV_NOT_SUPPORTED
);
791 conn_param
.private_data
= (void *)&req_hdr
;
792 conn_param
.private_data_len
= sizeof(struct iser_cm_hdr
);
794 ret
= rdma_connect(cma_id
, &conn_param
);
796 iser_err("failure connecting: %d\n", ret
);
802 iser_connect_error(cma_id
);
805 static void iser_connected_handler(struct rdma_cm_id
*cma_id
)
807 struct iser_conn
*iser_conn
;
808 struct ib_qp_attr attr
;
809 struct ib_qp_init_attr init_attr
;
811 iser_conn
= (struct iser_conn
*)cma_id
->context
;
812 if (iser_conn
->state
!= ISER_CONN_PENDING
)
816 (void)ib_query_qp(cma_id
->qp
, &attr
, ~0, &init_attr
);
817 iser_info("remote qpn:%x my qpn:%x\n", attr
.dest_qp_num
, cma_id
->qp
->qp_num
);
819 iser_conn
->state
= ISER_CONN_UP
;
820 complete(&iser_conn
->up_completion
);
823 static void iser_disconnected_handler(struct rdma_cm_id
*cma_id
)
825 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
827 if (iser_conn_terminate(iser_conn
)) {
828 if (iser_conn
->iscsi_conn
)
829 iscsi_conn_failure(iser_conn
->iscsi_conn
,
830 ISCSI_ERR_CONN_FAILED
);
832 iser_err("iscsi_iser connection isn't bound\n");
836 static void iser_cleanup_handler(struct rdma_cm_id
*cma_id
,
839 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
842 * We are not guaranteed that we visited disconnected_handler
843 * by now, call it here to be safe that we handle CM drep
846 iser_disconnected_handler(cma_id
);
847 iser_free_ib_conn_res(iser_conn
, destroy_device
);
848 complete(&iser_conn
->ib_completion
);
851 static int iser_cma_handler(struct rdma_cm_id
*cma_id
, struct rdma_cm_event
*event
)
853 struct iser_conn
*iser_conn
;
856 iser_conn
= (struct iser_conn
*)cma_id
->context
;
857 iser_info("event %d status %d conn %p id %p\n",
858 event
->event
, event
->status
, cma_id
->context
, cma_id
);
860 mutex_lock(&iser_conn
->state_mutex
);
861 switch (event
->event
) {
862 case RDMA_CM_EVENT_ADDR_RESOLVED
:
863 iser_addr_handler(cma_id
);
865 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
866 iser_route_handler(cma_id
);
868 case RDMA_CM_EVENT_ESTABLISHED
:
869 iser_connected_handler(cma_id
);
871 case RDMA_CM_EVENT_ADDR_ERROR
:
872 case RDMA_CM_EVENT_ROUTE_ERROR
:
873 case RDMA_CM_EVENT_CONNECT_ERROR
:
874 case RDMA_CM_EVENT_UNREACHABLE
:
875 case RDMA_CM_EVENT_REJECTED
:
876 iser_connect_error(cma_id
);
878 case RDMA_CM_EVENT_DISCONNECTED
:
879 case RDMA_CM_EVENT_ADDR_CHANGE
:
880 iser_disconnected_handler(cma_id
);
882 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
884 * we *must* destroy the device as we cannot rely
885 * on iscsid to be around to initiate error handling.
886 * also implicitly destroy the cma_id.
888 iser_cleanup_handler(cma_id
, true);
889 iser_conn
->ib_conn
.cma_id
= NULL
;
892 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
893 iser_cleanup_handler(cma_id
, false);
896 iser_err("Unexpected RDMA CM event (%d)\n", event
->event
);
899 mutex_unlock(&iser_conn
->state_mutex
);
904 void iser_conn_init(struct iser_conn
*iser_conn
)
906 iser_conn
->state
= ISER_CONN_INIT
;
907 iser_conn
->ib_conn
.post_recv_buf_count
= 0;
908 atomic_set(&iser_conn
->ib_conn
.post_send_buf_count
, 0);
909 init_completion(&iser_conn
->stop_completion
);
910 init_completion(&iser_conn
->ib_completion
);
911 init_completion(&iser_conn
->up_completion
);
912 INIT_LIST_HEAD(&iser_conn
->conn_list
);
913 spin_lock_init(&iser_conn
->ib_conn
.lock
);
914 mutex_init(&iser_conn
->state_mutex
);
918 * starts the process of connecting to the target
919 * sleeps until the connection is established or rejected
921 int iser_connect(struct iser_conn
*iser_conn
,
922 struct sockaddr
*src_addr
,
923 struct sockaddr
*dst_addr
,
926 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
929 mutex_lock(&iser_conn
->state_mutex
);
931 sprintf(iser_conn
->name
, "%pISp", dst_addr
);
933 iser_info("connecting to: %s\n", iser_conn
->name
);
935 /* the device is known only --after-- address resolution */
936 ib_conn
->device
= NULL
;
938 iser_conn
->state
= ISER_CONN_PENDING
;
940 ib_conn
->cma_id
= rdma_create_id(iser_cma_handler
,
942 RDMA_PS_TCP
, IB_QPT_RC
);
943 if (IS_ERR(ib_conn
->cma_id
)) {
944 err
= PTR_ERR(ib_conn
->cma_id
);
945 iser_err("rdma_create_id failed: %d\n", err
);
949 err
= rdma_resolve_addr(ib_conn
->cma_id
, src_addr
, dst_addr
, 1000);
951 iser_err("rdma_resolve_addr failed: %d\n", err
);
956 wait_for_completion_interruptible(&iser_conn
->up_completion
);
958 if (iser_conn
->state
!= ISER_CONN_UP
) {
960 goto connect_failure
;
963 mutex_unlock(&iser_conn
->state_mutex
);
965 mutex_lock(&ig
.connlist_mutex
);
966 list_add(&iser_conn
->conn_list
, &ig
.connlist
);
967 mutex_unlock(&ig
.connlist_mutex
);
971 ib_conn
->cma_id
= NULL
;
973 iser_conn
->state
= ISER_CONN_DOWN
;
975 mutex_unlock(&iser_conn
->state_mutex
);
976 iser_conn_release(iser_conn
);
981 * iser_reg_page_vec - Register physical memory
983 * returns: 0 on success, errno code on failure
985 int iser_reg_page_vec(struct ib_conn
*ib_conn
,
986 struct iser_page_vec
*page_vec
,
987 struct iser_mem_reg
*mem_reg
)
989 struct ib_pool_fmr
*mem
;
994 page_list
= page_vec
->pages
;
995 io_addr
= page_list
[0];
997 mem
= ib_fmr_pool_map_phys(ib_conn
->fmr
.pool
,
1003 status
= (int)PTR_ERR(mem
);
1004 iser_err("ib_fmr_pool_map_phys failed: %d\n", status
);
1008 mem_reg
->lkey
= mem
->fmr
->lkey
;
1009 mem_reg
->rkey
= mem
->fmr
->rkey
;
1010 mem_reg
->len
= page_vec
->length
* SIZE_4K
;
1011 mem_reg
->va
= io_addr
;
1013 mem_reg
->mem_h
= (void *)mem
;
1015 mem_reg
->va
+= page_vec
->offset
;
1016 mem_reg
->len
= page_vec
->data_size
;
1018 iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
1019 "entry[0]: (0x%08lx,%ld)] -> "
1020 "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
1021 page_vec
, page_vec
->length
,
1022 (unsigned long)page_vec
->pages
[0],
1023 (unsigned long)page_vec
->data_size
,
1024 (unsigned int)mem_reg
->lkey
, mem_reg
->mem_h
,
1025 (unsigned long)mem_reg
->va
, (unsigned long)mem_reg
->len
);
1030 * Unregister (previosuly registered using FMR) memory.
1031 * If memory is non-FMR does nothing.
1033 void iser_unreg_mem_fmr(struct iscsi_iser_task
*iser_task
,
1034 enum iser_data_dir cmd_dir
)
1036 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1042 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg
->mem_h
);
1044 ret
= ib_fmr_pool_unmap((struct ib_pool_fmr
*)reg
->mem_h
);
1046 iser_err("ib_fmr_pool_unmap failed %d\n", ret
);
1051 void iser_unreg_mem_fastreg(struct iscsi_iser_task
*iser_task
,
1052 enum iser_data_dir cmd_dir
)
1054 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1055 struct iser_conn
*iser_conn
= iser_task
->iser_conn
;
1056 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1057 struct fast_reg_descriptor
*desc
= reg
->mem_h
;
1064 spin_lock_bh(&ib_conn
->lock
);
1065 list_add_tail(&desc
->list
, &ib_conn
->fastreg
.pool
);
1066 spin_unlock_bh(&ib_conn
->lock
);
1069 int iser_post_recvl(struct iser_conn
*iser_conn
)
1071 struct ib_recv_wr rx_wr
, *rx_wr_failed
;
1072 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1076 sge
.addr
= iser_conn
->login_resp_dma
;
1077 sge
.length
= ISER_RX_LOGIN_SIZE
;
1078 sge
.lkey
= ib_conn
->device
->mr
->lkey
;
1080 rx_wr
.wr_id
= (unsigned long)iser_conn
->login_resp_buf
;
1081 rx_wr
.sg_list
= &sge
;
1085 ib_conn
->post_recv_buf_count
++;
1086 ib_ret
= ib_post_recv(ib_conn
->qp
, &rx_wr
, &rx_wr_failed
);
1088 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1089 ib_conn
->post_recv_buf_count
--;
1094 int iser_post_recvm(struct iser_conn
*iser_conn
, int count
)
1096 struct ib_recv_wr
*rx_wr
, *rx_wr_failed
;
1098 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1099 unsigned int my_rx_head
= iser_conn
->rx_desc_head
;
1100 struct iser_rx_desc
*rx_desc
;
1102 for (rx_wr
= ib_conn
->rx_wr
, i
= 0; i
< count
; i
++, rx_wr
++) {
1103 rx_desc
= &iser_conn
->rx_descs
[my_rx_head
];
1104 rx_wr
->wr_id
= (unsigned long)rx_desc
;
1105 rx_wr
->sg_list
= &rx_desc
->rx_sg
;
1107 rx_wr
->next
= rx_wr
+ 1;
1108 my_rx_head
= (my_rx_head
+ 1) & iser_conn
->qp_max_recv_dtos_mask
;
1112 rx_wr
->next
= NULL
; /* mark end of work requests list */
1114 ib_conn
->post_recv_buf_count
+= count
;
1115 ib_ret
= ib_post_recv(ib_conn
->qp
, ib_conn
->rx_wr
, &rx_wr_failed
);
1117 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1118 ib_conn
->post_recv_buf_count
-= count
;
1120 iser_conn
->rx_desc_head
= my_rx_head
;
1126 * iser_start_send - Initiate a Send DTO operation
1128 * returns 0 on success, -1 on failure
1130 int iser_post_send(struct ib_conn
*ib_conn
, struct iser_tx_desc
*tx_desc
)
1133 struct ib_send_wr send_wr
, *send_wr_failed
;
1135 ib_dma_sync_single_for_device(ib_conn
->device
->ib_device
,
1136 tx_desc
->dma_addr
, ISER_HEADERS_LEN
,
1139 send_wr
.next
= NULL
;
1140 send_wr
.wr_id
= (unsigned long)tx_desc
;
1141 send_wr
.sg_list
= tx_desc
->tx_sg
;
1142 send_wr
.num_sge
= tx_desc
->num_sge
;
1143 send_wr
.opcode
= IB_WR_SEND
;
1144 send_wr
.send_flags
= IB_SEND_SIGNALED
;
1146 atomic_inc(&ib_conn
->post_send_buf_count
);
1148 ib_ret
= ib_post_send(ib_conn
->qp
, &send_wr
, &send_wr_failed
);
1150 iser_err("ib_post_send failed, ret:%d\n", ib_ret
);
1151 atomic_dec(&ib_conn
->post_send_buf_count
);
1157 * iser_handle_comp_error() - Handle error completion
1158 * @desc: iser TX descriptor
1159 * @ib_conn: connection RDMA resources
1160 * @wc: work completion
1162 * Notes: We may handle a FLUSH error completion and in this case
1163 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1164 * error completion we should also notify iscsi layer that
1165 * connection is failed (in case we passed bind stage).
1168 iser_handle_comp_error(struct iser_tx_desc
*desc
,
1169 struct ib_conn
*ib_conn
,
1172 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
1175 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1176 if (iser_conn
->iscsi_conn
)
1177 iscsi_conn_failure(iser_conn
->iscsi_conn
,
1178 ISCSI_ERR_CONN_FAILED
);
1180 if (desc
&& desc
->type
== ISCSI_TX_DATAOUT
)
1181 kmem_cache_free(ig
.desc_cache
, desc
);
1184 static int iser_drain_tx_cq(struct iser_comp
*comp
)
1186 struct ib_cq
*cq
= comp
->tx_cq
;
1188 struct iser_tx_desc
*tx_desc
;
1189 struct ib_conn
*ib_conn
;
1190 int completed_tx
= 0;
1192 while (ib_poll_cq(cq
, 1, &wc
) == 1) {
1193 tx_desc
= (struct iser_tx_desc
*) (unsigned long) wc
.wr_id
;
1194 ib_conn
= wc
.qp
->qp_context
;
1195 if (wc
.status
== IB_WC_SUCCESS
) {
1196 if (wc
.opcode
== IB_WC_SEND
)
1197 iser_snd_completion(tx_desc
, ib_conn
);
1199 iser_err("expected opcode %d got %d\n",
1200 IB_WC_SEND
, wc
.opcode
);
1202 iser_err("tx id %llx status %d vend_err %x\n",
1203 wc
.wr_id
, wc
.status
, wc
.vendor_err
);
1204 if (wc
.wr_id
!= ISER_FASTREG_LI_WRID
) {
1205 atomic_dec(&ib_conn
->post_send_buf_count
);
1206 iser_handle_comp_error(tx_desc
, ib_conn
, &wc
);
1211 return completed_tx
;
1215 static void iser_cq_tasklet_fn(unsigned long data
)
1217 struct iser_comp
*comp
= (struct iser_comp
*)data
;
1218 struct ib_cq
*cq
= comp
->rx_cq
;
1220 struct iser_rx_desc
*desc
;
1221 unsigned long xfer_len
;
1222 struct ib_conn
*ib_conn
;
1223 int completed_tx
, completed_rx
= 0;
1225 /* First do tx drain, so in a case where we have rx flushes and a successful
1226 * tx completion we will still go through completion error handling.
1228 completed_tx
= iser_drain_tx_cq(comp
);
1230 while (ib_poll_cq(cq
, 1, &wc
) == 1) {
1231 desc
= (struct iser_rx_desc
*) (unsigned long) wc
.wr_id
;
1232 BUG_ON(desc
== NULL
);
1233 ib_conn
= wc
.qp
->qp_context
;
1234 if (wc
.status
== IB_WC_SUCCESS
) {
1235 if (wc
.opcode
== IB_WC_RECV
) {
1236 xfer_len
= (unsigned long)wc
.byte_len
;
1237 iser_rcv_completion(desc
, xfer_len
, ib_conn
);
1239 iser_err("expected opcode %d got %d\n",
1240 IB_WC_RECV
, wc
.opcode
);
1242 if (wc
.status
!= IB_WC_WR_FLUSH_ERR
)
1243 iser_err("rx id %llx status %d vend_err %x\n",
1244 wc
.wr_id
, wc
.status
, wc
.vendor_err
);
1245 ib_conn
->post_recv_buf_count
--;
1246 iser_handle_comp_error(NULL
, ib_conn
, &wc
);
1249 if (!(completed_rx
& 63))
1250 completed_tx
+= iser_drain_tx_cq(comp
);
1252 /* #warning "it is assumed here that arming CQ only once its empty" *
1253 * " would not cause interrupts to be missed" */
1254 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1256 iser_dbg("got %d rx %d tx completions\n", completed_rx
, completed_tx
);
1259 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
)
1261 struct iser_comp
*comp
= cq_context
;
1263 tasklet_schedule(&comp
->tasklet
);
1266 u8
iser_check_task_pi_status(struct iscsi_iser_task
*iser_task
,
1267 enum iser_data_dir cmd_dir
, sector_t
*sector
)
1269 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1270 struct fast_reg_descriptor
*desc
= reg
->mem_h
;
1271 unsigned long sector_size
= iser_task
->sc
->device
->sector_size
;
1272 struct ib_mr_status mr_status
;
1275 if (desc
&& desc
->reg_indicators
& ISER_FASTREG_PROTECTED
) {
1276 desc
->reg_indicators
&= ~ISER_FASTREG_PROTECTED
;
1277 ret
= ib_check_mr_status(desc
->pi_ctx
->sig_mr
,
1278 IB_MR_CHECK_SIG_STATUS
, &mr_status
);
1280 pr_err("ib_check_mr_status failed, ret %d\n", ret
);
1284 if (mr_status
.fail_status
& IB_MR_CHECK_SIG_STATUS
) {
1285 sector_t sector_off
= mr_status
.sig_err
.sig_err_offset
;
1287 do_div(sector_off
, sector_size
+ 8);
1288 *sector
= scsi_get_lba(iser_task
->sc
) + sector_off
;
1290 pr_err("PI error found type %d at sector %llx "
1291 "expected %x vs actual %x\n",
1292 mr_status
.sig_err
.err_type
,
1293 (unsigned long long)*sector
,
1294 mr_status
.sig_err
.expected
,
1295 mr_status
.sig_err
.actual
);
1297 switch (mr_status
.sig_err
.err_type
) {
1298 case IB_SIG_BAD_GUARD
:
1300 case IB_SIG_BAD_REFTAG
:
1302 case IB_SIG_BAD_APPTAG
:
1310 /* Not alot we can do here, return ambiguous guard error */