blob: 699e075ae1b366e69ef39bd44c21893ba01c162c [file] [log] [blame]
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03001/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
Or Gerlitz3ee07d22014-04-01 16:28:41 +03004 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +03005 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030033 */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030034#include <linux/kernel.h>
35#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030037#include <linux/delay.h>
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030038
39#include "iscsi_iser.h"
40
41#define ISCSI_ISER_MAX_CONN 8
Sagi Grimberg6aabfa72014-10-01 14:02:09 +030042#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)
Sagi Grimbergff3dd522014-10-01 14:02:10 +030044#define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
45 ISCSI_ISER_MAX_CONN)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030046
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030047static void iser_qp_event_callback(struct ib_event *cause, void *context)
48{
Sagi Grimberg871e00a2015-05-18 13:40:30 +030049 iser_err("qp event %s (%d)\n",
50 ib_event_msg(cause->event), cause->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030051}
52
Or Gerlitz2110f9b2010-05-05 17:30:10 +030053static void iser_event_handler(struct ib_event_handler *handler,
54 struct ib_event *event)
55{
Sagi Grimberg871e00a2015-05-18 13:40:30 +030056 iser_err("async event %s (%d) on device %s port %d\n",
57 ib_event_msg(event->event), event->event,
Jason Gunthorpe6c854112018-09-20 16:42:27 -060058 dev_name(&event->device->dev), event->element.port_num);
Or Gerlitz2110f9b2010-05-05 17:30:10 +030059}
60
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -070061/*
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030062 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
63 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -070064 * the adaptor.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030065 *
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -070066 * Return: 0 on success, -1 on failure
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030067 */
68static int iser_create_device_ib_res(struct iser_device *device)
69{
Or Gerlitz4a061b22015-12-18 10:59:46 +020070 struct ib_device *ib_dev = device->ib_device;
Alex Tabachnik5a33a662012-09-23 15:17:44 +000071
Israel Rukshin1fc43132020-05-28 16:45:43 -030072 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) {
73 iser_err("IB device does not support memory registrations\n");
74 return -1;
75 }
Sagi Grimbergb4e155f2013-07-28 12:35:39 +030076
Christoph Hellwig8e612122016-09-05 12:56:18 +020077 device->pd = ib_alloc_pd(ib_dev,
78 iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030079 if (IS_ERR(device->pd))
80 goto pd_err;
81
Doug Ledford882f3b32015-12-22 17:03:15 -050082 INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
83 iser_event_handler);
Leon Romanovskydcc98812017-08-17 15:50:36 +030084 ib_register_event_handler(&device->event_handler);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030085 return 0;
86
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030087pd_err:
88 iser_err("failed to allocate an IB resource\n");
89 return -1;
90}
91
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -070092/*
Oliver Pinter38dc7322008-01-25 14:15:32 -080093 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -070094 * CQ and PD created with the device associated with the adaptor.
Or Gerlitz1cfa0a72006-05-11 10:02:46 +030095 */
96static void iser_free_device_ib_res(struct iser_device *device)
97{
Leon Romanovskydcc98812017-08-17 15:50:36 +030098 ib_unregister_event_handler(&device->event_handler);
Jason Gunthorpe7dd78642015-08-05 14:34:31 -060099 ib_dealloc_pd(device->pd);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300100
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300101 device->pd = NULL;
102}
103
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300104static struct iser_fr_desc *
Sagi Grimberg318d3112016-02-29 19:07:34 +0200105iser_create_fastreg_desc(struct iser_device *device,
Sagi Grimbergf8db6512015-08-06 18:33:03 +0300106 struct ib_pd *pd,
107 bool pi_enable,
108 unsigned int size)
Sagi Grimberg310b3472014-03-05 19:43:41 +0200109{
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300110 struct iser_fr_desc *desc;
Israel Rukshinb76a4392019-06-11 18:52:47 +0300111 struct ib_device *ib_dev = device->ib_device;
112 enum ib_mr_type mr_type;
Sagi Grimberg310b3472014-03-05 19:43:41 +0200113 int ret;
114
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300115 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
116 if (!desc)
117 return ERR_PTR(-ENOMEM);
118
Israel Rukshinb76a4392019-06-11 18:52:47 +0300119 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
120 mr_type = IB_MR_TYPE_SG_GAPS;
121 else
122 mr_type = IB_MR_TYPE_MEM_REG;
123
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);
128 goto err_alloc_mr;
129 }
Sagi Grimberg310b3472014-03-05 19:43:41 +0200130
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200131 if (pi_enable) {
Israel Rukshinb76a4392019-06-11 18:52:47 +0300132 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;
137 }
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200138 }
Israel Rukshinb76a4392019-06-11 18:52:47 +0300139 desc->rsc.mr_valid = 0;
Alex Tabachnik6b5a8fb2014-03-05 19:43:47 +0200140
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300141 return desc;
Sagi Grimbergd711d812015-08-06 18:32:53 +0300142
Israel Rukshinb76a4392019-06-11 18:52:47 +0300143err_alloc_mr_integrity:
144 ib_dereg_mr(desc->rsc.mr);
145err_alloc_mr:
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300146 kfree(desc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200147
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300148 return ERR_PTR(ret);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200149}
150
Israel Rukshinb76a4392019-06-11 18:52:47 +0300151static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc)
152{
153 struct iser_reg_resources *res = &desc->rsc;
154
155 ib_dereg_mr(res->mr);
156 if (res->sig_mr) {
157 ib_dereg_mr(res->sig_mr);
158 res->sig_mr = NULL;
159 }
160 kfree(desc);
161}
162
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300163/**
Sagi Grimberg48afbff2015-08-06 18:32:56 +0300164 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
Sagi Grimberg55878562013-07-28 12:35:42 +0300165 * for fast registration work requests.
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700166 * @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
169 *
170 * Return: 0 on success, or errno code on failure
Sagi Grimberg55878562013-07-28 12:35:42 +0300171 */
Sagi Grimbergf8db6512015-08-06 18:33:03 +0300172int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
173 unsigned cmds_max,
174 unsigned int size)
Sagi Grimberg55878562013-07-28 12:35:42 +0300175{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300176 struct iser_device *device = ib_conn->device;
Sagi Grimberg385ad872015-08-06 18:32:58 +0300177 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300178 struct iser_fr_desc *desc;
Sagi Grimberg55878562013-07-28 12:35:42 +0300179 int i, ret;
180
Adir Lev2b3bf952015-08-06 18:32:59 +0300181 INIT_LIST_HEAD(&fr_pool->list);
Sagi Grimbergea174c92017-02-27 20:16:33 +0200182 INIT_LIST_HEAD(&fr_pool->all_list);
Sagi Grimberg385ad872015-08-06 18:32:58 +0300183 spin_lock_init(&fr_pool->lock);
Adir Lev2b3bf952015-08-06 18:32:59 +0300184 fr_pool->size = 0;
Sagi Grimberg55878562013-07-28 12:35:42 +0300185 for (i = 0; i < cmds_max; i++) {
Sagi Grimberg318d3112016-02-29 19:07:34 +0200186 desc = iser_create_fastreg_desc(device, device->pd,
Sagi Grimbergf8db6512015-08-06 18:33:03 +0300187 ib_conn->pi_support, size);
Sagi Grimbergeb6ea8c32015-08-06 18:32:57 +0300188 if (IS_ERR(desc)) {
189 ret = PTR_ERR(desc);
Sagi Grimberg310b3472014-03-05 19:43:41 +0200190 goto err;
Sagi Grimberg55878562013-07-28 12:35:42 +0300191 }
192
Adir Lev2b3bf952015-08-06 18:32:59 +0300193 list_add_tail(&desc->list, &fr_pool->list);
Sagi Grimbergea174c92017-02-27 20:16:33 +0200194 list_add_tail(&desc->all_list, &fr_pool->all_list);
Adir Lev2b3bf952015-08-06 18:32:59 +0300195 fr_pool->size++;
Sagi Grimberg55878562013-07-28 12:35:42 +0300196 }
197
198 return 0;
Roi Dayan27ae2d12013-08-19 16:41:51 +0300199
Sagi Grimberg55878562013-07-28 12:35:42 +0300200err:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300201 iser_free_fastreg_pool(ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300202 return ret;
203}
204
205/**
Sagi Grimberg7306b8f2014-03-05 19:43:39 +0200206 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700207 * @ib_conn: connection RDMA resources
Sagi Grimberg55878562013-07-28 12:35:42 +0300208 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300209void iser_free_fastreg_pool(struct ib_conn *ib_conn)
Sagi Grimberg55878562013-07-28 12:35:42 +0300210{
Sagi Grimberg385ad872015-08-06 18:32:58 +0300211 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300212 struct iser_fr_desc *desc, *tmp;
Sagi Grimberg55878562013-07-28 12:35:42 +0300213 int i = 0;
214
Sagi Grimbergea174c92017-02-27 20:16:33 +0200215 if (list_empty(&fr_pool->all_list))
Sagi Grimberg55878562013-07-28 12:35:42 +0300216 return;
217
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300218 iser_info("freeing conn %p fr pool\n", ib_conn);
Sagi Grimberg55878562013-07-28 12:35:42 +0300219
Sagi Grimbergea174c92017-02-27 20:16:33 +0200220 list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
221 list_del(&desc->all_list);
Israel Rukshinb76a4392019-06-11 18:52:47 +0300222 iser_destroy_fastreg_desc(desc);
Sagi Grimberg55878562013-07-28 12:35:42 +0300223 ++i;
224 }
225
Adir Lev2b3bf952015-08-06 18:32:59 +0300226 if (i < fr_pool->size)
Sagi Grimberg55878562013-07-28 12:35:42 +0300227 iser_warn("pool still has %d regions registered\n",
Adir Lev2b3bf952015-08-06 18:32:59 +0300228 fr_pool->size - i);
Sagi Grimberg55878562013-07-28 12:35:42 +0300229}
230
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700231/*
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300232 * iser_create_ib_conn_res - Queue-Pair (QP)
233 *
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700234 * Return: 0 on success, -1 on failure
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300235 */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300236static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300237{
Sagi Grimberg7edc5a92015-11-04 10:50:32 +0200238 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300239 struct iser_device *device;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200240 struct ib_device *ib_dev;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300241 struct ib_qp_init_attr init_attr;
242 int ret = -ENOMEM;
Yamin Friedmand56a7852020-07-22 16:56:27 +0300243 unsigned int max_send_wr, cq_size;
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300244
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300245 BUG_ON(ib_conn->device == NULL);
Shlomo Pongratz986db0d2013-07-28 12:35:37 +0300246
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300247 device = ib_conn->device;
Or Gerlitz4a061b22015-12-18 10:59:46 +0200248 ib_dev = device->ib_device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300249
Yamin Friedmand56a7852020-07-22 16:56:27 +0300250 if (ib_conn->pi_support)
251 max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
252 else
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);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300256
Yamin Friedmand56a7852020-07-22 16:56:27 +0300257 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);
261 goto cq_err;
Sagi Grimbergbf175542014-10-01 14:02:07 +0300262 }
Yamin Friedmand56a7852020-07-22 16:56:27 +0300263 ib_conn->cq_size = cq_size;
264
265 memset(&init_attr, 0, sizeof(init_attr));
Alex Tabachnik5a33a662012-09-23 15:17:44 +0000266
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300267 init_attr.event_handler = iser_qp_event_callback;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300268 init_attr.qp_context = (void *)ib_conn;
Yamin Friedmand56a7852020-07-22 16:56:27 +0300269 init_attr.send_cq = ib_conn->cq;
270 init_attr.recv_cq = ib_conn->cq;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300271 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
Or Gerlitzf19624a2010-02-08 13:19:56 +0000272 init_attr.cap.max_send_sge = 2;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000273 init_attr.cap.max_recv_sge = 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
Yamin Friedmand56a7852020-07-22 16:56:27 +0300276 init_attr.cap.max_send_wr = max_send_wr;
277 if (ib_conn->pi_support)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300278 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
Yamin Friedmand56a7852020-07-22 16:56:27 +0300279 iser_conn->max_cmds = ISER_GET_MAX_XMIT_CMDS(max_send_wr - 1);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300280
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300281 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300282 if (ret)
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300283 goto out_err;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300284
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300285 ib_conn->qp = ib_conn->cma_id->qp;
Yamin Friedmand56a7852020-07-22 16:56:27 +0300286 iser_info("setting conn %p cma_id %p qp %p max_send_wr %d\n",
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300287 ib_conn, ib_conn->cma_id,
Yamin Friedmand56a7852020-07-22 16:56:27 +0300288 ib_conn->cma_id->qp, max_send_wr);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300289 return ret;
290
Dan Carpenter9fda1ac2010-05-06 16:22:21 +0300291out_err:
Yamin Friedmand56a7852020-07-22 16:56:27 +0300292 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
293cq_err:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300294 iser_err("unable to alloc mem or create resource, err %d\n", ret);
Sagi Grimberg93acb7b2014-12-07 16:09:55 +0200295
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300296 return ret;
297}
298
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700299/*
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300300 * based on the resolved device node GUID see if there already allocated
301 * device for this device. If there's no such, create one.
302 */
303static
304struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
305{
Arne Redlich9a378272008-03-04 14:07:22 +0200306 struct iser_device *device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300307
308 mutex_lock(&ig.device_list_mutex);
309
Arne Redlich9a378272008-03-04 14:07:22 +0200310 list_for_each_entry(device, &ig.device_list, ig_list)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300311 /* find if there's a match using the node GUID */
312 if (device->ib_device->node_guid == cma_id->device->node_guid)
Arne Redlichd33ed422008-03-04 14:11:54 +0200313 goto inc_refcnt;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300314
Arne Redlich9a378272008-03-04 14:07:22 +0200315 device = kzalloc(sizeof *device, GFP_KERNEL);
316 if (device == NULL)
317 goto out;
318
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)) {
323 kfree(device);
324 device = NULL;
325 goto out;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300326 }
Arne Redlich9a378272008-03-04 14:07:22 +0200327 list_add(&device->ig_list, &ig.device_list);
328
Arne Redlichd33ed422008-03-04 14:11:54 +0200329inc_refcnt:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300330 device->refcount++;
Arne Redlichd33ed422008-03-04 14:11:54 +0200331out:
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300332 mutex_unlock(&ig.device_list_mutex);
333 return device;
334}
335
336/* if there's no demand for this device, release it */
337static void iser_device_try_release(struct iser_device *device)
338{
339 mutex_lock(&ig.device_list_mutex);
340 device->refcount--;
Roi Dayan4f363882013-05-01 13:25:25 +0000341 iser_info("device %p refcount %d\n", device, device->refcount);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300342 if (!device->refcount) {
343 iser_free_device_ib_res(device);
344 list_del(&device->ig_list);
345 kfree(device);
346 }
347 mutex_unlock(&ig.device_list_mutex);
348}
349
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700350/*
Ariel Nahum504130c2014-07-31 13:27:49 +0300351 * Called with state mutex held
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700352 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300353static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
354 enum iser_conn_state comp,
355 enum iser_conn_state exch)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300356{
357 int ret;
358
Sagi Grimberg5716af62014-10-01 14:01:57 +0300359 ret = (iser_conn->state == comp);
360 if (ret)
361 iser_conn->state = exch;
362
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300363 return ret;
364}
365
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300366void iser_release_work(struct work_struct *work)
367{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300368 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300369
Sagi Grimberg5716af62014-10-01 14:01:57 +0300370 iser_conn = container_of(work, struct iser_conn, release_work);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300371
Sagi Grimbergc107a6c2014-10-01 14:02:02 +0300372 /* 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);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300376
Sagi Grimberg5716af62014-10-01 14:01:57 +0300377 mutex_lock(&iser_conn->state_mutex);
378 iser_conn->state = ISER_CONN_DOWN;
379 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300380
Sagi Grimberg5716af62014-10-01 14:01:57 +0300381 iser_conn_release(iser_conn);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300382}
383
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300384/**
Sagi Grimberg96f15192014-10-01 14:02:00 +0300385 * iser_free_ib_conn_res - release IB related resources
386 * @iser_conn: iser connection struct
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200387 * @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).
Sagi Grimberg96f15192014-10-01 14:02:00 +0300390 *
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.
394 */
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300395static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200396 bool destroy)
Sagi Grimberg96f15192014-10-01 14:02:00 +0300397{
398 struct ib_conn *ib_conn = &iser_conn->ib_conn;
399 struct iser_device *device = ib_conn->device;
400
401 iser_info("freeing conn %p cma_id %p qp %p\n",
402 iser_conn, ib_conn->cma_id, ib_conn->qp);
403
Sagi Grimberg96f15192014-10-01 14:02:00 +0300404 if (ib_conn->qp != NULL) {
Sagi Grimberg96f15192014-10-01 14:02:00 +0300405 rdma_destroy_qp(ib_conn->cma_id);
Yamin Friedmand56a7852020-07-22 16:56:27 +0300406 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
Sagi Grimberg96f15192014-10-01 14:02:00 +0300407 ib_conn->qp = NULL;
408 }
409
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200410 if (destroy) {
411 if (iser_conn->rx_descs)
412 iser_free_rx_descriptors(iser_conn);
413
414 if (device != NULL) {
415 iser_device_try_release(device);
416 ib_conn->device = NULL;
417 }
Sagi Grimberg96f15192014-10-01 14:02:00 +0300418 }
419}
420
421/**
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700422 * iser_conn_release - Frees all conn objects and deallocs conn descriptor
423 * @iser_conn: iSER connection context
Roland Dreier41179e22007-07-17 18:37:42 -0700424 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300425void iser_conn_release(struct iser_conn *iser_conn)
Roland Dreier41179e22007-07-17 18:37:42 -0700426{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300427 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Roland Dreier41179e22007-07-17 18:37:42 -0700428
Roland Dreier41179e22007-07-17 18:37:42 -0700429 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300430 list_del(&iser_conn->conn_list);
Roland Dreier41179e22007-07-17 18:37:42 -0700431 mutex_unlock(&ig.connlist_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300432
Sagi Grimberg5716af62014-10-01 14:01:57 +0300433 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200434 /* In case we endup here without ep_disconnect being invoked. */
Ariel Nahum5426b172014-12-07 16:09:54 +0200435 if (iser_conn->state != ISER_CONN_DOWN) {
Ariel Nahumaea8f4d2014-10-01 14:02:06 +0300436 iser_warn("iser conn %p state %d, expected state down.\n",
437 iser_conn, iser_conn->state);
Ariel Nahum9a3119e2015-01-18 16:51:07 +0200438 iscsi_destroy_endpoint(iser_conn->ep);
Ariel Nahum5426b172014-12-07 16:09:54 +0200439 iser_conn->state = ISER_CONN_DOWN;
440 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300441 /*
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).
444 */
445 iser_free_ib_conn_res(iser_conn, true);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300446 mutex_unlock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300447
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300448 if (ib_conn->cma_id != NULL) {
449 rdma_destroy_id(ib_conn->cma_id);
450 ib_conn->cma_id = NULL;
Roi Dayan5b61ff42013-05-08 12:21:17 +0000451 }
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300452
Sagi Grimberg5716af62014-10-01 14:01:57 +0300453 kfree(iser_conn);
Roland Dreier41179e22007-07-17 18:37:42 -0700454}
455
456/**
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700457 * iser_conn_terminate - triggers start of the disconnect procedures and
458 * waits for them to be done
459 * @iser_conn: iSER connection context
460 *
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300461 * Called with state mutex held
462 */
463int iser_conn_terminate(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300464{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300465 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300466 int err = 0;
467
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300468 /* 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))
471 return 0;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300472
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300473 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
474
475 /* suspend queuing of new iscsi commands */
476 if (iser_conn->iscsi_conn)
477 iscsi_suspend_queue(iser_conn->iscsi_conn);
478
479 /*
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
482 * state to ERROR.
483 */
484 if (ib_conn->cma_id) {
485 err = rdma_disconnect(ib_conn->cma_id);
486 if (err)
487 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
488 iser_conn, err);
489
Steve Wise4c8ba942016-02-17 08:17:12 -0800490 /* block until all flush errors are consumed */
491 ib_drain_sq(ib_conn->qp);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300492 }
493
494 return 1;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300495}
496
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700497/*
Ariel Nahum504130c2014-07-31 13:27:49 +0300498 * Called with state mutex held
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700499 */
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300500static void iser_connect_error(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300501{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300502 struct iser_conn *iser_conn;
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300503
Sagi Grimberg5716af62014-10-01 14:01:57 +0300504 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimbergc4de4662015-04-14 18:08:11 +0300505 iser_conn->state = ISER_CONN_TERMINATING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300506}
507
Sagi Grimbergdf749cdc2015-08-06 18:33:04 +0300508static void
509iser_calc_scsi_params(struct iser_conn *iser_conn,
510 unsigned int max_sectors)
511{
512 struct iser_device *device = iser_conn->ib_conn.device;
Sergey Gorenko434dda42018-05-21 18:55:53 +0300513 struct ib_device_attr *attr = &device->ib_device->attrs;
Sagi Grimbergdf749cdc2015-08-06 18:33:04 +0300514 unsigned short sg_tablesize, sup_sg_tablesize;
Sergey Gorenko434dda42018-05-21 18:55:53 +0300515 unsigned short reserved_mr_pages;
Israel Rukshinb76a4392019-06-11 18:52:47 +0300516 u32 max_num_sg;
Sergey Gorenko434dda42018-05-21 18:55:53 +0300517
518 /*
Israel Rukshin1fc43132020-05-28 16:45:43 -0300519 * 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
522 * entry is required.
Sergey Gorenko434dda42018-05-21 18:55:53 +0300523 */
Israel Rukshin1fc43132020-05-28 16:45:43 -0300524 if (attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
Sergey Gorenko434dda42018-05-21 18:55:53 +0300525 reserved_mr_pages = 0;
526 else
527 reserved_mr_pages = 1;
Sagi Grimbergdf749cdc2015-08-06 18:33:04 +0300528
Israel Rukshinb76a4392019-06-11 18:52:47 +0300529 if (iser_conn->ib_conn.pi_support)
530 max_num_sg = attr->max_pi_fast_reg_page_list_len;
531 else
532 max_num_sg = attr->max_fast_reg_page_list_len;
533
Max Gurtovoy6eeff062019-09-25 00:05:48 +0300534 sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K);
Israel Rukshin1fc43132020-05-28 16:45:43 -0300535 sup_sg_tablesize = min_t(uint, ISCSI_ISER_MAX_SG_TABLESIZE,
536 max_num_sg - reserved_mr_pages);
Max Gurtovoy83236f02017-01-18 00:40:40 +0200537 iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
Sergey Gorenko434dda42018-05-21 18:55:53 +0300538 iser_conn->pages_per_mr =
539 iser_conn->scsi_sg_tablesize + reserved_mr_pages;
Sagi Grimbergdf749cdc2015-08-06 18:33:04 +0300540}
541
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700542/*
Ariel Nahum504130c2014-07-31 13:27:49 +0300543 * Called with state mutex held
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700544 */
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300545static void iser_addr_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300546{
547 struct iser_device *device;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300548 struct iser_conn *iser_conn;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300549 struct ib_conn *ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300550 int ret;
551
Sagi Grimberg5716af62014-10-01 14:01:57 +0300552 iser_conn = (struct iser_conn *)cma_id->context;
553 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300554 /* bailout */
555 return;
556
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300557 ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300558 device = iser_device_find_by_ib_device(cma_id);
Arne Redlichd33ed422008-03-04 14:11:54 +0200559 if (!device) {
560 iser_err("device lookup/creation failed\n");
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300561 iser_connect_error(cma_id);
562 return;
Arne Redlichd33ed422008-03-04 14:11:54 +0200563 }
564
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300565 ib_conn->device = device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300566
Alex Tabachnik7f733842014-03-05 19:43:46 +0200567 /* connection T10-PI support */
568 if (iser_pi_enable) {
Or Gerlitz4a061b22015-12-18 10:59:46 +0200569 if (!(device->ib_device->attrs.device_cap_flags &
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300570 IB_DEVICE_INTEGRITY_HANDOVER)) {
Alex Tabachnik7f733842014-03-05 19:43:46 +0200571 iser_warn("T10-PI requested but not supported on %s, "
572 "continue without T10-PI\n",
Jason Gunthorpe6c854112018-09-20 16:42:27 -0600573 dev_name(&ib_conn->device->ib_device->dev));
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300574 ib_conn->pi_support = false;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200575 } else {
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300576 ib_conn->pi_support = true;
Alex Tabachnik7f733842014-03-05 19:43:46 +0200577 }
578 }
579
Sagi Grimbergdf749cdc2015-08-06 18:33:04 +0300580 iser_calc_scsi_params(iser_conn, iser_max_sectors);
581
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300582 ret = rdma_resolve_route(cma_id, 1000);
583 if (ret) {
584 iser_err("resolve route failed: %d\n", ret);
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300585 iser_connect_error(cma_id);
586 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300587 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300588}
589
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700590/*
Ariel Nahum504130c2014-07-31 13:27:49 +0300591 * Called with state mutex held
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700592 */
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300593static void iser_route_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300594{
595 struct rdma_conn_param conn_param;
596 int ret;
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000597 struct iser_cm_hdr req_hdr;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300598 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300599 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Israel Rukshin1fc43132020-05-28 16:45:43 -0300600 struct ib_device *ib_dev = ib_conn->device->ib_device;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300601
Sagi Grimberg5716af62014-10-01 14:01:57 +0300602 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300603 /* bailout */
604 return;
605
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300606 ret = iser_create_ib_conn_res(ib_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300607 if (ret)
608 goto failure;
609
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300610 memset(&conn_param, 0, sizeof conn_param);
Israel Rukshin1fc43132020-05-28 16:45:43 -0300611 conn_param.responder_resources = ib_dev->attrs.max_qp_rd_atom;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300612 conn_param.initiator_depth = 1;
613 conn_param.retry_count = 7;
614 conn_param.rnr_retry_count = 6;
615
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000616 memset(&req_hdr, 0, sizeof(req_hdr));
Jenny Derzhavetz59caaed2015-12-24 12:20:48 +0200617 req_hdr.flags = ISER_ZBVA_NOT_SUP;
Israel Rukshin1fc43132020-05-28 16:45:43 -0300618 if (!iser_always_reg)
Jenny Derzhavetz59caaed2015-12-24 12:20:48 +0200619 req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
Sagi Grimbergd3cf81f2015-12-09 14:12:03 +0200620 conn_param.private_data = (void *)&req_hdr;
621 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
Or Gerlitz8d8399d2013-05-01 13:25:27 +0000622
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300623 ret = rdma_connect(cma_id, &conn_param);
624 if (ret) {
625 iser_err("failure connecting: %d\n", ret);
626 goto failure;
627 }
628
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300629 return;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300630failure:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300631 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300632}
633
Jenny Derzhavetz59caaed2015-12-24 12:20:48 +0200634static void iser_connected_handler(struct rdma_cm_id *cma_id,
635 const void *private_data)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300636{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300637 struct iser_conn *iser_conn;
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300638 struct ib_qp_attr attr;
639 struct ib_qp_init_attr init_attr;
640
Sagi Grimberg5716af62014-10-01 14:01:57 +0300641 iser_conn = (struct iser_conn *)cma_id->context;
642 if (iser_conn->state != ISER_CONN_PENDING)
Ariel Nahum504130c2014-07-31 13:27:49 +0300643 /* bailout */
644 return;
645
Or Gerlitz4f9208a2014-04-01 16:28:40 +0300646 (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);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300648
Jenny Derzhavetz59caaed2015-12-24 12:20:48 +0200649 if (private_data) {
650 u8 flags = *(u8 *)private_data;
651
652 iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
653 }
654
655 iser_info("conn %p: negotiated %s invalidation\n",
656 iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
657
Sagi Grimberg5716af62014-10-01 14:01:57 +0300658 iser_conn->state = ISER_CONN_UP;
659 complete(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300660}
661
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300662static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300663{
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300664 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300665
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300666 if (iser_conn_terminate(iser_conn)) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300667 if (iser_conn->iscsi_conn)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300668 iscsi_conn_failure(iser_conn->iscsi_conn,
669 ISCSI_ERR_CONN_FAILED);
Roi Dayan7d9eacf2014-02-04 16:54:54 +0200670 else
671 iser_err("iscsi_iser connection isn't bound\n");
672 }
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300673}
674
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300675static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200676 bool destroy)
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300677{
678 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
679
680 /*
681 * We are not guaranteed that we visited disconnected_handler
682 * by now, call it here to be safe that we handle CM drep
683 * and flush errors.
684 */
685 iser_disconnected_handler(cma_id);
Sagi Grimberg6606e6a2015-01-18 16:51:06 +0200686 iser_free_ib_conn_res(iser_conn, destroy);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300687 complete(&iser_conn->ib_completion);
688};
689
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300690static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
691{
Sagi Grimberg5716af62014-10-01 14:01:57 +0300692 struct iser_conn *iser_conn;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300693 int ret = 0;
Ariel Nahum504130c2014-07-31 13:27:49 +0300694
Sagi Grimberg5716af62014-10-01 14:01:57 +0300695 iser_conn = (struct iser_conn *)cma_id->context;
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300696 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);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300699
Sagi Grimberg5716af62014-10-01 14:01:57 +0300700 mutex_lock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300701 switch (event->event) {
702 case RDMA_CM_EVENT_ADDR_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300703 iser_addr_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300704 break;
705 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300706 iser_route_handler(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300707 break;
708 case RDMA_CM_EVENT_ESTABLISHED:
Jenny Derzhavetz59caaed2015-12-24 12:20:48 +0200709 iser_connected_handler(cma_id, event->param.conn.private_data);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300710 break;
Steve Wise97540bb2016-10-26 12:36:47 -0700711 case RDMA_CM_EVENT_REJECTED:
712 iser_info("Connection rejected: %s\n",
713 rdma_reject_msg(cma_id, event->status));
714 /* FALLTHROUGH */
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300715 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:
Ariel Nahumb73c3ad2014-05-22 11:00:18 +0300719 iser_connect_error(cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300720 break;
721 case RDMA_CM_EVENT_DISCONNECTED:
Or Gerlitz2f5de152008-07-22 14:16:21 -0700722 case RDMA_CM_EVENT_ADDR_CHANGE:
Ariel Nahum5426b172014-12-07 16:09:54 +0200723 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
724 iser_cleanup_handler(cma_id, false);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300725 break;
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300726 case RDMA_CM_EVENT_DEVICE_REMOVAL:
727 /*
728 * we *must* destroy the device as we cannot rely
729 * on iscsid to be around to initiate error handling.
Ariel Nahum5426b172014-12-07 16:09:54 +0200730 * also if we are not in state DOWN implicitly destroy
731 * the cma_id.
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300732 */
733 iser_cleanup_handler(cma_id, true);
Ariel Nahum5426b172014-12-07 16:09:54 +0200734 if (iser_conn->state != ISER_CONN_DOWN) {
735 iser_conn->ib_conn.cma_id = NULL;
736 ret = 1;
737 }
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300738 break;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300739 default:
Sagi Grimberg871e00a2015-05-18 13:40:30 +0300740 iser_err("Unexpected RDMA CM event: %s (%d)\n",
741 rdma_event_msg(event->event), event->event);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300742 break;
743 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300744 mutex_unlock(&iser_conn->state_mutex);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300745
746 return ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300747}
748
Sagi Grimberg5716af62014-10-01 14:01:57 +0300749void iser_conn_init(struct iser_conn *iser_conn)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300750{
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800751 struct ib_conn *ib_conn = &iser_conn->ib_conn;
752
Sagi Grimberg5716af62014-10-01 14:01:57 +0300753 iser_conn->state = ISER_CONN_INIT;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300754 init_completion(&iser_conn->stop_completion);
Sagi Grimbergc47a3c92014-10-01 14:02:01 +0300755 init_completion(&iser_conn->ib_completion);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300756 init_completion(&iser_conn->up_completion);
757 INIT_LIST_HEAD(&iser_conn->conn_list);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300758 mutex_init(&iser_conn->state_mutex);
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800759
760 ib_conn->post_recv_buf_count = 0;
761 ib_conn->reg_cqe.done = iser_reg_comp;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300762}
763
764 /**
765 * starts the process of connecting to the target
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200766 * sleeps until the connection is established or rejected
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300767 */
Sagi Grimberg5716af62014-10-01 14:01:57 +0300768int iser_connect(struct iser_conn *iser_conn,
Roi Dayan96ed02d2014-07-31 13:27:44 +0300769 struct sockaddr *src_addr,
770 struct sockaddr *dst_addr,
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300771 int non_blocking)
772{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300773 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300774 int err = 0;
775
Sagi Grimberg5716af62014-10-01 14:01:57 +0300776 mutex_lock(&iser_conn->state_mutex);
Ariel Nahum504130c2014-07-31 13:27:49 +0300777
Sagi Grimberg5716af62014-10-01 14:01:57 +0300778 sprintf(iser_conn->name, "%pISp", dst_addr);
Roi Dayan96ed02d2014-07-31 13:27:44 +0300779
Sagi Grimberg5716af62014-10-01 14:01:57 +0300780 iser_info("connecting to: %s\n", iser_conn->name);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300781
782 /* the device is known only --after-- address resolution */
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300783 ib_conn->device = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300784
Sagi Grimberg5716af62014-10-01 14:01:57 +0300785 iser_conn->state = ISER_CONN_PENDING;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300786
Guy Shapirofa201052015-10-22 15:20:10 +0300787 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300788 (void *)iser_conn,
789 RDMA_PS_TCP, IB_QPT_RC);
790 if (IS_ERR(ib_conn->cma_id)) {
791 err = PTR_ERR(ib_conn->cma_id);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300792 iser_err("rdma_create_id failed: %d\n", err);
793 goto id_failure;
794 }
795
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300796 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300797 if (err) {
798 iser_err("rdma_resolve_addr failed: %d\n", err);
799 goto addr_failure;
800 }
801
802 if (!non_blocking) {
Sagi Grimberg5716af62014-10-01 14:01:57 +0300803 wait_for_completion_interruptible(&iser_conn->up_completion);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300804
Sagi Grimberg5716af62014-10-01 14:01:57 +0300805 if (iser_conn->state != ISER_CONN_UP) {
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300806 err = -EIO;
807 goto connect_failure;
808 }
809 }
Sagi Grimberg5716af62014-10-01 14:01:57 +0300810 mutex_unlock(&iser_conn->state_mutex);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300811
812 mutex_lock(&ig.connlist_mutex);
Sagi Grimberg5716af62014-10-01 14:01:57 +0300813 list_add(&iser_conn->conn_list, &ig.connlist);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300814 mutex_unlock(&ig.connlist_mutex);
815 return 0;
816
817id_failure:
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300818 ib_conn->cma_id = NULL;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300819addr_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +0300820 iser_conn->state = ISER_CONN_DOWN;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300821connect_failure:
Sagi Grimberg5716af62014-10-01 14:01:57 +0300822 mutex_unlock(&iser_conn->state_mutex);
823 iser_conn_release(iser_conn);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300824 return err;
825}
826
Sagi Grimberg5716af62014-10-01 14:01:57 +0300827int iser_post_recvl(struct iser_conn *iser_conn)
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000828{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300829 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimberg0f512b32015-11-04 10:50:31 +0200830 struct iser_login_desc *desc = &iser_conn->login_desc;
Bart Van Assche604dbdc2018-07-18 09:25:19 -0700831 struct ib_recv_wr wr;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000832 int ib_ret;
833
Sagi Grimberg0f512b32015-11-04 10:50:31 +0200834 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;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000837
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800838 desc->cqe.done = iser_login_rsp;
839 wr.wr_cqe = &desc->cqe;
840 wr.sg_list = &desc->sge;
841 wr.num_sge = 1;
842 wr.next = NULL;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000843
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300844 ib_conn->post_recv_buf_count++;
Bart Van Assche604dbdc2018-07-18 09:25:19 -0700845 ib_ret = ib_post_recv(ib_conn->qp, &wr, NULL);
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000846 if (ib_ret) {
847 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300848 ib_conn->post_recv_buf_count--;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000849 }
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800850
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000851 return ib_ret;
852}
853
Sagi Grimberg5716af62014-10-01 14:01:57 +0300854int iser_post_recvm(struct iser_conn *iser_conn, int count)
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000855{
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300856 struct ib_conn *ib_conn = &iser_conn->ib_conn;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300857 unsigned int my_rx_head = iser_conn->rx_desc_head;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000858 struct iser_rx_desc *rx_desc;
Bart Van Assche604dbdc2018-07-18 09:25:19 -0700859 struct ib_recv_wr *wr;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800860 int i, ib_ret;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000861
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800862 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;
867 wr->num_sge = 1;
868 wr->next = wr + 1;
Sagi Grimberg5716af62014-10-01 14:01:57 +0300869 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000870 }
871
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800872 wr--;
873 wr->next = NULL; /* mark end of work requests list */
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000874
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300875 ib_conn->post_recv_buf_count += count;
Bart Van Assche604dbdc2018-07-18 09:25:19 -0700876 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, NULL);
Max Gurtovoy70bcc632019-09-24 00:32:49 +0300877 if (unlikely(ib_ret)) {
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000878 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300879 ib_conn->post_recv_buf_count -= count;
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000880 } else
Sagi Grimberg5716af62014-10-01 14:01:57 +0300881 iser_conn->rx_desc_head = my_rx_head;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800882
Or Gerlitzbcc60c32010-02-08 13:17:42 +0000883 return ib_ret;
884}
885
886
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300887/**
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700888 * 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
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300892 *
rd.dunlab@gmail.com134a42a2019-10-09 20:52:47 -0700893 * Return: 0 on success, -1 on failure
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300894 */
Sagi Grimberg6df5a122014-10-01 14:02:12 +0300895int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
896 bool signal)
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300897{
Israel Rukshinb9294f82019-06-11 18:52:48 +0300898 struct ib_send_wr *wr = &tx_desc->send_wr;
899 struct ib_send_wr *first_wr;
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300900 int ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300901
Sagi Grimberga4ee3532014-10-01 14:01:58 +0300902 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
Sagi Grimberg5716af62014-10-01 14:01:57 +0300903 tx_desc->dma_addr, ISER_HEADERS_LEN,
904 DMA_TO_DEVICE);
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300905
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300906 wr->next = NULL;
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800907 wr->wr_cqe = &tx_desc->cqe;
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300908 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;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300912
Israel Rukshinb9294f82019-06-11 18:52:48 +0300913 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;
917 else
918 first_wr = wr;
919
920 ib_ret = ib_post_send(ib_conn->qp, first_wr, NULL);
Max Gurtovoy70bcc632019-09-24 00:32:49 +0300921 if (unlikely(ib_ret))
Sagi Grimberg7332bed2015-08-06 18:33:06 +0300922 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
Bart Van Assche604dbdc2018-07-18 09:25:19 -0700923 ib_ret, wr->opcode);
Sagi Grimbergff3dd522014-10-01 14:02:10 +0300924
Or Gerlitzf19624a2010-02-08 13:19:56 +0000925 return ib_ret;
Or Gerlitz1cfa0a72006-05-11 10:02:46 +0300926}
927
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200928u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
929 enum iser_data_dir cmd_dir, sector_t *sector)
930{
Sagi Grimbergb130ede2015-04-14 18:08:19 +0300931 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
Sagi Grimberg5190cc22015-08-06 18:32:54 +0300932 struct iser_fr_desc *desc = reg->mem_h;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200933 unsigned long sector_size = iser_task->sc->device->sector_size;
934 struct ib_mr_status mr_status;
935 int ret;
936
Israel Rukshinb76a4392019-06-11 18:52:47 +0300937 if (desc && desc->sig_protected) {
zhengbinc9348332019-12-24 16:40:10 +0800938 desc->sig_protected = false;
Israel Rukshinb76a4392019-06-11 18:52:47 +0300939 ret = ib_check_mr_status(desc->rsc.sig_mr,
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200940 IB_MR_CHECK_SIG_STATUS, &mr_status);
941 if (ret) {
Max Gurtovoy3466c062019-10-06 15:56:20 +0300942 iser_err("ib_check_mr_status failed, ret %d\n", ret);
Sagi Grimberg24c34562018-11-14 10:17:01 -0800943 /* Not a lot we can do, return ambiguous guard error */
944 *sector = 0;
945 return 0x1;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200946 }
947
948 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
949 sector_t sector_off = mr_status.sig_err.sig_err_offset;
950
Arnd Bergmann2c63d102015-11-20 17:41:36 +0100951 sector_div(sector_off, sector_size + 8);
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200952 *sector = scsi_get_lba(iser_task->sc) + sector_off;
953
Max Gurtovoy3466c062019-10-06 15:56:20 +0300954 iser_err("PI error found type %d at sector %llx "
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200955 "expected %x vs actual %x\n",
Randy Dunlap39c978c2014-03-28 14:03:40 -0700956 mr_status.sig_err.err_type,
957 (unsigned long long)*sector,
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200958 mr_status.sig_err.expected,
959 mr_status.sig_err.actual);
960
961 switch (mr_status.sig_err.err_type) {
962 case IB_SIG_BAD_GUARD:
963 return 0x1;
964 case IB_SIG_BAD_REFTAG:
965 return 0x3;
966 case IB_SIG_BAD_APPTAG:
967 return 0x2;
968 }
969 }
970 }
971
972 return 0;
Sagi Grimberg0a7a08a2014-03-05 19:43:50 +0200973}
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800974
975void iser_err_comp(struct ib_wc *wc, const char *type)
976{
977 if (wc->status != IB_WC_WR_FLUSH_ERR) {
978 struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
979
Ajaykumar Hotchandanib04dc192017-09-27 21:26:08 +0300980 iser_err("%s failure: %s (%d) vend_err %#x\n", type,
Christoph Hellwigcfeb91b2015-12-11 11:54:28 -0800981 ib_wc_status_msg(wc->status), wc->status,
982 wc->vendor_err);
983
984 if (iser_conn->iscsi_conn)
985 iscsi_conn_failure(iser_conn->iscsi_conn,
986 ISCSI_ERR_CONN_FAILED);
987 } else {
988 iser_dbg("%s failure: %s (%d)\n", type,
989 ib_wc_status_msg(wc->status), wc->status);
990 }
991}