scsi: iser: Use scsi_get_sector() instead of scsi_get_lba()
[linux-2.6-block.git] / drivers / infiniband / ulp / iser / iser_verbs.c
CommitLineData
1cfa0a75
OG
1/*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
3ee07d27 4 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
1cfa0a75
OG
5 *
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.
1cfa0a75 33 */
1cfa0a75
OG
34#include <linux/kernel.h>
35#include <linux/module.h>
5a0e3ad6 36#include <linux/slab.h>
1cfa0a75 37#include <linux/delay.h>
1cfa0a75
OG
38
39#include "iscsi_iser.h"
40
41#define ISCSI_ISER_MAX_CONN 8
6aabfa76
SG
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)
ff3dd52d
SG
44#define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
45 ISCSI_ISER_MAX_CONN)
1cfa0a75 46
1cfa0a75
OG
47static void iser_qp_event_callback(struct ib_event *cause, void *context)
48{
871e00af
SG
49 iser_err("qp event %s (%d)\n",
50 ib_event_msg(cause->event), cause->event);
1cfa0a75
OG
51}
52
2110f9bf
OG
53static void iser_event_handler(struct ib_event_handler *handler,
54 struct ib_event *event)
55{
871e00af
SG
56 iser_err("async event %s (%d) on device %s port %d\n",
57 ib_event_msg(event->event), event->event,
6c854111 58 dev_name(&event->device->dev), event->element.port_num);
2110f9bf
OG
59}
60
134a42a6 61/*
1cfa0a75
OG
62 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
63 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
134a42a6 64 * the adaptor.
1cfa0a75 65 *
134a42a6 66 * Return: 0 on success, -1 on failure
1cfa0a75
OG
67 */
68static int iser_create_device_ib_res(struct iser_device *device)
69{
4a061b28 70 struct ib_device *ib_dev = device->ib_device;
5a33a669 71
1fc43132
IR
72 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 }
b4e155ff 76
8e61212d
CH
77 device->pd = ib_alloc_pd(ib_dev,
78 iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
1cfa0a75
OG
79 if (IS_ERR(device->pd))
80 goto pd_err;
81
882f3b3b
DL
82 INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
83 iser_event_handler);
dcc9881e 84 ib_register_event_handler(&device->event_handler);
1cfa0a75
OG
85 return 0;
86
1cfa0a75
OG
87pd_err:
88 iser_err("failed to allocate an IB resource\n");
89 return -1;
90}
91
134a42a6 92/*
38dc732f 93 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
134a42a6 94 * CQ and PD created with the device associated with the adaptor.
1cfa0a75
OG
95 */
96static void iser_free_device_ib_res(struct iser_device *device)
97{
dcc9881e 98 ib_unregister_event_handler(&device->event_handler);
7dd78647 99 ib_dealloc_pd(device->pd);
1cfa0a75 100
1cfa0a75
OG
101 device->pd = NULL;
102}
103
b76a4399
IR
104static struct iser_fr_desc *
105iser_create_fastreg_desc(struct iser_device *device,
106 struct ib_pd *pd,
107 bool pi_enable,
108 unsigned int size)
d711d81d 109{
b76a4399 110 struct iser_fr_desc *desc;
318d311e
SG
111 struct ib_device *ib_dev = device->ib_device;
112 enum ib_mr_type mr_type;
d711d81d
SG
113 int ret;
114
b76a4399
IR
115 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
116 if (!desc)
117 return ERR_PTR(-ENOMEM);
118
318d311e
SG
119 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
b76a4399
IR
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);
d711d81d 127 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
b76a4399 128 goto err_alloc_mr;
4dec2a27 129 }
4dec2a27 130
b76a4399
IR
131 if (pi_enable) {
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;
137 }
4dec2a27 138 }
b76a4399 139 desc->rsc.mr_valid = 0;
4dec2a27 140
b76a4399 141 return desc;
4dec2a27 142
b76a4399
IR
143err_alloc_mr_integrity:
144 ib_dereg_mr(desc->rsc.mr);
145err_alloc_mr:
146 kfree(desc);
4dec2a27 147
b76a4399 148 return ERR_PTR(ret);
4dec2a27
SG
149}
150
b76a4399 151static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc)
4dec2a27 152{
b76a4399 153 struct iser_reg_resources *res = &desc->rsc;
310b347c 154
b76a4399
IR
155 ib_dereg_mr(res->mr);
156 if (res->sig_mr) {
157 ib_dereg_mr(res->sig_mr);
158 res->sig_mr = NULL;
6b5a8fb0 159 }
eb6ea8c3 160 kfree(desc);
310b347c
SG
161}
162
5587856c 163/**
48afbff6 164 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
5587856c 165 * for fast registration work requests.
134a42a6 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
169 *
170 * Return: 0 on success, or errno code on failure
5587856c 171 */
f8db651d
SG
172int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
173 unsigned cmds_max,
174 unsigned int size)
5587856c 175{
a4ee3539 176 struct iser_device *device = ib_conn->device;
385ad87d 177 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
5190cc26 178 struct iser_fr_desc *desc;
5587856c
SG
179 int i, ret;
180
2b3bf958 181 INIT_LIST_HEAD(&fr_pool->list);
ea174c95 182 INIT_LIST_HEAD(&fr_pool->all_list);
385ad87d 183 spin_lock_init(&fr_pool->lock);
2b3bf958 184 fr_pool->size = 0;
5587856c 185 for (i = 0; i < cmds_max; i++) {
318d311e 186 desc = iser_create_fastreg_desc(device, device->pd,
f8db651d 187 ib_conn->pi_support, size);
eb6ea8c3
SG
188 if (IS_ERR(desc)) {
189 ret = PTR_ERR(desc);
310b347c 190 goto err;
5587856c
SG
191 }
192
2b3bf958 193 list_add_tail(&desc->list, &fr_pool->list);
ea174c95 194 list_add_tail(&desc->all_list, &fr_pool->all_list);
2b3bf958 195 fr_pool->size++;
5587856c
SG
196 }
197
198 return 0;
27ae2d1e 199
5587856c 200err:
a4ee3539 201 iser_free_fastreg_pool(ib_conn);
5587856c
SG
202 return ret;
203}
204
205/**
7306b8fa 206 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
134a42a6 207 * @ib_conn: connection RDMA resources
5587856c 208 */
a4ee3539 209void iser_free_fastreg_pool(struct ib_conn *ib_conn)
5587856c 210{
385ad87d 211 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
5190cc26 212 struct iser_fr_desc *desc, *tmp;
5587856c
SG
213 int i = 0;
214
ea174c95 215 if (list_empty(&fr_pool->all_list))
5587856c
SG
216 return;
217
a4ee3539 218 iser_info("freeing conn %p fr pool\n", ib_conn);
5587856c 219
ea174c95
SG
220 list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
221 list_del(&desc->all_list);
b76a4399 222 iser_destroy_fastreg_desc(desc);
5587856c
SG
223 ++i;
224 }
225
2b3bf958 226 if (i < fr_pool->size)
5587856c 227 iser_warn("pool still has %d regions registered\n",
2b3bf958 228 fr_pool->size - i);
5587856c
SG
229}
230
134a42a6 231/*
986db0d6
SP
232 * iser_create_ib_conn_res - Queue-Pair (QP)
233 *
134a42a6 234 * Return: 0 on success, -1 on failure
986db0d6 235 */
a4ee3539 236static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
986db0d6 237{
7edc5a99 238 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
986db0d6 239 struct iser_device *device;
4a061b28 240 struct ib_device *ib_dev;
986db0d6
SP
241 struct ib_qp_init_attr init_attr;
242 int ret = -ENOMEM;
d56a7852 243 unsigned int max_send_wr, cq_size;
986db0d6 244
a4ee3539 245 BUG_ON(ib_conn->device == NULL);
986db0d6 246
a4ee3539 247 device = ib_conn->device;
4a061b28 248 ib_dev = device->ib_device;
1cfa0a75 249
d56a7852
YF
250 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);
256
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);
261 goto cq_err;
bf175540 262 }
d56a7852
YF
263 ib_conn->cq_size = cq_size;
264
265 memset(&init_attr, 0, sizeof(init_attr));
5a33a669 266
1cfa0a75 267 init_attr.event_handler = iser_qp_event_callback;
a4ee3539 268 init_attr.qp_context = (void *)ib_conn;
d56a7852
YF
269 init_attr.send_cq = ib_conn->cq;
270 init_attr.recv_cq = ib_conn->cq;
1cfa0a75 271 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
f19624aa 272 init_attr.cap.max_send_sge = 2;
bcc60c38 273 init_attr.cap.max_recv_sge = 1;
1cfa0a75
OG
274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
d56a7852
YF
276 init_attr.cap.max_send_wr = max_send_wr;
277 if (ib_conn->pi_support)
c0a6cbb9 278 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
d56a7852 279 iser_conn->max_cmds = ISER_GET_MAX_XMIT_CMDS(max_send_wr - 1);
1cfa0a75 280
a4ee3539 281 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
1cfa0a75 282 if (ret)
9fda1ac5 283 goto out_err;
1cfa0a75 284
a4ee3539 285 ib_conn->qp = ib_conn->cma_id->qp;
d56a7852 286 iser_info("setting conn %p cma_id %p qp %p max_send_wr %d\n",
a4ee3539 287 ib_conn, ib_conn->cma_id,
d56a7852 288 ib_conn->cma_id->qp, max_send_wr);
1cfa0a75
OG
289 return ret;
290
9fda1ac5 291out_err:
d56a7852
YF
292 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
293cq_err:
1cfa0a75 294 iser_err("unable to alloc mem or create resource, err %d\n", ret);
93acb7bb 295
1cfa0a75
OG
296 return ret;
297}
298
134a42a6 299/*
1cfa0a75
OG
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.
302 */
303static
304struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
305{
9a378270 306 struct iser_device *device;
1cfa0a75
OG
307
308 mutex_lock(&ig.device_list_mutex);
309
9a378270 310 list_for_each_entry(device, &ig.device_list, ig_list)
1cfa0a75
OG
311 /* find if there's a match using the node GUID */
312 if (device->ib_device->node_guid == cma_id->device->node_guid)
d33ed425 313 goto inc_refcnt;
9a378270
AR
314
315 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;
1cfa0a75 326 }
9a378270
AR
327 list_add(&device->ig_list, &ig.device_list);
328
d33ed425 329inc_refcnt:
1cfa0a75 330 device->refcount++;
d33ed425 331out:
1cfa0a75
OG
332 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--;
4f363882 341 iser_info("device %p refcount %d\n", device, device->refcount);
1cfa0a75
OG
342 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
134a42a6 350/*
504130c0 351 * Called with state mutex held
134a42a6 352 */
5716af6e
SG
353static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
354 enum iser_conn_state comp,
355 enum iser_conn_state exch)
1cfa0a75
OG
356{
357 int ret;
358
5716af6e
SG
359 ret = (iser_conn->state == comp);
360 if (ret)
361 iser_conn->state = exch;
362
1cfa0a75
OG
363 return ret;
364}
365
b73c3ada
AN
366void iser_release_work(struct work_struct *work)
367{
5716af6e 368 struct iser_conn *iser_conn;
b73c3ada 369
5716af6e 370 iser_conn = container_of(work, struct iser_conn, release_work);
b73c3ada 371
c107a6c0
SG
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);
b73c3ada 376
5716af6e
SG
377 mutex_lock(&iser_conn->state_mutex);
378 iser_conn->state = ISER_CONN_DOWN;
379 mutex_unlock(&iser_conn->state_mutex);
504130c0 380
5716af6e 381 iser_conn_release(iser_conn);
b73c3ada
AN
382}
383
96f15198
SG
384/**
385 * iser_free_ib_conn_res - release IB related resources
386 * @iser_conn: iser connection struct
6606e6a2
SG
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).
96f15198
SG
390 *
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 */
c47a3c9e 395static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
6606e6a2 396 bool destroy)
96f15198
SG
397{
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
96f15198 404 if (ib_conn->qp != NULL) {
96f15198 405 rdma_destroy_qp(ib_conn->cma_id);
d56a7852 406 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
96f15198
SG
407 ib_conn->qp = NULL;
408 }
409
6606e6a2
SG
410 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 }
96f15198
SG
418 }
419}
420
41179e2d 421/**
134a42a6 422 * iser_conn_release - Frees all conn objects and deallocs conn descriptor
423 * @iser_conn: iSER connection context
41179e2d 424 */
5716af6e 425void iser_conn_release(struct iser_conn *iser_conn)
41179e2d 426{
a4ee3539 427 struct ib_conn *ib_conn = &iser_conn->ib_conn;
41179e2d 428
41179e2d 429 mutex_lock(&ig.connlist_mutex);
5716af6e 430 list_del(&iser_conn->conn_list);
41179e2d 431 mutex_unlock(&ig.connlist_mutex);
504130c0 432
5716af6e 433 mutex_lock(&iser_conn->state_mutex);
9a3119e4 434 /* In case we endup here without ep_disconnect being invoked. */
5426b171 435 if (iser_conn->state != ISER_CONN_DOWN) {
aea8f4df
AN
436 iser_warn("iser conn %p state %d, expected state down.\n",
437 iser_conn, iser_conn->state);
9a3119e4 438 iscsi_destroy_endpoint(iser_conn->ep);
5426b171
AN
439 iser_conn->state = ISER_CONN_DOWN;
440 }
c47a3c9e
SG
441 /*
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);
5716af6e 446 mutex_unlock(&iser_conn->state_mutex);
504130c0 447
a4ee3539
SG
448 if (ib_conn->cma_id != NULL) {
449 rdma_destroy_id(ib_conn->cma_id);
450 ib_conn->cma_id = NULL;
5b61ff43 451 }
a4ee3539 452
5716af6e 453 kfree(iser_conn);
41179e2d
RD
454}
455
1cfa0a75 456/**
134a42a6 457 * iser_conn_terminate - triggers start of the disconnect procedures and
458 * waits for them to be done
459 * @iser_conn: iSER connection context
460 *
c47a3c9e 461 * Called with state mutex held
1cfa0a75 462 */
c47a3c9e 463int iser_conn_terminate(struct iser_conn *iser_conn)
1cfa0a75 464{
a4ee3539 465 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1cfa0a75
OG
466 int err = 0;
467
c47a3c9e
SG
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))
471 return 0;
472
473 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.
1cfa0a75 483 */
c47a3c9e
SG
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
4c8ba94d
SW
490 /* block until all flush errors are consumed */
491 ib_drain_sq(ib_conn->qp);
c47a3c9e 492 }
1cfa0a75 493
c47a3c9e 494 return 1;
1cfa0a75
OG
495}
496
134a42a6 497/*
504130c0 498 * Called with state mutex held
134a42a6 499 */
b73c3ada 500static void iser_connect_error(struct rdma_cm_id *cma_id)
1cfa0a75 501{
5716af6e 502 struct iser_conn *iser_conn;
b73c3ada 503
5716af6e 504 iser_conn = (struct iser_conn *)cma_id->context;
c4de4663 505 iser_conn->state = ISER_CONN_TERMINATING;
1cfa0a75
OG
506}
507
df749cdc
SG
508static 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;
434dda42 513 struct ib_device_attr *attr = &device->ib_device->attrs;
df749cdc 514 unsigned short sg_tablesize, sup_sg_tablesize;
434dda42 515 unsigned short reserved_mr_pages;
b76a4399 516 u32 max_num_sg;
434dda42
SG
517
518 /*
1fc43132
IR
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
522 * entry is required.
434dda42 523 */
1fc43132 524 if (attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
434dda42
SG
525 reserved_mr_pages = 0;
526 else
527 reserved_mr_pages = 1;
df749cdc 528
b76a4399
IR
529 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
6eeff06d 534 sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K);
1fc43132
IR
535 sup_sg_tablesize = min_t(uint, ISCSI_ISER_MAX_SG_TABLESIZE,
536 max_num_sg - reserved_mr_pages);
83236f01 537 iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
434dda42
SG
538 iser_conn->pages_per_mr =
539 iser_conn->scsi_sg_tablesize + reserved_mr_pages;
df749cdc
SG
540}
541
134a42a6 542/*
504130c0 543 * Called with state mutex held
134a42a6 544 */
b73c3ada 545static void iser_addr_handler(struct rdma_cm_id *cma_id)
1cfa0a75
OG
546{
547 struct iser_device *device;
5716af6e 548 struct iser_conn *iser_conn;
a4ee3539 549 struct ib_conn *ib_conn;
1cfa0a75
OG
550 int ret;
551
5716af6e
SG
552 iser_conn = (struct iser_conn *)cma_id->context;
553 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
554 /* bailout */
555 return;
556
a4ee3539 557 ib_conn = &iser_conn->ib_conn;
1cfa0a75 558 device = iser_device_find_by_ib_device(cma_id);
d33ed425
AR
559 if (!device) {
560 iser_err("device lookup/creation failed\n");
b73c3ada
AN
561 iser_connect_error(cma_id);
562 return;
d33ed425
AR
563 }
564
a4ee3539 565 ib_conn->device = device;
1cfa0a75 566
7f733847
AT
567 /* connection T10-PI support */
568 if (iser_pi_enable) {
4a061b28 569 if (!(device->ib_device->attrs.device_cap_flags &
c0a6cbb9 570 IB_DEVICE_INTEGRITY_HANDOVER)) {
7f733847
AT
571 iser_warn("T10-PI requested but not supported on %s, "
572 "continue without T10-PI\n",
6c854111 573 dev_name(&ib_conn->device->ib_device->dev));
a4ee3539 574 ib_conn->pi_support = false;
7f733847 575 } else {
a4ee3539 576 ib_conn->pi_support = true;
7f733847
AT
577 }
578 }
579
df749cdc
SG
580 iser_calc_scsi_params(iser_conn, iser_max_sectors);
581
1cfa0a75
OG
582 ret = rdma_resolve_route(cma_id, 1000);
583 if (ret) {
584 iser_err("resolve route failed: %d\n", ret);
b73c3ada
AN
585 iser_connect_error(cma_id);
586 return;
1cfa0a75 587 }
1cfa0a75
OG
588}
589
134a42a6 590/*
504130c0 591 * Called with state mutex held
134a42a6 592 */
b73c3ada 593static void iser_route_handler(struct rdma_cm_id *cma_id)
1cfa0a75
OG
594{
595 struct rdma_conn_param conn_param;
596 int ret;
8d8399de 597 struct iser_cm_hdr req_hdr;
5716af6e 598 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
a4ee3539 599 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1fc43132 600 struct ib_device *ib_dev = ib_conn->device->ib_device;
1cfa0a75 601
5716af6e 602 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
603 /* bailout */
604 return;
605
a4ee3539 606 ret = iser_create_ib_conn_res(ib_conn);
1cfa0a75
OG
607 if (ret)
608 goto failure;
609
1cfa0a75 610 memset(&conn_param, 0, sizeof conn_param);
1fc43132 611 conn_param.responder_resources = ib_dev->attrs.max_qp_rd_atom;
1cfa0a75
OG
612 conn_param.initiator_depth = 1;
613 conn_param.retry_count = 7;
614 conn_param.rnr_retry_count = 6;
615
8d8399de 616 memset(&req_hdr, 0, sizeof(req_hdr));
59caaed7 617 req_hdr.flags = ISER_ZBVA_NOT_SUP;
1fc43132 618 if (!iser_always_reg)
59caaed7 619 req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
d3cf81f9
SG
620 conn_param.private_data = (void *)&req_hdr;
621 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
8d8399de 622
071ba4cc 623 ret = rdma_connect_locked(cma_id, &conn_param);
1cfa0a75
OG
624 if (ret) {
625 iser_err("failure connecting: %d\n", ret);
626 goto failure;
627 }
628
b73c3ada 629 return;
1cfa0a75 630failure:
b73c3ada 631 iser_connect_error(cma_id);
1cfa0a75
OG
632}
633
59caaed7
JD
634static void iser_connected_handler(struct rdma_cm_id *cma_id,
635 const void *private_data)
1cfa0a75 636{
5716af6e 637 struct iser_conn *iser_conn;
4f9208ad
OG
638 struct ib_qp_attr attr;
639 struct ib_qp_init_attr init_attr;
640
5716af6e
SG
641 iser_conn = (struct iser_conn *)cma_id->context;
642 if (iser_conn->state != ISER_CONN_PENDING)
504130c0
AN
643 /* bailout */
644 return;
645
4f9208ad
OG
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);
1cfa0a75 648
59caaed7
JD
649 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
5716af6e
SG
658 iser_conn->state = ISER_CONN_UP;
659 complete(&iser_conn->up_completion);
1cfa0a75
OG
660}
661
b73c3ada 662static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
1cfa0a75 663{
c47a3c9e 664 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
1cfa0a75 665
c47a3c9e 666 if (iser_conn_terminate(iser_conn)) {
5716af6e 667 if (iser_conn->iscsi_conn)
c47a3c9e
SG
668 iscsi_conn_failure(iser_conn->iscsi_conn,
669 ISCSI_ERR_CONN_FAILED);
7d9eacf9
RD
670 else
671 iser_err("iscsi_iser connection isn't bound\n");
672 }
c47a3c9e
SG
673}
674
675static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
6606e6a2 676 bool destroy)
c47a3c9e
SG
677{
678 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
1cfa0a75 679
c47a3c9e
SG
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.
8d4aca7f 684 */
c47a3c9e 685 iser_disconnected_handler(cma_id);
6606e6a2 686 iser_free_ib_conn_res(iser_conn, destroy);
c47a3c9e 687 complete(&iser_conn->ib_completion);
5bf0e4b8 688}
1cfa0a75
OG
689
690static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
691{
5716af6e 692 struct iser_conn *iser_conn;
c47a3c9e 693 int ret = 0;
504130c0 694
5716af6e 695 iser_conn = (struct iser_conn *)cma_id->context;
871e00af
SG
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);
1cfa0a75 699
5716af6e 700 mutex_lock(&iser_conn->state_mutex);
1cfa0a75
OG
701 switch (event->event) {
702 case RDMA_CM_EVENT_ADDR_RESOLVED:
b73c3ada 703 iser_addr_handler(cma_id);
1cfa0a75
OG
704 break;
705 case RDMA_CM_EVENT_ROUTE_RESOLVED:
b73c3ada 706 iser_route_handler(cma_id);
1cfa0a75
OG
707 break;
708 case RDMA_CM_EVENT_ESTABLISHED:
59caaed7 709 iser_connected_handler(cma_id, event->param.conn.private_data);
1cfa0a75 710 break;
97540bb9
SW
711 case RDMA_CM_EVENT_REJECTED:
712 iser_info("Connection rejected: %s\n",
713 rdma_reject_msg(cma_id, event->status));
df561f66 714 fallthrough;
1cfa0a75
OG
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:
b73c3ada 719 iser_connect_error(cma_id);
1cfa0a75
OG
720 break;
721 case RDMA_CM_EVENT_DISCONNECTED:
2f5de151 722 case RDMA_CM_EVENT_ADDR_CHANGE:
5426b171
AN
723 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
724 iser_cleanup_handler(cma_id, false);
1cfa0a75 725 break;
c47a3c9e
SG
726 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.
5426b171
AN
730 * also if we are not in state DOWN implicitly destroy
731 * the cma_id.
c47a3c9e
SG
732 */
733 iser_cleanup_handler(cma_id, true);
5426b171
AN
734 if (iser_conn->state != ISER_CONN_DOWN) {
735 iser_conn->ib_conn.cma_id = NULL;
736 ret = 1;
737 }
c47a3c9e 738 break;
1cfa0a75 739 default:
871e00af
SG
740 iser_err("Unexpected RDMA CM event: %s (%d)\n",
741 rdma_event_msg(event->event), event->event);
1cfa0a75
OG
742 break;
743 }
5716af6e 744 mutex_unlock(&iser_conn->state_mutex);
c47a3c9e
SG
745
746 return ret;
1cfa0a75
OG
747}
748
5716af6e 749void iser_conn_init(struct iser_conn *iser_conn)
1cfa0a75 750{
cfeb91b3
CH
751 struct ib_conn *ib_conn = &iser_conn->ib_conn;
752
5716af6e 753 iser_conn->state = ISER_CONN_INIT;
5716af6e 754 init_completion(&iser_conn->stop_completion);
c47a3c9e 755 init_completion(&iser_conn->ib_completion);
5716af6e
SG
756 init_completion(&iser_conn->up_completion);
757 INIT_LIST_HEAD(&iser_conn->conn_list);
5716af6e 758 mutex_init(&iser_conn->state_mutex);
cfeb91b3
CH
759
760 ib_conn->post_recv_buf_count = 0;
761 ib_conn->reg_cqe.done = iser_reg_comp;
1cfa0a75
OG
762}
763
bf194997 764/*
1cfa0a75 765 * starts the process of connecting to the target
94e2bd68 766 * sleeps until the connection is established or rejected
1cfa0a75 767 */
5716af6e 768int iser_connect(struct iser_conn *iser_conn,
96ed02d4
RD
769 struct sockaddr *src_addr,
770 struct sockaddr *dst_addr,
1cfa0a75
OG
771 int non_blocking)
772{
a4ee3539 773 struct ib_conn *ib_conn = &iser_conn->ib_conn;
1cfa0a75
OG
774 int err = 0;
775
5716af6e 776 mutex_lock(&iser_conn->state_mutex);
504130c0 777
5716af6e 778 sprintf(iser_conn->name, "%pISp", dst_addr);
96ed02d4 779
5716af6e 780 iser_info("connecting to: %s\n", iser_conn->name);
1cfa0a75
OG
781
782 /* the device is known only --after-- address resolution */
a4ee3539 783 ib_conn->device = NULL;
1cfa0a75 784
5716af6e 785 iser_conn->state = ISER_CONN_PENDING;
1cfa0a75 786
fa20105e 787 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
a4ee3539
SG
788 (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);
1cfa0a75
OG
792 iser_err("rdma_create_id failed: %d\n", err);
793 goto id_failure;
794 }
795
a4ee3539 796 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
1cfa0a75
OG
797 if (err) {
798 iser_err("rdma_resolve_addr failed: %d\n", err);
799 goto addr_failure;
800 }
801
802 if (!non_blocking) {
5716af6e 803 wait_for_completion_interruptible(&iser_conn->up_completion);
1cfa0a75 804
5716af6e 805 if (iser_conn->state != ISER_CONN_UP) {
1cfa0a75
OG
806 err = -EIO;
807 goto connect_failure;
808 }
809 }
5716af6e 810 mutex_unlock(&iser_conn->state_mutex);
1cfa0a75
OG
811
812 mutex_lock(&ig.connlist_mutex);
5716af6e 813 list_add(&iser_conn->conn_list, &ig.connlist);
1cfa0a75
OG
814 mutex_unlock(&ig.connlist_mutex);
815 return 0;
816
817id_failure:
a4ee3539 818 ib_conn->cma_id = NULL;
1cfa0a75 819addr_failure:
5716af6e 820 iser_conn->state = ISER_CONN_DOWN;
1cfa0a75 821connect_failure:
5716af6e
SG
822 mutex_unlock(&iser_conn->state_mutex);
823 iser_conn_release(iser_conn);
1cfa0a75
OG
824 return err;
825}
826
5716af6e 827int iser_post_recvl(struct iser_conn *iser_conn)
bcc60c38 828{
a4ee3539 829 struct ib_conn *ib_conn = &iser_conn->ib_conn;
0f512b34 830 struct iser_login_desc *desc = &iser_conn->login_desc;
604dbdc4 831 struct ib_recv_wr wr;
bcc60c38
OG
832 int ib_ret;
833
0f512b34
SG
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;
bcc60c38 837
cfeb91b3
CH
838 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;
bcc60c38 843
a4ee3539 844 ib_conn->post_recv_buf_count++;
604dbdc4 845 ib_ret = ib_post_recv(ib_conn->qp, &wr, NULL);
bcc60c38
OG
846 if (ib_ret) {
847 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
a4ee3539 848 ib_conn->post_recv_buf_count--;
bcc60c38 849 }
cfeb91b3 850
bcc60c38
OG
851 return ib_ret;
852}
853
5716af6e 854int iser_post_recvm(struct iser_conn *iser_conn, int count)
bcc60c38 855{
a4ee3539 856 struct ib_conn *ib_conn = &iser_conn->ib_conn;
5716af6e 857 unsigned int my_rx_head = iser_conn->rx_desc_head;
bcc60c38 858 struct iser_rx_desc *rx_desc;
604dbdc4 859 struct ib_recv_wr *wr;
cfeb91b3 860 int i, ib_ret;
bcc60c38 861
cfeb91b3
CH
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;
867 wr->num_sge = 1;
868 wr->next = wr + 1;
5716af6e 869 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
bcc60c38
OG
870 }
871
cfeb91b3
CH
872 wr--;
873 wr->next = NULL; /* mark end of work requests list */
bcc60c38 874
a4ee3539 875 ib_conn->post_recv_buf_count += count;
604dbdc4 876 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, NULL);
70bcc63f 877 if (unlikely(ib_ret)) {
bcc60c38 878 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
a4ee3539 879 ib_conn->post_recv_buf_count -= count;
bcc60c38 880 } else
5716af6e 881 iser_conn->rx_desc_head = my_rx_head;
cfeb91b3 882
bcc60c38
OG
883 return ib_ret;
884}
885
886
1cfa0a75 887/**
134a42a6 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
1cfa0a75 892 *
134a42a6 893 * Return: 0 on success, -1 on failure
1cfa0a75 894 */
6df5a128
SG
895int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
896 bool signal)
1cfa0a75 897{
b9294f8b
IR
898 struct ib_send_wr *wr = &tx_desc->send_wr;
899 struct ib_send_wr *first_wr;
7332bed0 900 int ib_ret;
1cfa0a75 901
a4ee3539 902 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
5716af6e
SG
903 tx_desc->dma_addr, ISER_HEADERS_LEN,
904 DMA_TO_DEVICE);
1cfa0a75 905
7332bed0 906 wr->next = NULL;
cfeb91b3 907 wr->wr_cqe = &tx_desc->cqe;
7332bed0
SG
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;
1cfa0a75 912
b9294f8b
IR
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;
917 else
918 first_wr = wr;
919
920 ib_ret = ib_post_send(ib_conn->qp, first_wr, NULL);
70bcc63f 921 if (unlikely(ib_ret))
7332bed0 922 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
604dbdc4 923 ib_ret, wr->opcode);
ff3dd52d 924
f19624aa 925 return ib_ret;
1cfa0a75
OG
926}
927
0a7a08ad
SG
928u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
929 enum iser_data_dir cmd_dir, sector_t *sector)
930{
b130eded 931 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
5190cc26 932 struct iser_fr_desc *desc = reg->mem_h;
0a7a08ad
SG
933 unsigned long sector_size = iser_task->sc->device->sector_size;
934 struct ib_mr_status mr_status;
935 int ret;
936
b76a4399 937 if (desc && desc->sig_protected) {
c934833e 938 desc->sig_protected = false;
b76a4399 939 ret = ib_check_mr_status(desc->rsc.sig_mr,
0a7a08ad
SG
940 IB_MR_CHECK_SIG_STATUS, &mr_status);
941 if (ret) {
3466c060 942 iser_err("ib_check_mr_status failed, ret %d\n", ret);
24c3456c
SG
943 /* Not a lot we can do, return ambiguous guard error */
944 *sector = 0;
945 return 0x1;
0a7a08ad
SG
946 }
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
2c63d107 951 sector_div(sector_off, sector_size + 8);
87662a47 952 *sector = scsi_get_sector(iser_task->sc) + sector_off;
0a7a08ad 953
3466c060 954 iser_err("PI error found type %d at sector %llx "
0a7a08ad 955 "expected %x vs actual %x\n",
39c978cd
RD
956 mr_status.sig_err.err_type,
957 (unsigned long long)*sector,
0a7a08ad
SG
958 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;
0a7a08ad 973}
cfeb91b3
CH
974
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
b04dc199 980 iser_err("%s failure: %s (%d) vend_err %#x\n", type,
cfeb91b3
CH
981 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}