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