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("got cq event %d \n", cause
->event
);
57 static void iser_qp_event_callback(struct ib_event
*cause
, void *context
)
59 iser_err("got qp event %d\n",cause
->event
);
62 static void iser_event_handler(struct ib_event_handler
*handler
,
63 struct ib_event
*event
)
65 iser_err("async event %d on device %s port %d\n", event
->event
,
66 event
->device
->name
, event
->element
.port_num
);
70 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
71 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
74 * returns 0 on success, -1 on failure
76 static int iser_create_device_ib_res(struct iser_device
*device
)
78 struct ib_device_attr
*dev_attr
= &device
->dev_attr
;
81 ret
= ib_query_device(device
->ib_device
, dev_attr
);
83 pr_warn("Query device failed for %s\n", device
->ib_device
->name
);
87 /* Assign function handles - based on FMR support */
88 if (device
->ib_device
->alloc_fmr
&& device
->ib_device
->dealloc_fmr
&&
89 device
->ib_device
->map_phys_fmr
&& device
->ib_device
->unmap_fmr
) {
90 iser_info("FMR supported, using FMR for registration\n");
91 device
->iser_alloc_rdma_reg_res
= iser_create_fmr_pool
;
92 device
->iser_free_rdma_reg_res
= iser_free_fmr_pool
;
93 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fmr
;
94 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fmr
;
96 if (dev_attr
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
97 iser_info("FastReg supported, using FastReg for registration\n");
98 device
->iser_alloc_rdma_reg_res
= iser_create_fastreg_pool
;
99 device
->iser_free_rdma_reg_res
= iser_free_fastreg_pool
;
100 device
->iser_reg_rdma_mem
= iser_reg_rdma_mem_fastreg
;
101 device
->iser_unreg_rdma_mem
= iser_unreg_mem_fastreg
;
103 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
107 device
->comps_used
= min(ISER_MAX_CQ
,
108 device
->ib_device
->num_comp_vectors
);
109 iser_info("using %d CQs, device %s supports %d vectors\n",
110 device
->comps_used
, device
->ib_device
->name
,
111 device
->ib_device
->num_comp_vectors
);
113 device
->pd
= ib_alloc_pd(device
->ib_device
);
114 if (IS_ERR(device
->pd
))
117 for (i
= 0; i
< device
->comps_used
; i
++) {
118 struct iser_comp
*comp
= &device
->comps
[i
];
120 comp
->device
= device
;
121 comp
->cq
= ib_create_cq(device
->ib_device
,
123 iser_cq_event_callback
,
126 if (IS_ERR(comp
->cq
)) {
131 if (ib_req_notify_cq(comp
->cq
, IB_CQ_NEXT_COMP
))
134 tasklet_init(&comp
->tasklet
, iser_cq_tasklet_fn
,
135 (unsigned long)comp
);
138 device
->mr
= ib_get_dma_mr(device
->pd
, IB_ACCESS_LOCAL_WRITE
|
139 IB_ACCESS_REMOTE_WRITE
|
140 IB_ACCESS_REMOTE_READ
);
141 if (IS_ERR(device
->mr
))
144 INIT_IB_EVENT_HANDLER(&device
->event_handler
, device
->ib_device
,
146 if (ib_register_event_handler(&device
->event_handler
))
152 ib_dereg_mr(device
->mr
);
154 for (i
= 0; i
< device
->comps_used
; i
++)
155 tasklet_kill(&device
->comps
[i
].tasklet
);
157 for (i
= 0; i
< device
->comps_used
; i
++) {
158 struct iser_comp
*comp
= &device
->comps
[i
];
161 ib_destroy_cq(comp
->cq
);
163 ib_dealloc_pd(device
->pd
);
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
);
195 * iser_create_fmr_pool - Creates FMR pool and page_vector
197 * returns 0 on success, or errno code on failure
199 int iser_create_fmr_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
201 struct iser_device
*device
= ib_conn
->device
;
202 struct ib_fmr_pool_param params
;
205 ib_conn
->fmr
.page_vec
= kmalloc(sizeof(*ib_conn
->fmr
.page_vec
) +
206 (sizeof(u64
)*(ISCSI_ISER_SG_TABLESIZE
+ 1)),
208 if (!ib_conn
->fmr
.page_vec
)
211 ib_conn
->fmr
.page_vec
->pages
= (u64
*)(ib_conn
->fmr
.page_vec
+ 1);
213 params
.page_shift
= SHIFT_4K
;
214 /* when the first/last SG element are not start/end *
215 * page aligned, the map whould be of N+1 pages */
216 params
.max_pages_per_fmr
= ISCSI_ISER_SG_TABLESIZE
+ 1;
217 /* make the pool size twice the max number of SCSI commands *
218 * the ML is expected to queue, watermark for unmap at 50% */
219 params
.pool_size
= cmds_max
* 2;
220 params
.dirty_watermark
= cmds_max
;
222 params
.flush_function
= NULL
;
223 params
.access
= (IB_ACCESS_LOCAL_WRITE
|
224 IB_ACCESS_REMOTE_WRITE
|
225 IB_ACCESS_REMOTE_READ
);
227 ib_conn
->fmr
.pool
= ib_create_fmr_pool(device
->pd
, ¶ms
);
228 if (!IS_ERR(ib_conn
->fmr
.pool
))
231 /* no FMR => no need for page_vec */
232 kfree(ib_conn
->fmr
.page_vec
);
233 ib_conn
->fmr
.page_vec
= NULL
;
235 ret
= PTR_ERR(ib_conn
->fmr
.pool
);
236 ib_conn
->fmr
.pool
= NULL
;
237 if (ret
!= -ENOSYS
) {
238 iser_err("FMR allocation failed, err %d\n", ret
);
241 iser_warn("FMRs are not supported, using unaligned mode\n");
247 * iser_free_fmr_pool - releases the FMR pool and page vec
249 void iser_free_fmr_pool(struct ib_conn
*ib_conn
)
251 iser_info("freeing conn %p fmr pool %p\n",
252 ib_conn
, ib_conn
->fmr
.pool
);
254 if (ib_conn
->fmr
.pool
!= NULL
)
255 ib_destroy_fmr_pool(ib_conn
->fmr
.pool
);
257 ib_conn
->fmr
.pool
= NULL
;
259 kfree(ib_conn
->fmr
.page_vec
);
260 ib_conn
->fmr
.page_vec
= NULL
;
264 iser_create_fastreg_desc(struct ib_device
*ib_device
, struct ib_pd
*pd
,
265 bool pi_enable
, struct fast_reg_descriptor
*desc
)
269 desc
->data_frpl
= ib_alloc_fast_reg_page_list(ib_device
,
270 ISCSI_ISER_SG_TABLESIZE
+ 1);
271 if (IS_ERR(desc
->data_frpl
)) {
272 ret
= PTR_ERR(desc
->data_frpl
);
273 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
275 return PTR_ERR(desc
->data_frpl
);
278 desc
->data_mr
= ib_alloc_fast_reg_mr(pd
, ISCSI_ISER_SG_TABLESIZE
+ 1);
279 if (IS_ERR(desc
->data_mr
)) {
280 ret
= PTR_ERR(desc
->data_mr
);
281 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret
);
282 goto fast_reg_mr_failure
;
284 desc
->reg_indicators
|= ISER_DATA_KEY_VALID
;
287 struct ib_mr_init_attr mr_init_attr
= {0};
288 struct iser_pi_context
*pi_ctx
= NULL
;
290 desc
->pi_ctx
= kzalloc(sizeof(*desc
->pi_ctx
), GFP_KERNEL
);
292 iser_err("Failed to allocate pi context\n");
294 goto pi_ctx_alloc_failure
;
296 pi_ctx
= desc
->pi_ctx
;
298 pi_ctx
->prot_frpl
= ib_alloc_fast_reg_page_list(ib_device
,
299 ISCSI_ISER_SG_TABLESIZE
);
300 if (IS_ERR(pi_ctx
->prot_frpl
)) {
301 ret
= PTR_ERR(pi_ctx
->prot_frpl
);
302 iser_err("Failed to allocate prot frpl ret=%d\n",
304 goto prot_frpl_failure
;
307 pi_ctx
->prot_mr
= ib_alloc_fast_reg_mr(pd
,
308 ISCSI_ISER_SG_TABLESIZE
+ 1);
309 if (IS_ERR(pi_ctx
->prot_mr
)) {
310 ret
= PTR_ERR(pi_ctx
->prot_mr
);
311 iser_err("Failed to allocate prot frmr ret=%d\n",
313 goto prot_mr_failure
;
315 desc
->reg_indicators
|= ISER_PROT_KEY_VALID
;
317 mr_init_attr
.max_reg_descriptors
= 2;
318 mr_init_attr
.flags
|= IB_MR_SIGNATURE_EN
;
319 pi_ctx
->sig_mr
= ib_create_mr(pd
, &mr_init_attr
);
320 if (IS_ERR(pi_ctx
->sig_mr
)) {
321 ret
= PTR_ERR(pi_ctx
->sig_mr
);
322 iser_err("Failed to allocate signature enabled mr err=%d\n",
326 desc
->reg_indicators
|= ISER_SIG_KEY_VALID
;
328 desc
->reg_indicators
&= ~ISER_FASTREG_PROTECTED
;
330 iser_dbg("Create fr_desc %p page_list %p\n",
331 desc
, desc
->data_frpl
->page_list
);
335 ib_dereg_mr(desc
->pi_ctx
->prot_mr
);
337 ib_free_fast_reg_page_list(desc
->pi_ctx
->prot_frpl
);
340 pi_ctx_alloc_failure
:
341 ib_dereg_mr(desc
->data_mr
);
343 ib_free_fast_reg_page_list(desc
->data_frpl
);
349 * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
350 * for fast registration work requests.
351 * returns 0 on success, or errno code on failure
353 int iser_create_fastreg_pool(struct ib_conn
*ib_conn
, unsigned cmds_max
)
355 struct iser_device
*device
= ib_conn
->device
;
356 struct fast_reg_descriptor
*desc
;
359 INIT_LIST_HEAD(&ib_conn
->fastreg
.pool
);
360 ib_conn
->fastreg
.pool_size
= 0;
361 for (i
= 0; i
< cmds_max
; i
++) {
362 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
364 iser_err("Failed to allocate a new fast_reg descriptor\n");
369 ret
= iser_create_fastreg_desc(device
->ib_device
, device
->pd
,
370 ib_conn
->pi_support
, desc
);
372 iser_err("Failed to create fastreg descriptor err=%d\n",
378 list_add_tail(&desc
->list
, &ib_conn
->fastreg
.pool
);
379 ib_conn
->fastreg
.pool_size
++;
385 iser_free_fastreg_pool(ib_conn
);
390 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
392 void iser_free_fastreg_pool(struct ib_conn
*ib_conn
)
394 struct fast_reg_descriptor
*desc
, *tmp
;
397 if (list_empty(&ib_conn
->fastreg
.pool
))
400 iser_info("freeing conn %p fr pool\n", ib_conn
);
402 list_for_each_entry_safe(desc
, tmp
, &ib_conn
->fastreg
.pool
, list
) {
403 list_del(&desc
->list
);
404 ib_free_fast_reg_page_list(desc
->data_frpl
);
405 ib_dereg_mr(desc
->data_mr
);
407 ib_free_fast_reg_page_list(desc
->pi_ctx
->prot_frpl
);
408 ib_dereg_mr(desc
->pi_ctx
->prot_mr
);
409 ib_destroy_mr(desc
->pi_ctx
->sig_mr
);
416 if (i
< ib_conn
->fastreg
.pool_size
)
417 iser_warn("pool still has %d regions registered\n",
418 ib_conn
->fastreg
.pool_size
- i
);
422 * iser_create_ib_conn_res - Queue-Pair (QP)
424 * returns 0 on success, -1 on failure
426 static int iser_create_ib_conn_res(struct ib_conn
*ib_conn
)
428 struct iser_device
*device
;
429 struct ib_qp_init_attr init_attr
;
431 int index
, min_index
= 0;
433 BUG_ON(ib_conn
->device
== NULL
);
435 device
= ib_conn
->device
;
437 memset(&init_attr
, 0, sizeof init_attr
);
439 mutex_lock(&ig
.connlist_mutex
);
440 /* select the CQ with the minimal number of usages */
441 for (index
= 0; index
< device
->comps_used
; index
++) {
442 if (device
->comps
[index
].active_qps
<
443 device
->comps
[min_index
].active_qps
)
446 ib_conn
->comp
= &device
->comps
[min_index
];
447 ib_conn
->comp
->active_qps
++;
448 mutex_unlock(&ig
.connlist_mutex
);
449 iser_info("cq index %d used for ib_conn %p\n", min_index
, ib_conn
);
451 init_attr
.event_handler
= iser_qp_event_callback
;
452 init_attr
.qp_context
= (void *)ib_conn
;
453 init_attr
.send_cq
= ib_conn
->comp
->cq
;
454 init_attr
.recv_cq
= ib_conn
->comp
->cq
;
455 init_attr
.cap
.max_recv_wr
= ISER_QP_MAX_RECV_DTOS
;
456 init_attr
.cap
.max_send_sge
= 2;
457 init_attr
.cap
.max_recv_sge
= 1;
458 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
459 init_attr
.qp_type
= IB_QPT_RC
;
460 if (ib_conn
->pi_support
) {
461 init_attr
.cap
.max_send_wr
= ISER_QP_SIG_MAX_REQ_DTOS
+ 1;
462 init_attr
.create_flags
|= IB_QP_CREATE_SIGNATURE_EN
;
464 init_attr
.cap
.max_send_wr
= ISER_QP_MAX_REQ_DTOS
+ 1;
467 ret
= rdma_create_qp(ib_conn
->cma_id
, device
->pd
, &init_attr
);
471 ib_conn
->qp
= ib_conn
->cma_id
->qp
;
472 iser_info("setting conn %p cma_id %p qp %p\n",
473 ib_conn
, ib_conn
->cma_id
,
474 ib_conn
->cma_id
->qp
);
478 iser_err("unable to alloc mem or create resource, err %d\n", ret
);
483 * based on the resolved device node GUID see if there already allocated
484 * device for this device. If there's no such, create one.
487 struct iser_device
*iser_device_find_by_ib_device(struct rdma_cm_id
*cma_id
)
489 struct iser_device
*device
;
491 mutex_lock(&ig
.device_list_mutex
);
493 list_for_each_entry(device
, &ig
.device_list
, ig_list
)
494 /* find if there's a match using the node GUID */
495 if (device
->ib_device
->node_guid
== cma_id
->device
->node_guid
)
498 device
= kzalloc(sizeof *device
, GFP_KERNEL
);
502 /* assign this device to the device */
503 device
->ib_device
= cma_id
->device
;
504 /* init the device and link it into ig device list */
505 if (iser_create_device_ib_res(device
)) {
510 list_add(&device
->ig_list
, &ig
.device_list
);
515 mutex_unlock(&ig
.device_list_mutex
);
519 /* if there's no demand for this device, release it */
520 static void iser_device_try_release(struct iser_device
*device
)
522 mutex_lock(&ig
.device_list_mutex
);
524 iser_info("device %p refcount %d\n", device
, device
->refcount
);
525 if (!device
->refcount
) {
526 iser_free_device_ib_res(device
);
527 list_del(&device
->ig_list
);
530 mutex_unlock(&ig
.device_list_mutex
);
534 * Called with state mutex held
536 static int iser_conn_state_comp_exch(struct iser_conn
*iser_conn
,
537 enum iser_conn_state comp
,
538 enum iser_conn_state exch
)
542 ret
= (iser_conn
->state
== comp
);
544 iser_conn
->state
= exch
;
549 void iser_release_work(struct work_struct
*work
)
551 struct iser_conn
*iser_conn
;
553 iser_conn
= container_of(work
, struct iser_conn
, release_work
);
555 /* Wait for conn_stop to complete */
556 wait_for_completion(&iser_conn
->stop_completion
);
557 /* Wait for IB resouces cleanup to complete */
558 wait_for_completion(&iser_conn
->ib_completion
);
560 mutex_lock(&iser_conn
->state_mutex
);
561 iser_conn
->state
= ISER_CONN_DOWN
;
562 mutex_unlock(&iser_conn
->state_mutex
);
564 iser_conn_release(iser_conn
);
568 * iser_free_ib_conn_res - release IB related resources
569 * @iser_conn: iser connection struct
570 * @destroy_device: indicator if we need to try to release
571 * the iser device (only iscsi shutdown and DEVICE_REMOVAL
574 * This routine is called with the iser state mutex held
575 * so the cm_id removal is out of here. It is Safe to
576 * be invoked multiple times.
578 static void iser_free_ib_conn_res(struct iser_conn
*iser_conn
,
581 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
582 struct iser_device
*device
= ib_conn
->device
;
584 iser_info("freeing conn %p cma_id %p qp %p\n",
585 iser_conn
, ib_conn
->cma_id
, ib_conn
->qp
);
587 iser_free_rx_descriptors(iser_conn
);
589 if (ib_conn
->qp
!= NULL
) {
590 ib_conn
->comp
->active_qps
--;
591 rdma_destroy_qp(ib_conn
->cma_id
);
595 if (destroy_device
&& device
!= NULL
) {
596 iser_device_try_release(device
);
597 ib_conn
->device
= NULL
;
602 * Frees all conn objects and deallocs conn descriptor
604 void iser_conn_release(struct iser_conn
*iser_conn
)
606 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
608 mutex_lock(&ig
.connlist_mutex
);
609 list_del(&iser_conn
->conn_list
);
610 mutex_unlock(&ig
.connlist_mutex
);
612 mutex_lock(&iser_conn
->state_mutex
);
613 if (iser_conn
->state
!= ISER_CONN_DOWN
)
614 iser_warn("iser conn %p state %d, expected state down.\n",
615 iser_conn
, iser_conn
->state
);
617 * In case we never got to bind stage, we still need to
618 * release IB resources (which is safe to call more than once).
620 iser_free_ib_conn_res(iser_conn
, true);
621 mutex_unlock(&iser_conn
->state_mutex
);
623 if (ib_conn
->cma_id
!= NULL
) {
624 rdma_destroy_id(ib_conn
->cma_id
);
625 ib_conn
->cma_id
= NULL
;
632 * triggers start of the disconnect procedures and wait for them to be done
633 * Called with state mutex held
635 int iser_conn_terminate(struct iser_conn
*iser_conn
)
637 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
638 struct ib_send_wr
*bad_wr
;
641 /* terminate the iser conn only if the conn state is UP */
642 if (!iser_conn_state_comp_exch(iser_conn
, ISER_CONN_UP
,
643 ISER_CONN_TERMINATING
))
646 iser_info("iser_conn %p state %d\n", iser_conn
, iser_conn
->state
);
648 /* suspend queuing of new iscsi commands */
649 if (iser_conn
->iscsi_conn
)
650 iscsi_suspend_queue(iser_conn
->iscsi_conn
);
653 * In case we didn't already clean up the cma_id (peer initiated
654 * a disconnection), we need to Cause the CMA to change the QP
657 if (ib_conn
->cma_id
) {
658 err
= rdma_disconnect(ib_conn
->cma_id
);
660 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
663 /* post an indication that all flush errors were consumed */
664 err
= ib_post_send(ib_conn
->qp
, &ib_conn
->beacon
, &bad_wr
);
666 iser_err("conn %p failed to post beacon", ib_conn
);
668 wait_for_completion(&ib_conn
->flush_comp
);
675 * Called with state mutex held
677 static void iser_connect_error(struct rdma_cm_id
*cma_id
)
679 struct iser_conn
*iser_conn
;
681 iser_conn
= (struct iser_conn
*)cma_id
->context
;
682 iser_conn
->state
= ISER_CONN_DOWN
;
686 * Called with state mutex held
688 static void iser_addr_handler(struct rdma_cm_id
*cma_id
)
690 struct iser_device
*device
;
691 struct iser_conn
*iser_conn
;
692 struct ib_conn
*ib_conn
;
695 iser_conn
= (struct iser_conn
*)cma_id
->context
;
696 if (iser_conn
->state
!= ISER_CONN_PENDING
)
700 ib_conn
= &iser_conn
->ib_conn
;
701 device
= iser_device_find_by_ib_device(cma_id
);
703 iser_err("device lookup/creation failed\n");
704 iser_connect_error(cma_id
);
708 ib_conn
->device
= device
;
710 /* connection T10-PI support */
711 if (iser_pi_enable
) {
712 if (!(device
->dev_attr
.device_cap_flags
&
713 IB_DEVICE_SIGNATURE_HANDOVER
)) {
714 iser_warn("T10-PI requested but not supported on %s, "
715 "continue without T10-PI\n",
716 ib_conn
->device
->ib_device
->name
);
717 ib_conn
->pi_support
= false;
719 ib_conn
->pi_support
= true;
723 ret
= rdma_resolve_route(cma_id
, 1000);
725 iser_err("resolve route failed: %d\n", ret
);
726 iser_connect_error(cma_id
);
732 * Called with state mutex held
734 static void iser_route_handler(struct rdma_cm_id
*cma_id
)
736 struct rdma_conn_param conn_param
;
738 struct iser_cm_hdr req_hdr
;
739 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
740 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
741 struct iser_device
*device
= ib_conn
->device
;
743 if (iser_conn
->state
!= ISER_CONN_PENDING
)
747 ret
= iser_create_ib_conn_res(ib_conn
);
751 memset(&conn_param
, 0, sizeof conn_param
);
752 conn_param
.responder_resources
= device
->dev_attr
.max_qp_rd_atom
;
753 conn_param
.initiator_depth
= 1;
754 conn_param
.retry_count
= 7;
755 conn_param
.rnr_retry_count
= 6;
757 memset(&req_hdr
, 0, sizeof(req_hdr
));
758 req_hdr
.flags
= (ISER_ZBVA_NOT_SUPPORTED
|
759 ISER_SEND_W_INV_NOT_SUPPORTED
);
760 conn_param
.private_data
= (void *)&req_hdr
;
761 conn_param
.private_data_len
= sizeof(struct iser_cm_hdr
);
763 ret
= rdma_connect(cma_id
, &conn_param
);
765 iser_err("failure connecting: %d\n", ret
);
771 iser_connect_error(cma_id
);
774 static void iser_connected_handler(struct rdma_cm_id
*cma_id
)
776 struct iser_conn
*iser_conn
;
777 struct ib_qp_attr attr
;
778 struct ib_qp_init_attr init_attr
;
780 iser_conn
= (struct iser_conn
*)cma_id
->context
;
781 if (iser_conn
->state
!= ISER_CONN_PENDING
)
785 (void)ib_query_qp(cma_id
->qp
, &attr
, ~0, &init_attr
);
786 iser_info("remote qpn:%x my qpn:%x\n", attr
.dest_qp_num
, cma_id
->qp
->qp_num
);
788 iser_conn
->state
= ISER_CONN_UP
;
789 complete(&iser_conn
->up_completion
);
792 static void iser_disconnected_handler(struct rdma_cm_id
*cma_id
)
794 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
796 if (iser_conn_terminate(iser_conn
)) {
797 if (iser_conn
->iscsi_conn
)
798 iscsi_conn_failure(iser_conn
->iscsi_conn
,
799 ISCSI_ERR_CONN_FAILED
);
801 iser_err("iscsi_iser connection isn't bound\n");
805 static void iser_cleanup_handler(struct rdma_cm_id
*cma_id
,
808 struct iser_conn
*iser_conn
= (struct iser_conn
*)cma_id
->context
;
811 * We are not guaranteed that we visited disconnected_handler
812 * by now, call it here to be safe that we handle CM drep
815 iser_disconnected_handler(cma_id
);
816 iser_free_ib_conn_res(iser_conn
, destroy_device
);
817 complete(&iser_conn
->ib_completion
);
820 static int iser_cma_handler(struct rdma_cm_id
*cma_id
, struct rdma_cm_event
*event
)
822 struct iser_conn
*iser_conn
;
825 iser_conn
= (struct iser_conn
*)cma_id
->context
;
826 iser_info("event %d status %d conn %p id %p\n",
827 event
->event
, event
->status
, cma_id
->context
, cma_id
);
829 mutex_lock(&iser_conn
->state_mutex
);
830 switch (event
->event
) {
831 case RDMA_CM_EVENT_ADDR_RESOLVED
:
832 iser_addr_handler(cma_id
);
834 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
835 iser_route_handler(cma_id
);
837 case RDMA_CM_EVENT_ESTABLISHED
:
838 iser_connected_handler(cma_id
);
840 case RDMA_CM_EVENT_ADDR_ERROR
:
841 case RDMA_CM_EVENT_ROUTE_ERROR
:
842 case RDMA_CM_EVENT_CONNECT_ERROR
:
843 case RDMA_CM_EVENT_UNREACHABLE
:
844 case RDMA_CM_EVENT_REJECTED
:
845 iser_connect_error(cma_id
);
847 case RDMA_CM_EVENT_DISCONNECTED
:
848 case RDMA_CM_EVENT_ADDR_CHANGE
:
849 iser_disconnected_handler(cma_id
);
851 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
853 * we *must* destroy the device as we cannot rely
854 * on iscsid to be around to initiate error handling.
855 * also implicitly destroy the cma_id.
857 iser_cleanup_handler(cma_id
, true);
858 iser_conn
->ib_conn
.cma_id
= NULL
;
861 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
862 iser_cleanup_handler(cma_id
, false);
865 iser_err("Unexpected RDMA CM event (%d)\n", event
->event
);
868 mutex_unlock(&iser_conn
->state_mutex
);
873 void iser_conn_init(struct iser_conn
*iser_conn
)
875 iser_conn
->state
= ISER_CONN_INIT
;
876 iser_conn
->ib_conn
.post_recv_buf_count
= 0;
877 init_completion(&iser_conn
->ib_conn
.flush_comp
);
878 init_completion(&iser_conn
->stop_completion
);
879 init_completion(&iser_conn
->ib_completion
);
880 init_completion(&iser_conn
->up_completion
);
881 INIT_LIST_HEAD(&iser_conn
->conn_list
);
882 spin_lock_init(&iser_conn
->ib_conn
.lock
);
883 mutex_init(&iser_conn
->state_mutex
);
887 * starts the process of connecting to the target
888 * sleeps until the connection is established or rejected
890 int iser_connect(struct iser_conn
*iser_conn
,
891 struct sockaddr
*src_addr
,
892 struct sockaddr
*dst_addr
,
895 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
898 mutex_lock(&iser_conn
->state_mutex
);
900 sprintf(iser_conn
->name
, "%pISp", dst_addr
);
902 iser_info("connecting to: %s\n", iser_conn
->name
);
904 /* the device is known only --after-- address resolution */
905 ib_conn
->device
= NULL
;
907 iser_conn
->state
= ISER_CONN_PENDING
;
909 ib_conn
->beacon
.wr_id
= ISER_BEACON_WRID
;
910 ib_conn
->beacon
.opcode
= IB_WR_SEND
;
912 ib_conn
->cma_id
= rdma_create_id(iser_cma_handler
,
914 RDMA_PS_TCP
, IB_QPT_RC
);
915 if (IS_ERR(ib_conn
->cma_id
)) {
916 err
= PTR_ERR(ib_conn
->cma_id
);
917 iser_err("rdma_create_id failed: %d\n", err
);
921 err
= rdma_resolve_addr(ib_conn
->cma_id
, src_addr
, dst_addr
, 1000);
923 iser_err("rdma_resolve_addr failed: %d\n", err
);
928 wait_for_completion_interruptible(&iser_conn
->up_completion
);
930 if (iser_conn
->state
!= ISER_CONN_UP
) {
932 goto connect_failure
;
935 mutex_unlock(&iser_conn
->state_mutex
);
937 mutex_lock(&ig
.connlist_mutex
);
938 list_add(&iser_conn
->conn_list
, &ig
.connlist
);
939 mutex_unlock(&ig
.connlist_mutex
);
943 ib_conn
->cma_id
= NULL
;
945 iser_conn
->state
= ISER_CONN_DOWN
;
947 mutex_unlock(&iser_conn
->state_mutex
);
948 iser_conn_release(iser_conn
);
953 * iser_reg_page_vec - Register physical memory
955 * returns: 0 on success, errno code on failure
957 int iser_reg_page_vec(struct ib_conn
*ib_conn
,
958 struct iser_page_vec
*page_vec
,
959 struct iser_mem_reg
*mem_reg
)
961 struct ib_pool_fmr
*mem
;
966 page_list
= page_vec
->pages
;
967 io_addr
= page_list
[0];
969 mem
= ib_fmr_pool_map_phys(ib_conn
->fmr
.pool
,
975 status
= (int)PTR_ERR(mem
);
976 iser_err("ib_fmr_pool_map_phys failed: %d\n", status
);
980 mem_reg
->lkey
= mem
->fmr
->lkey
;
981 mem_reg
->rkey
= mem
->fmr
->rkey
;
982 mem_reg
->len
= page_vec
->length
* SIZE_4K
;
983 mem_reg
->va
= io_addr
;
985 mem_reg
->mem_h
= (void *)mem
;
987 mem_reg
->va
+= page_vec
->offset
;
988 mem_reg
->len
= page_vec
->data_size
;
990 iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
991 "entry[0]: (0x%08lx,%ld)] -> "
992 "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
993 page_vec
, page_vec
->length
,
994 (unsigned long)page_vec
->pages
[0],
995 (unsigned long)page_vec
->data_size
,
996 (unsigned int)mem_reg
->lkey
, mem_reg
->mem_h
,
997 (unsigned long)mem_reg
->va
, (unsigned long)mem_reg
->len
);
1002 * Unregister (previosuly registered using FMR) memory.
1003 * If memory is non-FMR does nothing.
1005 void iser_unreg_mem_fmr(struct iscsi_iser_task
*iser_task
,
1006 enum iser_data_dir cmd_dir
)
1008 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1014 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg
->mem_h
);
1016 ret
= ib_fmr_pool_unmap((struct ib_pool_fmr
*)reg
->mem_h
);
1018 iser_err("ib_fmr_pool_unmap failed %d\n", ret
);
1023 void iser_unreg_mem_fastreg(struct iscsi_iser_task
*iser_task
,
1024 enum iser_data_dir cmd_dir
)
1026 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1027 struct iser_conn
*iser_conn
= iser_task
->iser_conn
;
1028 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1029 struct fast_reg_descriptor
*desc
= reg
->mem_h
;
1036 spin_lock_bh(&ib_conn
->lock
);
1037 list_add_tail(&desc
->list
, &ib_conn
->fastreg
.pool
);
1038 spin_unlock_bh(&ib_conn
->lock
);
1041 int iser_post_recvl(struct iser_conn
*iser_conn
)
1043 struct ib_recv_wr rx_wr
, *rx_wr_failed
;
1044 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1048 sge
.addr
= iser_conn
->login_resp_dma
;
1049 sge
.length
= ISER_RX_LOGIN_SIZE
;
1050 sge
.lkey
= ib_conn
->device
->mr
->lkey
;
1052 rx_wr
.wr_id
= (unsigned long)iser_conn
->login_resp_buf
;
1053 rx_wr
.sg_list
= &sge
;
1057 ib_conn
->post_recv_buf_count
++;
1058 ib_ret
= ib_post_recv(ib_conn
->qp
, &rx_wr
, &rx_wr_failed
);
1060 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1061 ib_conn
->post_recv_buf_count
--;
1066 int iser_post_recvm(struct iser_conn
*iser_conn
, int count
)
1068 struct ib_recv_wr
*rx_wr
, *rx_wr_failed
;
1070 struct ib_conn
*ib_conn
= &iser_conn
->ib_conn
;
1071 unsigned int my_rx_head
= iser_conn
->rx_desc_head
;
1072 struct iser_rx_desc
*rx_desc
;
1074 for (rx_wr
= ib_conn
->rx_wr
, i
= 0; i
< count
; i
++, rx_wr
++) {
1075 rx_desc
= &iser_conn
->rx_descs
[my_rx_head
];
1076 rx_wr
->wr_id
= (unsigned long)rx_desc
;
1077 rx_wr
->sg_list
= &rx_desc
->rx_sg
;
1079 rx_wr
->next
= rx_wr
+ 1;
1080 my_rx_head
= (my_rx_head
+ 1) & iser_conn
->qp_max_recv_dtos_mask
;
1084 rx_wr
->next
= NULL
; /* mark end of work requests list */
1086 ib_conn
->post_recv_buf_count
+= count
;
1087 ib_ret
= ib_post_recv(ib_conn
->qp
, ib_conn
->rx_wr
, &rx_wr_failed
);
1089 iser_err("ib_post_recv failed ret=%d\n", ib_ret
);
1090 ib_conn
->post_recv_buf_count
-= count
;
1092 iser_conn
->rx_desc_head
= my_rx_head
;
1098 * iser_start_send - Initiate a Send DTO operation
1100 * returns 0 on success, -1 on failure
1102 int iser_post_send(struct ib_conn
*ib_conn
, struct iser_tx_desc
*tx_desc
,
1106 struct ib_send_wr send_wr
, *send_wr_failed
;
1108 ib_dma_sync_single_for_device(ib_conn
->device
->ib_device
,
1109 tx_desc
->dma_addr
, ISER_HEADERS_LEN
,
1112 send_wr
.next
= NULL
;
1113 send_wr
.wr_id
= (unsigned long)tx_desc
;
1114 send_wr
.sg_list
= tx_desc
->tx_sg
;
1115 send_wr
.num_sge
= tx_desc
->num_sge
;
1116 send_wr
.opcode
= IB_WR_SEND
;
1117 send_wr
.send_flags
= signal
? IB_SEND_SIGNALED
: 0;
1119 ib_ret
= ib_post_send(ib_conn
->qp
, &send_wr
, &send_wr_failed
);
1121 iser_err("ib_post_send failed, ret:%d\n", ib_ret
);
1127 * is_iser_tx_desc - Indicate if the completion wr_id
1128 * is a TX descriptor or not.
1129 * @iser_conn: iser connection
1130 * @wr_id: completion WR identifier
1132 * Since we cannot rely on wc opcode in FLUSH errors
1133 * we must work around it by checking if the wr_id address
1134 * falls in the iser connection rx_descs buffer. If so
1135 * it is an RX descriptor, otherwize it is a TX.
1138 is_iser_tx_desc(struct iser_conn
*iser_conn
, void *wr_id
)
1140 void *start
= iser_conn
->rx_descs
;
1141 int len
= iser_conn
->num_rx_descs
* sizeof(*iser_conn
->rx_descs
);
1143 if (wr_id
>= start
&& wr_id
< start
+ len
)
1150 * iser_handle_comp_error() - Handle error completion
1151 * @ib_conn: connection RDMA resources
1152 * @wc: work completion
1154 * Notes: We may handle a FLUSH error completion and in this case
1155 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1156 * error completion we should also notify iscsi layer that
1157 * connection is failed (in case we passed bind stage).
1160 iser_handle_comp_error(struct ib_conn
*ib_conn
,
1163 struct iser_conn
*iser_conn
= container_of(ib_conn
, struct iser_conn
,
1166 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1167 if (iser_conn
->iscsi_conn
)
1168 iscsi_conn_failure(iser_conn
->iscsi_conn
,
1169 ISCSI_ERR_CONN_FAILED
);
1171 if (is_iser_tx_desc(iser_conn
, (void *)wc
->wr_id
)) {
1172 struct iser_tx_desc
*desc
= (struct iser_tx_desc
*)wc
->wr_id
;
1174 if (desc
->type
== ISCSI_TX_DATAOUT
)
1175 kmem_cache_free(ig
.desc_cache
, desc
);
1177 ib_conn
->post_recv_buf_count
--;
1182 * iser_handle_wc - handle a single work completion
1183 * @wc: work completion
1185 * Soft-IRQ context, work completion can be either
1186 * SEND or RECV, and can turn out successful or
1187 * with error (or flush error).
1189 static void iser_handle_wc(struct ib_wc
*wc
)
1191 struct ib_conn
*ib_conn
;
1192 struct iser_tx_desc
*tx_desc
;
1193 struct iser_rx_desc
*rx_desc
;
1195 ib_conn
= wc
->qp
->qp_context
;
1196 if (wc
->status
== IB_WC_SUCCESS
) {
1197 if (wc
->opcode
== IB_WC_RECV
) {
1198 rx_desc
= (struct iser_rx_desc
*)wc
->wr_id
;
1199 iser_rcv_completion(rx_desc
, wc
->byte_len
,
1202 if (wc
->opcode
== IB_WC_SEND
) {
1203 tx_desc
= (struct iser_tx_desc
*)wc
->wr_id
;
1204 iser_snd_completion(tx_desc
, ib_conn
);
1206 iser_err("Unknown wc opcode %d\n", wc
->opcode
);
1209 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
1210 iser_err("wr id %llx status %d vend_err %x\n",
1211 wc
->wr_id
, wc
->status
, wc
->vendor_err
);
1213 iser_dbg("flush error: wr id %llx\n", wc
->wr_id
);
1215 if (wc
->wr_id
!= ISER_FASTREG_LI_WRID
&&
1216 wc
->wr_id
!= ISER_BEACON_WRID
)
1217 iser_handle_comp_error(ib_conn
, wc
);
1219 /* complete in case all flush errors were consumed */
1220 if (wc
->wr_id
== ISER_BEACON_WRID
)
1221 complete(&ib_conn
->flush_comp
);
1226 * iser_cq_tasklet_fn - iSER completion polling loop
1227 * @data: iSER completion context
1229 * Soft-IRQ context, polling connection CQ until
1230 * either CQ was empty or we exausted polling budget
1232 static void iser_cq_tasklet_fn(unsigned long data
)
1234 struct iser_comp
*comp
= (struct iser_comp
*)data
;
1235 struct ib_cq
*cq
= comp
->cq
;
1236 struct ib_wc
*const wcs
= comp
->wcs
;
1237 int i
, n
, completed
= 0;
1239 while ((n
= ib_poll_cq(cq
, ARRAY_SIZE(comp
->wcs
), wcs
)) > 0) {
1240 for (i
= 0; i
< n
; i
++)
1241 iser_handle_wc(&wcs
[i
]);
1244 if (completed
>= iser_cq_poll_limit
)
1249 * It is assumed here that arming CQ only once its empty
1250 * would not cause interrupts to be missed.
1252 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1254 iser_dbg("got %d completions\n", completed
);
1257 static void iser_cq_callback(struct ib_cq
*cq
, void *cq_context
)
1259 struct iser_comp
*comp
= cq_context
;
1261 tasklet_schedule(&comp
->tasklet
);
1264 u8
iser_check_task_pi_status(struct iscsi_iser_task
*iser_task
,
1265 enum iser_data_dir cmd_dir
, sector_t
*sector
)
1267 struct iser_mem_reg
*reg
= &iser_task
->rdma_regd
[cmd_dir
].reg
;
1268 struct fast_reg_descriptor
*desc
= reg
->mem_h
;
1269 unsigned long sector_size
= iser_task
->sc
->device
->sector_size
;
1270 struct ib_mr_status mr_status
;
1273 if (desc
&& desc
->reg_indicators
& ISER_FASTREG_PROTECTED
) {
1274 desc
->reg_indicators
&= ~ISER_FASTREG_PROTECTED
;
1275 ret
= ib_check_mr_status(desc
->pi_ctx
->sig_mr
,
1276 IB_MR_CHECK_SIG_STATUS
, &mr_status
);
1278 pr_err("ib_check_mr_status failed, ret %d\n", ret
);
1282 if (mr_status
.fail_status
& IB_MR_CHECK_SIG_STATUS
) {
1283 sector_t sector_off
= mr_status
.sig_err
.sig_err_offset
;
1285 do_div(sector_off
, sector_size
+ 8);
1286 *sector
= scsi_get_lba(iser_task
->sc
) + sector_off
;
1288 pr_err("PI error found type %d at sector %llx "
1289 "expected %x vs actual %x\n",
1290 mr_status
.sig_err
.err_type
,
1291 (unsigned long long)*sector
,
1292 mr_status
.sig_err
.expected
,
1293 mr_status
.sig_err
.actual
);
1295 switch (mr_status
.sig_err
.err_type
) {
1296 case IB_SIG_BAD_GUARD
:
1298 case IB_SIG_BAD_REFTAG
:
1300 case IB_SIG_BAD_APPTAG
:
1308 /* Not alot we can do here, return ambiguous guard error */