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