IB/iser: Use pd->local_dma_lkey
[linux-2.6/btrfs-unstable.git] / drivers / infiniband / ulp / iser / iser_verbs.c
blobad2d2b50cd7f09449d810196b58d47330719d9bf
1 /*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
4 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
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
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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
32 * SOFTWARE.
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 + \
45 ISCSI_ISER_MAX_CONN)
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);
72 /**
73 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
74 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
75 * the adapator.
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;
82 int ret, i, max_cqe;
84 ret = ib_query_device(device->ib_device, dev_attr);
85 if (ret) {
86 pr_warn("Query device failed for %s\n", device->ib_device->name);
87 return ret;
90 ret = iser_assign_reg_ops(device);
91 if (ret)
92 return ret;
94 device->comps_used = min_t(int, num_online_cpus(),
95 device->ib_device->num_comp_vectors);
97 device->comps = kcalloc(device->comps_used, sizeof(*device->comps),
98 GFP_KERNEL);
99 if (!device->comps)
100 goto comps_err;
102 max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
104 iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n",
105 device->comps_used, device->ib_device->name,
106 device->ib_device->num_comp_vectors, max_cqe);
108 device->pd = ib_alloc_pd(device->ib_device);
109 if (IS_ERR(device->pd))
110 goto pd_err;
112 for (i = 0; i < device->comps_used; i++) {
113 struct ib_cq_init_attr cq_attr = {};
114 struct iser_comp *comp = &device->comps[i];
116 comp->device = device;
117 cq_attr.cqe = max_cqe;
118 cq_attr.comp_vector = i;
119 comp->cq = ib_create_cq(device->ib_device,
120 iser_cq_callback,
121 iser_cq_event_callback,
122 (void *)comp,
123 &cq_attr);
124 if (IS_ERR(comp->cq)) {
125 comp->cq = NULL;
126 goto cq_err;
129 if (ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP))
130 goto cq_err;
132 tasklet_init(&comp->tasklet, iser_cq_tasklet_fn,
133 (unsigned long)comp);
136 device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
137 IB_ACCESS_REMOTE_WRITE |
138 IB_ACCESS_REMOTE_READ);
139 if (IS_ERR(device->mr))
140 goto dma_mr_err;
142 INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
143 iser_event_handler);
144 if (ib_register_event_handler(&device->event_handler))
145 goto handler_err;
147 return 0;
149 handler_err:
150 ib_dereg_mr(device->mr);
151 dma_mr_err:
152 for (i = 0; i < device->comps_used; i++)
153 tasklet_kill(&device->comps[i].tasklet);
154 cq_err:
155 for (i = 0; i < device->comps_used; i++) {
156 struct iser_comp *comp = &device->comps[i];
158 if (comp->cq)
159 ib_destroy_cq(comp->cq);
161 ib_dealloc_pd(device->pd);
162 pd_err:
163 kfree(device->comps);
164 comps_err:
165 iser_err("failed to allocate an IB resource\n");
166 return -1;
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)
175 int i;
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);
183 comp->cq = NULL;
186 (void)ib_unregister_event_handler(&device->event_handler);
187 (void)ib_dereg_mr(device->mr);
188 (void)ib_dealloc_pd(device->pd);
190 kfree(device->comps);
191 device->comps = NULL;
193 device->mr = NULL;
194 device->pd = NULL;
198 * iser_alloc_fmr_pool - Creates FMR pool and page_vector
200 * returns 0 on success, or errno code on failure
202 int iser_alloc_fmr_pool(struct ib_conn *ib_conn,
203 unsigned cmds_max,
204 unsigned int size)
206 struct iser_device *device = ib_conn->device;
207 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
208 struct iser_page_vec *page_vec;
209 struct iser_fr_desc *desc;
210 struct ib_fmr_pool *fmr_pool;
211 struct ib_fmr_pool_param params;
212 int ret;
214 INIT_LIST_HEAD(&fr_pool->list);
215 spin_lock_init(&fr_pool->lock);
217 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
218 if (!desc)
219 return -ENOMEM;
221 page_vec = kmalloc(sizeof(*page_vec) + (sizeof(u64) * size),
222 GFP_KERNEL);
223 if (!page_vec) {
224 ret = -ENOMEM;
225 goto err_frpl;
228 page_vec->pages = (u64 *)(page_vec + 1);
230 params.page_shift = SHIFT_4K;
231 params.max_pages_per_fmr = size;
232 /* make the pool size twice the max number of SCSI commands *
233 * the ML is expected to queue, watermark for unmap at 50% */
234 params.pool_size = cmds_max * 2;
235 params.dirty_watermark = cmds_max;
236 params.cache = 0;
237 params.flush_function = NULL;
238 params.access = (IB_ACCESS_LOCAL_WRITE |
239 IB_ACCESS_REMOTE_WRITE |
240 IB_ACCESS_REMOTE_READ);
242 fmr_pool = ib_create_fmr_pool(device->pd, &params);
243 if (IS_ERR(fmr_pool)) {
244 ret = PTR_ERR(fmr_pool);
245 iser_err("FMR allocation failed, err %d\n", ret);
246 goto err_fmr;
249 desc->rsc.page_vec = page_vec;
250 desc->rsc.fmr_pool = fmr_pool;
251 list_add(&desc->list, &fr_pool->list);
253 return 0;
255 err_fmr:
256 kfree(page_vec);
257 err_frpl:
258 kfree(desc);
260 return ret;
264 * iser_free_fmr_pool - releases the FMR pool and page vec
266 void iser_free_fmr_pool(struct ib_conn *ib_conn)
268 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
269 struct iser_fr_desc *desc;
271 desc = list_first_entry(&fr_pool->list,
272 struct iser_fr_desc, list);
273 list_del(&desc->list);
275 iser_info("freeing conn %p fmr pool %p\n",
276 ib_conn, desc->rsc.fmr_pool);
278 ib_destroy_fmr_pool(desc->rsc.fmr_pool);
279 kfree(desc->rsc.page_vec);
280 kfree(desc);
283 static int
284 iser_alloc_reg_res(struct ib_device *ib_device,
285 struct ib_pd *pd,
286 struct iser_reg_resources *res,
287 unsigned int size)
289 int ret;
291 res->frpl = ib_alloc_fast_reg_page_list(ib_device, size);
292 if (IS_ERR(res->frpl)) {
293 ret = PTR_ERR(res->frpl);
294 iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
295 ret);
296 return PTR_ERR(res->frpl);
299 res->mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG, size);
300 if (IS_ERR(res->mr)) {
301 ret = PTR_ERR(res->mr);
302 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
303 goto fast_reg_mr_failure;
305 res->mr_valid = 1;
307 return 0;
309 fast_reg_mr_failure:
310 ib_free_fast_reg_page_list(res->frpl);
312 return ret;
315 static void
316 iser_free_reg_res(struct iser_reg_resources *rsc)
318 ib_dereg_mr(rsc->mr);
319 ib_free_fast_reg_page_list(rsc->frpl);
322 static int
323 iser_alloc_pi_ctx(struct ib_device *ib_device,
324 struct ib_pd *pd,
325 struct iser_fr_desc *desc,
326 unsigned int size)
328 struct iser_pi_context *pi_ctx = NULL;
329 int ret;
331 desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
332 if (!desc->pi_ctx)
333 return -ENOMEM;
335 pi_ctx = desc->pi_ctx;
337 ret = iser_alloc_reg_res(ib_device, pd, &pi_ctx->rsc, size);
338 if (ret) {
339 iser_err("failed to allocate reg_resources\n");
340 goto alloc_reg_res_err;
343 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
344 if (IS_ERR(pi_ctx->sig_mr)) {
345 ret = PTR_ERR(pi_ctx->sig_mr);
346 goto sig_mr_failure;
348 pi_ctx->sig_mr_valid = 1;
349 desc->pi_ctx->sig_protected = 0;
351 return 0;
353 sig_mr_failure:
354 iser_free_reg_res(&pi_ctx->rsc);
355 alloc_reg_res_err:
356 kfree(desc->pi_ctx);
358 return ret;
361 static void
362 iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
364 iser_free_reg_res(&pi_ctx->rsc);
365 ib_dereg_mr(pi_ctx->sig_mr);
366 kfree(pi_ctx);
369 static struct iser_fr_desc *
370 iser_create_fastreg_desc(struct ib_device *ib_device,
371 struct ib_pd *pd,
372 bool pi_enable,
373 unsigned int size)
375 struct iser_fr_desc *desc;
376 int ret;
378 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
379 if (!desc)
380 return ERR_PTR(-ENOMEM);
382 ret = iser_alloc_reg_res(ib_device, pd, &desc->rsc, size);
383 if (ret)
384 goto reg_res_alloc_failure;
386 if (pi_enable) {
387 ret = iser_alloc_pi_ctx(ib_device, pd, desc, size);
388 if (ret)
389 goto pi_ctx_alloc_failure;
392 return desc;
394 pi_ctx_alloc_failure:
395 iser_free_reg_res(&desc->rsc);
396 reg_res_alloc_failure:
397 kfree(desc);
399 return ERR_PTR(ret);
403 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
404 * for fast registration work requests.
405 * returns 0 on success, or errno code on failure
407 int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
408 unsigned cmds_max,
409 unsigned int size)
411 struct iser_device *device = ib_conn->device;
412 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
413 struct iser_fr_desc *desc;
414 int i, ret;
416 INIT_LIST_HEAD(&fr_pool->list);
417 spin_lock_init(&fr_pool->lock);
418 fr_pool->size = 0;
419 for (i = 0; i < cmds_max; i++) {
420 desc = iser_create_fastreg_desc(device->ib_device, device->pd,
421 ib_conn->pi_support, size);
422 if (IS_ERR(desc)) {
423 ret = PTR_ERR(desc);
424 goto err;
427 list_add_tail(&desc->list, &fr_pool->list);
428 fr_pool->size++;
431 return 0;
433 err:
434 iser_free_fastreg_pool(ib_conn);
435 return ret;
439 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
441 void iser_free_fastreg_pool(struct ib_conn *ib_conn)
443 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
444 struct iser_fr_desc *desc, *tmp;
445 int i = 0;
447 if (list_empty(&fr_pool->list))
448 return;
450 iser_info("freeing conn %p fr pool\n", ib_conn);
452 list_for_each_entry_safe(desc, tmp, &fr_pool->list, list) {
453 list_del(&desc->list);
454 iser_free_reg_res(&desc->rsc);
455 if (desc->pi_ctx)
456 iser_free_pi_ctx(desc->pi_ctx);
457 kfree(desc);
458 ++i;
461 if (i < fr_pool->size)
462 iser_warn("pool still has %d regions registered\n",
463 fr_pool->size - i);
467 * iser_create_ib_conn_res - Queue-Pair (QP)
469 * returns 0 on success, -1 on failure
471 static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
473 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
474 ib_conn);
475 struct iser_device *device;
476 struct ib_device_attr *dev_attr;
477 struct ib_qp_init_attr init_attr;
478 int ret = -ENOMEM;
479 int index, min_index = 0;
481 BUG_ON(ib_conn->device == NULL);
483 device = ib_conn->device;
484 dev_attr = &device->dev_attr;
486 memset(&init_attr, 0, sizeof init_attr);
488 mutex_lock(&ig.connlist_mutex);
489 /* select the CQ with the minimal number of usages */
490 for (index = 0; index < device->comps_used; index++) {
491 if (device->comps[index].active_qps <
492 device->comps[min_index].active_qps)
493 min_index = index;
495 ib_conn->comp = &device->comps[min_index];
496 ib_conn->comp->active_qps++;
497 mutex_unlock(&ig.connlist_mutex);
498 iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
500 init_attr.event_handler = iser_qp_event_callback;
501 init_attr.qp_context = (void *)ib_conn;
502 init_attr.send_cq = ib_conn->comp->cq;
503 init_attr.recv_cq = ib_conn->comp->cq;
504 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
505 init_attr.cap.max_send_sge = 2;
506 init_attr.cap.max_recv_sge = 1;
507 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
508 init_attr.qp_type = IB_QPT_RC;
509 if (ib_conn->pi_support) {
510 init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
511 init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
512 iser_conn->max_cmds =
513 ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
514 } else {
515 if (dev_attr->max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
516 init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
517 iser_conn->max_cmds =
518 ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
519 } else {
520 init_attr.cap.max_send_wr = dev_attr->max_qp_wr;
521 iser_conn->max_cmds =
522 ISER_GET_MAX_XMIT_CMDS(dev_attr->max_qp_wr);
523 iser_dbg("device %s supports max_send_wr %d\n",
524 device->ib_device->name, dev_attr->max_qp_wr);
528 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
529 if (ret)
530 goto out_err;
532 ib_conn->qp = ib_conn->cma_id->qp;
533 iser_info("setting conn %p cma_id %p qp %p\n",
534 ib_conn, ib_conn->cma_id,
535 ib_conn->cma_id->qp);
536 return ret;
538 out_err:
539 mutex_lock(&ig.connlist_mutex);
540 ib_conn->comp->active_qps--;
541 mutex_unlock(&ig.connlist_mutex);
542 iser_err("unable to alloc mem or create resource, err %d\n", ret);
544 return ret;
548 * based on the resolved device node GUID see if there already allocated
549 * device for this device. If there's no such, create one.
551 static
552 struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
554 struct iser_device *device;
556 mutex_lock(&ig.device_list_mutex);
558 list_for_each_entry(device, &ig.device_list, ig_list)
559 /* find if there's a match using the node GUID */
560 if (device->ib_device->node_guid == cma_id->device->node_guid)
561 goto inc_refcnt;
563 device = kzalloc(sizeof *device, GFP_KERNEL);
564 if (device == NULL)
565 goto out;
567 /* assign this device to the device */
568 device->ib_device = cma_id->device;
569 /* init the device and link it into ig device list */
570 if (iser_create_device_ib_res(device)) {
571 kfree(device);
572 device = NULL;
573 goto out;
575 list_add(&device->ig_list, &ig.device_list);
577 inc_refcnt:
578 device->refcount++;
579 out:
580 mutex_unlock(&ig.device_list_mutex);
581 return device;
584 /* if there's no demand for this device, release it */
585 static void iser_device_try_release(struct iser_device *device)
587 mutex_lock(&ig.device_list_mutex);
588 device->refcount--;
589 iser_info("device %p refcount %d\n", device, device->refcount);
590 if (!device->refcount) {
591 iser_free_device_ib_res(device);
592 list_del(&device->ig_list);
593 kfree(device);
595 mutex_unlock(&ig.device_list_mutex);
599 * Called with state mutex held
601 static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
602 enum iser_conn_state comp,
603 enum iser_conn_state exch)
605 int ret;
607 ret = (iser_conn->state == comp);
608 if (ret)
609 iser_conn->state = exch;
611 return ret;
614 void iser_release_work(struct work_struct *work)
616 struct iser_conn *iser_conn;
618 iser_conn = container_of(work, struct iser_conn, release_work);
620 /* Wait for conn_stop to complete */
621 wait_for_completion(&iser_conn->stop_completion);
622 /* Wait for IB resouces cleanup to complete */
623 wait_for_completion(&iser_conn->ib_completion);
625 mutex_lock(&iser_conn->state_mutex);
626 iser_conn->state = ISER_CONN_DOWN;
627 mutex_unlock(&iser_conn->state_mutex);
629 iser_conn_release(iser_conn);
633 * iser_free_ib_conn_res - release IB related resources
634 * @iser_conn: iser connection struct
635 * @destroy: indicator if we need to try to release the
636 * iser device and memory regoins pool (only iscsi
637 * shutdown and DEVICE_REMOVAL will use this).
639 * This routine is called with the iser state mutex held
640 * so the cm_id removal is out of here. It is Safe to
641 * be invoked multiple times.
643 static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
644 bool destroy)
646 struct ib_conn *ib_conn = &iser_conn->ib_conn;
647 struct iser_device *device = ib_conn->device;
649 iser_info("freeing conn %p cma_id %p qp %p\n",
650 iser_conn, ib_conn->cma_id, ib_conn->qp);
652 if (ib_conn->qp != NULL) {
653 ib_conn->comp->active_qps--;
654 rdma_destroy_qp(ib_conn->cma_id);
655 ib_conn->qp = NULL;
658 if (destroy) {
659 if (iser_conn->rx_descs)
660 iser_free_rx_descriptors(iser_conn);
662 if (device != NULL) {
663 iser_device_try_release(device);
664 ib_conn->device = NULL;
670 * Frees all conn objects and deallocs conn descriptor
672 void iser_conn_release(struct iser_conn *iser_conn)
674 struct ib_conn *ib_conn = &iser_conn->ib_conn;
676 mutex_lock(&ig.connlist_mutex);
677 list_del(&iser_conn->conn_list);
678 mutex_unlock(&ig.connlist_mutex);
680 mutex_lock(&iser_conn->state_mutex);
681 /* In case we endup here without ep_disconnect being invoked. */
682 if (iser_conn->state != ISER_CONN_DOWN) {
683 iser_warn("iser conn %p state %d, expected state down.\n",
684 iser_conn, iser_conn->state);
685 iscsi_destroy_endpoint(iser_conn->ep);
686 iser_conn->state = ISER_CONN_DOWN;
689 * In case we never got to bind stage, we still need to
690 * release IB resources (which is safe to call more than once).
692 iser_free_ib_conn_res(iser_conn, true);
693 mutex_unlock(&iser_conn->state_mutex);
695 if (ib_conn->cma_id != NULL) {
696 rdma_destroy_id(ib_conn->cma_id);
697 ib_conn->cma_id = NULL;
700 kfree(iser_conn);
704 * triggers start of the disconnect procedures and wait for them to be done
705 * Called with state mutex held
707 int iser_conn_terminate(struct iser_conn *iser_conn)
709 struct ib_conn *ib_conn = &iser_conn->ib_conn;
710 struct ib_send_wr *bad_wr;
711 int err = 0;
713 /* terminate the iser conn only if the conn state is UP */
714 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
715 ISER_CONN_TERMINATING))
716 return 0;
718 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
720 /* suspend queuing of new iscsi commands */
721 if (iser_conn->iscsi_conn)
722 iscsi_suspend_queue(iser_conn->iscsi_conn);
725 * In case we didn't already clean up the cma_id (peer initiated
726 * a disconnection), we need to Cause the CMA to change the QP
727 * state to ERROR.
729 if (ib_conn->cma_id) {
730 err = rdma_disconnect(ib_conn->cma_id);
731 if (err)
732 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
733 iser_conn, err);
735 /* post an indication that all flush errors were consumed */
736 err = ib_post_send(ib_conn->qp, &ib_conn->beacon, &bad_wr);
737 if (err) {
738 iser_err("conn %p failed to post beacon", ib_conn);
739 return 1;
742 wait_for_completion(&ib_conn->flush_comp);
745 return 1;
749 * Called with state mutex held
751 static void iser_connect_error(struct rdma_cm_id *cma_id)
753 struct iser_conn *iser_conn;
755 iser_conn = (struct iser_conn *)cma_id->context;
756 iser_conn->state = ISER_CONN_TERMINATING;
759 static void
760 iser_calc_scsi_params(struct iser_conn *iser_conn,
761 unsigned int max_sectors)
763 struct iser_device *device = iser_conn->ib_conn.device;
764 unsigned short sg_tablesize, sup_sg_tablesize;
766 sg_tablesize = DIV_ROUND_UP(max_sectors * 512, SIZE_4K);
767 sup_sg_tablesize = min_t(unsigned, ISCSI_ISER_MAX_SG_TABLESIZE,
768 device->dev_attr.max_fast_reg_page_list_len);
770 if (sg_tablesize > sup_sg_tablesize) {
771 sg_tablesize = sup_sg_tablesize;
772 iser_conn->scsi_max_sectors = sg_tablesize * SIZE_4K / 512;
773 } else {
774 iser_conn->scsi_max_sectors = max_sectors;
777 iser_conn->scsi_sg_tablesize = sg_tablesize;
779 iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
780 iser_conn, iser_conn->scsi_sg_tablesize,
781 iser_conn->scsi_max_sectors);
785 * Called with state mutex held
787 static void iser_addr_handler(struct rdma_cm_id *cma_id)
789 struct iser_device *device;
790 struct iser_conn *iser_conn;
791 struct ib_conn *ib_conn;
792 int ret;
794 iser_conn = (struct iser_conn *)cma_id->context;
795 if (iser_conn->state != ISER_CONN_PENDING)
796 /* bailout */
797 return;
799 ib_conn = &iser_conn->ib_conn;
800 device = iser_device_find_by_ib_device(cma_id);
801 if (!device) {
802 iser_err("device lookup/creation failed\n");
803 iser_connect_error(cma_id);
804 return;
807 ib_conn->device = device;
809 /* connection T10-PI support */
810 if (iser_pi_enable) {
811 if (!(device->dev_attr.device_cap_flags &
812 IB_DEVICE_SIGNATURE_HANDOVER)) {
813 iser_warn("T10-PI requested but not supported on %s, "
814 "continue without T10-PI\n",
815 ib_conn->device->ib_device->name);
816 ib_conn->pi_support = false;
817 } else {
818 ib_conn->pi_support = true;
822 iser_calc_scsi_params(iser_conn, iser_max_sectors);
824 ret = rdma_resolve_route(cma_id, 1000);
825 if (ret) {
826 iser_err("resolve route failed: %d\n", ret);
827 iser_connect_error(cma_id);
828 return;
833 * Called with state mutex held
835 static void iser_route_handler(struct rdma_cm_id *cma_id)
837 struct rdma_conn_param conn_param;
838 int ret;
839 struct iser_cm_hdr req_hdr;
840 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
841 struct ib_conn *ib_conn = &iser_conn->ib_conn;
842 struct iser_device *device = ib_conn->device;
844 if (iser_conn->state != ISER_CONN_PENDING)
845 /* bailout */
846 return;
848 ret = iser_create_ib_conn_res(ib_conn);
849 if (ret)
850 goto failure;
852 memset(&conn_param, 0, sizeof conn_param);
853 conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
854 conn_param.initiator_depth = 1;
855 conn_param.retry_count = 7;
856 conn_param.rnr_retry_count = 6;
858 memset(&req_hdr, 0, sizeof(req_hdr));
859 req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
860 ISER_SEND_W_INV_NOT_SUPPORTED);
861 conn_param.private_data = (void *)&req_hdr;
862 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
864 ret = rdma_connect(cma_id, &conn_param);
865 if (ret) {
866 iser_err("failure connecting: %d\n", ret);
867 goto failure;
870 return;
871 failure:
872 iser_connect_error(cma_id);
875 static void iser_connected_handler(struct rdma_cm_id *cma_id)
877 struct iser_conn *iser_conn;
878 struct ib_qp_attr attr;
879 struct ib_qp_init_attr init_attr;
881 iser_conn = (struct iser_conn *)cma_id->context;
882 if (iser_conn->state != ISER_CONN_PENDING)
883 /* bailout */
884 return;
886 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
887 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
889 iser_conn->state = ISER_CONN_UP;
890 complete(&iser_conn->up_completion);
893 static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
895 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
897 if (iser_conn_terminate(iser_conn)) {
898 if (iser_conn->iscsi_conn)
899 iscsi_conn_failure(iser_conn->iscsi_conn,
900 ISCSI_ERR_CONN_FAILED);
901 else
902 iser_err("iscsi_iser connection isn't bound\n");
906 static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
907 bool destroy)
909 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
912 * We are not guaranteed that we visited disconnected_handler
913 * by now, call it here to be safe that we handle CM drep
914 * and flush errors.
916 iser_disconnected_handler(cma_id);
917 iser_free_ib_conn_res(iser_conn, destroy);
918 complete(&iser_conn->ib_completion);
921 static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
923 struct iser_conn *iser_conn;
924 int ret = 0;
926 iser_conn = (struct iser_conn *)cma_id->context;
927 iser_info("%s (%d): status %d conn %p id %p\n",
928 rdma_event_msg(event->event), event->event,
929 event->status, cma_id->context, cma_id);
931 mutex_lock(&iser_conn->state_mutex);
932 switch (event->event) {
933 case RDMA_CM_EVENT_ADDR_RESOLVED:
934 iser_addr_handler(cma_id);
935 break;
936 case RDMA_CM_EVENT_ROUTE_RESOLVED:
937 iser_route_handler(cma_id);
938 break;
939 case RDMA_CM_EVENT_ESTABLISHED:
940 iser_connected_handler(cma_id);
941 break;
942 case RDMA_CM_EVENT_ADDR_ERROR:
943 case RDMA_CM_EVENT_ROUTE_ERROR:
944 case RDMA_CM_EVENT_CONNECT_ERROR:
945 case RDMA_CM_EVENT_UNREACHABLE:
946 case RDMA_CM_EVENT_REJECTED:
947 iser_connect_error(cma_id);
948 break;
949 case RDMA_CM_EVENT_DISCONNECTED:
950 case RDMA_CM_EVENT_ADDR_CHANGE:
951 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
952 iser_cleanup_handler(cma_id, false);
953 break;
954 case RDMA_CM_EVENT_DEVICE_REMOVAL:
956 * we *must* destroy the device as we cannot rely
957 * on iscsid to be around to initiate error handling.
958 * also if we are not in state DOWN implicitly destroy
959 * the cma_id.
961 iser_cleanup_handler(cma_id, true);
962 if (iser_conn->state != ISER_CONN_DOWN) {
963 iser_conn->ib_conn.cma_id = NULL;
964 ret = 1;
966 break;
967 default:
968 iser_err("Unexpected RDMA CM event: %s (%d)\n",
969 rdma_event_msg(event->event), event->event);
970 break;
972 mutex_unlock(&iser_conn->state_mutex);
974 return ret;
977 void iser_conn_init(struct iser_conn *iser_conn)
979 iser_conn->state = ISER_CONN_INIT;
980 iser_conn->ib_conn.post_recv_buf_count = 0;
981 init_completion(&iser_conn->ib_conn.flush_comp);
982 init_completion(&iser_conn->stop_completion);
983 init_completion(&iser_conn->ib_completion);
984 init_completion(&iser_conn->up_completion);
985 INIT_LIST_HEAD(&iser_conn->conn_list);
986 mutex_init(&iser_conn->state_mutex);
990 * starts the process of connecting to the target
991 * sleeps until the connection is established or rejected
993 int iser_connect(struct iser_conn *iser_conn,
994 struct sockaddr *src_addr,
995 struct sockaddr *dst_addr,
996 int non_blocking)
998 struct ib_conn *ib_conn = &iser_conn->ib_conn;
999 int err = 0;
1001 mutex_lock(&iser_conn->state_mutex);
1003 sprintf(iser_conn->name, "%pISp", dst_addr);
1005 iser_info("connecting to: %s\n", iser_conn->name);
1007 /* the device is known only --after-- address resolution */
1008 ib_conn->device = NULL;
1010 iser_conn->state = ISER_CONN_PENDING;
1012 ib_conn->beacon.wr_id = ISER_BEACON_WRID;
1013 ib_conn->beacon.opcode = IB_WR_SEND;
1015 ib_conn->cma_id = rdma_create_id(iser_cma_handler,
1016 (void *)iser_conn,
1017 RDMA_PS_TCP, IB_QPT_RC);
1018 if (IS_ERR(ib_conn->cma_id)) {
1019 err = PTR_ERR(ib_conn->cma_id);
1020 iser_err("rdma_create_id failed: %d\n", err);
1021 goto id_failure;
1024 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
1025 if (err) {
1026 iser_err("rdma_resolve_addr failed: %d\n", err);
1027 goto addr_failure;
1030 if (!non_blocking) {
1031 wait_for_completion_interruptible(&iser_conn->up_completion);
1033 if (iser_conn->state != ISER_CONN_UP) {
1034 err = -EIO;
1035 goto connect_failure;
1038 mutex_unlock(&iser_conn->state_mutex);
1040 mutex_lock(&ig.connlist_mutex);
1041 list_add(&iser_conn->conn_list, &ig.connlist);
1042 mutex_unlock(&ig.connlist_mutex);
1043 return 0;
1045 id_failure:
1046 ib_conn->cma_id = NULL;
1047 addr_failure:
1048 iser_conn->state = ISER_CONN_DOWN;
1049 connect_failure:
1050 mutex_unlock(&iser_conn->state_mutex);
1051 iser_conn_release(iser_conn);
1052 return err;
1055 int iser_post_recvl(struct iser_conn *iser_conn)
1057 struct ib_recv_wr rx_wr, *rx_wr_failed;
1058 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1059 struct ib_sge sge;
1060 int ib_ret;
1062 sge.addr = iser_conn->login_resp_dma;
1063 sge.length = ISER_RX_LOGIN_SIZE;
1064 sge.lkey = ib_conn->device->pd->local_dma_lkey;
1066 rx_wr.wr_id = (uintptr_t)iser_conn->login_resp_buf;
1067 rx_wr.sg_list = &sge;
1068 rx_wr.num_sge = 1;
1069 rx_wr.next = NULL;
1071 ib_conn->post_recv_buf_count++;
1072 ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
1073 if (ib_ret) {
1074 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
1075 ib_conn->post_recv_buf_count--;
1077 return ib_ret;
1080 int iser_post_recvm(struct iser_conn *iser_conn, int count)
1082 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1083 int i, ib_ret;
1084 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1085 unsigned int my_rx_head = iser_conn->rx_desc_head;
1086 struct iser_rx_desc *rx_desc;
1088 for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
1089 rx_desc = &iser_conn->rx_descs[my_rx_head];
1090 rx_wr->wr_id = (uintptr_t)rx_desc;
1091 rx_wr->sg_list = &rx_desc->rx_sg;
1092 rx_wr->num_sge = 1;
1093 rx_wr->next = rx_wr + 1;
1094 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
1097 rx_wr--;
1098 rx_wr->next = NULL; /* mark end of work requests list */
1100 ib_conn->post_recv_buf_count += count;
1101 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
1102 if (ib_ret) {
1103 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
1104 ib_conn->post_recv_buf_count -= count;
1105 } else
1106 iser_conn->rx_desc_head = my_rx_head;
1107 return ib_ret;
1112 * iser_start_send - Initiate a Send DTO operation
1114 * returns 0 on success, -1 on failure
1116 int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
1117 bool signal)
1119 struct ib_send_wr *bad_wr, *wr = iser_tx_next_wr(tx_desc);
1120 int ib_ret;
1122 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
1123 tx_desc->dma_addr, ISER_HEADERS_LEN,
1124 DMA_TO_DEVICE);
1126 wr->next = NULL;
1127 wr->wr_id = (uintptr_t)tx_desc;
1128 wr->sg_list = tx_desc->tx_sg;
1129 wr->num_sge = tx_desc->num_sge;
1130 wr->opcode = IB_WR_SEND;
1131 wr->send_flags = signal ? IB_SEND_SIGNALED : 0;
1133 ib_ret = ib_post_send(ib_conn->qp, &tx_desc->wrs[0], &bad_wr);
1134 if (ib_ret)
1135 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
1136 ib_ret, bad_wr->opcode);
1138 return ib_ret;
1142 * is_iser_tx_desc - Indicate if the completion wr_id
1143 * is a TX descriptor or not.
1144 * @iser_conn: iser connection
1145 * @wr_id: completion WR identifier
1147 * Since we cannot rely on wc opcode in FLUSH errors
1148 * we must work around it by checking if the wr_id address
1149 * falls in the iser connection rx_descs buffer. If so
1150 * it is an RX descriptor, otherwize it is a TX.
1152 static inline bool
1153 is_iser_tx_desc(struct iser_conn *iser_conn, void *wr_id)
1155 void *start = iser_conn->rx_descs;
1156 int len = iser_conn->num_rx_descs * sizeof(*iser_conn->rx_descs);
1158 if (wr_id >= start && wr_id < start + len)
1159 return false;
1161 return true;
1165 * iser_handle_comp_error() - Handle error completion
1166 * @ib_conn: connection RDMA resources
1167 * @wc: work completion
1169 * Notes: We may handle a FLUSH error completion and in this case
1170 * we only cleanup in case TX type was DATAOUT. For non-FLUSH
1171 * error completion we should also notify iscsi layer that
1172 * connection is failed (in case we passed bind stage).
1174 static void
1175 iser_handle_comp_error(struct ib_conn *ib_conn,
1176 struct ib_wc *wc)
1178 void *wr_id = (void *)(uintptr_t)wc->wr_id;
1179 struct iser_conn *iser_conn = container_of(ib_conn, struct iser_conn,
1180 ib_conn);
1182 if (wc->status != IB_WC_WR_FLUSH_ERR)
1183 if (iser_conn->iscsi_conn)
1184 iscsi_conn_failure(iser_conn->iscsi_conn,
1185 ISCSI_ERR_CONN_FAILED);
1187 if (wc->wr_id == ISER_FASTREG_LI_WRID)
1188 return;
1190 if (is_iser_tx_desc(iser_conn, wr_id)) {
1191 struct iser_tx_desc *desc = wr_id;
1193 if (desc->type == ISCSI_TX_DATAOUT)
1194 kmem_cache_free(ig.desc_cache, desc);
1195 } else {
1196 ib_conn->post_recv_buf_count--;
1201 * iser_handle_wc - handle a single work completion
1202 * @wc: work completion
1204 * Soft-IRQ context, work completion can be either
1205 * SEND or RECV, and can turn out successful or
1206 * with error (or flush error).
1208 static void iser_handle_wc(struct ib_wc *wc)
1210 struct ib_conn *ib_conn;
1211 struct iser_tx_desc *tx_desc;
1212 struct iser_rx_desc *rx_desc;
1214 ib_conn = wc->qp->qp_context;
1215 if (likely(wc->status == IB_WC_SUCCESS)) {
1216 if (wc->opcode == IB_WC_RECV) {
1217 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
1218 iser_rcv_completion(rx_desc, wc->byte_len,
1219 ib_conn);
1220 } else
1221 if (wc->opcode == IB_WC_SEND) {
1222 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
1223 iser_snd_completion(tx_desc, ib_conn);
1224 } else {
1225 iser_err("Unknown wc opcode %d\n", wc->opcode);
1227 } else {
1228 if (wc->status != IB_WC_WR_FLUSH_ERR)
1229 iser_err("%s (%d): wr id %llx vend_err %x\n",
1230 ib_wc_status_msg(wc->status), wc->status,
1231 wc->wr_id, wc->vendor_err);
1232 else
1233 iser_dbg("%s (%d): wr id %llx\n",
1234 ib_wc_status_msg(wc->status), wc->status,
1235 wc->wr_id);
1237 if (wc->wr_id == ISER_BEACON_WRID)
1238 /* all flush errors were consumed */
1239 complete(&ib_conn->flush_comp);
1240 else
1241 iser_handle_comp_error(ib_conn, wc);
1246 * iser_cq_tasklet_fn - iSER completion polling loop
1247 * @data: iSER completion context
1249 * Soft-IRQ context, polling connection CQ until
1250 * either CQ was empty or we exausted polling budget
1252 static void iser_cq_tasklet_fn(unsigned long data)
1254 struct iser_comp *comp = (struct iser_comp *)data;
1255 struct ib_cq *cq = comp->cq;
1256 struct ib_wc *const wcs = comp->wcs;
1257 int i, n, completed = 0;
1259 while ((n = ib_poll_cq(cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
1260 for (i = 0; i < n; i++)
1261 iser_handle_wc(&wcs[i]);
1263 completed += n;
1264 if (completed >= iser_cq_poll_limit)
1265 break;
1269 * It is assumed here that arming CQ only once its empty
1270 * would not cause interrupts to be missed.
1272 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1274 iser_dbg("got %d completions\n", completed);
1277 static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
1279 struct iser_comp *comp = cq_context;
1281 tasklet_schedule(&comp->tasklet);
1284 u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
1285 enum iser_data_dir cmd_dir, sector_t *sector)
1287 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
1288 struct iser_fr_desc *desc = reg->mem_h;
1289 unsigned long sector_size = iser_task->sc->device->sector_size;
1290 struct ib_mr_status mr_status;
1291 int ret;
1293 if (desc && desc->pi_ctx->sig_protected) {
1294 desc->pi_ctx->sig_protected = 0;
1295 ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
1296 IB_MR_CHECK_SIG_STATUS, &mr_status);
1297 if (ret) {
1298 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1299 goto err;
1302 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1303 sector_t sector_off = mr_status.sig_err.sig_err_offset;
1305 do_div(sector_off, sector_size + 8);
1306 *sector = scsi_get_lba(iser_task->sc) + sector_off;
1308 pr_err("PI error found type %d at sector %llx "
1309 "expected %x vs actual %x\n",
1310 mr_status.sig_err.err_type,
1311 (unsigned long long)*sector,
1312 mr_status.sig_err.expected,
1313 mr_status.sig_err.actual);
1315 switch (mr_status.sig_err.err_type) {
1316 case IB_SIG_BAD_GUARD:
1317 return 0x1;
1318 case IB_SIG_BAD_REFTAG:
1319 return 0x3;
1320 case IB_SIG_BAD_APPTAG:
1321 return 0x2;
1326 return 0;
1327 err:
1328 /* Not alot we can do here, return ambiguous guard error */
1329 return 0x1;