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 void iser_qp_event_callback(struct ib_event *cause, void *context)
49 iser_err("qp event %s (%d)\n",
50 ib_event_msg(cause->event), cause->event);
53 static void iser_event_handler(struct ib_event_handler *handler,
54 struct ib_event *event)
56 iser_err("async event %s (%d) on device %s port %d\n",
57 ib_event_msg(event->event), event->event,
58 dev_name(&event->device->dev), event->element.port_num);
62 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
63 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
66 * Return: 0 on success, -1 on failure
68 static int iser_create_device_ib_res(struct iser_device *device)
70 struct ib_device *ib_dev = device->ib_device;
72 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) {
73 iser_err("IB device does not support memory registrations\n");
77 device->pd = ib_alloc_pd(ib_dev,
78 iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
79 if (IS_ERR(device->pd))
82 INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
84 ib_register_event_handler(&device->event_handler);
88 iser_err("failed to allocate an IB resource\n");
93 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
94 * CQ and PD created with the device associated with the adaptor.
96 static void iser_free_device_ib_res(struct iser_device *device)
98 ib_unregister_event_handler(&device->event_handler);
99 ib_dealloc_pd(device->pd);
104 static struct iser_fr_desc *
105 iser_create_fastreg_desc(struct iser_device *device,
110 struct iser_fr_desc *desc;
111 struct ib_device *ib_dev = device->ib_device;
112 enum ib_mr_type mr_type;
115 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
117 return ERR_PTR(-ENOMEM);
119 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
120 mr_type = IB_MR_TYPE_SG_GAPS;
122 mr_type = IB_MR_TYPE_MEM_REG;
124 desc->rsc.mr = ib_alloc_mr(pd, mr_type, size);
125 if (IS_ERR(desc->rsc.mr)) {
126 ret = PTR_ERR(desc->rsc.mr);
127 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
132 desc->rsc.sig_mr = ib_alloc_mr_integrity(pd, size, size);
133 if (IS_ERR(desc->rsc.sig_mr)) {
134 ret = PTR_ERR(desc->rsc.sig_mr);
135 iser_err("Failed to allocate sig_mr err=%d\n", ret);
136 goto err_alloc_mr_integrity;
139 desc->rsc.mr_valid = 0;
143 err_alloc_mr_integrity:
144 ib_dereg_mr(desc->rsc.mr);
151 static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc)
153 struct iser_reg_resources *res = &desc->rsc;
155 ib_dereg_mr(res->mr);
157 ib_dereg_mr(res->sig_mr);
164 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
165 * for fast registration work requests.
166 * @ib_conn: connection RDMA resources
167 * @cmds_max: max number of SCSI commands for this connection
168 * @size: max number of pages per map request
170 * Return: 0 on success, or errno code on failure
172 int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
176 struct iser_device *device = ib_conn->device;
177 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
178 struct iser_fr_desc *desc;
181 INIT_LIST_HEAD(&fr_pool->list);
182 INIT_LIST_HEAD(&fr_pool->all_list);
183 spin_lock_init(&fr_pool->lock);
185 for (i = 0; i < cmds_max; i++) {
186 desc = iser_create_fastreg_desc(device, device->pd,
187 ib_conn->pi_support, size);
193 list_add_tail(&desc->list, &fr_pool->list);
194 list_add_tail(&desc->all_list, &fr_pool->all_list);
201 iser_free_fastreg_pool(ib_conn);
206 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
207 * @ib_conn: connection RDMA resources
209 void iser_free_fastreg_pool(struct ib_conn *ib_conn)
211 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
212 struct iser_fr_desc *desc, *tmp;
215 if (list_empty(&fr_pool->all_list))
218 iser_info("freeing conn %p fr pool\n", ib_conn);
220 list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
221 list_del(&desc->all_list);
222 iser_destroy_fastreg_desc(desc);
226 if (i < fr_pool->size)
227 iser_warn("pool still has %d regions registered\n",
232 * iser_create_ib_conn_res - Queue-Pair (QP)
234 * Return: 0 on success, -1 on failure
236 static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
238 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
239 struct iser_device *device;
240 struct ib_device *ib_dev;
241 struct ib_qp_init_attr init_attr;
243 unsigned int max_send_wr, cq_size;
245 BUG_ON(ib_conn->device == NULL);
247 device = ib_conn->device;
248 ib_dev = device->ib_device;
250 if (ib_conn->pi_support)
251 max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
253 max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
254 max_send_wr = min_t(unsigned int, max_send_wr,
255 (unsigned int)ib_dev->attrs.max_qp_wr);
257 cq_size = max_send_wr + ISER_QP_MAX_RECV_DTOS;
258 ib_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_SOFTIRQ);
259 if (IS_ERR(ib_conn->cq)) {
260 ret = PTR_ERR(ib_conn->cq);
263 ib_conn->cq_size = cq_size;
265 memset(&init_attr, 0, sizeof(init_attr));
267 init_attr.event_handler = iser_qp_event_callback;
268 init_attr.qp_context = (void *)ib_conn;
269 init_attr.send_cq = ib_conn->cq;
270 init_attr.recv_cq = ib_conn->cq;
271 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
272 init_attr.cap.max_send_sge = 2;
273 init_attr.cap.max_recv_sge = 1;
274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
276 init_attr.cap.max_send_wr = max_send_wr;
277 if (ib_conn->pi_support)
278 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
279 iser_conn->max_cmds = ISER_GET_MAX_XMIT_CMDS(max_send_wr - 1);
281 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
285 ib_conn->qp = ib_conn->cma_id->qp;
286 iser_info("setting conn %p cma_id %p qp %p max_send_wr %d\n",
287 ib_conn, ib_conn->cma_id,
288 ib_conn->cma_id->qp, max_send_wr);
292 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
294 iser_err("unable to alloc mem or create resource, err %d\n", ret);
300 * based on the resolved device node GUID see if there already allocated
301 * device for this device. If there's no such, create one.
304 struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
306 struct iser_device *device;
308 mutex_lock(&ig.device_list_mutex);
310 list_for_each_entry(device, &ig.device_list, ig_list)
311 /* find if there's a match using the node GUID */
312 if (device->ib_device->node_guid == cma_id->device->node_guid)
315 device = kzalloc(sizeof *device, GFP_KERNEL);
319 /* assign this device to the device */
320 device->ib_device = cma_id->device;
321 /* init the device and link it into ig device list */
322 if (iser_create_device_ib_res(device)) {
327 list_add(&device->ig_list, &ig.device_list);
332 mutex_unlock(&ig.device_list_mutex);
336 /* if there's no demand for this device, release it */
337 static void iser_device_try_release(struct iser_device *device)
339 mutex_lock(&ig.device_list_mutex);
341 iser_info("device %p refcount %d\n", device, device->refcount);
342 if (!device->refcount) {
343 iser_free_device_ib_res(device);
344 list_del(&device->ig_list);
347 mutex_unlock(&ig.device_list_mutex);
351 * Called with state mutex held
353 static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
354 enum iser_conn_state comp,
355 enum iser_conn_state exch)
359 ret = (iser_conn->state == comp);
361 iser_conn->state = exch;
366 void iser_release_work(struct work_struct *work)
368 struct iser_conn *iser_conn;
370 iser_conn = container_of(work, struct iser_conn, release_work);
372 /* Wait for conn_stop to complete */
373 wait_for_completion(&iser_conn->stop_completion);
374 /* Wait for IB resouces cleanup to complete */
375 wait_for_completion(&iser_conn->ib_completion);
377 mutex_lock(&iser_conn->state_mutex);
378 iser_conn->state = ISER_CONN_DOWN;
379 mutex_unlock(&iser_conn->state_mutex);
381 iser_conn_release(iser_conn);
385 * iser_free_ib_conn_res - release IB related resources
386 * @iser_conn: iser connection struct
387 * @destroy: indicator if we need to try to release the
388 * iser device and memory regoins pool (only iscsi
389 * shutdown and DEVICE_REMOVAL will use this).
391 * This routine is called with the iser state mutex held
392 * so the cm_id removal is out of here. It is Safe to
393 * be invoked multiple times.
395 static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
398 struct ib_conn *ib_conn = &iser_conn->ib_conn;
399 struct iser_device *device = ib_conn->device;
401 iser_info("freeing conn %p cma_id %p qp %p\n",
402 iser_conn, ib_conn->cma_id, ib_conn->qp);
404 if (ib_conn->qp != NULL) {
405 rdma_destroy_qp(ib_conn->cma_id);
406 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
411 if (iser_conn->rx_descs)
412 iser_free_rx_descriptors(iser_conn);
414 if (device != NULL) {
415 iser_device_try_release(device);
416 ib_conn->device = NULL;
422 * iser_conn_release - Frees all conn objects and deallocs conn descriptor
423 * @iser_conn: iSER connection context
425 void iser_conn_release(struct iser_conn *iser_conn)
427 struct ib_conn *ib_conn = &iser_conn->ib_conn;
429 mutex_lock(&ig.connlist_mutex);
430 list_del(&iser_conn->conn_list);
431 mutex_unlock(&ig.connlist_mutex);
433 mutex_lock(&iser_conn->state_mutex);
434 /* In case we endup here without ep_disconnect being invoked. */
435 if (iser_conn->state != ISER_CONN_DOWN) {
436 iser_warn("iser conn %p state %d, expected state down.\n",
437 iser_conn, iser_conn->state);
438 iscsi_destroy_endpoint(iser_conn->ep);
439 iser_conn->state = ISER_CONN_DOWN;
442 * In case we never got to bind stage, we still need to
443 * release IB resources (which is safe to call more than once).
445 iser_free_ib_conn_res(iser_conn, true);
446 mutex_unlock(&iser_conn->state_mutex);
448 if (ib_conn->cma_id != NULL) {
449 rdma_destroy_id(ib_conn->cma_id);
450 ib_conn->cma_id = NULL;
457 * iser_conn_terminate - triggers start of the disconnect procedures and
458 * waits for them to be done
459 * @iser_conn: iSER connection context
461 * Called with state mutex held
463 int iser_conn_terminate(struct iser_conn *iser_conn)
465 struct ib_conn *ib_conn = &iser_conn->ib_conn;
468 /* terminate the iser conn only if the conn state is UP */
469 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
470 ISER_CONN_TERMINATING))
473 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
475 /* suspend queuing of new iscsi commands */
476 if (iser_conn->iscsi_conn)
477 iscsi_suspend_queue(iser_conn->iscsi_conn);
480 * In case we didn't already clean up the cma_id (peer initiated
481 * a disconnection), we need to Cause the CMA to change the QP
484 if (ib_conn->cma_id) {
485 err = rdma_disconnect(ib_conn->cma_id);
487 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
490 /* block until all flush errors are consumed */
491 ib_drain_sq(ib_conn->qp);
498 * Called with state mutex held
500 static void iser_connect_error(struct rdma_cm_id *cma_id)
502 struct iser_conn *iser_conn;
504 iser_conn = (struct iser_conn *)cma_id->context;
505 iser_conn->state = ISER_CONN_TERMINATING;
509 iser_calc_scsi_params(struct iser_conn *iser_conn,
510 unsigned int max_sectors)
512 struct iser_device *device = iser_conn->ib_conn.device;
513 struct ib_device_attr *attr = &device->ib_device->attrs;
514 unsigned short sg_tablesize, sup_sg_tablesize;
515 unsigned short reserved_mr_pages;
519 * FRs without SG_GAPS can only map up to a (device) page per entry,
520 * but if the first entry is misaligned we'll end up using two entries
521 * (head and tail) for a single page worth data, so one additional
524 if (attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
525 reserved_mr_pages = 0;
527 reserved_mr_pages = 1;
529 if (iser_conn->ib_conn.pi_support)
530 max_num_sg = attr->max_pi_fast_reg_page_list_len;
532 max_num_sg = attr->max_fast_reg_page_list_len;
534 sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K);
535 sup_sg_tablesize = min_t(uint, ISCSI_ISER_MAX_SG_TABLESIZE,
536 max_num_sg - reserved_mr_pages);
537 iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
538 iser_conn->pages_per_mr =
539 iser_conn->scsi_sg_tablesize + reserved_mr_pages;
543 * Called with state mutex held
545 static void iser_addr_handler(struct rdma_cm_id *cma_id)
547 struct iser_device *device;
548 struct iser_conn *iser_conn;
549 struct ib_conn *ib_conn;
552 iser_conn = (struct iser_conn *)cma_id->context;
553 if (iser_conn->state != ISER_CONN_PENDING)
557 ib_conn = &iser_conn->ib_conn;
558 device = iser_device_find_by_ib_device(cma_id);
560 iser_err("device lookup/creation failed\n");
561 iser_connect_error(cma_id);
565 ib_conn->device = device;
567 /* connection T10-PI support */
568 if (iser_pi_enable) {
569 if (!(device->ib_device->attrs.device_cap_flags &
570 IB_DEVICE_INTEGRITY_HANDOVER)) {
571 iser_warn("T10-PI requested but not supported on %s, "
572 "continue without T10-PI\n",
573 dev_name(&ib_conn->device->ib_device->dev));
574 ib_conn->pi_support = false;
576 ib_conn->pi_support = true;
580 iser_calc_scsi_params(iser_conn, iser_max_sectors);
582 ret = rdma_resolve_route(cma_id, 1000);
584 iser_err("resolve route failed: %d\n", ret);
585 iser_connect_error(cma_id);
591 * Called with state mutex held
593 static void iser_route_handler(struct rdma_cm_id *cma_id)
595 struct rdma_conn_param conn_param;
597 struct iser_cm_hdr req_hdr;
598 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
599 struct ib_conn *ib_conn = &iser_conn->ib_conn;
600 struct ib_device *ib_dev = ib_conn->device->ib_device;
602 if (iser_conn->state != ISER_CONN_PENDING)
606 ret = iser_create_ib_conn_res(ib_conn);
610 memset(&conn_param, 0, sizeof conn_param);
611 conn_param.responder_resources = ib_dev->attrs.max_qp_rd_atom;
612 conn_param.initiator_depth = 1;
613 conn_param.retry_count = 7;
614 conn_param.rnr_retry_count = 6;
616 memset(&req_hdr, 0, sizeof(req_hdr));
617 req_hdr.flags = ISER_ZBVA_NOT_SUP;
618 if (!iser_always_reg)
619 req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
620 conn_param.private_data = (void *)&req_hdr;
621 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
623 ret = rdma_connect(cma_id, &conn_param);
625 iser_err("failure connecting: %d\n", ret);
631 iser_connect_error(cma_id);
634 static void iser_connected_handler(struct rdma_cm_id *cma_id,
635 const void *private_data)
637 struct iser_conn *iser_conn;
638 struct ib_qp_attr attr;
639 struct ib_qp_init_attr init_attr;
641 iser_conn = (struct iser_conn *)cma_id->context;
642 if (iser_conn->state != ISER_CONN_PENDING)
646 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
647 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
650 u8 flags = *(u8 *)private_data;
652 iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
655 iser_info("conn %p: negotiated %s invalidation\n",
656 iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
658 iser_conn->state = ISER_CONN_UP;
659 complete(&iser_conn->up_completion);
662 static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
664 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
666 if (iser_conn_terminate(iser_conn)) {
667 if (iser_conn->iscsi_conn)
668 iscsi_conn_failure(iser_conn->iscsi_conn,
669 ISCSI_ERR_CONN_FAILED);
671 iser_err("iscsi_iser connection isn't bound\n");
675 static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
678 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
681 * We are not guaranteed that we visited disconnected_handler
682 * by now, call it here to be safe that we handle CM drep
685 iser_disconnected_handler(cma_id);
686 iser_free_ib_conn_res(iser_conn, destroy);
687 complete(&iser_conn->ib_completion);
690 static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
692 struct iser_conn *iser_conn;
695 iser_conn = (struct iser_conn *)cma_id->context;
696 iser_info("%s (%d): status %d conn %p id %p\n",
697 rdma_event_msg(event->event), event->event,
698 event->status, cma_id->context, cma_id);
700 mutex_lock(&iser_conn->state_mutex);
701 switch (event->event) {
702 case RDMA_CM_EVENT_ADDR_RESOLVED:
703 iser_addr_handler(cma_id);
705 case RDMA_CM_EVENT_ROUTE_RESOLVED:
706 iser_route_handler(cma_id);
708 case RDMA_CM_EVENT_ESTABLISHED:
709 iser_connected_handler(cma_id, event->param.conn.private_data);
711 case RDMA_CM_EVENT_REJECTED:
712 iser_info("Connection rejected: %s\n",
713 rdma_reject_msg(cma_id, event->status));
715 case RDMA_CM_EVENT_ADDR_ERROR:
716 case RDMA_CM_EVENT_ROUTE_ERROR:
717 case RDMA_CM_EVENT_CONNECT_ERROR:
718 case RDMA_CM_EVENT_UNREACHABLE:
719 iser_connect_error(cma_id);
721 case RDMA_CM_EVENT_DISCONNECTED:
722 case RDMA_CM_EVENT_ADDR_CHANGE:
723 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
724 iser_cleanup_handler(cma_id, false);
726 case RDMA_CM_EVENT_DEVICE_REMOVAL:
728 * we *must* destroy the device as we cannot rely
729 * on iscsid to be around to initiate error handling.
730 * also if we are not in state DOWN implicitly destroy
733 iser_cleanup_handler(cma_id, true);
734 if (iser_conn->state != ISER_CONN_DOWN) {
735 iser_conn->ib_conn.cma_id = NULL;
740 iser_err("Unexpected RDMA CM event: %s (%d)\n",
741 rdma_event_msg(event->event), event->event);
744 mutex_unlock(&iser_conn->state_mutex);
749 void iser_conn_init(struct iser_conn *iser_conn)
751 struct ib_conn *ib_conn = &iser_conn->ib_conn;
753 iser_conn->state = ISER_CONN_INIT;
754 init_completion(&iser_conn->stop_completion);
755 init_completion(&iser_conn->ib_completion);
756 init_completion(&iser_conn->up_completion);
757 INIT_LIST_HEAD(&iser_conn->conn_list);
758 mutex_init(&iser_conn->state_mutex);
760 ib_conn->post_recv_buf_count = 0;
761 ib_conn->reg_cqe.done = iser_reg_comp;
765 * starts the process of connecting to the target
766 * sleeps until the connection is established or rejected
768 int iser_connect(struct iser_conn *iser_conn,
769 struct sockaddr *src_addr,
770 struct sockaddr *dst_addr,
773 struct ib_conn *ib_conn = &iser_conn->ib_conn;
776 mutex_lock(&iser_conn->state_mutex);
778 sprintf(iser_conn->name, "%pISp", dst_addr);
780 iser_info("connecting to: %s\n", iser_conn->name);
782 /* the device is known only --after-- address resolution */
783 ib_conn->device = NULL;
785 iser_conn->state = ISER_CONN_PENDING;
787 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
789 RDMA_PS_TCP, IB_QPT_RC);
790 if (IS_ERR(ib_conn->cma_id)) {
791 err = PTR_ERR(ib_conn->cma_id);
792 iser_err("rdma_create_id failed: %d\n", err);
796 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
798 iser_err("rdma_resolve_addr failed: %d\n", err);
803 wait_for_completion_interruptible(&iser_conn->up_completion);
805 if (iser_conn->state != ISER_CONN_UP) {
807 goto connect_failure;
810 mutex_unlock(&iser_conn->state_mutex);
812 mutex_lock(&ig.connlist_mutex);
813 list_add(&iser_conn->conn_list, &ig.connlist);
814 mutex_unlock(&ig.connlist_mutex);
818 ib_conn->cma_id = NULL;
820 iser_conn->state = ISER_CONN_DOWN;
822 mutex_unlock(&iser_conn->state_mutex);
823 iser_conn_release(iser_conn);
827 int iser_post_recvl(struct iser_conn *iser_conn)
829 struct ib_conn *ib_conn = &iser_conn->ib_conn;
830 struct iser_login_desc *desc = &iser_conn->login_desc;
831 struct ib_recv_wr wr;
834 desc->sge.addr = desc->rsp_dma;
835 desc->sge.length = ISER_RX_LOGIN_SIZE;
836 desc->sge.lkey = ib_conn->device->pd->local_dma_lkey;
838 desc->cqe.done = iser_login_rsp;
839 wr.wr_cqe = &desc->cqe;
840 wr.sg_list = &desc->sge;
844 ib_conn->post_recv_buf_count++;
845 ib_ret = ib_post_recv(ib_conn->qp, &wr, NULL);
847 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
848 ib_conn->post_recv_buf_count--;
854 int iser_post_recvm(struct iser_conn *iser_conn, int count)
856 struct ib_conn *ib_conn = &iser_conn->ib_conn;
857 unsigned int my_rx_head = iser_conn->rx_desc_head;
858 struct iser_rx_desc *rx_desc;
859 struct ib_recv_wr *wr;
862 for (wr = ib_conn->rx_wr, i = 0; i < count; i++, wr++) {
863 rx_desc = &iser_conn->rx_descs[my_rx_head];
864 rx_desc->cqe.done = iser_task_rsp;
865 wr->wr_cqe = &rx_desc->cqe;
866 wr->sg_list = &rx_desc->rx_sg;
869 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
873 wr->next = NULL; /* mark end of work requests list */
875 ib_conn->post_recv_buf_count += count;
876 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, NULL);
877 if (unlikely(ib_ret)) {
878 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
879 ib_conn->post_recv_buf_count -= count;
881 iser_conn->rx_desc_head = my_rx_head;
888 * iser_post_send - Initiate a Send DTO operation
889 * @ib_conn: connection RDMA resources
890 * @tx_desc: iSER TX descriptor
891 * @signal: true to send work request as SIGNALED
893 * Return: 0 on success, -1 on failure
895 int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
898 struct ib_send_wr *wr = &tx_desc->send_wr;
899 struct ib_send_wr *first_wr;
902 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
903 tx_desc->dma_addr, ISER_HEADERS_LEN,
907 wr->wr_cqe = &tx_desc->cqe;
908 wr->sg_list = tx_desc->tx_sg;
909 wr->num_sge = tx_desc->num_sge;
910 wr->opcode = IB_WR_SEND;
911 wr->send_flags = signal ? IB_SEND_SIGNALED : 0;
913 if (tx_desc->inv_wr.next)
914 first_wr = &tx_desc->inv_wr;
915 else if (tx_desc->reg_wr.wr.next)
916 first_wr = &tx_desc->reg_wr.wr;
920 ib_ret = ib_post_send(ib_conn->qp, first_wr, NULL);
921 if (unlikely(ib_ret))
922 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
928 u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
929 enum iser_data_dir cmd_dir, sector_t *sector)
931 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
932 struct iser_fr_desc *desc = reg->mem_h;
933 unsigned long sector_size = iser_task->sc->device->sector_size;
934 struct ib_mr_status mr_status;
937 if (desc && desc->sig_protected) {
938 desc->sig_protected = false;
939 ret = ib_check_mr_status(desc->rsc.sig_mr,
940 IB_MR_CHECK_SIG_STATUS, &mr_status);
942 iser_err("ib_check_mr_status failed, ret %d\n", ret);
943 /* Not a lot we can do, return ambiguous guard error */
948 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
949 sector_t sector_off = mr_status.sig_err.sig_err_offset;
951 sector_div(sector_off, sector_size + 8);
952 *sector = scsi_get_lba(iser_task->sc) + sector_off;
954 iser_err("PI error found type %d at sector %llx "
955 "expected %x vs actual %x\n",
956 mr_status.sig_err.err_type,
957 (unsigned long long)*sector,
958 mr_status.sig_err.expected,
959 mr_status.sig_err.actual);
961 switch (mr_status.sig_err.err_type) {
962 case IB_SIG_BAD_GUARD:
964 case IB_SIG_BAD_REFTAG:
966 case IB_SIG_BAD_APPTAG:
975 void iser_err_comp(struct ib_wc *wc, const char *type)
977 if (wc->status != IB_WC_WR_FLUSH_ERR) {
978 struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
980 iser_err("%s failure: %s (%d) vend_err %#x\n", type,
981 ib_wc_status_msg(wc->status), wc->status,
984 if (iser_conn->iscsi_conn)
985 iscsi_conn_failure(iser_conn->iscsi_conn,
986 ISCSI_ERR_CONN_FAILED);
988 iser_dbg("%s failure: %s (%d)\n", type,
989 ib_wc_status_msg(wc->status), wc->status);