]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/infiniband/ulp/isert/ib_isert.c
iser-target: Fix pending connections handling in target stack shutdown sequnce
[mirror_ubuntu-zesty-kernel.git] / drivers / infiniband / ulp / isert / ib_isert.c
CommitLineData
b8d26b3b
NB
1/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
4c76251e 4 * (c) Copyright 2013 Datera, Inc.
b8d26b3b
NB
5 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
531b7bf4 30#include <linux/semaphore.h>
b8d26b3b
NB
31
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
bdf20e72
SG
38#define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
39 ISERT_MAX_CONN)
b8d26b3b 40
45678b6b 41static int isert_debug_level;
24f412dd
SG
42module_param_named(debug_level, isert_debug_level, int, 0644);
43MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
44
b8d26b3b
NB
45static DEFINE_MUTEX(device_list_mutex);
46static LIST_HEAD(device_list);
b8d26b3b 47static struct workqueue_struct *isert_comp_wq;
b02efbfc 48static struct workqueue_struct *isert_release_wq;
b8d26b3b 49
d40945d8
VP
50static void
51isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52static int
53isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
59464ef4 55static void
a3a5a826 56isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
59464ef4 57static int
a3a5a826
SG
58isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
59 struct isert_rdma_wr *wr);
f93f3a70
SG
60static int
61isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
2371e5da
SG
62static int
63isert_rdma_post_recvl(struct isert_conn *isert_conn);
64static int
65isert_rdma_accept(struct isert_conn *isert_conn);
ca6c1d82 66struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
d40945d8 67
2f1b6b7d
SG
68static void isert_release_work(struct work_struct *work);
69
302cc7c3
SG
70static inline bool
71isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
72{
23a548ee 73 return (conn->pi_support &&
302cc7c3
SG
74 cmd->prot_op != TARGET_PROT_NORMAL);
75}
76
77
b8d26b3b
NB
78static void
79isert_qp_event_callback(struct ib_event *e, void *context)
80{
6700425e 81 struct isert_conn *isert_conn = context;
b8d26b3b 82
ea8a1616
SG
83 isert_err("%s (%d): conn %p\n",
84 ib_event_msg(e->event), e->event, isert_conn);
85
b8d26b3b
NB
86 switch (e->event) {
87 case IB_EVENT_COMM_EST:
dac6ab30 88 rdma_notify(isert_conn->cm_id, IB_EVENT_COMM_EST);
b8d26b3b
NB
89 break;
90 case IB_EVENT_QP_LAST_WQE_REACHED:
4c22e07f 91 isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
b8d26b3b
NB
92 break;
93 default:
94 break;
95 }
96}
97
98static int
99isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
100{
101 int ret;
102
103 ret = ib_query_device(ib_dev, devattr);
104 if (ret) {
24f412dd 105 isert_err("ib_query_device() failed: %d\n", ret);
b8d26b3b
NB
106 return ret;
107 }
24f412dd
SG
108 isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
109 isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
b8d26b3b
NB
110
111 return 0;
112}
113
40fc069a
SG
114static struct isert_comp *
115isert_comp_get(struct isert_conn *isert_conn)
b8d26b3b 116{
dac6ab30 117 struct isert_device *device = isert_conn->device;
4a295bae 118 struct isert_comp *comp;
40fc069a 119 int i, min = 0;
b8d26b3b 120
b8d26b3b 121 mutex_lock(&device_list_mutex);
4a295bae
SG
122 for (i = 0; i < device->comps_used; i++)
123 if (device->comps[i].active_qps <
124 device->comps[min].active_qps)
125 min = i;
126 comp = &device->comps[min];
127 comp->active_qps++;
40fc069a
SG
128 mutex_unlock(&device_list_mutex);
129
24f412dd 130 isert_info("conn %p, using comp %p min_index: %d\n",
4a295bae 131 isert_conn, comp, min);
40fc069a
SG
132
133 return comp;
134}
135
136static void
137isert_comp_put(struct isert_comp *comp)
138{
139 mutex_lock(&device_list_mutex);
140 comp->active_qps--;
b8d26b3b 141 mutex_unlock(&device_list_mutex);
40fc069a
SG
142}
143
144static struct ib_qp *
145isert_create_qp(struct isert_conn *isert_conn,
146 struct isert_comp *comp,
147 struct rdma_cm_id *cma_id)
148{
dac6ab30 149 struct isert_device *device = isert_conn->device;
40fc069a
SG
150 struct ib_qp_init_attr attr;
151 int ret;
b8d26b3b
NB
152
153 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
154 attr.event_handler = isert_qp_event_callback;
155 attr.qp_context = isert_conn;
6f0fae3d
SG
156 attr.send_cq = comp->cq;
157 attr.recv_cq = comp->cq;
b8d26b3b 158 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
bdf20e72 159 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
b8d26b3b
NB
160 /*
161 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
f57915cf
OG
162 * work-around for RDMA_READs with ConnectX-2.
163 *
164 * Also, still make sure to have at least two SGEs for
165 * outgoing control PDU responses.
b8d26b3b 166 */
f57915cf 167 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
b8d26b3b
NB
168 isert_conn->max_sge = attr.cap.max_send_sge;
169
170 attr.cap.max_recv_sge = 1;
171 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
172 attr.qp_type = IB_QPT_RC;
570db170 173 if (device->pi_capable)
d3e125da 174 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
b8d26b3b 175
67cb3949 176 ret = rdma_create_qp(cma_id, device->pd, &attr);
b8d26b3b 177 if (ret) {
24f412dd 178 isert_err("rdma_create_qp failed for cma_id %d\n", ret);
40fc069a
SG
179 return ERR_PTR(ret);
180 }
181
182 return cma_id->qp;
183}
184
185static int
186isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
187{
188 struct isert_comp *comp;
189 int ret;
190
191 comp = isert_comp_get(isert_conn);
dac6ab30
SG
192 isert_conn->qp = isert_create_qp(isert_conn, comp, cma_id);
193 if (IS_ERR(isert_conn->qp)) {
194 ret = PTR_ERR(isert_conn->qp);
19e2090f 195 goto err;
b8d26b3b 196 }
b8d26b3b
NB
197
198 return 0;
19e2090f 199err:
40fc069a 200 isert_comp_put(comp);
19e2090f 201 return ret;
b8d26b3b
NB
202}
203
204static void
205isert_cq_event_callback(struct ib_event *e, void *context)
206{
4c22e07f 207 isert_dbg("event: %d\n", e->event);
b8d26b3b
NB
208}
209
210static int
211isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
212{
dac6ab30 213 struct isert_device *device = isert_conn->device;
67cb3949 214 struct ib_device *ib_dev = device->ib_device;
b8d26b3b
NB
215 struct iser_rx_desc *rx_desc;
216 struct ib_sge *rx_sg;
217 u64 dma_addr;
218 int i, j;
219
dac6ab30 220 isert_conn->rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
b8d26b3b 221 sizeof(struct iser_rx_desc), GFP_KERNEL);
dac6ab30 222 if (!isert_conn->rx_descs)
b8d26b3b
NB
223 goto fail;
224
dac6ab30 225 rx_desc = isert_conn->rx_descs;
b8d26b3b
NB
226
227 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
228 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
229 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
230 if (ib_dma_mapping_error(ib_dev, dma_addr))
231 goto dma_map_fail;
232
233 rx_desc->dma_addr = dma_addr;
234
235 rx_sg = &rx_desc->rx_sg;
236 rx_sg->addr = rx_desc->dma_addr;
237 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
34efc7df 238 rx_sg->lkey = device->pd->local_dma_lkey;
b8d26b3b
NB
239 }
240
dac6ab30 241 isert_conn->rx_desc_head = 0;
4c22e07f 242
b8d26b3b
NB
243 return 0;
244
245dma_map_fail:
dac6ab30 246 rx_desc = isert_conn->rx_descs;
b8d26b3b
NB
247 for (j = 0; j < i; j++, rx_desc++) {
248 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
249 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
250 }
dac6ab30
SG
251 kfree(isert_conn->rx_descs);
252 isert_conn->rx_descs = NULL;
b8d26b3b 253fail:
4c22e07f
SG
254 isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
255
b8d26b3b
NB
256 return -ENOMEM;
257}
258
259static void
260isert_free_rx_descriptors(struct isert_conn *isert_conn)
261{
dac6ab30 262 struct ib_device *ib_dev = isert_conn->device->ib_device;
b8d26b3b
NB
263 struct iser_rx_desc *rx_desc;
264 int i;
265
dac6ab30 266 if (!isert_conn->rx_descs)
b8d26b3b
NB
267 return;
268
dac6ab30 269 rx_desc = isert_conn->rx_descs;
b8d26b3b
NB
270 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
271 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
272 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
273 }
274
dac6ab30
SG
275 kfree(isert_conn->rx_descs);
276 isert_conn->rx_descs = NULL;
b8d26b3b
NB
277}
278
6f0fae3d
SG
279static void isert_cq_work(struct work_struct *);
280static void isert_cq_callback(struct ib_cq *, void *);
b8d26b3b 281
172369c5
SG
282static void
283isert_free_comps(struct isert_device *device)
b8d26b3b 284{
172369c5 285 int i;
59464ef4 286
172369c5
SG
287 for (i = 0; i < device->comps_used; i++) {
288 struct isert_comp *comp = &device->comps[i];
b1a5ad00 289
172369c5
SG
290 if (comp->cq) {
291 cancel_work_sync(&comp->work);
292 ib_destroy_cq(comp->cq);
293 }
59464ef4 294 }
172369c5
SG
295 kfree(device->comps);
296}
d40945d8 297
172369c5
SG
298static int
299isert_alloc_comps(struct isert_device *device,
300 struct ib_device_attr *attr)
301{
302 int i, max_cqe, ret = 0;
d3e125da 303
4a295bae 304 device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
172369c5
SG
305 device->ib_device->num_comp_vectors));
306
24f412dd 307 isert_info("Using %d CQs, %s supports %d vectors support "
4a295bae
SG
308 "Fast registration %d pi_capable %d\n",
309 device->comps_used, device->ib_device->name,
310 device->ib_device->num_comp_vectors, device->use_fastreg,
311 device->pi_capable);
312
313 device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
314 GFP_KERNEL);
315 if (!device->comps) {
24f412dd 316 isert_err("Unable to allocate completion contexts\n");
b8d26b3b
NB
317 return -ENOMEM;
318 }
4a295bae 319
172369c5
SG
320 max_cqe = min(ISER_MAX_CQ_LEN, attr->max_cqe);
321
4a295bae 322 for (i = 0; i < device->comps_used; i++) {
8e37210b 323 struct ib_cq_init_attr cq_attr = {};
4a295bae
SG
324 struct isert_comp *comp = &device->comps[i];
325
326 comp->device = device;
6f0fae3d 327 INIT_WORK(&comp->work, isert_cq_work);
8e37210b
MB
328 cq_attr.cqe = max_cqe;
329 cq_attr.comp_vector = i;
6f0fae3d
SG
330 comp->cq = ib_create_cq(device->ib_device,
331 isert_cq_callback,
332 isert_cq_event_callback,
333 (void *)comp,
8e37210b 334 &cq_attr);
6f0fae3d 335 if (IS_ERR(comp->cq)) {
172369c5 336 isert_err("Unable to allocate cq\n");
6f0fae3d
SG
337 ret = PTR_ERR(comp->cq);
338 comp->cq = NULL;
b8d26b3b 339 goto out_cq;
94a71110 340 }
b8d26b3b 341
6f0fae3d 342 ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
94a71110 343 if (ret)
b8d26b3b
NB
344 goto out_cq;
345 }
346
172369c5
SG
347 return 0;
348out_cq:
349 isert_free_comps(device);
350 return ret;
351}
352
353static int
354isert_create_device_ib_res(struct isert_device *device)
355{
172369c5 356 struct ib_device_attr *dev_attr;
fd8205e8 357 int ret;
172369c5
SG
358
359 dev_attr = &device->dev_attr;
fd8205e8 360 ret = isert_query_device(device->ib_device, dev_attr);
172369c5
SG
361 if (ret)
362 return ret;
363
364 /* asign function handlers */
365 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
366 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
367 device->use_fastreg = 1;
368 device->reg_rdma_mem = isert_reg_rdma;
369 device->unreg_rdma_mem = isert_unreg_rdma;
370 } else {
371 device->use_fastreg = 0;
372 device->reg_rdma_mem = isert_map_rdma;
373 device->unreg_rdma_mem = isert_unmap_cmd;
374 }
375
376 ret = isert_alloc_comps(device, dev_attr);
377 if (ret)
378 return ret;
379
67cb3949
SG
380 device->pd = ib_alloc_pd(device->ib_device);
381 if (IS_ERR(device->pd)) {
382 ret = PTR_ERR(device->pd);
383 isert_err("failed to allocate pd, device %p, ret=%d\n",
384 device, ret);
385 goto out_cq;
386 }
387
172369c5
SG
388 /* Check signature cap */
389 device->pi_capable = dev_attr->device_cap_flags &
390 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
67cb3949 391
b8d26b3b
NB
392 return 0;
393
394out_cq:
172369c5 395 isert_free_comps(device);
b8d26b3b
NB
396 return ret;
397}
398
399static void
400isert_free_device_ib_res(struct isert_device *device)
401{
24f412dd 402 isert_info("device %p\n", device);
b8d26b3b 403
67cb3949 404 ib_dealloc_pd(device->pd);
172369c5 405 isert_free_comps(device);
b8d26b3b
NB
406}
407
408static void
cf8ae958 409isert_device_put(struct isert_device *device)
b8d26b3b
NB
410{
411 mutex_lock(&device_list_mutex);
412 device->refcount--;
4c22e07f 413 isert_info("device %p refcount %d\n", device, device->refcount);
b8d26b3b
NB
414 if (!device->refcount) {
415 isert_free_device_ib_res(device);
416 list_del(&device->dev_node);
417 kfree(device);
418 }
419 mutex_unlock(&device_list_mutex);
420}
421
422static struct isert_device *
cf8ae958 423isert_device_get(struct rdma_cm_id *cma_id)
b8d26b3b
NB
424{
425 struct isert_device *device;
426 int ret;
427
428 mutex_lock(&device_list_mutex);
429 list_for_each_entry(device, &device_list, dev_node) {
430 if (device->ib_device->node_guid == cma_id->device->node_guid) {
431 device->refcount++;
4c22e07f
SG
432 isert_info("Found iser device %p refcount %d\n",
433 device, device->refcount);
b8d26b3b
NB
434 mutex_unlock(&device_list_mutex);
435 return device;
436 }
437 }
438
439 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
440 if (!device) {
441 mutex_unlock(&device_list_mutex);
442 return ERR_PTR(-ENOMEM);
443 }
444
445 INIT_LIST_HEAD(&device->dev_node);
446
447 device->ib_device = cma_id->device;
448 ret = isert_create_device_ib_res(device);
449 if (ret) {
450 kfree(device);
451 mutex_unlock(&device_list_mutex);
452 return ERR_PTR(ret);
453 }
454
455 device->refcount++;
456 list_add_tail(&device->dev_node, &device_list);
4c22e07f
SG
457 isert_info("Created a new iser device %p refcount %d\n",
458 device, device->refcount);
b8d26b3b
NB
459 mutex_unlock(&device_list_mutex);
460
461 return device;
462}
463
59464ef4 464static void
a3a5a826 465isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
59464ef4
VP
466{
467 struct fast_reg_descriptor *fr_desc, *tmp;
468 int i = 0;
469
dac6ab30 470 if (list_empty(&isert_conn->fr_pool))
59464ef4
VP
471 return;
472
4c22e07f 473 isert_info("Freeing conn %p fastreg pool", isert_conn);
59464ef4
VP
474
475 list_for_each_entry_safe(fr_desc, tmp,
dac6ab30 476 &isert_conn->fr_pool, list) {
59464ef4
VP
477 list_del(&fr_desc->list);
478 ib_free_fast_reg_page_list(fr_desc->data_frpl);
479 ib_dereg_mr(fr_desc->data_mr);
d3e125da
SG
480 if (fr_desc->pi_ctx) {
481 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
482 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
8b91ffc1 483 ib_dereg_mr(fr_desc->pi_ctx->sig_mr);
d3e125da
SG
484 kfree(fr_desc->pi_ctx);
485 }
59464ef4
VP
486 kfree(fr_desc);
487 ++i;
488 }
489
dac6ab30 490 if (i < isert_conn->fr_pool_size)
24f412dd 491 isert_warn("Pool still has %d regions registered\n",
dac6ab30 492 isert_conn->fr_pool_size - i);
59464ef4
VP
493}
494
570db170
SG
495static int
496isert_create_pi_ctx(struct fast_reg_descriptor *desc,
497 struct ib_device *device,
498 struct ib_pd *pd)
499{
570db170
SG
500 struct pi_context *pi_ctx;
501 int ret;
502
503 pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
504 if (!pi_ctx) {
24f412dd 505 isert_err("Failed to allocate pi context\n");
570db170
SG
506 return -ENOMEM;
507 }
508
509 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
510 ISCSI_ISER_SG_TABLESIZE);
511 if (IS_ERR(pi_ctx->prot_frpl)) {
24f412dd 512 isert_err("Failed to allocate prot frpl err=%ld\n",
570db170
SG
513 PTR_ERR(pi_ctx->prot_frpl));
514 ret = PTR_ERR(pi_ctx->prot_frpl);
515 goto err_pi_ctx;
516 }
517
a89be2cc
SG
518 pi_ctx->prot_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
519 ISCSI_ISER_SG_TABLESIZE);
570db170 520 if (IS_ERR(pi_ctx->prot_mr)) {
24f412dd 521 isert_err("Failed to allocate prot frmr err=%ld\n",
570db170
SG
522 PTR_ERR(pi_ctx->prot_mr));
523 ret = PTR_ERR(pi_ctx->prot_mr);
524 goto err_prot_frpl;
525 }
526 desc->ind |= ISERT_PROT_KEY_VALID;
527
9bee178b 528 pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
570db170 529 if (IS_ERR(pi_ctx->sig_mr)) {
24f412dd 530 isert_err("Failed to allocate signature enabled mr err=%ld\n",
570db170
SG
531 PTR_ERR(pi_ctx->sig_mr));
532 ret = PTR_ERR(pi_ctx->sig_mr);
533 goto err_prot_mr;
534 }
535
536 desc->pi_ctx = pi_ctx;
537 desc->ind |= ISERT_SIG_KEY_VALID;
538 desc->ind &= ~ISERT_PROTECTED;
539
540 return 0;
541
542err_prot_mr:
b2feda4f 543 ib_dereg_mr(pi_ctx->prot_mr);
570db170 544err_prot_frpl:
b2feda4f 545 ib_free_fast_reg_page_list(pi_ctx->prot_frpl);
570db170 546err_pi_ctx:
b2feda4f 547 kfree(pi_ctx);
570db170
SG
548
549 return ret;
550}
551
dc87a90f
SG
552static int
553isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
570db170 554 struct fast_reg_descriptor *fr_desc)
dc87a90f 555{
d3e125da
SG
556 int ret;
557
dc87a90f
SG
558 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
559 ISCSI_ISER_SG_TABLESIZE);
560 if (IS_ERR(fr_desc->data_frpl)) {
24f412dd 561 isert_err("Failed to allocate data frpl err=%ld\n",
4c22e07f 562 PTR_ERR(fr_desc->data_frpl));
dc87a90f
SG
563 return PTR_ERR(fr_desc->data_frpl);
564 }
565
a89be2cc
SG
566 fr_desc->data_mr = ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
567 ISCSI_ISER_SG_TABLESIZE);
dc87a90f 568 if (IS_ERR(fr_desc->data_mr)) {
24f412dd 569 isert_err("Failed to allocate data frmr err=%ld\n",
4c22e07f 570 PTR_ERR(fr_desc->data_mr));
d3e125da
SG
571 ret = PTR_ERR(fr_desc->data_mr);
572 goto err_data_frpl;
dc87a90f 573 }
d3e125da
SG
574 fr_desc->ind |= ISERT_DATA_KEY_VALID;
575
24f412dd 576 isert_dbg("Created fr_desc %p\n", fr_desc);
dc87a90f
SG
577
578 return 0;
570db170 579
d3e125da
SG
580err_data_frpl:
581 ib_free_fast_reg_page_list(fr_desc->data_frpl);
582
583 return ret;
59464ef4
VP
584}
585
586static int
570db170 587isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
59464ef4
VP
588{
589 struct fast_reg_descriptor *fr_desc;
dac6ab30 590 struct isert_device *device = isert_conn->device;
f46d6a8a
NB
591 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
592 struct se_node_acl *se_nacl = se_sess->se_node_acl;
593 int i, ret, tag_num;
594 /*
595 * Setup the number of FRMRs based upon the number of tags
596 * available to session in iscsi_target_locate_portal().
597 */
598 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
599 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
59464ef4 600
dac6ab30 601 isert_conn->fr_pool_size = 0;
f46d6a8a 602 for (i = 0; i < tag_num; i++) {
59464ef4
VP
603 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
604 if (!fr_desc) {
24f412dd 605 isert_err("Failed to allocate fast_reg descriptor\n");
59464ef4
VP
606 ret = -ENOMEM;
607 goto err;
608 }
609
dc87a90f 610 ret = isert_create_fr_desc(device->ib_device,
67cb3949 611 device->pd, fr_desc);
dc87a90f 612 if (ret) {
24f412dd 613 isert_err("Failed to create fastreg descriptor err=%d\n",
dc87a90f 614 ret);
a80e21b3 615 kfree(fr_desc);
59464ef4
VP
616 goto err;
617 }
59464ef4 618
dac6ab30
SG
619 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
620 isert_conn->fr_pool_size++;
59464ef4
VP
621 }
622
24f412dd 623 isert_dbg("Creating conn %p fastreg pool size=%d",
dac6ab30 624 isert_conn, isert_conn->fr_pool_size);
59464ef4
VP
625
626 return 0;
627
628err:
a3a5a826 629 isert_conn_free_fastreg_pool(isert_conn);
59464ef4
VP
630 return ret;
631}
632
ae9ea9ed
SG
633static void
634isert_init_conn(struct isert_conn *isert_conn)
b8d26b3b 635{
b8d26b3b 636 isert_conn->state = ISER_CONN_INIT;
bd379220 637 INIT_LIST_HEAD(&isert_conn->node);
dac6ab30 638 init_completion(&isert_conn->login_comp);
2371e5da 639 init_completion(&isert_conn->login_req_comp);
dac6ab30
SG
640 init_completion(&isert_conn->wait);
641 kref_init(&isert_conn->kref);
642 mutex_init(&isert_conn->mutex);
643 spin_lock_init(&isert_conn->pool_lock);
644 INIT_LIST_HEAD(&isert_conn->fr_pool);
2f1b6b7d 645 INIT_WORK(&isert_conn->release_work, isert_release_work);
ae9ea9ed 646}
b8d26b3b 647
ae9ea9ed
SG
648static void
649isert_free_login_buf(struct isert_conn *isert_conn)
650{
dac6ab30 651 struct ib_device *ib_dev = isert_conn->device->ib_device;
ae9ea9ed
SG
652
653 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
654 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
655 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
656 ISCSI_DEF_MAX_RECV_SEG_LEN,
657 DMA_FROM_DEVICE);
658 kfree(isert_conn->login_buf);
659}
660
661static int
662isert_alloc_login_buf(struct isert_conn *isert_conn,
663 struct ib_device *ib_dev)
664{
665 int ret;
b8d26b3b
NB
666
667 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
668 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
669 if (!isert_conn->login_buf) {
24f412dd 670 isert_err("Unable to allocate isert_conn->login_buf\n");
ae9ea9ed 671 return -ENOMEM;
b8d26b3b
NB
672 }
673
674 isert_conn->login_req_buf = isert_conn->login_buf;
675 isert_conn->login_rsp_buf = isert_conn->login_buf +
676 ISCSI_DEF_MAX_RECV_SEG_LEN;
ae9ea9ed 677
24f412dd 678 isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
b8d26b3b
NB
679 isert_conn->login_buf, isert_conn->login_req_buf,
680 isert_conn->login_rsp_buf);
681
682 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
683 (void *)isert_conn->login_req_buf,
684 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
685
686 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
687 if (ret) {
ae9ea9ed 688 isert_err("login_req_dma mapping error: %d\n", ret);
b8d26b3b
NB
689 isert_conn->login_req_dma = 0;
690 goto out_login_buf;
691 }
692
693 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
694 (void *)isert_conn->login_rsp_buf,
695 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
696
697 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
698 if (ret) {
ae9ea9ed 699 isert_err("login_rsp_dma mapping error: %d\n", ret);
b8d26b3b
NB
700 isert_conn->login_rsp_dma = 0;
701 goto out_req_dma_map;
702 }
703
ae9ea9ed
SG
704 return 0;
705
706out_req_dma_map:
707 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
708 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
709out_login_buf:
710 kfree(isert_conn->login_buf);
711 return ret;
712}
713
714static int
715isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
716{
717 struct isert_np *isert_np = cma_id->context;
718 struct iscsi_np *np = isert_np->np;
719 struct isert_conn *isert_conn;
720 struct isert_device *device;
721 int ret = 0;
722
723 spin_lock_bh(&np->np_thread_lock);
724 if (!np->enabled) {
725 spin_unlock_bh(&np->np_thread_lock);
726 isert_dbg("iscsi_np is not enabled, reject connect request\n");
727 return rdma_reject(cma_id, NULL, 0);
728 }
729 spin_unlock_bh(&np->np_thread_lock);
730
731 isert_dbg("cma_id: %p, portal: %p\n",
732 cma_id, cma_id->context);
733
734 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
735 if (!isert_conn)
736 return -ENOMEM;
737
738 isert_init_conn(isert_conn);
dac6ab30 739 isert_conn->cm_id = cma_id;
ae9ea9ed
SG
740
741 ret = isert_alloc_login_buf(isert_conn, cma_id->device);
742 if (ret)
743 goto out;
744
cf8ae958 745 device = isert_device_get(cma_id);
b8d26b3b
NB
746 if (IS_ERR(device)) {
747 ret = PTR_ERR(device);
748 goto out_rsp_dma_map;
749 }
dac6ab30 750 isert_conn->device = device;
b8d26b3b 751
1a92e17e
SG
752 /* Set max inflight RDMA READ requests */
753 isert_conn->initiator_depth = min_t(u8,
754 event->param.conn.initiator_depth,
755 device->dev_attr.max_qp_init_rd_atom);
24f412dd 756 isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
1a92e17e 757
570db170 758 ret = isert_conn_setup_qp(isert_conn, cma_id);
b8d26b3b
NB
759 if (ret)
760 goto out_conn_dev;
761
2371e5da
SG
762 ret = isert_rdma_post_recvl(isert_conn);
763 if (ret)
764 goto out_conn_dev;
765
766 ret = isert_rdma_accept(isert_conn);
767 if (ret)
768 goto out_conn_dev;
769
ed8cb0a4 770 mutex_lock(&isert_np->mutex);
bd379220 771 list_add_tail(&isert_conn->node, &isert_np->accepted);
ed8cb0a4 772 mutex_unlock(&isert_np->mutex);
b8d26b3b 773
b8d26b3b
NB
774 return 0;
775
776out_conn_dev:
cf8ae958 777 isert_device_put(device);
b8d26b3b 778out_rsp_dma_map:
ae9ea9ed 779 isert_free_login_buf(isert_conn);
b8d26b3b
NB
780out:
781 kfree(isert_conn);
2371e5da 782 rdma_reject(cma_id, NULL, 0);
b8d26b3b
NB
783 return ret;
784}
785
786static void
787isert_connect_release(struct isert_conn *isert_conn)
788{
dac6ab30 789 struct isert_device *device = isert_conn->device;
b8d26b3b 790
4c22e07f 791 isert_dbg("conn %p\n", isert_conn);
b8d26b3b 792
57df81e3
SG
793 BUG_ON(!device);
794
795 if (device->use_fastreg)
a3a5a826 796 isert_conn_free_fastreg_pool(isert_conn);
59464ef4 797
19e2090f 798 isert_free_rx_descriptors(isert_conn);
dac6ab30
SG
799 if (isert_conn->cm_id)
800 rdma_destroy_id(isert_conn->cm_id);
19e2090f 801
dac6ab30
SG
802 if (isert_conn->qp) {
803 struct isert_comp *comp = isert_conn->qp->recv_cq->cq_context;
4a295bae 804
40fc069a 805 isert_comp_put(comp);
dac6ab30 806 ib_destroy_qp(isert_conn->qp);
b8d26b3b
NB
807 }
808
ae9ea9ed
SG
809 if (isert_conn->login_buf)
810 isert_free_login_buf(isert_conn);
811
57df81e3 812 isert_device_put(device);
b8d26b3b 813
57df81e3 814 kfree(isert_conn);
b8d26b3b
NB
815}
816
817static void
818isert_connected_handler(struct rdma_cm_id *cma_id)
819{
19e2090f 820 struct isert_conn *isert_conn = cma_id->qp->qp_context;
bd379220 821 struct isert_np *isert_np = cma_id->context;
c2f88b17 822
24f412dd 823 isert_info("conn %p\n", isert_conn);
128e9cc8 824
dac6ab30 825 mutex_lock(&isert_conn->mutex);
bd379220
JD
826 isert_conn->state = ISER_CONN_UP;
827 kref_get(&isert_conn->kref);
dac6ab30 828 mutex_unlock(&isert_conn->mutex);
bd379220
JD
829
830 mutex_lock(&isert_np->mutex);
831 list_move_tail(&isert_conn->node, &isert_np->pending);
832 mutex_unlock(&isert_np->mutex);
833
834 isert_info("np %p: Allow accept_np to continue\n", isert_np);
835 up(&isert_np->sem);
b8d26b3b
NB
836}
837
838static void
dac6ab30 839isert_release_kref(struct kref *kref)
b8d26b3b
NB
840{
841 struct isert_conn *isert_conn = container_of(kref,
dac6ab30 842 struct isert_conn, kref);
b8d26b3b 843
4c22e07f
SG
844 isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
845 current->pid);
b8d26b3b
NB
846
847 isert_connect_release(isert_conn);
848}
849
850static void
851isert_put_conn(struct isert_conn *isert_conn)
852{
dac6ab30 853 kref_put(&isert_conn->kref, isert_release_kref);
b8d26b3b
NB
854}
855
954f2372
SG
856/**
857 * isert_conn_terminate() - Initiate connection termination
858 * @isert_conn: isert connection struct
859 *
860 * Notes:
128e9cc8 861 * In case the connection state is FULL_FEATURE, move state
954f2372 862 * to TEMINATING and start teardown sequence (rdma_disconnect).
128e9cc8 863 * In case the connection state is UP, complete flush as well.
954f2372 864 *
dac6ab30 865 * This routine must be called with mutex held. Thus it is
954f2372
SG
866 * safe to call multiple times.
867 */
868static void
869isert_conn_terminate(struct isert_conn *isert_conn)
870{
871 int err;
872
128e9cc8
SG
873 switch (isert_conn->state) {
874 case ISER_CONN_TERMINATING:
875 break;
876 case ISER_CONN_UP:
128e9cc8 877 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
24f412dd 878 isert_info("Terminating conn %p state %d\n",
954f2372 879 isert_conn, isert_conn->state);
128e9cc8 880 isert_conn->state = ISER_CONN_TERMINATING;
dac6ab30 881 err = rdma_disconnect(isert_conn->cm_id);
954f2372 882 if (err)
24f412dd 883 isert_warn("Failed rdma_disconnect isert_conn %p\n",
954f2372 884 isert_conn);
128e9cc8
SG
885 break;
886 default:
24f412dd 887 isert_warn("conn %p teminating in state %d\n",
128e9cc8 888 isert_conn, isert_conn->state);
954f2372
SG
889 }
890}
891
3b726ae2 892static int
ca6c1d82
SG
893isert_np_cma_handler(struct isert_np *isert_np,
894 enum rdma_cm_event_type event)
b8d26b3b 895{
ea8a1616
SG
896 isert_dbg("%s (%d): isert np %p\n",
897 rdma_event_msg(event), event, isert_np);
3b726ae2 898
ca6c1d82
SG
899 switch (event) {
900 case RDMA_CM_EVENT_DEVICE_REMOVAL:
ed8cb0a4 901 isert_np->cm_id = NULL;
ca6c1d82
SG
902 break;
903 case RDMA_CM_EVENT_ADDR_CHANGE:
ed8cb0a4
JD
904 isert_np->cm_id = isert_setup_id(isert_np);
905 if (IS_ERR(isert_np->cm_id)) {
24f412dd 906 isert_err("isert np %p setup id failed: %ld\n",
ed8cb0a4
JD
907 isert_np, PTR_ERR(isert_np->cm_id));
908 isert_np->cm_id = NULL;
ca6c1d82
SG
909 }
910 break;
911 default:
24f412dd 912 isert_err("isert np %p Unexpected event %d\n",
ca6c1d82 913 isert_np, event);
3b726ae2
SG
914 }
915
ca6c1d82
SG
916 return -1;
917}
918
919static int
920isert_disconnected_handler(struct rdma_cm_id *cma_id,
921 enum rdma_cm_event_type event)
922{
923 struct isert_np *isert_np = cma_id->context;
924 struct isert_conn *isert_conn;
2f1b6b7d 925 bool terminating = false;
ca6c1d82 926
ed8cb0a4 927 if (isert_np->cm_id == cma_id)
ca6c1d82
SG
928 return isert_np_cma_handler(cma_id->context, event);
929
19e2090f 930 isert_conn = cma_id->qp->qp_context;
b8d26b3b 931
dac6ab30 932 mutex_lock(&isert_conn->mutex);
2f1b6b7d 933 terminating = (isert_conn->state == ISER_CONN_TERMINATING);
128e9cc8 934 isert_conn_terminate(isert_conn);
dac6ab30 935 mutex_unlock(&isert_conn->mutex);
128e9cc8 936
dac6ab30
SG
937 isert_info("conn %p completing wait\n", isert_conn);
938 complete(&isert_conn->wait);
3b726ae2 939
2f1b6b7d
SG
940 if (terminating)
941 goto out;
942
ed8cb0a4 943 mutex_lock(&isert_np->mutex);
bd379220
JD
944 if (!list_empty(&isert_conn->node)) {
945 list_del_init(&isert_conn->node);
2f1b6b7d
SG
946 isert_put_conn(isert_conn);
947 queue_work(isert_release_wq, &isert_conn->release_work);
948 }
ed8cb0a4 949 mutex_unlock(&isert_np->mutex);
2f1b6b7d
SG
950
951out:
3b726ae2 952 return 0;
b8d26b3b
NB
953}
954
4a579da2 955static int
954f2372
SG
956isert_connect_error(struct rdma_cm_id *cma_id)
957{
19e2090f 958 struct isert_conn *isert_conn = cma_id->qp->qp_context;
954f2372 959
bd379220 960 list_del_init(&isert_conn->node);
dac6ab30 961 isert_conn->cm_id = NULL;
954f2372 962 isert_put_conn(isert_conn);
4a579da2
SG
963
964 return -1;
954f2372
SG
965}
966
b8d26b3b
NB
967static int
968isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
969{
970 int ret = 0;
971
ea8a1616
SG
972 isert_info("%s (%d): status %d id %p np %p\n",
973 rdma_event_msg(event->event), event->event,
4c22e07f 974 event->status, cma_id, cma_id->context);
b8d26b3b
NB
975
976 switch (event->event) {
977 case RDMA_CM_EVENT_CONNECT_REQUEST:
b8d26b3b 978 ret = isert_connect_request(cma_id, event);
3b726ae2 979 if (ret)
4c22e07f 980 isert_err("failed handle connect request %d\n", ret);
b8d26b3b
NB
981 break;
982 case RDMA_CM_EVENT_ESTABLISHED:
b8d26b3b
NB
983 isert_connected_handler(cma_id);
984 break;
88c4015f
SG
985 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
986 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
987 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
88c4015f 988 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
ca6c1d82 989 ret = isert_disconnected_handler(cma_id, event->event);
b8d26b3b 990 break;
954f2372
SG
991 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
992 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
b8d26b3b 993 case RDMA_CM_EVENT_CONNECT_ERROR:
4a579da2 994 ret = isert_connect_error(cma_id);
954f2372 995 break;
b8d26b3b 996 default:
24f412dd 997 isert_err("Unhandled RDMA CMA event: %d\n", event->event);
b8d26b3b
NB
998 break;
999 }
1000
b8d26b3b
NB
1001 return ret;
1002}
1003
1004static int
1005isert_post_recv(struct isert_conn *isert_conn, u32 count)
1006{
1007 struct ib_recv_wr *rx_wr, *rx_wr_failed;
1008 int i, ret;
dac6ab30 1009 unsigned int rx_head = isert_conn->rx_desc_head;
b8d26b3b
NB
1010 struct iser_rx_desc *rx_desc;
1011
dac6ab30
SG
1012 for (rx_wr = isert_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
1013 rx_desc = &isert_conn->rx_descs[rx_head];
b0a191e7 1014 rx_wr->wr_id = (uintptr_t)rx_desc;
b8d26b3b
NB
1015 rx_wr->sg_list = &rx_desc->rx_sg;
1016 rx_wr->num_sge = 1;
1017 rx_wr->next = rx_wr + 1;
1018 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
1019 }
1020
1021 rx_wr--;
1022 rx_wr->next = NULL; /* mark end of work requests list */
1023
1024 isert_conn->post_recv_buf_count += count;
dac6ab30 1025 ret = ib_post_recv(isert_conn->qp, isert_conn->rx_wr,
b8d26b3b
NB
1026 &rx_wr_failed);
1027 if (ret) {
24f412dd 1028 isert_err("ib_post_recv() failed with ret: %d\n", ret);
b8d26b3b
NB
1029 isert_conn->post_recv_buf_count -= count;
1030 } else {
11378cdb 1031 isert_dbg("Posted %d RX buffers\n", count);
dac6ab30 1032 isert_conn->rx_desc_head = rx_head;
b8d26b3b
NB
1033 }
1034 return ret;
1035}
1036
1037static int
1038isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
1039{
dac6ab30 1040 struct ib_device *ib_dev = isert_conn->cm_id->device;
b8d26b3b
NB
1041 struct ib_send_wr send_wr, *send_wr_failed;
1042 int ret;
1043
1044 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
1045 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1046
1047 send_wr.next = NULL;
b0a191e7 1048 send_wr.wr_id = (uintptr_t)tx_desc;
b8d26b3b
NB
1049 send_wr.sg_list = tx_desc->tx_sg;
1050 send_wr.num_sge = tx_desc->num_sge;
1051 send_wr.opcode = IB_WR_SEND;
1052 send_wr.send_flags = IB_SEND_SIGNALED;
1053
dac6ab30 1054 ret = ib_post_send(isert_conn->qp, &send_wr, &send_wr_failed);
bdf20e72 1055 if (ret)
24f412dd 1056 isert_err("ib_post_send() failed, ret: %d\n", ret);
b8d26b3b
NB
1057
1058 return ret;
1059}
1060
1061static void
1062isert_create_send_desc(struct isert_conn *isert_conn,
1063 struct isert_cmd *isert_cmd,
1064 struct iser_tx_desc *tx_desc)
1065{
dac6ab30 1066 struct isert_device *device = isert_conn->device;
67cb3949 1067 struct ib_device *ib_dev = device->ib_device;
b8d26b3b
NB
1068
1069 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
1070 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1071
1072 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
1073 tx_desc->iser_header.flags = ISER_VER;
1074
1075 tx_desc->num_sge = 1;
1076 tx_desc->isert_cmd = isert_cmd;
1077
34efc7df
JG
1078 if (tx_desc->tx_sg[0].lkey != device->pd->local_dma_lkey) {
1079 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
24f412dd 1080 isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
b8d26b3b
NB
1081 }
1082}
1083
1084static int
1085isert_init_tx_hdrs(struct isert_conn *isert_conn,
1086 struct iser_tx_desc *tx_desc)
1087{
dac6ab30 1088 struct isert_device *device = isert_conn->device;
67cb3949 1089 struct ib_device *ib_dev = device->ib_device;
b8d26b3b
NB
1090 u64 dma_addr;
1091
1092 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
1093 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1094 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
24f412dd 1095 isert_err("ib_dma_mapping_error() failed\n");
b8d26b3b
NB
1096 return -ENOMEM;
1097 }
1098
1099 tx_desc->dma_addr = dma_addr;
1100 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
1101 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
34efc7df 1102 tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
b8d26b3b 1103
4c22e07f
SG
1104 isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
1105 tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
1106 tx_desc->tx_sg[0].lkey);
b8d26b3b
NB
1107
1108 return 0;
1109}
1110
1111static void
95b60f07 1112isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
68a86dee 1113 struct ib_send_wr *send_wr)
b8d26b3b 1114{
95b60f07
NB
1115 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1116
b8d26b3b 1117 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
b0a191e7 1118 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
b8d26b3b 1119 send_wr->opcode = IB_WR_SEND;
95b60f07 1120 send_wr->sg_list = &tx_desc->tx_sg[0];
b8d26b3b 1121 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
95b60f07 1122 send_wr->send_flags = IB_SEND_SIGNALED;
b8d26b3b
NB
1123}
1124
1125static int
1126isert_rdma_post_recvl(struct isert_conn *isert_conn)
1127{
1128 struct ib_recv_wr rx_wr, *rx_wr_fail;
1129 struct ib_sge sge;
1130 int ret;
1131
1132 memset(&sge, 0, sizeof(struct ib_sge));
1133 sge.addr = isert_conn->login_req_dma;
1134 sge.length = ISER_RX_LOGIN_SIZE;
34efc7df 1135 sge.lkey = isert_conn->device->pd->local_dma_lkey;
b8d26b3b 1136
24f412dd 1137 isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
b8d26b3b
NB
1138 sge.addr, sge.length, sge.lkey);
1139
1140 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
b0a191e7 1141 rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
b8d26b3b
NB
1142 rx_wr.sg_list = &sge;
1143 rx_wr.num_sge = 1;
1144
1145 isert_conn->post_recv_buf_count++;
dac6ab30 1146 ret = ib_post_recv(isert_conn->qp, &rx_wr, &rx_wr_fail);
b8d26b3b 1147 if (ret) {
24f412dd 1148 isert_err("ib_post_recv() failed: %d\n", ret);
b8d26b3b
NB
1149 isert_conn->post_recv_buf_count--;
1150 }
1151
b8d26b3b
NB
1152 return ret;
1153}
1154
1155static int
1156isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1157 u32 length)
1158{
1159 struct isert_conn *isert_conn = conn->context;
dac6ab30 1160 struct isert_device *device = isert_conn->device;
67cb3949 1161 struct ib_device *ib_dev = device->ib_device;
dac6ab30 1162 struct iser_tx_desc *tx_desc = &isert_conn->login_tx_desc;
b8d26b3b
NB
1163 int ret;
1164
1165 isert_create_send_desc(isert_conn, NULL, tx_desc);
1166
1167 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1168 sizeof(struct iscsi_hdr));
1169
1170 isert_init_tx_hdrs(isert_conn, tx_desc);
1171
1172 if (length > 0) {
1173 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1174
1175 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1176 length, DMA_TO_DEVICE);
1177
1178 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1179
1180 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1181 length, DMA_TO_DEVICE);
1182
1183 tx_dsg->addr = isert_conn->login_rsp_dma;
1184 tx_dsg->length = length;
34efc7df 1185 tx_dsg->lkey = isert_conn->device->pd->local_dma_lkey;
b8d26b3b
NB
1186 tx_desc->num_sge = 2;
1187 }
1188 if (!login->login_failed) {
1189 if (login->login_complete) {
e0546fc1 1190 if (!conn->sess->sess_ops->SessionType &&
dac6ab30 1191 isert_conn->device->use_fastreg) {
570db170 1192 ret = isert_conn_create_fastreg_pool(isert_conn);
f46d6a8a 1193 if (ret) {
24f412dd 1194 isert_err("Conn: %p failed to create"
f46d6a8a
NB
1195 " fastreg pool\n", isert_conn);
1196 return ret;
1197 }
1198 }
1199
b8d26b3b
NB
1200 ret = isert_alloc_rx_descriptors(isert_conn);
1201 if (ret)
1202 return ret;
1203
1204 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1205 if (ret)
1206 return ret;
1207
128e9cc8 1208 /* Now we are in FULL_FEATURE phase */
dac6ab30 1209 mutex_lock(&isert_conn->mutex);
128e9cc8 1210 isert_conn->state = ISER_CONN_FULL_FEATURE;
dac6ab30 1211 mutex_unlock(&isert_conn->mutex);
b8d26b3b
NB
1212 goto post_send;
1213 }
1214
1215 ret = isert_rdma_post_recvl(isert_conn);
1216 if (ret)
1217 return ret;
1218 }
1219post_send:
1220 ret = isert_post_send(isert_conn, tx_desc);
1221 if (ret)
1222 return ret;
1223
1224 return 0;
1225}
1226
1227static void
2371e5da 1228isert_rx_login_req(struct isert_conn *isert_conn)
b8d26b3b 1229{
2371e5da
SG
1230 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1231 int rx_buflen = isert_conn->login_req_len;
b8d26b3b
NB
1232 struct iscsi_conn *conn = isert_conn->conn;
1233 struct iscsi_login *login = conn->conn_login;
1234 int size;
1235
24f412dd 1236 isert_info("conn %p\n", isert_conn);
2371e5da
SG
1237
1238 WARN_ON_ONCE(!login);
b8d26b3b
NB
1239
1240 if (login->first_request) {
1241 struct iscsi_login_req *login_req =
1242 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1243 /*
1244 * Setup the initial iscsi_login values from the leading
1245 * login request PDU.
1246 */
1247 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1248 login->current_stage =
1249 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1250 >> 2;
1251 login->version_min = login_req->min_version;
1252 login->version_max = login_req->max_version;
1253 memcpy(login->isid, login_req->isid, 6);
1254 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1255 login->init_task_tag = login_req->itt;
1256 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1257 login->cid = be16_to_cpu(login_req->cid);
1258 login->tsih = be16_to_cpu(login_req->tsih);
1259 }
1260
1261 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1262
1263 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
4c22e07f
SG
1264 isert_dbg("Using login payload size: %d, rx_buflen: %d "
1265 "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
1266 MAX_KEY_VALUE_PAIRS);
b8d26b3b
NB
1267 memcpy(login->req_buf, &rx_desc->data[0], size);
1268
6faaa85f 1269 if (login->first_request) {
dac6ab30 1270 complete(&isert_conn->login_comp);
6faaa85f
NB
1271 return;
1272 }
1273 schedule_delayed_work(&conn->login_work, 0);
b8d26b3b
NB
1274}
1275
b8d26b3b 1276static struct iscsi_cmd
676687c6 1277*isert_allocate_cmd(struct iscsi_conn *conn)
b8d26b3b 1278{
6700425e 1279 struct isert_conn *isert_conn = conn->context;
b8d26b3b 1280 struct isert_cmd *isert_cmd;
d703ce2f 1281 struct iscsi_cmd *cmd;
b8d26b3b 1282
676687c6 1283 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
d703ce2f 1284 if (!cmd) {
24f412dd 1285 isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
b8d26b3b
NB
1286 return NULL;
1287 }
d703ce2f 1288 isert_cmd = iscsit_priv_cmd(cmd);
b8d26b3b 1289 isert_cmd->conn = isert_conn;
d703ce2f 1290 isert_cmd->iscsi_cmd = cmd;
b8d26b3b 1291
d703ce2f 1292 return cmd;
b8d26b3b
NB
1293}
1294
1295static int
1296isert_handle_scsi_cmd(struct isert_conn *isert_conn,
d703ce2f
NB
1297 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1298 struct iser_rx_desc *rx_desc, unsigned char *buf)
b8d26b3b 1299{
b8d26b3b
NB
1300 struct iscsi_conn *conn = isert_conn->conn;
1301 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1302 struct scatterlist *sg;
1303 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1304 bool dump_payload = false;
1305
1306 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1307 if (rc < 0)
1308 return rc;
1309
1310 imm_data = cmd->immediate_data;
1311 imm_data_len = cmd->first_burst_len;
1312 unsol_data = cmd->unsolicited_data;
1313
1314 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1315 if (rc < 0) {
1316 return 0;
1317 } else if (rc > 0) {
1318 dump_payload = true;
1319 goto sequence_cmd;
1320 }
1321
1322 if (!imm_data)
1323 return 0;
1324
1325 sg = &cmd->se_cmd.t_data_sg[0];
1326 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1327
24f412dd 1328 isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
4c22e07f 1329 sg, sg_nents, &rx_desc->data[0], imm_data_len);
b8d26b3b
NB
1330
1331 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1332
1333 cmd->write_data_done += imm_data_len;
1334
1335 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1336 spin_lock_bh(&cmd->istate_lock);
1337 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1338 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1339 spin_unlock_bh(&cmd->istate_lock);
1340 }
1341
1342sequence_cmd:
561bf158 1343 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
b8d26b3b
NB
1344
1345 if (!rc && dump_payload == false && unsol_data)
1346 iscsit_set_unsoliticed_dataout(cmd);
6cc44a6f 1347 else if (dump_payload && imm_data)
afc16604 1348 target_put_sess_cmd(&cmd->se_cmd);
b8d26b3b 1349
b8d26b3b
NB
1350 return 0;
1351}
1352
1353static int
1354isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1355 struct iser_rx_desc *rx_desc, unsigned char *buf)
1356{
1357 struct scatterlist *sg_start;
1358 struct iscsi_conn *conn = isert_conn->conn;
1359 struct iscsi_cmd *cmd = NULL;
1360 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1361 u32 unsol_data_len = ntoh24(hdr->dlength);
1362 int rc, sg_nents, sg_off, page_off;
1363
1364 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1365 if (rc < 0)
1366 return rc;
1367 else if (!cmd)
1368 return 0;
1369 /*
1370 * FIXME: Unexpected unsolicited_data out
1371 */
1372 if (!cmd->unsolicited_data) {
24f412dd 1373 isert_err("Received unexpected solicited data payload\n");
b8d26b3b
NB
1374 dump_stack();
1375 return -1;
1376 }
1377
4c22e07f
SG
1378 isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
1379 "write_data_done: %u, data_length: %u\n",
1380 unsol_data_len, cmd->write_data_done,
1381 cmd->se_cmd.data_length);
b8d26b3b
NB
1382
1383 sg_off = cmd->write_data_done / PAGE_SIZE;
1384 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1385 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1386 page_off = cmd->write_data_done % PAGE_SIZE;
1387 /*
1388 * FIXME: Non page-aligned unsolicited_data out
1389 */
1390 if (page_off) {
4c22e07f 1391 isert_err("unexpected non-page aligned data payload\n");
b8d26b3b
NB
1392 dump_stack();
1393 return -1;
1394 }
4c22e07f
SG
1395 isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
1396 "sg_nents: %u from %p %u\n", sg_start, sg_off,
1397 sg_nents, &rx_desc->data[0], unsol_data_len);
b8d26b3b
NB
1398
1399 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1400 unsol_data_len);
1401
1402 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1403 if (rc < 0)
1404 return rc;
1405
1406 return 0;
1407}
1408
778de368
NB
1409static int
1410isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
d703ce2f
NB
1411 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1412 unsigned char *buf)
778de368 1413{
778de368
NB
1414 struct iscsi_conn *conn = isert_conn->conn;
1415 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1416 int rc;
1417
1418 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1419 if (rc < 0)
1420 return rc;
1421 /*
1422 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1423 */
1424
1425 return iscsit_process_nop_out(conn, cmd, hdr);
1426}
1427
adb54c29
NB
1428static int
1429isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
d703ce2f
NB
1430 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1431 struct iscsi_text *hdr)
adb54c29 1432{
adb54c29
NB
1433 struct iscsi_conn *conn = isert_conn->conn;
1434 u32 payload_length = ntoh24(hdr->dlength);
1435 int rc;
b44a2b67 1436 unsigned char *text_in = NULL;
adb54c29
NB
1437
1438 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1439 if (rc < 0)
1440 return rc;
1441
b44a2b67
SG
1442 if (payload_length) {
1443 text_in = kzalloc(payload_length, GFP_KERNEL);
1444 if (!text_in) {
1445 isert_err("Unable to allocate text_in of payload_length: %u\n",
1446 payload_length);
1447 return -ENOMEM;
1448 }
adb54c29
NB
1449 }
1450 cmd->text_in_ptr = text_in;
1451
1452 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1453
1454 return iscsit_process_text_cmd(conn, cmd, hdr);
1455}
1456
b8d26b3b
NB
1457static int
1458isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1459 uint32_t read_stag, uint64_t read_va,
1460 uint32_t write_stag, uint64_t write_va)
1461{
1462 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1463 struct iscsi_conn *conn = isert_conn->conn;
1464 struct iscsi_cmd *cmd;
1465 struct isert_cmd *isert_cmd;
1466 int ret = -EINVAL;
1467 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1468
fb140271 1469 if (conn->sess->sess_ops->SessionType &&
ca40d24e 1470 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
24f412dd 1471 isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
4c22e07f 1472 " ignoring\n", opcode);
ca40d24e
NB
1473 return 0;
1474 }
1475
b8d26b3b
NB
1476 switch (opcode) {
1477 case ISCSI_OP_SCSI_CMD:
676687c6 1478 cmd = isert_allocate_cmd(conn);
b8d26b3b
NB
1479 if (!cmd)
1480 break;
1481
d703ce2f 1482 isert_cmd = iscsit_priv_cmd(cmd);
b8d26b3b
NB
1483 isert_cmd->read_stag = read_stag;
1484 isert_cmd->read_va = read_va;
1485 isert_cmd->write_stag = write_stag;
1486 isert_cmd->write_va = write_va;
1487
d703ce2f 1488 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
b8d26b3b
NB
1489 rx_desc, (unsigned char *)hdr);
1490 break;
1491 case ISCSI_OP_NOOP_OUT:
676687c6 1492 cmd = isert_allocate_cmd(conn);
b8d26b3b
NB
1493 if (!cmd)
1494 break;
1495
d703ce2f
NB
1496 isert_cmd = iscsit_priv_cmd(cmd);
1497 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
778de368 1498 rx_desc, (unsigned char *)hdr);
b8d26b3b
NB
1499 break;
1500 case ISCSI_OP_SCSI_DATA_OUT:
1501 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1502 (unsigned char *)hdr);
1503 break;
1504 case ISCSI_OP_SCSI_TMFUNC:
676687c6 1505 cmd = isert_allocate_cmd(conn);
b8d26b3b
NB
1506 if (!cmd)
1507 break;
1508
1509 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1510 (unsigned char *)hdr);
1511 break;
1512 case ISCSI_OP_LOGOUT:
676687c6 1513 cmd = isert_allocate_cmd(conn);
b8d26b3b
NB
1514 if (!cmd)
1515 break;
1516
1517 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
b8d26b3b 1518 break;
adb54c29 1519 case ISCSI_OP_TEXT:
e4f4e801
SG
1520 if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
1521 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
1522 if (!cmd)
1523 break;
1524 } else {
1525 cmd = isert_allocate_cmd(conn);
1526 if (!cmd)
1527 break;
1528 }
adb54c29 1529
d703ce2f
NB
1530 isert_cmd = iscsit_priv_cmd(cmd);
1531 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
adb54c29
NB
1532 rx_desc, (struct iscsi_text *)hdr);
1533 break;
b8d26b3b 1534 default:
24f412dd 1535 isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
b8d26b3b
NB
1536 dump_stack();
1537 break;
1538 }
1539
1540 return ret;
1541}
1542
1543static void
1544isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1545{
1546 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1547 uint64_t read_va = 0, write_va = 0;
1548 uint32_t read_stag = 0, write_stag = 0;
1549 int rc;
1550
1551 switch (iser_hdr->flags & 0xF0) {
1552 case ISCSI_CTRL:
1553 if (iser_hdr->flags & ISER_RSV) {
1554 read_stag = be32_to_cpu(iser_hdr->read_stag);
1555 read_va = be64_to_cpu(iser_hdr->read_va);
4c22e07f
SG
1556 isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
1557 read_stag, (unsigned long long)read_va);
b8d26b3b
NB
1558 }
1559 if (iser_hdr->flags & ISER_WSV) {
1560 write_stag = be32_to_cpu(iser_hdr->write_stag);
1561 write_va = be64_to_cpu(iser_hdr->write_va);
4c22e07f
SG
1562 isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
1563 write_stag, (unsigned long long)write_va);
b8d26b3b
NB
1564 }
1565
24f412dd 1566 isert_dbg("ISER ISCSI_CTRL PDU\n");
b8d26b3b
NB
1567 break;
1568 case ISER_HELLO:
24f412dd 1569 isert_err("iSER Hello message\n");
b8d26b3b
NB
1570 break;
1571 default:
24f412dd 1572 isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
b8d26b3b
NB
1573 break;
1574 }
1575
1576 rc = isert_rx_opcode(isert_conn, rx_desc,
1577 read_stag, read_va, write_stag, write_va);
1578}
1579
1580static void
7748681b
SG
1581isert_rcv_completion(struct iser_rx_desc *desc,
1582 struct isert_conn *isert_conn,
1583 u32 xfer_len)
b8d26b3b 1584{
dac6ab30 1585 struct ib_device *ib_dev = isert_conn->cm_id->device;
b8d26b3b
NB
1586 struct iscsi_hdr *hdr;
1587 u64 rx_dma;
1588 int rx_buflen, outstanding;
1589
1590 if ((char *)desc == isert_conn->login_req_buf) {
1591 rx_dma = isert_conn->login_req_dma;
1592 rx_buflen = ISER_RX_LOGIN_SIZE;
4c22e07f 1593 isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
b8d26b3b
NB
1594 rx_dma, rx_buflen);
1595 } else {
1596 rx_dma = desc->dma_addr;
1597 rx_buflen = ISER_RX_PAYLOAD_SIZE;
4c22e07f 1598 isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
b8d26b3b
NB
1599 rx_dma, rx_buflen);
1600 }
1601
1602 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1603
1604 hdr = &desc->iscsi_header;
24f412dd 1605 isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
b8d26b3b
NB
1606 hdr->opcode, hdr->itt, hdr->flags,
1607 (int)(xfer_len - ISER_HEADERS_LEN));
1608
2371e5da
SG
1609 if ((char *)desc == isert_conn->login_req_buf) {
1610 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1611 if (isert_conn->conn) {
1612 struct iscsi_login *login = isert_conn->conn->conn_login;
1613
1614 if (login && !login->first_request)
1615 isert_rx_login_req(isert_conn);
1616 }
dac6ab30 1617 mutex_lock(&isert_conn->mutex);
2371e5da 1618 complete(&isert_conn->login_req_comp);
dac6ab30 1619 mutex_unlock(&isert_conn->mutex);
2371e5da 1620 } else {
b8d26b3b 1621 isert_rx_do_work(desc, isert_conn);
2371e5da 1622 }
b8d26b3b
NB
1623
1624 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1625 DMA_FROM_DEVICE);
1626
1627 isert_conn->post_recv_buf_count--;
4c22e07f
SG
1628 isert_dbg("Decremented post_recv_buf_count: %d\n",
1629 isert_conn->post_recv_buf_count);
b8d26b3b
NB
1630
1631 if ((char *)desc == isert_conn->login_req_buf)
1632 return;
1633
1634 outstanding = isert_conn->post_recv_buf_count;
1635 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1636 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1637 ISERT_MIN_POSTED_RX);
1638 err = isert_post_recv(isert_conn, count);
1639 if (err) {
24f412dd 1640 isert_err("isert_post_recv() count: %d failed, %d\n",
b8d26b3b
NB
1641 count, err);
1642 }
1643 }
1644}
1645
e3d7e4c3
SG
1646static int
1647isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1648 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1649 enum iser_ib_op_code op, struct isert_data_buf *data)
1650{
dac6ab30 1651 struct ib_device *ib_dev = isert_conn->cm_id->device;
e3d7e4c3
SG
1652
1653 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1654 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1655
1656 data->len = length - offset;
1657 data->offset = offset;
1658 data->sg_off = data->offset / PAGE_SIZE;
1659
1660 data->sg = &sg[data->sg_off];
1661 data->nents = min_t(unsigned int, nents - data->sg_off,
1662 ISCSI_ISER_SG_TABLESIZE);
1663 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1664 PAGE_SIZE);
1665
1666 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1667 data->dma_dir);
1668 if (unlikely(!data->dma_nents)) {
24f412dd 1669 isert_err("Cmd: unable to dma map SGs %p\n", sg);
e3d7e4c3
SG
1670 return -EINVAL;
1671 }
1672
24f412dd 1673 isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
4c22e07f 1674 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
e3d7e4c3
SG
1675
1676 return 0;
1677}
1678
1679static void
1680isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1681{
dac6ab30 1682 struct ib_device *ib_dev = isert_conn->cm_id->device;
e3d7e4c3
SG
1683
1684 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1685 memset(data, 0, sizeof(*data));
1686}
1687
1688
1689
b8d26b3b
NB
1690static void
1691isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1692{
1693 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
b8d26b3b 1694
4c22e07f 1695 isert_dbg("Cmd %p\n", isert_cmd);
e3d7e4c3
SG
1696
1697 if (wr->data.sg) {
4c22e07f 1698 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
e3d7e4c3 1699 isert_unmap_data_buf(isert_conn, &wr->data);
b8d26b3b
NB
1700 }
1701
90ecc6e2 1702 if (wr->send_wr) {
4c22e07f 1703 isert_dbg("Cmd %p free send_wr\n", isert_cmd);
90ecc6e2
VP
1704 kfree(wr->send_wr);
1705 wr->send_wr = NULL;
1706 }
b8d26b3b 1707
90ecc6e2 1708 if (wr->ib_sge) {
4c22e07f 1709 isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
90ecc6e2
VP
1710 kfree(wr->ib_sge);
1711 wr->ib_sge = NULL;
1712 }
b8d26b3b
NB
1713}
1714
59464ef4 1715static void
a3a5a826 1716isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
59464ef4
VP
1717{
1718 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
59464ef4 1719
4c22e07f 1720 isert_dbg("Cmd %p\n", isert_cmd);
59464ef4
VP
1721
1722 if (wr->fr_desc) {
4c22e07f 1723 isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
9e961ae7
SG
1724 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1725 isert_unmap_data_buf(isert_conn, &wr->prot);
1726 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1727 }
dac6ab30
SG
1728 spin_lock_bh(&isert_conn->pool_lock);
1729 list_add_tail(&wr->fr_desc->list, &isert_conn->fr_pool);
1730 spin_unlock_bh(&isert_conn->pool_lock);
59464ef4
VP
1731 wr->fr_desc = NULL;
1732 }
1733
e3d7e4c3 1734 if (wr->data.sg) {
4c22e07f 1735 isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
e3d7e4c3 1736 isert_unmap_data_buf(isert_conn, &wr->data);
59464ef4
VP
1737 }
1738
1739 wr->ib_sge = NULL;
1740 wr->send_wr = NULL;
1741}
1742
b8d26b3b 1743static void
03e7848a 1744isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
b8d26b3b 1745{
d703ce2f 1746 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
b8d26b3b 1747 struct isert_conn *isert_conn = isert_cmd->conn;
186a9647 1748 struct iscsi_conn *conn = isert_conn->conn;
dac6ab30 1749 struct isert_device *device = isert_conn->device;
e4f4e801 1750 struct iscsi_text_rsp *hdr;
b8d26b3b 1751
4c22e07f 1752 isert_dbg("Cmd %p\n", isert_cmd);
b8d26b3b
NB
1753
1754 switch (cmd->iscsi_opcode) {
1755 case ISCSI_OP_SCSI_CMD:
b8d26b3b
NB
1756 spin_lock_bh(&conn->cmd_lock);
1757 if (!list_empty(&cmd->i_conn_node))
5159d763 1758 list_del_init(&cmd->i_conn_node);
b8d26b3b
NB
1759 spin_unlock_bh(&conn->cmd_lock);
1760
03e7848a 1761 if (cmd->data_direction == DMA_TO_DEVICE) {
b8d26b3b 1762 iscsit_stop_dataout_timer(cmd);
03e7848a
NB
1763 /*
1764 * Check for special case during comp_err where
1765 * WRITE_PENDING has been handed off from core,
1766 * but requires an extra target_put_sess_cmd()
1767 * before transport_generic_free_cmd() below.
1768 */
1769 if (comp_err &&
1770 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1771 struct se_cmd *se_cmd = &cmd->se_cmd;
1772
afc16604 1773 target_put_sess_cmd(se_cmd);
03e7848a
NB
1774 }
1775 }
b8d26b3b 1776
d40945d8 1777 device->unreg_rdma_mem(isert_cmd, isert_conn);
186a9647
NB
1778 transport_generic_free_cmd(&cmd->se_cmd, 0);
1779 break;
b8d26b3b 1780 case ISCSI_OP_SCSI_TMFUNC:
186a9647
NB
1781 spin_lock_bh(&conn->cmd_lock);
1782 if (!list_empty(&cmd->i_conn_node))
5159d763 1783 list_del_init(&cmd->i_conn_node);
186a9647
NB
1784 spin_unlock_bh(&conn->cmd_lock);
1785
b8d26b3b
NB
1786 transport_generic_free_cmd(&cmd->se_cmd, 0);
1787 break;
1788 case ISCSI_OP_REJECT:
1789 case ISCSI_OP_NOOP_OUT:
adb54c29 1790 case ISCSI_OP_TEXT:
e4f4e801
SG
1791 hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1792 /* If the continue bit is on, keep the command alive */
1793 if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
1794 break;
1795
b8d26b3b
NB
1796 spin_lock_bh(&conn->cmd_lock);
1797 if (!list_empty(&cmd->i_conn_node))
5159d763 1798 list_del_init(&cmd->i_conn_node);
b8d26b3b
NB
1799 spin_unlock_bh(&conn->cmd_lock);
1800
1801 /*
1802 * Handle special case for REJECT when iscsi_add_reject*() has
1803 * overwritten the original iscsi_opcode assignment, and the
1804 * associated cmd->se_cmd needs to be released.
1805 */
1806 if (cmd->se_cmd.se_tfo != NULL) {
11378cdb 1807 isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
3df8f68a 1808 cmd->iscsi_opcode);
b8d26b3b
NB
1809 transport_generic_free_cmd(&cmd->se_cmd, 0);
1810 break;
1811 }
1812 /*
1813 * Fall-through
1814 */
1815 default:
d703ce2f 1816 iscsit_release_cmd(cmd);
b8d26b3b
NB
1817 break;
1818 }
1819}
1820
1821static void
1822isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1823{
1824 if (tx_desc->dma_addr != 0) {
4c22e07f 1825 isert_dbg("unmap single for tx_desc->dma_addr\n");
b8d26b3b
NB
1826 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1827 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1828 tx_desc->dma_addr = 0;
1829 }
1830}
1831
1832static void
1833isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
03e7848a 1834 struct ib_device *ib_dev, bool comp_err)
b8d26b3b 1835{
dbbc5d11 1836 if (isert_cmd->pdu_buf_dma != 0) {
4c22e07f 1837 isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
dbbc5d11
NB
1838 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1839 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1840 isert_cmd->pdu_buf_dma = 0;
b8d26b3b
NB
1841 }
1842
1843 isert_unmap_tx_desc(tx_desc, ib_dev);
03e7848a 1844 isert_put_cmd(isert_cmd, comp_err);
b8d26b3b
NB
1845}
1846
96b7973e
SG
1847static int
1848isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1849{
1850 struct ib_mr_status mr_status;
1851 int ret;
1852
1853 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1854 if (ret) {
24f412dd 1855 isert_err("ib_check_mr_status failed, ret %d\n", ret);
96b7973e
SG
1856 goto fail_mr_status;
1857 }
1858
1859 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1860 u64 sec_offset_err;
1861 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1862
1863 switch (mr_status.sig_err.err_type) {
1864 case IB_SIG_BAD_GUARD:
1865 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1866 break;
1867 case IB_SIG_BAD_REFTAG:
1868 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1869 break;
1870 case IB_SIG_BAD_APPTAG:
1871 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1872 break;
1873 }
1874 sec_offset_err = mr_status.sig_err.sig_err_offset;
1875 do_div(sec_offset_err, block_size);
1876 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1877
4c22e07f
SG
1878 isert_err("PI error found type %d at sector 0x%llx "
1879 "expected 0x%x vs actual 0x%x\n",
1880 mr_status.sig_err.err_type,
1881 (unsigned long long)se_cmd->bad_sector,
1882 mr_status.sig_err.expected,
1883 mr_status.sig_err.actual);
96b7973e
SG
1884 ret = 1;
1885 }
1886
1887fail_mr_status:
1888 return ret;
1889}
1890
f93f3a70
SG
1891static void
1892isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1893 struct isert_cmd *isert_cmd)
1894{
9e961ae7 1895 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
f93f3a70 1896 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
9e961ae7 1897 struct se_cmd *se_cmd = &cmd->se_cmd;
f93f3a70 1898 struct isert_conn *isert_conn = isert_cmd->conn;
dac6ab30 1899 struct isert_device *device = isert_conn->device;
9e961ae7
SG
1900 int ret = 0;
1901
1902 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
96b7973e
SG
1903 ret = isert_check_pi_status(se_cmd,
1904 wr->fr_desc->pi_ctx->sig_mr);
1905 wr->fr_desc->ind &= ~ISERT_PROTECTED;
9e961ae7 1906 }
f93f3a70
SG
1907
1908 device->unreg_rdma_mem(isert_cmd, isert_conn);
897bb2c9 1909 wr->send_wr_num = 0;
9e961ae7
SG
1910 if (ret)
1911 transport_send_check_condition_and_sense(se_cmd,
1912 se_cmd->pi_err, 0);
1913 else
1914 isert_put_response(isert_conn->conn, cmd);
f93f3a70
SG
1915}
1916
b8d26b3b
NB
1917static void
1918isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1919 struct isert_cmd *isert_cmd)
1920{
1921 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
d703ce2f 1922 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
b8d26b3b 1923 struct se_cmd *se_cmd = &cmd->se_cmd;
90ecc6e2 1924 struct isert_conn *isert_conn = isert_cmd->conn;
dac6ab30 1925 struct isert_device *device = isert_conn->device;
5bac4b1a 1926 int ret = 0;
b8d26b3b 1927
9e961ae7 1928 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
96b7973e
SG
1929 ret = isert_check_pi_status(se_cmd,
1930 wr->fr_desc->pi_ctx->sig_mr);
1931 wr->fr_desc->ind &= ~ISERT_PROTECTED;
9e961ae7
SG
1932 }
1933
b8d26b3b 1934 iscsit_stop_dataout_timer(cmd);
d40945d8 1935 device->unreg_rdma_mem(isert_cmd, isert_conn);
e3d7e4c3 1936 cmd->write_data_done = wr->data.len;
b6b87a1d 1937 wr->send_wr_num = 0;
b8d26b3b 1938
24f412dd 1939 isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
b8d26b3b
NB
1940 spin_lock_bh(&cmd->istate_lock);
1941 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1942 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1943 spin_unlock_bh(&cmd->istate_lock);
1944
364189f0 1945 if (ret) {
afc16604 1946 target_put_sess_cmd(se_cmd);
5bac4b1a
SG
1947 transport_send_check_condition_and_sense(se_cmd,
1948 se_cmd->pi_err, 0);
364189f0 1949 } else {
5bac4b1a 1950 target_execute_cmd(se_cmd);
364189f0 1951 }
b8d26b3b
NB
1952}
1953
1954static void
1955isert_do_control_comp(struct work_struct *work)
1956{
1957 struct isert_cmd *isert_cmd = container_of(work,
1958 struct isert_cmd, comp_work);
1959 struct isert_conn *isert_conn = isert_cmd->conn;
dac6ab30 1960 struct ib_device *ib_dev = isert_conn->cm_id->device;
d703ce2f 1961 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
b8d26b3b 1962
4c22e07f
SG
1963 isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
1964
b8d26b3b
NB
1965 switch (cmd->i_state) {
1966 case ISTATE_SEND_TASKMGTRSP:
b8d26b3b 1967 iscsit_tmr_post_handler(cmd, cmd->conn);
10633c37
SG
1968 case ISTATE_SEND_REJECT: /* FALLTHRU */
1969 case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
b8d26b3b 1970 cmd->i_state = ISTATE_SENT_STATUS;
4c22e07f
SG
1971 isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
1972 ib_dev, false);
3df8f68a 1973 break;
b8d26b3b 1974 case ISTATE_SEND_LOGOUTRSP:
b8d26b3b
NB
1975 iscsit_logout_post_handler(cmd, cmd->conn);
1976 break;
1977 default:
4c22e07f 1978 isert_err("Unknown i_state %d\n", cmd->i_state);
b8d26b3b
NB
1979 dump_stack();
1980 break;
1981 }
1982}
1983
1984static void
1985isert_response_completion(struct iser_tx_desc *tx_desc,
1986 struct isert_cmd *isert_cmd,
1987 struct isert_conn *isert_conn,
1988 struct ib_device *ib_dev)
1989{
d703ce2f 1990 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
b8d26b3b
NB
1991
1992 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
3df8f68a 1993 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
adb54c29
NB
1994 cmd->i_state == ISTATE_SEND_REJECT ||
1995 cmd->i_state == ISTATE_SEND_TEXTRSP) {
b8d26b3b
NB
1996 isert_unmap_tx_desc(tx_desc, ib_dev);
1997
1998 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1999 queue_work(isert_comp_wq, &isert_cmd->comp_work);
2000 return;
2001 }
897bb2c9 2002
b8d26b3b 2003 cmd->i_state = ISTATE_SENT_STATUS;
03e7848a 2004 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
b8d26b3b
NB
2005}
2006
2007static void
7748681b 2008isert_snd_completion(struct iser_tx_desc *tx_desc,
68a86dee 2009 struct isert_conn *isert_conn)
b8d26b3b 2010{
dac6ab30 2011 struct ib_device *ib_dev = isert_conn->cm_id->device;
b8d26b3b
NB
2012 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
2013 struct isert_rdma_wr *wr;
2014
2015 if (!isert_cmd) {
b8d26b3b
NB
2016 isert_unmap_tx_desc(tx_desc, ib_dev);
2017 return;
2018 }
2019 wr = &isert_cmd->rdma_wr;
2020
4c22e07f
SG
2021 isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
2022
b8d26b3b 2023 switch (wr->iser_ib_op) {
b8d26b3b 2024 case ISER_IB_SEND:
b8d26b3b
NB
2025 isert_response_completion(tx_desc, isert_cmd,
2026 isert_conn, ib_dev);
2027 break;
2028 case ISER_IB_RDMA_WRITE:
f93f3a70 2029 isert_completion_rdma_write(tx_desc, isert_cmd);
b8d26b3b
NB
2030 break;
2031 case ISER_IB_RDMA_READ:
b8d26b3b
NB
2032 isert_completion_rdma_read(tx_desc, isert_cmd);
2033 break;
2034 default:
4c22e07f 2035 isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
b8d26b3b
NB
2036 dump_stack();
2037 break;
2038 }
2039}
2040
6f0fae3d
SG
2041/**
2042 * is_isert_tx_desc() - Indicate if the completion wr_id
2043 * is a TX descriptor or not.
2044 * @isert_conn: iser connection
2045 * @wr_id: completion WR identifier
2046 *
2047 * Since we cannot rely on wc opcode in FLUSH errors
2048 * we must work around it by checking if the wr_id address
2049 * falls in the iser connection rx_descs buffer. If so
2050 * it is an RX descriptor, otherwize it is a TX.
2051 */
2052static inline bool
2053is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
2054{
dac6ab30
SG
2055 void *start = isert_conn->rx_descs;
2056 int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->rx_descs);
6f0fae3d
SG
2057
2058 if (wr_id >= start && wr_id < start + len)
2059 return false;
2060
2061 return true;
2062}
2063
b8d26b3b 2064static void
6f0fae3d 2065isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
b8d26b3b 2066{
bdf20e72 2067 if (wc->wr_id == ISER_BEACON_WRID) {
dac6ab30 2068 isert_info("conn %p completing wait_comp_err\n",
bdf20e72 2069 isert_conn);
dac6ab30 2070 complete(&isert_conn->wait_comp_err);
ed4520ae 2071 } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
dac6ab30 2072 struct ib_device *ib_dev = isert_conn->cm_id->device;
df43debd 2073 struct isert_cmd *isert_cmd;
6f0fae3d 2074 struct iser_tx_desc *desc;
df43debd 2075
6f0fae3d
SG
2076 desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
2077 isert_cmd = desc->isert_cmd;
df43debd
SG
2078 if (!isert_cmd)
2079 isert_unmap_tx_desc(desc, ib_dev);
2080 else
2081 isert_completion_put(desc, isert_cmd, ib_dev, true);
df43debd
SG
2082 } else {
2083 isert_conn->post_recv_buf_count--;
bdf20e72
SG
2084 if (!isert_conn->post_recv_buf_count)
2085 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
df43debd 2086 }
b8d26b3b
NB
2087}
2088
2089static void
6f0fae3d 2090isert_handle_wc(struct ib_wc *wc)
b8d26b3b 2091{
b8d26b3b
NB
2092 struct isert_conn *isert_conn;
2093 struct iser_tx_desc *tx_desc;
6f0fae3d 2094 struct iser_rx_desc *rx_desc;
b8d26b3b 2095
6f0fae3d
SG
2096 isert_conn = wc->qp->qp_context;
2097 if (likely(wc->status == IB_WC_SUCCESS)) {
2098 if (wc->opcode == IB_WC_RECV) {
2099 rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
7748681b 2100 isert_rcv_completion(rx_desc, isert_conn, wc->byte_len);
b8d26b3b 2101 } else {
6f0fae3d 2102 tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
7748681b 2103 isert_snd_completion(tx_desc, isert_conn);
b8d26b3b 2104 }
6f0fae3d
SG
2105 } else {
2106 if (wc->status != IB_WC_WR_FLUSH_ERR)
ea8a1616
SG
2107 isert_err("%s (%d): wr id %llx vend_err %x\n",
2108 ib_wc_status_msg(wc->status), wc->status,
2109 wc->wr_id, wc->vendor_err);
6f0fae3d 2110 else
ea8a1616
SG
2111 isert_dbg("%s (%d): wr id %llx\n",
2112 ib_wc_status_msg(wc->status), wc->status,
2113 wc->wr_id);
b8d26b3b 2114
6f0fae3d
SG
2115 if (wc->wr_id != ISER_FASTREG_LI_WRID)
2116 isert_cq_comp_err(isert_conn, wc);
2117 }
b8d26b3b
NB
2118}
2119
2120static void
6f0fae3d 2121isert_cq_work(struct work_struct *work)
b8d26b3b 2122{
37d9fe80 2123 enum { isert_poll_budget = 65536 };
4a295bae 2124 struct isert_comp *comp = container_of(work, struct isert_comp,
6f0fae3d 2125 work);
36ea63b5
SG
2126 struct ib_wc *const wcs = comp->wcs;
2127 int i, n, completed = 0;
b8d26b3b 2128
36ea63b5
SG
2129 while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
2130 for (i = 0; i < n; i++)
2131 isert_handle_wc(&wcs[i]);
b8d26b3b 2132
36ea63b5
SG
2133 completed += n;
2134 if (completed >= isert_poll_budget)
37d9fe80
SG
2135 break;
2136 }
2137
6f0fae3d 2138 ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
b8d26b3b
NB
2139}
2140
2141static void
6f0fae3d 2142isert_cq_callback(struct ib_cq *cq, void *context)
b8d26b3b 2143{
4a295bae 2144 struct isert_comp *comp = context;
b8d26b3b 2145
6f0fae3d 2146 queue_work(isert_comp_wq, &comp->work);
b8d26b3b
NB
2147}
2148
2149static int
2150isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2151{
2152 struct ib_send_wr *wr_failed;
2153 int ret;
2154
dac6ab30 2155 ret = ib_post_send(isert_conn->qp, &isert_cmd->tx_desc.send_wr,
b8d26b3b
NB
2156 &wr_failed);
2157 if (ret) {
24f412dd 2158 isert_err("ib_post_send failed with %d\n", ret);
b8d26b3b
NB
2159 return ret;
2160 }
2161 return ret;
2162}
2163
2164static int
2165isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2166{
d703ce2f 2167 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2168 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
2169 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2170 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2171 &isert_cmd->tx_desc.iscsi_header;
2172
2173 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2174 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2175 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2176 /*
2177 * Attach SENSE DATA payload to iSCSI Response PDU
2178 */
2179 if (cmd->se_cmd.sense_buffer &&
2180 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2181 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
dac6ab30 2182 struct isert_device *device = isert_conn->device;
67cb3949 2183 struct ib_device *ib_dev = device->ib_device;
b8d26b3b 2184 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
dbbc5d11 2185 u32 padding, pdu_len;
b8d26b3b
NB
2186
2187 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2188 cmd->sense_buffer);
2189 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2190
2191 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2192 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
dbbc5d11 2193 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
b8d26b3b 2194
dbbc5d11
NB
2195 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2196 (void *)cmd->sense_buffer, pdu_len,
b8d26b3b
NB
2197 DMA_TO_DEVICE);
2198
dbbc5d11
NB
2199 isert_cmd->pdu_buf_len = pdu_len;
2200 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2201 tx_dsg->length = pdu_len;
34efc7df 2202 tx_dsg->lkey = device->pd->local_dma_lkey;
b8d26b3b
NB
2203 isert_cmd->tx_desc.num_sge = 2;
2204 }
2205
68a86dee 2206 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
b8d26b3b 2207
4c22e07f 2208 isert_dbg("Posting SCSI Response\n");
b8d26b3b
NB
2209
2210 return isert_post_response(isert_conn, isert_cmd);
2211}
2212
131e6abc
NB
2213static void
2214isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2215{
2216 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2217 struct isert_conn *isert_conn = conn->context;
dac6ab30 2218 struct isert_device *device = isert_conn->device;
131e6abc
NB
2219
2220 spin_lock_bh(&conn->cmd_lock);
2221 if (!list_empty(&cmd->i_conn_node))
2222 list_del_init(&cmd->i_conn_node);
2223 spin_unlock_bh(&conn->cmd_lock);
2224
2225 if (cmd->data_direction == DMA_TO_DEVICE)
2226 iscsit_stop_dataout_timer(cmd);
2227
2228 device->unreg_rdma_mem(isert_cmd, isert_conn);
2229}
2230
e70beee7
NB
2231static enum target_prot_op
2232isert_get_sup_prot_ops(struct iscsi_conn *conn)
2233{
6700425e 2234 struct isert_conn *isert_conn = conn->context;
dac6ab30 2235 struct isert_device *device = isert_conn->device;
e70beee7 2236
23a548ee
SG
2237 if (conn->tpg->tpg_attrib.t10_pi) {
2238 if (device->pi_capable) {
24f412dd 2239 isert_info("conn %p PI offload enabled\n", isert_conn);
23a548ee
SG
2240 isert_conn->pi_support = true;
2241 return TARGET_PROT_ALL;
2242 }
2243 }
2244
24f412dd 2245 isert_info("conn %p PI offload disabled\n", isert_conn);
23a548ee 2246 isert_conn->pi_support = false;
e70beee7
NB
2247
2248 return TARGET_PROT_NORMAL;
2249}
2250
b8d26b3b
NB
2251static int
2252isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2253 bool nopout_response)
2254{
d703ce2f 2255 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2256 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
2257 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2258
2259 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2260 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2261 &isert_cmd->tx_desc.iscsi_header,
2262 nopout_response);
2263 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
68a86dee 2264 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
b8d26b3b 2265
4c22e07f 2266 isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
b8d26b3b
NB
2267
2268 return isert_post_response(isert_conn, isert_cmd);
2269}
2270
2271static int
2272isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2273{
d703ce2f 2274 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2275 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
2276 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2277
2278 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2279 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2280 &isert_cmd->tx_desc.iscsi_header);
2281 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
68a86dee 2282 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
b8d26b3b 2283
4c22e07f 2284 isert_dbg("conn %p Posting Logout Response\n", isert_conn);
b8d26b3b
NB
2285
2286 return isert_post_response(isert_conn, isert_cmd);
2287}
2288
2289static int
2290isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2291{
d703ce2f 2292 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2293 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
2294 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2295
2296 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2297 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2298 &isert_cmd->tx_desc.iscsi_header);
2299 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
68a86dee 2300 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
b8d26b3b 2301
4c22e07f 2302 isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
b8d26b3b
NB
2303
2304 return isert_post_response(isert_conn, isert_cmd);
2305}
2306
2307static int
2308isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2309{
d703ce2f 2310 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2311 struct isert_conn *isert_conn = conn->context;
b8d26b3b 2312 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
dac6ab30 2313 struct isert_device *device = isert_conn->device;
67cb3949 2314 struct ib_device *ib_dev = device->ib_device;
3df8f68a
NB
2315 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2316 struct iscsi_reject *hdr =
2317 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
b8d26b3b
NB
2318
2319 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
3df8f68a 2320 iscsit_build_reject(cmd, conn, hdr);
b8d26b3b 2321 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
3df8f68a
NB
2322
2323 hton24(hdr->dlength, ISCSI_HDR_LEN);
dbbc5d11 2324 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
3df8f68a
NB
2325 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2326 DMA_TO_DEVICE);
dbbc5d11
NB
2327 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2328 tx_dsg->addr = isert_cmd->pdu_buf_dma;
3df8f68a 2329 tx_dsg->length = ISCSI_HDR_LEN;
34efc7df 2330 tx_dsg->lkey = device->pd->local_dma_lkey;
3df8f68a
NB
2331 isert_cmd->tx_desc.num_sge = 2;
2332
68a86dee 2333 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
b8d26b3b 2334
4c22e07f 2335 isert_dbg("conn %p Posting Reject\n", isert_conn);
b8d26b3b
NB
2336
2337 return isert_post_response(isert_conn, isert_cmd);
2338}
2339
adb54c29
NB
2340static int
2341isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2342{
d703ce2f 2343 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2344 struct isert_conn *isert_conn = conn->context;
adb54c29
NB
2345 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2346 struct iscsi_text_rsp *hdr =
2347 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2348 u32 txt_rsp_len;
2349 int rc;
2350
2351 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
22c7aaa5 2352 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
adb54c29
NB
2353 if (rc < 0)
2354 return rc;
2355
2356 txt_rsp_len = rc;
2357 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2358
2359 if (txt_rsp_len) {
dac6ab30 2360 struct isert_device *device = isert_conn->device;
67cb3949 2361 struct ib_device *ib_dev = device->ib_device;
adb54c29
NB
2362 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2363 void *txt_rsp_buf = cmd->buf_ptr;
2364
2365 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2366 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2367
2368 isert_cmd->pdu_buf_len = txt_rsp_len;
2369 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2370 tx_dsg->length = txt_rsp_len;
34efc7df 2371 tx_dsg->lkey = device->pd->local_dma_lkey;
adb54c29
NB
2372 isert_cmd->tx_desc.num_sge = 2;
2373 }
68a86dee 2374 isert_init_send_wr(isert_conn, isert_cmd, send_wr);
adb54c29 2375
f64d2792 2376 isert_dbg("conn %p Text Response\n", isert_conn);
adb54c29
NB
2377
2378 return isert_post_response(isert_conn, isert_cmd);
2379}
2380
b8d26b3b
NB
2381static int
2382isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2383 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2384 u32 data_left, u32 offset)
2385{
d703ce2f 2386 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
b8d26b3b 2387 struct scatterlist *sg_start, *tmp_sg;
dac6ab30 2388 struct isert_device *device = isert_conn->device;
67cb3949 2389 struct ib_device *ib_dev = device->ib_device;
b8d26b3b
NB
2390 u32 sg_off, page_off;
2391 int i = 0, sg_nents;
2392
2393 sg_off = offset / PAGE_SIZE;
2394 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2395 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2396 page_off = offset % PAGE_SIZE;
2397
2398 send_wr->sg_list = ib_sge;
b0a191e7 2399 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
b8d26b3b
NB
2400 /*
2401 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2402 */
2403 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
4c22e07f
SG
2404 isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
2405 "page_off: %u\n",
2406 (unsigned long long)tmp_sg->dma_address,
2407 tmp_sg->length, page_off);
b8d26b3b
NB
2408
2409 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2410 ib_sge->length = min_t(u32, data_left,
2411 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
34efc7df 2412 ib_sge->lkey = device->pd->local_dma_lkey;
b8d26b3b 2413
4c22e07f
SG
2414 isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
2415 ib_sge->addr, ib_sge->length, ib_sge->lkey);
b8d26b3b
NB
2416 page_off = 0;
2417 data_left -= ib_sge->length;
9253e667
SG
2418 if (!data_left)
2419 break;
b8d26b3b 2420 ib_sge++;
24f412dd 2421 isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
b8d26b3b
NB
2422 }
2423
9253e667 2424 send_wr->num_sge = ++i;
24f412dd 2425 isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
4c22e07f 2426 send_wr->sg_list, send_wr->num_sge);
b8d26b3b 2427
9253e667 2428 return send_wr->num_sge;
b8d26b3b
NB
2429}
2430
2431static int
90ecc6e2
VP
2432isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2433 struct isert_rdma_wr *wr)
b8d26b3b
NB
2434{
2435 struct se_cmd *se_cmd = &cmd->se_cmd;
d703ce2f 2436 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
6700425e 2437 struct isert_conn *isert_conn = conn->context;
e3d7e4c3 2438 struct isert_data_buf *data = &wr->data;
90ecc6e2 2439 struct ib_send_wr *send_wr;
b8d26b3b 2440 struct ib_sge *ib_sge;
e3d7e4c3
SG
2441 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2442 int ret = 0, i, ib_sge_cnt;
90ecc6e2 2443
e3d7e4c3 2444 isert_cmd->tx_desc.isert_cmd = isert_cmd;
b8d26b3b 2445
e3d7e4c3
SG
2446 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2447 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2448 se_cmd->t_data_nents, se_cmd->data_length,
2449 offset, wr->iser_ib_op, &wr->data);
2450 if (ret)
2451 return ret;
b8d26b3b 2452
e3d7e4c3
SG
2453 data_left = data->len;
2454 offset = data->offset;
b8d26b3b 2455
e3d7e4c3 2456 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
b8d26b3b 2457 if (!ib_sge) {
24f412dd 2458 isert_warn("Unable to allocate ib_sge\n");
b8d26b3b 2459 ret = -ENOMEM;
e3d7e4c3 2460 goto unmap_cmd;
b8d26b3b 2461 }
90ecc6e2 2462 wr->ib_sge = ib_sge;
b8d26b3b 2463
e3d7e4c3 2464 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
b8d26b3b
NB
2465 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2466 GFP_KERNEL);
2467 if (!wr->send_wr) {
24f412dd 2468 isert_dbg("Unable to allocate wr->send_wr\n");
b8d26b3b 2469 ret = -ENOMEM;
e3d7e4c3 2470 goto unmap_cmd;
b8d26b3b 2471 }
b8d26b3b
NB
2472
2473 wr->isert_cmd = isert_cmd;
2474 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
b8d26b3b
NB
2475
2476 for (i = 0; i < wr->send_wr_num; i++) {
2477 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2478 data_len = min(data_left, rdma_write_max);
2479
b8d26b3b 2480 send_wr->send_flags = 0;
90ecc6e2
VP
2481 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2482 send_wr->opcode = IB_WR_RDMA_WRITE;
2483 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2484 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2485 if (i + 1 == wr->send_wr_num)
2486 send_wr->next = &isert_cmd->tx_desc.send_wr;
2487 else
2488 send_wr->next = &wr->send_wr[i + 1];
2489 } else {
2490 send_wr->opcode = IB_WR_RDMA_READ;
2491 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2492 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2493 if (i + 1 == wr->send_wr_num)
2494 send_wr->send_flags = IB_SEND_SIGNALED;
2495 else
2496 send_wr->next = &wr->send_wr[i + 1];
2497 }
b8d26b3b
NB
2498
2499 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2500 send_wr, data_len, offset);
2501 ib_sge += ib_sge_cnt;
2502
b8d26b3b 2503 offset += data_len;
90ecc6e2 2504 va_offset += data_len;
b8d26b3b
NB
2505 data_left -= data_len;
2506 }
90ecc6e2
VP
2507
2508 return 0;
e3d7e4c3
SG
2509unmap_cmd:
2510 isert_unmap_data_buf(isert_conn, data);
2511
90ecc6e2
VP
2512 return ret;
2513}
2514
59464ef4
VP
2515static int
2516isert_map_fr_pagelist(struct ib_device *ib_dev,
2517 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2518{
2519 u64 start_addr, end_addr, page, chunk_start = 0;
2520 struct scatterlist *tmp_sg;
2521 int i = 0, new_chunk, last_ent, n_pages;
2522
2523 n_pages = 0;
2524 new_chunk = 1;
2525 last_ent = sg_nents - 1;
2526 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2527 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2528 if (new_chunk)
2529 chunk_start = start_addr;
2530 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2531
4c22e07f
SG
2532 isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
2533 i, (unsigned long long)tmp_sg->dma_address,
2534 tmp_sg->length);
59464ef4
VP
2535
2536 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2537 new_chunk = 0;
2538 continue;
2539 }
2540 new_chunk = 1;
2541
2542 page = chunk_start & PAGE_MASK;
2543 do {
2544 fr_pl[n_pages++] = page;
4c22e07f
SG
2545 isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
2546 n_pages - 1, page);
59464ef4
VP
2547 page += PAGE_SIZE;
2548 } while (page < end_addr);
2549 }
2550
2551 return n_pages;
2552}
2553
10633c37
SG
2554static inline void
2555isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
2556{
2557 u32 rkey;
2558
2559 memset(inv_wr, 0, sizeof(*inv_wr));
2560 inv_wr->wr_id = ISER_FASTREG_LI_WRID;
2561 inv_wr->opcode = IB_WR_LOCAL_INV;
2562 inv_wr->ex.invalidate_rkey = mr->rkey;
2563
2564 /* Bump the key */
2565 rkey = ib_inc_rkey(mr->rkey);
2566 ib_update_fast_reg_key(mr, rkey);
2567}
2568
59464ef4 2569static int
e3d7e4c3
SG
2570isert_fast_reg_mr(struct isert_conn *isert_conn,
2571 struct fast_reg_descriptor *fr_desc,
2572 struct isert_data_buf *mem,
9e961ae7 2573 enum isert_indicator ind,
e3d7e4c3 2574 struct ib_sge *sge)
59464ef4 2575{
dac6ab30 2576 struct isert_device *device = isert_conn->device;
67cb3949 2577 struct ib_device *ib_dev = device->ib_device;
9e961ae7
SG
2578 struct ib_mr *mr;
2579 struct ib_fast_reg_page_list *frpl;
59464ef4
VP
2580 struct ib_send_wr fr_wr, inv_wr;
2581 struct ib_send_wr *bad_wr, *wr = NULL;
9bd626e7
SG
2582 int ret, pagelist_len;
2583 u32 page_off;
59464ef4 2584
e3d7e4c3 2585 if (mem->dma_nents == 1) {
34efc7df 2586 sge->lkey = device->pd->local_dma_lkey;
e3d7e4c3
SG
2587 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2588 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
4c22e07f
SG
2589 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2590 sge->addr, sge->length, sge->lkey);
e3d7e4c3
SG
2591 return 0;
2592 }
2593
9e961ae7
SG
2594 if (ind == ISERT_DATA_KEY_VALID) {
2595 /* Registering data buffer */
2596 mr = fr_desc->data_mr;
2597 frpl = fr_desc->data_frpl;
2598 } else {
2599 /* Registering protection buffer */
2600 mr = fr_desc->pi_ctx->prot_mr;
2601 frpl = fr_desc->pi_ctx->prot_frpl;
2602 }
2603
e3d7e4c3 2604 page_off = mem->offset % PAGE_SIZE;
59464ef4 2605
24f412dd 2606 isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
4c22e07f 2607 fr_desc, mem->nents, mem->offset);
59464ef4 2608
e3d7e4c3 2609 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
9e961ae7 2610 &frpl->page_list[0]);
59464ef4 2611
10633c37
SG
2612 if (!(fr_desc->ind & ind)) {
2613 isert_inv_rkey(&inv_wr, mr);
59464ef4 2614 wr = &inv_wr;
59464ef4
VP
2615 }
2616
2617 /* Prepare FASTREG WR */
2618 memset(&fr_wr, 0, sizeof(fr_wr));
9bb4ca68 2619 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
59464ef4 2620 fr_wr.opcode = IB_WR_FAST_REG_MR;
9e961ae7
SG
2621 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2622 fr_wr.wr.fast_reg.page_list = frpl;
59464ef4
VP
2623 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2624 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
e3d7e4c3 2625 fr_wr.wr.fast_reg.length = mem->len;
9e961ae7 2626 fr_wr.wr.fast_reg.rkey = mr->rkey;
59464ef4
VP
2627 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2628
2629 if (!wr)
2630 wr = &fr_wr;
2631 else
2632 wr->next = &fr_wr;
2633
dac6ab30 2634 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
59464ef4 2635 if (ret) {
24f412dd 2636 isert_err("fast registration failed, ret:%d\n", ret);
59464ef4
VP
2637 return ret;
2638 }
9e961ae7 2639 fr_desc->ind &= ~ind;
59464ef4 2640
9e961ae7
SG
2641 sge->lkey = mr->lkey;
2642 sge->addr = frpl->page_list[0] + page_off;
e3d7e4c3 2643 sge->length = mem->len;
59464ef4 2644
4c22e07f
SG
2645 isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
2646 sge->addr, sge->length, sge->lkey);
9e961ae7
SG
2647
2648 return ret;
2649}
2650
3d73cf1a
SG
2651static inline void
2652isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2653 struct ib_sig_domain *domain)
2654{
78eda2bb 2655 domain->sig_type = IB_SIG_TYPE_T10_DIF;
3d73cf1a
SG
2656 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2657 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2658 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
78eda2bb
SG
2659 /*
2660 * At the moment we hard code those, but if in the future
2661 * the target core would like to use it, we will take it
2662 * from se_cmd.
2663 */
2664 domain->sig.dif.apptag_check_mask = 0xffff;
2665 domain->sig.dif.app_escape = true;
2666 domain->sig.dif.ref_escape = true;
2667 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2668 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2669 domain->sig.dif.ref_remap = true;
3d73cf1a
SG
2670};
2671
9e961ae7
SG
2672static int
2673isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2674{
9e961ae7
SG
2675 switch (se_cmd->prot_op) {
2676 case TARGET_PROT_DIN_INSERT:
2677 case TARGET_PROT_DOUT_STRIP:
78eda2bb 2678 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
3d73cf1a 2679 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
9e961ae7
SG
2680 break;
2681 case TARGET_PROT_DOUT_INSERT:
2682 case TARGET_PROT_DIN_STRIP:
78eda2bb 2683 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
3d73cf1a 2684 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
9e961ae7
SG
2685 break;
2686 case TARGET_PROT_DIN_PASS:
2687 case TARGET_PROT_DOUT_PASS:
3d73cf1a
SG
2688 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2689 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
9e961ae7
SG
2690 break;
2691 default:
24f412dd 2692 isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
9e961ae7
SG
2693 return -EINVAL;
2694 }
2695
2696 return 0;
2697}
2698
2699static inline u8
2700isert_set_prot_checks(u8 prot_checks)
2701{
2702 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2703 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2704 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2705}
2706
2707static int
570db170
SG
2708isert_reg_sig_mr(struct isert_conn *isert_conn,
2709 struct se_cmd *se_cmd,
2710 struct isert_rdma_wr *rdma_wr,
2711 struct fast_reg_descriptor *fr_desc)
9e961ae7
SG
2712{
2713 struct ib_send_wr sig_wr, inv_wr;
2714 struct ib_send_wr *bad_wr, *wr = NULL;
2715 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2716 struct ib_sig_attrs sig_attrs;
2717 int ret;
9e961ae7
SG
2718
2719 memset(&sig_attrs, 0, sizeof(sig_attrs));
2720 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2721 if (ret)
2722 goto err;
59464ef4 2723
9e961ae7
SG
2724 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2725
2726 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
10633c37 2727 isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
9e961ae7 2728 wr = &inv_wr;
9e961ae7
SG
2729 }
2730
2731 memset(&sig_wr, 0, sizeof(sig_wr));
2732 sig_wr.opcode = IB_WR_REG_SIG_MR;
c2caa207 2733 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
570db170 2734 sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
9e961ae7
SG
2735 sig_wr.num_sge = 1;
2736 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2737 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2738 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2739 if (se_cmd->t_prot_sg)
570db170 2740 sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
9e961ae7
SG
2741
2742 if (!wr)
2743 wr = &sig_wr;
2744 else
2745 wr->next = &sig_wr;
2746
dac6ab30 2747 ret = ib_post_send(isert_conn->qp, wr, &bad_wr);
9e961ae7 2748 if (ret) {
24f412dd 2749 isert_err("fast registration failed, ret:%d\n", ret);
9e961ae7
SG
2750 goto err;
2751 }
2752 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2753
570db170
SG
2754 rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
2755 rdma_wr->ib_sg[SIG].addr = 0;
2756 rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
9e961ae7
SG
2757 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2758 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2759 /*
2760 * We have protection guards on the wire
2761 * so we need to set a larget transfer
2762 */
570db170 2763 rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
9e961ae7 2764
24f412dd 2765 isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
570db170
SG
2766 rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
2767 rdma_wr->ib_sg[SIG].lkey);
9e961ae7 2768err:
59464ef4
VP
2769 return ret;
2770}
2771
570db170
SG
2772static int
2773isert_handle_prot_cmd(struct isert_conn *isert_conn,
2774 struct isert_cmd *isert_cmd,
2775 struct isert_rdma_wr *wr)
2776{
dac6ab30 2777 struct isert_device *device = isert_conn->device;
570db170
SG
2778 struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
2779 int ret;
2780
2781 if (!wr->fr_desc->pi_ctx) {
2782 ret = isert_create_pi_ctx(wr->fr_desc,
2783 device->ib_device,
67cb3949 2784 device->pd);
570db170 2785 if (ret) {
24f412dd 2786 isert_err("conn %p failed to allocate pi_ctx\n",
570db170
SG
2787 isert_conn);
2788 return ret;
2789 }
2790 }
2791
2792 if (se_cmd->t_prot_sg) {
2793 ret = isert_map_data_buf(isert_conn, isert_cmd,
2794 se_cmd->t_prot_sg,
2795 se_cmd->t_prot_nents,
2796 se_cmd->prot_length,
2797 0, wr->iser_ib_op, &wr->prot);
2798 if (ret) {
24f412dd 2799 isert_err("conn %p failed to map protection buffer\n",
570db170
SG
2800 isert_conn);
2801 return ret;
2802 }
2803
2804 memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
2805 ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
2806 ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
2807 if (ret) {
24f412dd 2808 isert_err("conn %p failed to fast reg mr\n",
570db170
SG
2809 isert_conn);
2810 goto unmap_prot_cmd;
2811 }
2812 }
2813
2814 ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
2815 if (ret) {
24f412dd 2816 isert_err("conn %p failed to fast reg mr\n",
570db170
SG
2817 isert_conn);
2818 goto unmap_prot_cmd;
2819 }
2820 wr->fr_desc->ind |= ISERT_PROTECTED;
2821
2822 return 0;
2823
2824unmap_prot_cmd:
2825 if (se_cmd->t_prot_sg)
2826 isert_unmap_data_buf(isert_conn, &wr->prot);
2827
2828 return ret;
2829}
2830
59464ef4 2831static int
a3a5a826
SG
2832isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2833 struct isert_rdma_wr *wr)
59464ef4
VP
2834{
2835 struct se_cmd *se_cmd = &cmd->se_cmd;
2836 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
e3d7e4c3 2837 struct isert_conn *isert_conn = conn->context;
e3d7e4c3 2838 struct fast_reg_descriptor *fr_desc = NULL;
570db170
SG
2839 struct ib_send_wr *send_wr;
2840 struct ib_sge *ib_sg;
e3d7e4c3
SG
2841 u32 offset;
2842 int ret = 0;
59464ef4
VP
2843 unsigned long flags;
2844
e3d7e4c3 2845 isert_cmd->tx_desc.isert_cmd = isert_cmd;
59464ef4 2846
e3d7e4c3
SG
2847 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2848 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2849 se_cmd->t_data_nents, se_cmd->data_length,
2850 offset, wr->iser_ib_op, &wr->data);
2851 if (ret)
2852 return ret;
59464ef4 2853
302cc7c3 2854 if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
dac6ab30
SG
2855 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2856 fr_desc = list_first_entry(&isert_conn->fr_pool,
e3d7e4c3
SG
2857 struct fast_reg_descriptor, list);
2858 list_del(&fr_desc->list);
dac6ab30 2859 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
e3d7e4c3 2860 wr->fr_desc = fr_desc;
59464ef4 2861 }
59464ef4 2862
9e961ae7 2863 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
570db170 2864 ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
e3d7e4c3
SG
2865 if (ret)
2866 goto unmap_cmd;
59464ef4 2867
302cc7c3 2868 if (isert_prot_cmd(isert_conn, se_cmd)) {
570db170 2869 ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
9e961ae7 2870 if (ret)
570db170 2871 goto unmap_cmd;
9e961ae7 2872
570db170
SG
2873 ib_sg = &wr->ib_sg[SIG];
2874 } else {
2875 ib_sg = &wr->ib_sg[DATA];
2876 }
9e961ae7 2877
570db170 2878 memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
e3d7e4c3 2879 wr->ib_sge = &wr->s_ib_sge;
59464ef4
VP
2880 wr->send_wr_num = 1;
2881 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2882 wr->send_wr = &wr->s_send_wr;
59464ef4 2883 wr->isert_cmd = isert_cmd;
59464ef4
VP
2884
2885 send_wr = &isert_cmd->rdma_wr.s_send_wr;
e3d7e4c3 2886 send_wr->sg_list = &wr->s_ib_sge;
59464ef4 2887 send_wr->num_sge = 1;
b0a191e7 2888 send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
59464ef4
VP
2889 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2890 send_wr->opcode = IB_WR_RDMA_WRITE;
2891 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2892 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
302cc7c3 2893 send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
9e961ae7 2894 0 : IB_SEND_SIGNALED;
59464ef4
VP
2895 } else {
2896 send_wr->opcode = IB_WR_RDMA_READ;
2897 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2898 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2899 send_wr->send_flags = IB_SEND_SIGNALED;
2900 }
2901
e3d7e4c3 2902 return 0;
570db170 2903
e3d7e4c3
SG
2904unmap_cmd:
2905 if (fr_desc) {
dac6ab30
SG
2906 spin_lock_irqsave(&isert_conn->pool_lock, flags);
2907 list_add_tail(&fr_desc->list, &isert_conn->fr_pool);
2908 spin_unlock_irqrestore(&isert_conn->pool_lock, flags);
59464ef4 2909 }
e3d7e4c3 2910 isert_unmap_data_buf(isert_conn, &wr->data);
59464ef4 2911
59464ef4
VP
2912 return ret;
2913}
2914
90ecc6e2
VP
2915static int
2916isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2917{
2918 struct se_cmd *se_cmd = &cmd->se_cmd;
59464ef4 2919 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
90ecc6e2 2920 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
6700425e 2921 struct isert_conn *isert_conn = conn->context;
dac6ab30 2922 struct isert_device *device = isert_conn->device;
90ecc6e2
VP
2923 struct ib_send_wr *wr_failed;
2924 int rc;
2925
24f412dd 2926 isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
90ecc6e2 2927 isert_cmd, se_cmd->data_length);
4c22e07f 2928
90ecc6e2 2929 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
d40945d8 2930 rc = device->reg_rdma_mem(conn, cmd, wr);
90ecc6e2 2931 if (rc) {
24f412dd 2932 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
90ecc6e2
VP
2933 return rc;
2934 }
2935
302cc7c3 2936 if (!isert_prot_cmd(isert_conn, se_cmd)) {
9e961ae7
SG
2937 /*
2938 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2939 */
2940 isert_create_send_desc(isert_conn, isert_cmd,
2941 &isert_cmd->tx_desc);
2942 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2943 &isert_cmd->tx_desc.iscsi_header);
2944 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2945 isert_init_send_wr(isert_conn, isert_cmd,
68a86dee 2946 &isert_cmd->tx_desc.send_wr);
9e961ae7 2947 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
897bb2c9 2948 wr->send_wr_num += 1;
9e961ae7 2949 }
b8d26b3b 2950
dac6ab30 2951 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
bdf20e72 2952 if (rc)
24f412dd 2953 isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
9e961ae7 2954
302cc7c3 2955 if (!isert_prot_cmd(isert_conn, se_cmd))
24f412dd 2956 isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
9e961ae7
SG
2957 "READ\n", isert_cmd);
2958 else
24f412dd 2959 isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
9e961ae7 2960 isert_cmd);
b8d26b3b 2961
90ecc6e2 2962 return 1;
b8d26b3b
NB
2963}
2964
2965static int
2966isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2967{
2968 struct se_cmd *se_cmd = &cmd->se_cmd;
d703ce2f 2969 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
b8d26b3b 2970 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
6700425e 2971 struct isert_conn *isert_conn = conn->context;
dac6ab30 2972 struct isert_device *device = isert_conn->device;
90ecc6e2
VP
2973 struct ib_send_wr *wr_failed;
2974 int rc;
b8d26b3b 2975
24f412dd 2976 isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
90ecc6e2 2977 isert_cmd, se_cmd->data_length, cmd->write_data_done);
b8d26b3b 2978 wr->iser_ib_op = ISER_IB_RDMA_READ;
d40945d8 2979 rc = device->reg_rdma_mem(conn, cmd, wr);
90ecc6e2 2980 if (rc) {
24f412dd 2981 isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
90ecc6e2 2982 return rc;
b8d26b3b
NB
2983 }
2984
dac6ab30 2985 rc = ib_post_send(isert_conn->qp, wr->send_wr, &wr_failed);
bdf20e72 2986 if (rc)
24f412dd 2987 isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
bdf20e72 2988
24f412dd 2989 isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
90ecc6e2 2990 isert_cmd);
b8d26b3b 2991
90ecc6e2 2992 return 0;
b8d26b3b
NB
2993}
2994
2995static int
2996isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2997{
a4c15cd9
JD
2998 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2999 int ret = 0;
b8d26b3b
NB
3000
3001 switch (state) {
a4c15cd9
JD
3002 case ISTATE_REMOVE:
3003 spin_lock_bh(&conn->cmd_lock);
3004 list_del_init(&cmd->i_conn_node);
3005 spin_unlock_bh(&conn->cmd_lock);
3006 isert_put_cmd(isert_cmd, true);
3007 break;
b8d26b3b
NB
3008 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3009 ret = isert_put_nopin(cmd, conn, false);
3010 break;
3011 default:
24f412dd 3012 isert_err("Unknown immediate state: 0x%02x\n", state);
b8d26b3b
NB
3013 ret = -EINVAL;
3014 break;
3015 }
3016
3017 return ret;
3018}
3019
3020static int
3021isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3022{
991bb764 3023 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
3024 int ret;
3025
3026 switch (state) {
3027 case ISTATE_SEND_LOGOUTRSP:
3028 ret = isert_put_logout_rsp(cmd, conn);
991bb764
SG
3029 if (!ret)
3030 isert_conn->logout_posted = true;
b8d26b3b
NB
3031 break;
3032 case ISTATE_SEND_NOPIN:
3033 ret = isert_put_nopin(cmd, conn, true);
3034 break;
3035 case ISTATE_SEND_TASKMGTRSP:
3036 ret = isert_put_tm_rsp(cmd, conn);
3037 break;
3038 case ISTATE_SEND_REJECT:
3039 ret = isert_put_reject(cmd, conn);
3040 break;
adb54c29
NB
3041 case ISTATE_SEND_TEXTRSP:
3042 ret = isert_put_text_rsp(cmd, conn);
3043 break;
b8d26b3b
NB
3044 case ISTATE_SEND_STATUS:
3045 /*
3046 * Special case for sending non GOOD SCSI status from TX thread
3047 * context during pre se_cmd excecution failure.
3048 */
3049 ret = isert_put_response(conn, cmd);
3050 break;
3051 default:
24f412dd 3052 isert_err("Unknown response state: 0x%02x\n", state);
b8d26b3b
NB
3053 ret = -EINVAL;
3054 break;
3055 }
3056
3057 return ret;
3058}
3059
ca6c1d82
SG
3060struct rdma_cm_id *
3061isert_setup_id(struct isert_np *isert_np)
3062{
3063 struct iscsi_np *np = isert_np->np;
3064 struct rdma_cm_id *id;
3065 struct sockaddr *sa;
3066 int ret;
3067
3068 sa = (struct sockaddr *)&np->np_sockaddr;
24f412dd 3069 isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
ca6c1d82
SG
3070
3071 id = rdma_create_id(isert_cma_handler, isert_np,
3072 RDMA_PS_TCP, IB_QPT_RC);
3073 if (IS_ERR(id)) {
24f412dd 3074 isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
ca6c1d82
SG
3075 ret = PTR_ERR(id);
3076 goto out;
3077 }
24f412dd 3078 isert_dbg("id %p context %p\n", id, id->context);
ca6c1d82
SG
3079
3080 ret = rdma_bind_addr(id, sa);
3081 if (ret) {
24f412dd 3082 isert_err("rdma_bind_addr() failed: %d\n", ret);
ca6c1d82
SG
3083 goto out_id;
3084 }
3085
992607e8 3086 ret = rdma_listen(id, 0);
ca6c1d82 3087 if (ret) {
24f412dd 3088 isert_err("rdma_listen() failed: %d\n", ret);
ca6c1d82
SG
3089 goto out_id;
3090 }
3091
3092 return id;
3093out_id:
3094 rdma_destroy_id(id);
3095out:
3096 return ERR_PTR(ret);
3097}
3098
b8d26b3b
NB
3099static int
3100isert_setup_np(struct iscsi_np *np,
13a3cf08 3101 struct sockaddr_storage *ksockaddr)
b8d26b3b
NB
3102{
3103 struct isert_np *isert_np;
3104 struct rdma_cm_id *isert_lid;
b8d26b3b
NB
3105 int ret;
3106
3107 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3108 if (!isert_np) {
24f412dd 3109 isert_err("Unable to allocate struct isert_np\n");
b8d26b3b
NB
3110 return -ENOMEM;
3111 }
ed8cb0a4
JD
3112 sema_init(&isert_np->sem, 0);
3113 mutex_init(&isert_np->mutex);
bd379220
JD
3114 INIT_LIST_HEAD(&isert_np->accepted);
3115 INIT_LIST_HEAD(&isert_np->pending);
ca6c1d82 3116 isert_np->np = np;
b8d26b3b 3117
b8d26b3b
NB
3118 /*
3119 * Setup the np->np_sockaddr from the passed sockaddr setup
3120 * in iscsi_target_configfs.c code..
3121 */
3122 memcpy(&np->np_sockaddr, ksockaddr,
13a3cf08 3123 sizeof(struct sockaddr_storage));
b8d26b3b 3124
ca6c1d82 3125 isert_lid = isert_setup_id(isert_np);
b8d26b3b 3126 if (IS_ERR(isert_lid)) {
b8d26b3b
NB
3127 ret = PTR_ERR(isert_lid);
3128 goto out;
3129 }
3130
ed8cb0a4 3131 isert_np->cm_id = isert_lid;
b8d26b3b 3132 np->np_context = isert_np;
b8d26b3b
NB
3133
3134 return 0;
3135
b8d26b3b
NB
3136out:
3137 kfree(isert_np);
ca6c1d82 3138
b8d26b3b
NB
3139 return ret;
3140}
3141
b8d26b3b
NB
3142static int
3143isert_rdma_accept(struct isert_conn *isert_conn)
3144{
dac6ab30 3145 struct rdma_cm_id *cm_id = isert_conn->cm_id;
b8d26b3b
NB
3146 struct rdma_conn_param cp;
3147 int ret;
3148
3149 memset(&cp, 0, sizeof(struct rdma_conn_param));
b8d26b3b
NB
3150 cp.initiator_depth = isert_conn->initiator_depth;
3151 cp.retry_count = 7;
3152 cp.rnr_retry_count = 7;
3153
b8d26b3b
NB
3154 ret = rdma_accept(cm_id, &cp);
3155 if (ret) {
24f412dd 3156 isert_err("rdma_accept() failed with: %d\n", ret);
b8d26b3b
NB
3157 return ret;
3158 }
3159
b8d26b3b
NB
3160 return 0;
3161}
3162
3163static int
3164isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3165{
6700425e 3166 struct isert_conn *isert_conn = conn->context;
b8d26b3b
NB
3167 int ret;
3168
24f412dd 3169 isert_info("before login_req comp conn: %p\n", isert_conn);
2371e5da
SG
3170 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
3171 if (ret) {
24f412dd 3172 isert_err("isert_conn %p interrupted before got login req\n",
2371e5da
SG
3173 isert_conn);
3174 return ret;
3175 }
3176 reinit_completion(&isert_conn->login_req_comp);
3177
6faaa85f
NB
3178 /*
3179 * For login requests after the first PDU, isert_rx_login_req() will
3180 * kick schedule_delayed_work(&conn->login_work) as the packet is
3181 * received, which turns this callback from iscsi_target_do_login_rx()
3182 * into a NOP.
3183 */
3184 if (!login->first_request)
3185 return 0;
b8d26b3b 3186
2371e5da
SG
3187 isert_rx_login_req(isert_conn);
3188
dac6ab30
SG
3189 isert_info("before login_comp conn: %p\n", conn);
3190 ret = wait_for_completion_interruptible(&isert_conn->login_comp);
b8d26b3b
NB
3191 if (ret)
3192 return ret;
3193
24f412dd 3194 isert_info("processing login->req: %p\n", login->req);
2371e5da 3195
b8d26b3b
NB
3196 return 0;
3197}
3198
3199static void
3200isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3201 struct isert_conn *isert_conn)
3202{
dac6ab30 3203 struct rdma_cm_id *cm_id = isert_conn->cm_id;
b8d26b3b 3204 struct rdma_route *cm_route = &cm_id->route;
b8d26b3b
NB
3205
3206 conn->login_family = np->np_sockaddr.ss_family;
3207
dc58f760
AG
3208 conn->login_sockaddr = cm_route->addr.dst_addr;
3209 conn->local_sockaddr = cm_route->addr.src_addr;
b8d26b3b
NB
3210}
3211
3212static int
3213isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3214{
6700425e 3215 struct isert_np *isert_np = np->np_context;
b8d26b3b 3216 struct isert_conn *isert_conn;
c6b8e918 3217 int ret;
b8d26b3b
NB
3218
3219accept_wait:
ed8cb0a4 3220 ret = down_interruptible(&isert_np->sem);
c6b8e918 3221 if (ret)
b8d26b3b
NB
3222 return -ENODEV;
3223
3224 spin_lock_bh(&np->np_thread_lock);
e346ab34 3225 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
b8d26b3b 3226 spin_unlock_bh(&np->np_thread_lock);
11378cdb 3227 isert_dbg("np_thread_state %d\n",
e346ab34
SG
3228 np->np_thread_state);
3229 /**
3230 * No point in stalling here when np_thread
3231 * is in state RESET/SHUTDOWN/EXIT - bail
3232 **/
b8d26b3b
NB
3233 return -ENODEV;
3234 }
3235 spin_unlock_bh(&np->np_thread_lock);
3236
ed8cb0a4 3237 mutex_lock(&isert_np->mutex);
bd379220 3238 if (list_empty(&isert_np->pending)) {
ed8cb0a4 3239 mutex_unlock(&isert_np->mutex);
b8d26b3b
NB
3240 goto accept_wait;
3241 }
bd379220
JD
3242 isert_conn = list_first_entry(&isert_np->pending,
3243 struct isert_conn, node);
3244 list_del_init(&isert_conn->node);
ed8cb0a4 3245 mutex_unlock(&isert_np->mutex);
b8d26b3b
NB
3246
3247 conn->context = isert_conn;
3248 isert_conn->conn = conn;
b8d26b3b 3249
b8d26b3b
NB
3250 isert_set_conn_info(np, conn, isert_conn);
3251
24f412dd 3252 isert_dbg("Processing isert_conn: %p\n", isert_conn);
2371e5da 3253
b8d26b3b
NB
3254 return 0;
3255}
3256
3257static void
3258isert_free_np(struct iscsi_np *np)
3259{
6700425e 3260 struct isert_np *isert_np = np->np_context;
268e6811 3261 struct isert_conn *isert_conn, *n;
b8d26b3b 3262
ed8cb0a4
JD
3263 if (isert_np->cm_id)
3264 rdma_destroy_id(isert_np->cm_id);
b8d26b3b 3265
268e6811
SG
3266 /*
3267 * FIXME: At this point we don't have a good way to insure
3268 * that at this point we don't have hanging connections that
3269 * completed RDMA establishment but didn't start iscsi login
3270 * process. So work-around this by cleaning up what ever piled
bd379220 3271 * up in accepted and pending lists.
268e6811 3272 */
ed8cb0a4 3273 mutex_lock(&isert_np->mutex);
bd379220
JD
3274 if (!list_empty(&isert_np->pending)) {
3275 isert_info("Still have isert pending connections\n");
3276 list_for_each_entry_safe(isert_conn, n,
3277 &isert_np->pending,
3278 node) {
3279 isert_info("cleaning isert_conn %p state (%d)\n",
3280 isert_conn, isert_conn->state);
3281 isert_connect_release(isert_conn);
3282 }
3283 }
3284
3285 if (!list_empty(&isert_np->accepted)) {
3286 isert_info("Still have isert accepted connections\n");
268e6811 3287 list_for_each_entry_safe(isert_conn, n,
bd379220
JD
3288 &isert_np->accepted,
3289 node) {
24f412dd 3290 isert_info("cleaning isert_conn %p state (%d)\n",
268e6811
SG
3291 isert_conn, isert_conn->state);
3292 isert_connect_release(isert_conn);
3293 }
3294 }
ed8cb0a4 3295 mutex_unlock(&isert_np->mutex);
268e6811 3296
b8d26b3b
NB
3297 np->np_context = NULL;
3298 kfree(isert_np);
3299}
3300
b02efbfc
SG
3301static void isert_release_work(struct work_struct *work)
3302{
3303 struct isert_conn *isert_conn = container_of(work,
3304 struct isert_conn,
3305 release_work);
3306
24f412dd 3307 isert_info("Starting release conn %p\n", isert_conn);
b02efbfc 3308
dac6ab30 3309 wait_for_completion(&isert_conn->wait);
b02efbfc 3310
dac6ab30 3311 mutex_lock(&isert_conn->mutex);
b02efbfc 3312 isert_conn->state = ISER_CONN_DOWN;
dac6ab30 3313 mutex_unlock(&isert_conn->mutex);
b02efbfc 3314
24f412dd 3315 isert_info("Destroying conn %p\n", isert_conn);
b02efbfc
SG
3316 isert_put_conn(isert_conn);
3317}
3318
991bb764
SG
3319static void
3320isert_wait4logout(struct isert_conn *isert_conn)
3321{
3322 struct iscsi_conn *conn = isert_conn->conn;
3323
4c22e07f
SG
3324 isert_info("conn %p\n", isert_conn);
3325
991bb764 3326 if (isert_conn->logout_posted) {
24f412dd 3327 isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
991bb764
SG
3328 wait_for_completion_timeout(&conn->conn_logout_comp,
3329 SECONDS_FOR_LOGOUT_COMP * HZ);
3330 }
3331}
3332
c7e160ee
SG
3333static void
3334isert_wait4cmds(struct iscsi_conn *conn)
3335{
4c22e07f
SG
3336 isert_info("iscsi_conn %p\n", conn);
3337
c7e160ee
SG
3338 if (conn->sess) {
3339 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
3340 target_wait_for_sess_cmds(conn->sess->se_sess);
3341 }
3342}
3343
bdf20e72
SG
3344static void
3345isert_wait4flush(struct isert_conn *isert_conn)
3346{
3347 struct ib_recv_wr *bad_wr;
3348
4c22e07f
SG
3349 isert_info("conn %p\n", isert_conn);
3350
dac6ab30 3351 init_completion(&isert_conn->wait_comp_err);
bdf20e72
SG
3352 isert_conn->beacon.wr_id = ISER_BEACON_WRID;
3353 /* post an indication that all flush errors were consumed */
dac6ab30 3354 if (ib_post_recv(isert_conn->qp, &isert_conn->beacon, &bad_wr)) {
24f412dd 3355 isert_err("conn %p failed to post beacon", isert_conn);
bdf20e72
SG
3356 return;
3357 }
3358
dac6ab30 3359 wait_for_completion(&isert_conn->wait_comp_err);
bdf20e72
SG
3360}
3361
3e03c4b0
JD
3362/**
3363 * isert_put_unsol_pending_cmds() - Drop commands waiting for
3364 * unsolicitate dataout
3365 * @conn: iscsi connection
3366 *
3367 * We might still have commands that are waiting for unsolicited
3368 * dataouts messages. We must put the extra reference on those
3369 * before blocking on the target_wait_for_session_cmds
3370 */
3371static void
3372isert_put_unsol_pending_cmds(struct iscsi_conn *conn)
3373{
3374 struct iscsi_cmd *cmd, *tmp;
3375 static LIST_HEAD(drop_cmd_list);
3376
3377 spin_lock_bh(&conn->cmd_lock);
3378 list_for_each_entry_safe(cmd, tmp, &conn->conn_cmd_list, i_conn_node) {
3379 if ((cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA) &&
3380 (cmd->write_data_done < conn->sess->sess_ops->FirstBurstLength) &&
3381 (cmd->write_data_done < cmd->se_cmd.data_length))
3382 list_move_tail(&cmd->i_conn_node, &drop_cmd_list);
3383 }
3384 spin_unlock_bh(&conn->cmd_lock);
3385
3386 list_for_each_entry_safe(cmd, tmp, &drop_cmd_list, i_conn_node) {
3387 list_del_init(&cmd->i_conn_node);
3388 if (cmd->i_state != ISTATE_REMOVE) {
3389 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
3390
3391 isert_info("conn %p dropping cmd %p\n", conn, cmd);
3392 isert_put_cmd(isert_cmd, true);
3393 }
3394 }
3395}
3396
defd8848 3397static void isert_wait_conn(struct iscsi_conn *conn)
b8d26b3b
NB
3398{
3399 struct isert_conn *isert_conn = conn->context;
3400
4c22e07f 3401 isert_info("Starting conn %p\n", isert_conn);
b8d26b3b 3402
dac6ab30 3403 mutex_lock(&isert_conn->mutex);
b8d26b3b 3404 /*
dac6ab30 3405 * Only wait for wait_comp_err if the isert_conn made it
b8d26b3b
NB
3406 * into full feature phase..
3407 */
b2cb9649 3408 if (isert_conn->state == ISER_CONN_INIT) {
dac6ab30 3409 mutex_unlock(&isert_conn->mutex);
b2cb9649 3410 return;
b8d26b3b 3411 }
954f2372 3412 isert_conn_terminate(isert_conn);
dac6ab30 3413 mutex_unlock(&isert_conn->mutex);
b8d26b3b 3414
bdf20e72 3415 isert_wait4flush(isert_conn);
3e03c4b0
JD
3416 isert_put_unsol_pending_cmds(conn);
3417 isert_wait4cmds(conn);
991bb764 3418 isert_wait4logout(isert_conn);
954f2372 3419
b02efbfc 3420 queue_work(isert_release_wq, &isert_conn->release_work);
defd8848
NB
3421}
3422
3423static void isert_free_conn(struct iscsi_conn *conn)
3424{
3425 struct isert_conn *isert_conn = conn->context;
b8d26b3b 3426
524630d5 3427 isert_wait4flush(isert_conn);
b8d26b3b
NB
3428 isert_put_conn(isert_conn);
3429}
3430
3431static struct iscsit_transport iser_target_transport = {
3432 .name = "IB/iSER",
3433 .transport_type = ISCSI_INFINIBAND,
d703ce2f 3434 .priv_size = sizeof(struct isert_cmd),
b8d26b3b
NB
3435 .owner = THIS_MODULE,
3436 .iscsit_setup_np = isert_setup_np,
3437 .iscsit_accept_np = isert_accept_np,
3438 .iscsit_free_np = isert_free_np,
defd8848 3439 .iscsit_wait_conn = isert_wait_conn,
b8d26b3b 3440 .iscsit_free_conn = isert_free_conn,
b8d26b3b
NB
3441 .iscsit_get_login_rx = isert_get_login_rx,
3442 .iscsit_put_login_tx = isert_put_login_tx,
3443 .iscsit_immediate_queue = isert_immediate_queue,
3444 .iscsit_response_queue = isert_response_queue,
3445 .iscsit_get_dataout = isert_get_dataout,
3446 .iscsit_queue_data_in = isert_put_datain,
3447 .iscsit_queue_status = isert_put_response,
131e6abc 3448 .iscsit_aborted_task = isert_aborted_task,
e70beee7 3449 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
b8d26b3b
NB
3450};
3451
3452static int __init isert_init(void)
3453{
3454 int ret;
3455
631af550
SG
3456 isert_comp_wq = alloc_workqueue("isert_comp_wq",
3457 WQ_UNBOUND | WQ_HIGHPRI, 0);
b8d26b3b 3458 if (!isert_comp_wq) {
24f412dd 3459 isert_err("Unable to allocate isert_comp_wq\n");
b8d26b3b 3460 ret = -ENOMEM;
6f0fae3d 3461 return -ENOMEM;
b8d26b3b
NB
3462 }
3463
b02efbfc
SG
3464 isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
3465 WQ_UNBOUND_MAX_ACTIVE);
3466 if (!isert_release_wq) {
24f412dd 3467 isert_err("Unable to allocate isert_release_wq\n");
b02efbfc
SG
3468 ret = -ENOMEM;
3469 goto destroy_comp_wq;
3470 }
3471
b8d26b3b 3472 iscsit_register_transport(&iser_target_transport);
24f412dd 3473 isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
b02efbfc 3474
b8d26b3b
NB
3475 return 0;
3476
b02efbfc
SG
3477destroy_comp_wq:
3478 destroy_workqueue(isert_comp_wq);
6f0fae3d 3479
b8d26b3b
NB
3480 return ret;
3481}
3482
3483static void __exit isert_exit(void)
3484{
f5ebec96 3485 flush_scheduled_work();
b02efbfc 3486 destroy_workqueue(isert_release_wq);
b8d26b3b 3487 destroy_workqueue(isert_comp_wq);
b8d26b3b 3488 iscsit_unregister_transport(&iser_target_transport);
4c22e07f 3489 isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
b8d26b3b
NB
3490}
3491
3492MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
9e35eff4 3493MODULE_VERSION("1.0");
b8d26b3b
NB
3494MODULE_AUTHOR("nab@Linux-iSCSI.org");
3495MODULE_LICENSE("GPL");
3496
3497module_init(isert_init);
3498module_exit(isert_exit);