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