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