iser-target: Fix connected_handler + teardown flow race
[linux/fpc-iii.git] / drivers / infiniband / ulp / isert / ib_isert.c
blob2b6508ff7d583ee5a2797f03bf702262f5b2bcff
1 /*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
4 * (c) Copyright 2013 Datera, Inc.
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
19 #include <linux/string.h>
20 #include <linux/module.h>
21 #include <linux/scatterlist.h>
22 #include <linux/socket.h>
23 #include <linux/in.h>
24 #include <linux/in6.h>
25 #include <linux/llist.h>
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 #include <linux/semaphore.h>
33 #include "isert_proto.h"
34 #include "ib_isert.h"
36 #define ISERT_MAX_CONN 8
37 #define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38 #define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
40 static DEFINE_MUTEX(device_list_mutex);
41 static LIST_HEAD(device_list);
42 static struct workqueue_struct *isert_rx_wq;
43 static struct workqueue_struct *isert_comp_wq;
45 static void
46 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47 static int
48 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
49 struct isert_rdma_wr *wr);
50 static void
51 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52 static int
53 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
55 static int
56 isert_rdma_post_recvl(struct isert_conn *isert_conn);
57 static int
58 isert_rdma_accept(struct isert_conn *isert_conn);
60 static void
61 isert_qp_event_callback(struct ib_event *e, void *context)
63 struct isert_conn *isert_conn = (struct isert_conn *)context;
65 pr_err("isert_qp_event_callback event: %d\n", e->event);
66 switch (e->event) {
67 case IB_EVENT_COMM_EST:
68 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
69 break;
70 case IB_EVENT_QP_LAST_WQE_REACHED:
71 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
72 break;
73 default:
74 break;
78 static int
79 isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
81 int ret;
83 ret = ib_query_device(ib_dev, devattr);
84 if (ret) {
85 pr_err("ib_query_device() failed: %d\n", ret);
86 return ret;
88 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
89 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
91 return 0;
94 static int
95 isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
97 struct isert_device *device = isert_conn->conn_device;
98 struct ib_qp_init_attr attr;
99 int ret, index, min_index = 0;
101 mutex_lock(&device_list_mutex);
102 for (index = 0; index < device->cqs_used; index++)
103 if (device->cq_active_qps[index] <
104 device->cq_active_qps[min_index])
105 min_index = index;
106 device->cq_active_qps[min_index]++;
107 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
108 mutex_unlock(&device_list_mutex);
110 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
111 attr.event_handler = isert_qp_event_callback;
112 attr.qp_context = isert_conn;
113 attr.send_cq = device->dev_tx_cq[min_index];
114 attr.recv_cq = device->dev_rx_cq[min_index];
115 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
116 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
118 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
119 * work-around for RDMA_READs with ConnectX-2.
121 * Also, still make sure to have at least two SGEs for
122 * outgoing control PDU responses.
124 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
125 isert_conn->max_sge = attr.cap.max_send_sge;
127 attr.cap.max_recv_sge = 1;
128 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
129 attr.qp_type = IB_QPT_RC;
131 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
132 cma_id->device);
133 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
134 isert_conn->conn_pd->device);
136 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
137 if (ret) {
138 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
139 goto err;
141 isert_conn->conn_qp = cma_id->qp;
142 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
144 return 0;
145 err:
146 mutex_lock(&device_list_mutex);
147 device->cq_active_qps[min_index]--;
148 mutex_unlock(&device_list_mutex);
150 return ret;
153 static void
154 isert_cq_event_callback(struct ib_event *e, void *context)
156 pr_debug("isert_cq_event_callback event: %d\n", e->event);
159 static int
160 isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
162 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
163 struct iser_rx_desc *rx_desc;
164 struct ib_sge *rx_sg;
165 u64 dma_addr;
166 int i, j;
168 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
169 sizeof(struct iser_rx_desc), GFP_KERNEL);
170 if (!isert_conn->conn_rx_descs)
171 goto fail;
173 rx_desc = isert_conn->conn_rx_descs;
175 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
176 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
177 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
178 if (ib_dma_mapping_error(ib_dev, dma_addr))
179 goto dma_map_fail;
181 rx_desc->dma_addr = dma_addr;
183 rx_sg = &rx_desc->rx_sg;
184 rx_sg->addr = rx_desc->dma_addr;
185 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
186 rx_sg->lkey = isert_conn->conn_mr->lkey;
189 isert_conn->conn_rx_desc_head = 0;
190 return 0;
192 dma_map_fail:
193 rx_desc = isert_conn->conn_rx_descs;
194 for (j = 0; j < i; j++, rx_desc++) {
195 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
196 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
198 kfree(isert_conn->conn_rx_descs);
199 isert_conn->conn_rx_descs = NULL;
200 fail:
201 return -ENOMEM;
204 static void
205 isert_free_rx_descriptors(struct isert_conn *isert_conn)
207 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
208 struct iser_rx_desc *rx_desc;
209 int i;
211 if (!isert_conn->conn_rx_descs)
212 return;
214 rx_desc = isert_conn->conn_rx_descs;
215 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
216 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
217 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
220 kfree(isert_conn->conn_rx_descs);
221 isert_conn->conn_rx_descs = NULL;
224 static void isert_cq_tx_work(struct work_struct *);
225 static void isert_cq_tx_callback(struct ib_cq *, void *);
226 static void isert_cq_rx_work(struct work_struct *);
227 static void isert_cq_rx_callback(struct ib_cq *, void *);
229 static int
230 isert_create_device_ib_res(struct isert_device *device)
232 struct ib_device *ib_dev = device->ib_device;
233 struct isert_cq_desc *cq_desc;
234 struct ib_device_attr *dev_attr;
235 int ret = 0, i, j;
236 int max_rx_cqe, max_tx_cqe;
238 dev_attr = &device->dev_attr;
239 ret = isert_query_device(ib_dev, dev_attr);
240 if (ret)
241 return ret;
243 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
244 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
246 /* asign function handlers */
247 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
248 device->use_fastreg = 1;
249 device->reg_rdma_mem = isert_reg_rdma;
250 device->unreg_rdma_mem = isert_unreg_rdma;
251 } else {
252 device->use_fastreg = 0;
253 device->reg_rdma_mem = isert_map_rdma;
254 device->unreg_rdma_mem = isert_unmap_cmd;
257 device->cqs_used = min_t(int, num_online_cpus(),
258 device->ib_device->num_comp_vectors);
259 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
260 pr_debug("Using %d CQs, device %s supports %d vectors support "
261 "Fast registration %d\n",
262 device->cqs_used, device->ib_device->name,
263 device->ib_device->num_comp_vectors, device->use_fastreg);
264 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
265 device->cqs_used, GFP_KERNEL);
266 if (!device->cq_desc) {
267 pr_err("Unable to allocate device->cq_desc\n");
268 return -ENOMEM;
270 cq_desc = device->cq_desc;
272 for (i = 0; i < device->cqs_used; i++) {
273 cq_desc[i].device = device;
274 cq_desc[i].cq_index = i;
276 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
277 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
278 isert_cq_rx_callback,
279 isert_cq_event_callback,
280 (void *)&cq_desc[i],
281 max_rx_cqe, i);
282 if (IS_ERR(device->dev_rx_cq[i])) {
283 ret = PTR_ERR(device->dev_rx_cq[i]);
284 device->dev_rx_cq[i] = NULL;
285 goto out_cq;
288 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
289 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
290 isert_cq_tx_callback,
291 isert_cq_event_callback,
292 (void *)&cq_desc[i],
293 max_tx_cqe, i);
294 if (IS_ERR(device->dev_tx_cq[i])) {
295 ret = PTR_ERR(device->dev_tx_cq[i]);
296 device->dev_tx_cq[i] = NULL;
297 goto out_cq;
300 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
301 if (ret)
302 goto out_cq;
304 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
305 if (ret)
306 goto out_cq;
309 return 0;
311 out_cq:
312 for (j = 0; j < i; j++) {
313 cq_desc = &device->cq_desc[j];
315 if (device->dev_rx_cq[j]) {
316 cancel_work_sync(&cq_desc->cq_rx_work);
317 ib_destroy_cq(device->dev_rx_cq[j]);
319 if (device->dev_tx_cq[j]) {
320 cancel_work_sync(&cq_desc->cq_tx_work);
321 ib_destroy_cq(device->dev_tx_cq[j]);
324 kfree(device->cq_desc);
326 return ret;
329 static void
330 isert_free_device_ib_res(struct isert_device *device)
332 struct isert_cq_desc *cq_desc;
333 int i;
335 for (i = 0; i < device->cqs_used; i++) {
336 cq_desc = &device->cq_desc[i];
338 cancel_work_sync(&cq_desc->cq_rx_work);
339 cancel_work_sync(&cq_desc->cq_tx_work);
340 ib_destroy_cq(device->dev_rx_cq[i]);
341 ib_destroy_cq(device->dev_tx_cq[i]);
342 device->dev_rx_cq[i] = NULL;
343 device->dev_tx_cq[i] = NULL;
346 kfree(device->cq_desc);
349 static void
350 isert_device_try_release(struct isert_device *device)
352 mutex_lock(&device_list_mutex);
353 device->refcount--;
354 if (!device->refcount) {
355 isert_free_device_ib_res(device);
356 list_del(&device->dev_node);
357 kfree(device);
359 mutex_unlock(&device_list_mutex);
362 static struct isert_device *
363 isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
365 struct isert_device *device;
366 int ret;
368 mutex_lock(&device_list_mutex);
369 list_for_each_entry(device, &device_list, dev_node) {
370 if (device->ib_device->node_guid == cma_id->device->node_guid) {
371 device->refcount++;
372 mutex_unlock(&device_list_mutex);
373 return device;
377 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
378 if (!device) {
379 mutex_unlock(&device_list_mutex);
380 return ERR_PTR(-ENOMEM);
383 INIT_LIST_HEAD(&device->dev_node);
385 device->ib_device = cma_id->device;
386 ret = isert_create_device_ib_res(device);
387 if (ret) {
388 kfree(device);
389 mutex_unlock(&device_list_mutex);
390 return ERR_PTR(ret);
393 device->refcount++;
394 list_add_tail(&device->dev_node, &device_list);
395 mutex_unlock(&device_list_mutex);
397 return device;
400 static void
401 isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
403 struct fast_reg_descriptor *fr_desc, *tmp;
404 int i = 0;
406 if (list_empty(&isert_conn->conn_fr_pool))
407 return;
409 pr_debug("Freeing conn %p fastreg pool", isert_conn);
411 list_for_each_entry_safe(fr_desc, tmp,
412 &isert_conn->conn_fr_pool, list) {
413 list_del(&fr_desc->list);
414 ib_free_fast_reg_page_list(fr_desc->data_frpl);
415 ib_dereg_mr(fr_desc->data_mr);
416 kfree(fr_desc);
417 ++i;
420 if (i < isert_conn->conn_fr_pool_size)
421 pr_warn("Pool still has %d regions registered\n",
422 isert_conn->conn_fr_pool_size - i);
425 static int
426 isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
427 struct fast_reg_descriptor *fr_desc)
429 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
430 ISCSI_ISER_SG_TABLESIZE);
431 if (IS_ERR(fr_desc->data_frpl)) {
432 pr_err("Failed to allocate data frpl err=%ld\n",
433 PTR_ERR(fr_desc->data_frpl));
434 return PTR_ERR(fr_desc->data_frpl);
437 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
438 if (IS_ERR(fr_desc->data_mr)) {
439 pr_err("Failed to allocate data frmr err=%ld\n",
440 PTR_ERR(fr_desc->data_mr));
441 ib_free_fast_reg_page_list(fr_desc->data_frpl);
442 return PTR_ERR(fr_desc->data_mr);
444 pr_debug("Create fr_desc %p page_list %p\n",
445 fr_desc, fr_desc->data_frpl->page_list);
447 fr_desc->valid = true;
449 return 0;
452 static int
453 isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
455 struct fast_reg_descriptor *fr_desc;
456 struct isert_device *device = isert_conn->conn_device;
457 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
458 struct se_node_acl *se_nacl = se_sess->se_node_acl;
459 int i, ret, tag_num;
461 * Setup the number of FRMRs based upon the number of tags
462 * available to session in iscsi_target_locate_portal().
464 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
465 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
467 isert_conn->conn_fr_pool_size = 0;
468 for (i = 0; i < tag_num; i++) {
469 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
470 if (!fr_desc) {
471 pr_err("Failed to allocate fast_reg descriptor\n");
472 ret = -ENOMEM;
473 goto err;
476 ret = isert_create_fr_desc(device->ib_device,
477 isert_conn->conn_pd, fr_desc);
478 if (ret) {
479 pr_err("Failed to create fastreg descriptor err=%d\n",
480 ret);
481 kfree(fr_desc);
482 goto err;
485 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
486 isert_conn->conn_fr_pool_size++;
489 pr_debug("Creating conn %p fastreg pool size=%d",
490 isert_conn, isert_conn->conn_fr_pool_size);
492 return 0;
494 err:
495 isert_conn_free_fastreg_pool(isert_conn);
496 return ret;
499 static int
500 isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
502 struct iscsi_np *np = cma_id->context;
503 struct isert_np *isert_np = np->np_context;
504 struct isert_conn *isert_conn;
505 struct isert_device *device;
506 struct ib_device *ib_dev = cma_id->device;
507 int ret = 0;
509 spin_lock_bh(&np->np_thread_lock);
510 if (!np->enabled) {
511 spin_unlock_bh(&np->np_thread_lock);
512 pr_debug("iscsi_np is not enabled, reject connect request\n");
513 return rdma_reject(cma_id, NULL, 0);
515 spin_unlock_bh(&np->np_thread_lock);
517 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
518 cma_id, cma_id->context);
520 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
521 if (!isert_conn) {
522 pr_err("Unable to allocate isert_conn\n");
523 return -ENOMEM;
525 isert_conn->state = ISER_CONN_INIT;
526 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
527 init_completion(&isert_conn->conn_login_comp);
528 init_completion(&isert_conn->login_req_comp);
529 init_completion(&isert_conn->conn_wait);
530 init_completion(&isert_conn->conn_wait_comp_err);
531 kref_init(&isert_conn->conn_kref);
532 mutex_init(&isert_conn->conn_mutex);
533 spin_lock_init(&isert_conn->conn_lock);
534 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
536 isert_conn->conn_cm_id = cma_id;
537 isert_conn->responder_resources = event->param.conn.responder_resources;
538 isert_conn->initiator_depth = event->param.conn.initiator_depth;
539 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
540 isert_conn->responder_resources, isert_conn->initiator_depth);
542 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
543 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
544 if (!isert_conn->login_buf) {
545 pr_err("Unable to allocate isert_conn->login_buf\n");
546 ret = -ENOMEM;
547 goto out;
550 isert_conn->login_req_buf = isert_conn->login_buf;
551 isert_conn->login_rsp_buf = isert_conn->login_buf +
552 ISCSI_DEF_MAX_RECV_SEG_LEN;
553 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
554 isert_conn->login_buf, isert_conn->login_req_buf,
555 isert_conn->login_rsp_buf);
557 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
558 (void *)isert_conn->login_req_buf,
559 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
561 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
562 if (ret) {
563 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
564 ret);
565 isert_conn->login_req_dma = 0;
566 goto out_login_buf;
569 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
570 (void *)isert_conn->login_rsp_buf,
571 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
573 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
574 if (ret) {
575 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
576 ret);
577 isert_conn->login_rsp_dma = 0;
578 goto out_req_dma_map;
581 device = isert_device_find_by_ib_dev(cma_id);
582 if (IS_ERR(device)) {
583 ret = PTR_ERR(device);
584 goto out_rsp_dma_map;
587 isert_conn->conn_device = device;
588 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
589 if (IS_ERR(isert_conn->conn_pd)) {
590 ret = PTR_ERR(isert_conn->conn_pd);
591 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
592 isert_conn, ret);
593 goto out_pd;
596 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
597 IB_ACCESS_LOCAL_WRITE);
598 if (IS_ERR(isert_conn->conn_mr)) {
599 ret = PTR_ERR(isert_conn->conn_mr);
600 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
601 isert_conn, ret);
602 goto out_mr;
605 ret = isert_conn_setup_qp(isert_conn, cma_id);
606 if (ret)
607 goto out_conn_dev;
609 ret = isert_rdma_post_recvl(isert_conn);
610 if (ret)
611 goto out_conn_dev;
613 ret = isert_rdma_accept(isert_conn);
614 if (ret)
615 goto out_conn_dev;
617 mutex_lock(&isert_np->np_accept_mutex);
618 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
619 mutex_unlock(&isert_np->np_accept_mutex);
621 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
622 up(&isert_np->np_sem);
623 return 0;
625 out_conn_dev:
626 ib_dereg_mr(isert_conn->conn_mr);
627 out_mr:
628 ib_dealloc_pd(isert_conn->conn_pd);
629 out_pd:
630 isert_device_try_release(device);
631 out_rsp_dma_map:
632 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
633 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
634 out_req_dma_map:
635 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
636 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
637 out_login_buf:
638 kfree(isert_conn->login_buf);
639 out:
640 kfree(isert_conn);
641 rdma_reject(cma_id, NULL, 0);
642 return ret;
645 static void
646 isert_connect_release(struct isert_conn *isert_conn)
648 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
649 struct isert_device *device = isert_conn->conn_device;
650 int cq_index;
652 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
654 if (device && device->use_fastreg)
655 isert_conn_free_fastreg_pool(isert_conn);
657 isert_free_rx_descriptors(isert_conn);
658 rdma_destroy_id(isert_conn->conn_cm_id);
660 if (isert_conn->conn_qp) {
661 cq_index = ((struct isert_cq_desc *)
662 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
663 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
664 mutex_lock(&device_list_mutex);
665 isert_conn->conn_device->cq_active_qps[cq_index]--;
666 mutex_unlock(&device_list_mutex);
668 ib_destroy_qp(isert_conn->conn_qp);
671 ib_dereg_mr(isert_conn->conn_mr);
672 ib_dealloc_pd(isert_conn->conn_pd);
674 if (isert_conn->login_buf) {
675 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
676 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
677 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
678 ISCSI_DEF_MAX_RECV_SEG_LEN,
679 DMA_FROM_DEVICE);
680 kfree(isert_conn->login_buf);
682 kfree(isert_conn);
684 if (device)
685 isert_device_try_release(device);
687 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
690 static void
691 isert_connected_handler(struct rdma_cm_id *cma_id)
693 struct isert_conn *isert_conn = cma_id->qp->qp_context;
695 pr_info("conn %p\n", isert_conn);
697 if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
698 pr_warn("conn %p connect_release is running\n", isert_conn);
699 return;
702 mutex_lock(&isert_conn->conn_mutex);
703 if (isert_conn->state != ISER_CONN_FULL_FEATURE)
704 isert_conn->state = ISER_CONN_UP;
705 mutex_unlock(&isert_conn->conn_mutex);
708 static void
709 isert_release_conn_kref(struct kref *kref)
711 struct isert_conn *isert_conn = container_of(kref,
712 struct isert_conn, conn_kref);
714 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
715 current->comm, current->pid);
717 isert_connect_release(isert_conn);
720 static void
721 isert_put_conn(struct isert_conn *isert_conn)
723 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
727 * isert_conn_terminate() - Initiate connection termination
728 * @isert_conn: isert connection struct
730 * Notes:
731 * In case the connection state is FULL_FEATURE, move state
732 * to TEMINATING and start teardown sequence (rdma_disconnect).
733 * In case the connection state is UP, complete flush as well.
735 * This routine must be called with conn_mutex held. Thus it is
736 * safe to call multiple times.
738 static void
739 isert_conn_terminate(struct isert_conn *isert_conn)
741 int err;
743 switch (isert_conn->state) {
744 case ISER_CONN_TERMINATING:
745 break;
746 case ISER_CONN_UP:
748 * No flush completions will occur as we didn't
749 * get to ISER_CONN_FULL_FEATURE yet, complete
750 * to allow teardown progress.
752 complete(&isert_conn->conn_wait_comp_err);
753 case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
754 pr_info("Terminating conn %p state %d\n",
755 isert_conn, isert_conn->state);
756 isert_conn->state = ISER_CONN_TERMINATING;
757 err = rdma_disconnect(isert_conn->conn_cm_id);
758 if (err)
759 pr_warn("Failed rdma_disconnect isert_conn %p\n",
760 isert_conn);
761 break;
762 default:
763 pr_warn("conn %p teminating in state %d\n",
764 isert_conn, isert_conn->state);
768 static int
769 isert_disconnected_handler(struct rdma_cm_id *cma_id)
771 struct iscsi_np *np = cma_id->context;
772 struct isert_np *isert_np = np->np_context;
773 struct isert_conn *isert_conn;
775 if (isert_np->np_cm_id == cma_id) {
776 isert_np->np_cm_id = NULL;
777 return -1;
780 isert_conn = cma_id->qp->qp_context;
782 mutex_lock(&isert_conn->conn_mutex);
783 isert_conn_terminate(isert_conn);
784 mutex_unlock(&isert_conn->conn_mutex);
786 pr_info("conn %p completing conn_wait\n", isert_conn);
787 complete(&isert_conn->conn_wait);
789 return 0;
792 static void
793 isert_connect_error(struct rdma_cm_id *cma_id)
795 struct isert_conn *isert_conn = cma_id->qp->qp_context;
797 isert_put_conn(isert_conn);
800 static int
801 isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
803 int ret = 0;
805 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
806 event->event, event->status, cma_id->context, cma_id);
808 switch (event->event) {
809 case RDMA_CM_EVENT_CONNECT_REQUEST:
810 ret = isert_connect_request(cma_id, event);
811 if (ret)
812 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
813 event->event, ret);
814 break;
815 case RDMA_CM_EVENT_ESTABLISHED:
816 isert_connected_handler(cma_id);
817 break;
818 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
819 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
820 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
821 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
822 ret = isert_disconnected_handler(cma_id);
823 break;
824 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
825 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
826 case RDMA_CM_EVENT_CONNECT_ERROR:
827 isert_connect_error(cma_id);
828 break;
829 default:
830 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
831 break;
834 return ret;
837 static int
838 isert_post_recv(struct isert_conn *isert_conn, u32 count)
840 struct ib_recv_wr *rx_wr, *rx_wr_failed;
841 int i, ret;
842 unsigned int rx_head = isert_conn->conn_rx_desc_head;
843 struct iser_rx_desc *rx_desc;
845 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
846 rx_desc = &isert_conn->conn_rx_descs[rx_head];
847 rx_wr->wr_id = (unsigned long)rx_desc;
848 rx_wr->sg_list = &rx_desc->rx_sg;
849 rx_wr->num_sge = 1;
850 rx_wr->next = rx_wr + 1;
851 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
854 rx_wr--;
855 rx_wr->next = NULL; /* mark end of work requests list */
857 isert_conn->post_recv_buf_count += count;
858 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
859 &rx_wr_failed);
860 if (ret) {
861 pr_err("ib_post_recv() failed with ret: %d\n", ret);
862 isert_conn->post_recv_buf_count -= count;
863 } else {
864 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
865 isert_conn->conn_rx_desc_head = rx_head;
867 return ret;
870 static int
871 isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
873 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
874 struct ib_send_wr send_wr, *send_wr_failed;
875 int ret;
877 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
878 ISER_HEADERS_LEN, DMA_TO_DEVICE);
880 send_wr.next = NULL;
881 send_wr.wr_id = (unsigned long)tx_desc;
882 send_wr.sg_list = tx_desc->tx_sg;
883 send_wr.num_sge = tx_desc->num_sge;
884 send_wr.opcode = IB_WR_SEND;
885 send_wr.send_flags = IB_SEND_SIGNALED;
887 atomic_inc(&isert_conn->post_send_buf_count);
889 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
890 if (ret) {
891 pr_err("ib_post_send() failed, ret: %d\n", ret);
892 atomic_dec(&isert_conn->post_send_buf_count);
895 return ret;
898 static void
899 isert_create_send_desc(struct isert_conn *isert_conn,
900 struct isert_cmd *isert_cmd,
901 struct iser_tx_desc *tx_desc)
903 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
905 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
906 ISER_HEADERS_LEN, DMA_TO_DEVICE);
908 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
909 tx_desc->iser_header.flags = ISER_VER;
911 tx_desc->num_sge = 1;
912 tx_desc->isert_cmd = isert_cmd;
914 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
915 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
916 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
920 static int
921 isert_init_tx_hdrs(struct isert_conn *isert_conn,
922 struct iser_tx_desc *tx_desc)
924 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
925 u64 dma_addr;
927 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
928 ISER_HEADERS_LEN, DMA_TO_DEVICE);
929 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
930 pr_err("ib_dma_mapping_error() failed\n");
931 return -ENOMEM;
934 tx_desc->dma_addr = dma_addr;
935 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
936 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
937 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
939 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
940 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
941 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
943 return 0;
946 static void
947 isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
948 struct ib_send_wr *send_wr, bool coalesce)
950 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
952 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
953 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
954 send_wr->opcode = IB_WR_SEND;
955 send_wr->sg_list = &tx_desc->tx_sg[0];
956 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
958 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
959 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
961 mutex_lock(&isert_conn->conn_mutex);
962 if (coalesce && isert_conn->state == ISER_CONN_FULL_FEATURE &&
963 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
964 tx_desc->llnode_active = true;
965 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
966 mutex_unlock(&isert_conn->conn_mutex);
967 return;
969 isert_conn->conn_comp_batch = 0;
970 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
971 mutex_unlock(&isert_conn->conn_mutex);
973 send_wr->send_flags = IB_SEND_SIGNALED;
976 static int
977 isert_rdma_post_recvl(struct isert_conn *isert_conn)
979 struct ib_recv_wr rx_wr, *rx_wr_fail;
980 struct ib_sge sge;
981 int ret;
983 memset(&sge, 0, sizeof(struct ib_sge));
984 sge.addr = isert_conn->login_req_dma;
985 sge.length = ISER_RX_LOGIN_SIZE;
986 sge.lkey = isert_conn->conn_mr->lkey;
988 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
989 sge.addr, sge.length, sge.lkey);
991 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
992 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
993 rx_wr.sg_list = &sge;
994 rx_wr.num_sge = 1;
996 isert_conn->post_recv_buf_count++;
997 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
998 if (ret) {
999 pr_err("ib_post_recv() failed: %d\n", ret);
1000 isert_conn->post_recv_buf_count--;
1003 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1004 return ret;
1007 static int
1008 isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1009 u32 length)
1011 struct isert_conn *isert_conn = conn->context;
1012 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1013 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1014 int ret;
1016 isert_create_send_desc(isert_conn, NULL, tx_desc);
1018 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1019 sizeof(struct iscsi_hdr));
1021 isert_init_tx_hdrs(isert_conn, tx_desc);
1023 if (length > 0) {
1024 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1026 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1027 length, DMA_TO_DEVICE);
1029 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1031 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1032 length, DMA_TO_DEVICE);
1034 tx_dsg->addr = isert_conn->login_rsp_dma;
1035 tx_dsg->length = length;
1036 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1037 tx_desc->num_sge = 2;
1039 if (!login->login_failed) {
1040 if (login->login_complete) {
1041 if (!conn->sess->sess_ops->SessionType &&
1042 isert_conn->conn_device->use_fastreg) {
1043 ret = isert_conn_create_fastreg_pool(isert_conn);
1044 if (ret) {
1045 pr_err("Conn: %p failed to create"
1046 " fastreg pool\n", isert_conn);
1047 return ret;
1051 ret = isert_alloc_rx_descriptors(isert_conn);
1052 if (ret)
1053 return ret;
1055 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1056 if (ret)
1057 return ret;
1059 /* Now we are in FULL_FEATURE phase */
1060 mutex_lock(&isert_conn->conn_mutex);
1061 isert_conn->state = ISER_CONN_FULL_FEATURE;
1062 mutex_unlock(&isert_conn->conn_mutex);
1063 goto post_send;
1066 ret = isert_rdma_post_recvl(isert_conn);
1067 if (ret)
1068 return ret;
1070 post_send:
1071 ret = isert_post_send(isert_conn, tx_desc);
1072 if (ret)
1073 return ret;
1075 return 0;
1078 static void
1079 isert_rx_login_req(struct isert_conn *isert_conn)
1081 struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
1082 int rx_buflen = isert_conn->login_req_len;
1083 struct iscsi_conn *conn = isert_conn->conn;
1084 struct iscsi_login *login = conn->conn_login;
1085 int size;
1087 pr_info("conn %p\n", isert_conn);
1089 WARN_ON_ONCE(!login);
1091 if (login->first_request) {
1092 struct iscsi_login_req *login_req =
1093 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1095 * Setup the initial iscsi_login values from the leading
1096 * login request PDU.
1098 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1099 login->current_stage =
1100 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1101 >> 2;
1102 login->version_min = login_req->min_version;
1103 login->version_max = login_req->max_version;
1104 memcpy(login->isid, login_req->isid, 6);
1105 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1106 login->init_task_tag = login_req->itt;
1107 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1108 login->cid = be16_to_cpu(login_req->cid);
1109 login->tsih = be16_to_cpu(login_req->tsih);
1112 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1114 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1115 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1116 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1117 memcpy(login->req_buf, &rx_desc->data[0], size);
1119 if (login->first_request) {
1120 complete(&isert_conn->conn_login_comp);
1121 return;
1123 schedule_delayed_work(&conn->login_work, 0);
1126 static struct iscsi_cmd
1127 *isert_allocate_cmd(struct iscsi_conn *conn)
1129 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1130 struct isert_cmd *isert_cmd;
1131 struct iscsi_cmd *cmd;
1133 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1134 if (!cmd) {
1135 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1136 return NULL;
1138 isert_cmd = iscsit_priv_cmd(cmd);
1139 isert_cmd->conn = isert_conn;
1140 isert_cmd->iscsi_cmd = cmd;
1142 return cmd;
1145 static int
1146 isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1147 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1148 struct iser_rx_desc *rx_desc, unsigned char *buf)
1150 struct iscsi_conn *conn = isert_conn->conn;
1151 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1152 struct scatterlist *sg;
1153 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1154 bool dump_payload = false;
1156 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1157 if (rc < 0)
1158 return rc;
1160 imm_data = cmd->immediate_data;
1161 imm_data_len = cmd->first_burst_len;
1162 unsol_data = cmd->unsolicited_data;
1164 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1165 if (rc < 0) {
1166 return 0;
1167 } else if (rc > 0) {
1168 dump_payload = true;
1169 goto sequence_cmd;
1172 if (!imm_data)
1173 return 0;
1175 sg = &cmd->se_cmd.t_data_sg[0];
1176 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1178 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1179 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1181 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1183 cmd->write_data_done += imm_data_len;
1185 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1186 spin_lock_bh(&cmd->istate_lock);
1187 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1188 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1189 spin_unlock_bh(&cmd->istate_lock);
1192 sequence_cmd:
1193 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1195 if (!rc && dump_payload == false && unsol_data)
1196 iscsit_set_unsoliticed_dataout(cmd);
1197 else if (dump_payload && imm_data)
1198 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1200 return 0;
1203 static int
1204 isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1205 struct iser_rx_desc *rx_desc, unsigned char *buf)
1207 struct scatterlist *sg_start;
1208 struct iscsi_conn *conn = isert_conn->conn;
1209 struct iscsi_cmd *cmd = NULL;
1210 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1211 u32 unsol_data_len = ntoh24(hdr->dlength);
1212 int rc, sg_nents, sg_off, page_off;
1214 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1215 if (rc < 0)
1216 return rc;
1217 else if (!cmd)
1218 return 0;
1220 * FIXME: Unexpected unsolicited_data out
1222 if (!cmd->unsolicited_data) {
1223 pr_err("Received unexpected solicited data payload\n");
1224 dump_stack();
1225 return -1;
1228 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1229 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1231 sg_off = cmd->write_data_done / PAGE_SIZE;
1232 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1233 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1234 page_off = cmd->write_data_done % PAGE_SIZE;
1236 * FIXME: Non page-aligned unsolicited_data out
1238 if (page_off) {
1239 pr_err("Received unexpected non-page aligned data payload\n");
1240 dump_stack();
1241 return -1;
1243 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1244 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1246 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1247 unsol_data_len);
1249 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1250 if (rc < 0)
1251 return rc;
1253 return 0;
1256 static int
1257 isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1258 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1259 unsigned char *buf)
1261 struct iscsi_conn *conn = isert_conn->conn;
1262 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1263 int rc;
1265 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1266 if (rc < 0)
1267 return rc;
1269 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1272 return iscsit_process_nop_out(conn, cmd, hdr);
1275 static int
1276 isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1277 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1278 struct iscsi_text *hdr)
1280 struct iscsi_conn *conn = isert_conn->conn;
1281 u32 payload_length = ntoh24(hdr->dlength);
1282 int rc;
1283 unsigned char *text_in;
1285 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1286 if (rc < 0)
1287 return rc;
1289 text_in = kzalloc(payload_length, GFP_KERNEL);
1290 if (!text_in) {
1291 pr_err("Unable to allocate text_in of payload_length: %u\n",
1292 payload_length);
1293 return -ENOMEM;
1295 cmd->text_in_ptr = text_in;
1297 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1299 return iscsit_process_text_cmd(conn, cmd, hdr);
1302 static int
1303 isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1304 uint32_t read_stag, uint64_t read_va,
1305 uint32_t write_stag, uint64_t write_va)
1307 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1308 struct iscsi_conn *conn = isert_conn->conn;
1309 struct iscsi_session *sess = conn->sess;
1310 struct iscsi_cmd *cmd;
1311 struct isert_cmd *isert_cmd;
1312 int ret = -EINVAL;
1313 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1315 if (sess->sess_ops->SessionType &&
1316 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1317 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1318 " ignoring\n", opcode);
1319 return 0;
1322 switch (opcode) {
1323 case ISCSI_OP_SCSI_CMD:
1324 cmd = isert_allocate_cmd(conn);
1325 if (!cmd)
1326 break;
1328 isert_cmd = iscsit_priv_cmd(cmd);
1329 isert_cmd->read_stag = read_stag;
1330 isert_cmd->read_va = read_va;
1331 isert_cmd->write_stag = write_stag;
1332 isert_cmd->write_va = write_va;
1334 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1335 rx_desc, (unsigned char *)hdr);
1336 break;
1337 case ISCSI_OP_NOOP_OUT:
1338 cmd = isert_allocate_cmd(conn);
1339 if (!cmd)
1340 break;
1342 isert_cmd = iscsit_priv_cmd(cmd);
1343 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1344 rx_desc, (unsigned char *)hdr);
1345 break;
1346 case ISCSI_OP_SCSI_DATA_OUT:
1347 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1348 (unsigned char *)hdr);
1349 break;
1350 case ISCSI_OP_SCSI_TMFUNC:
1351 cmd = isert_allocate_cmd(conn);
1352 if (!cmd)
1353 break;
1355 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1356 (unsigned char *)hdr);
1357 break;
1358 case ISCSI_OP_LOGOUT:
1359 cmd = isert_allocate_cmd(conn);
1360 if (!cmd)
1361 break;
1363 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1364 if (ret > 0)
1365 wait_for_completion_timeout(&conn->conn_logout_comp,
1366 SECONDS_FOR_LOGOUT_COMP *
1367 HZ);
1368 break;
1369 case ISCSI_OP_TEXT:
1370 cmd = isert_allocate_cmd(conn);
1371 if (!cmd)
1372 break;
1374 isert_cmd = iscsit_priv_cmd(cmd);
1375 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1376 rx_desc, (struct iscsi_text *)hdr);
1377 break;
1378 default:
1379 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1380 dump_stack();
1381 break;
1384 return ret;
1387 static void
1388 isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1390 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1391 uint64_t read_va = 0, write_va = 0;
1392 uint32_t read_stag = 0, write_stag = 0;
1393 int rc;
1395 switch (iser_hdr->flags & 0xF0) {
1396 case ISCSI_CTRL:
1397 if (iser_hdr->flags & ISER_RSV) {
1398 read_stag = be32_to_cpu(iser_hdr->read_stag);
1399 read_va = be64_to_cpu(iser_hdr->read_va);
1400 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1401 read_stag, (unsigned long long)read_va);
1403 if (iser_hdr->flags & ISER_WSV) {
1404 write_stag = be32_to_cpu(iser_hdr->write_stag);
1405 write_va = be64_to_cpu(iser_hdr->write_va);
1406 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1407 write_stag, (unsigned long long)write_va);
1410 pr_debug("ISER ISCSI_CTRL PDU\n");
1411 break;
1412 case ISER_HELLO:
1413 pr_err("iSER Hello message\n");
1414 break;
1415 default:
1416 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1417 break;
1420 rc = isert_rx_opcode(isert_conn, rx_desc,
1421 read_stag, read_va, write_stag, write_va);
1424 static void
1425 isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1426 unsigned long xfer_len)
1428 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1429 struct iscsi_hdr *hdr;
1430 u64 rx_dma;
1431 int rx_buflen, outstanding;
1433 if ((char *)desc == isert_conn->login_req_buf) {
1434 rx_dma = isert_conn->login_req_dma;
1435 rx_buflen = ISER_RX_LOGIN_SIZE;
1436 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1437 rx_dma, rx_buflen);
1438 } else {
1439 rx_dma = desc->dma_addr;
1440 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1441 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1442 rx_dma, rx_buflen);
1445 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1447 hdr = &desc->iscsi_header;
1448 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1449 hdr->opcode, hdr->itt, hdr->flags,
1450 (int)(xfer_len - ISER_HEADERS_LEN));
1452 if ((char *)desc == isert_conn->login_req_buf) {
1453 isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
1454 if (isert_conn->conn) {
1455 struct iscsi_login *login = isert_conn->conn->conn_login;
1457 if (login && !login->first_request)
1458 isert_rx_login_req(isert_conn);
1460 mutex_lock(&isert_conn->conn_mutex);
1461 complete(&isert_conn->login_req_comp);
1462 mutex_unlock(&isert_conn->conn_mutex);
1463 } else {
1464 isert_rx_do_work(desc, isert_conn);
1467 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1468 DMA_FROM_DEVICE);
1470 isert_conn->post_recv_buf_count--;
1471 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1472 isert_conn->post_recv_buf_count);
1474 if ((char *)desc == isert_conn->login_req_buf)
1475 return;
1477 outstanding = isert_conn->post_recv_buf_count;
1478 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1479 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1480 ISERT_MIN_POSTED_RX);
1481 err = isert_post_recv(isert_conn, count);
1482 if (err) {
1483 pr_err("isert_post_recv() count: %d failed, %d\n",
1484 count, err);
1489 static void
1490 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1492 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1493 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1495 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1496 if (wr->sge) {
1497 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1498 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1499 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1500 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1501 wr->sge = NULL;
1504 if (wr->send_wr) {
1505 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1506 kfree(wr->send_wr);
1507 wr->send_wr = NULL;
1510 if (wr->ib_sge) {
1511 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1512 kfree(wr->ib_sge);
1513 wr->ib_sge = NULL;
1517 static void
1518 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1520 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1521 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1522 LIST_HEAD(unmap_list);
1524 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
1526 if (wr->fr_desc) {
1527 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
1528 isert_cmd, wr->fr_desc);
1529 spin_lock_bh(&isert_conn->conn_lock);
1530 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
1531 spin_unlock_bh(&isert_conn->conn_lock);
1532 wr->fr_desc = NULL;
1535 if (wr->sge) {
1536 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1537 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1538 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1539 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1540 wr->sge = NULL;
1543 wr->ib_sge = NULL;
1544 wr->send_wr = NULL;
1547 static void
1548 isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
1550 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1551 struct isert_conn *isert_conn = isert_cmd->conn;
1552 struct iscsi_conn *conn = isert_conn->conn;
1553 struct isert_device *device = isert_conn->conn_device;
1555 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1557 switch (cmd->iscsi_opcode) {
1558 case ISCSI_OP_SCSI_CMD:
1559 spin_lock_bh(&conn->cmd_lock);
1560 if (!list_empty(&cmd->i_conn_node))
1561 list_del_init(&cmd->i_conn_node);
1562 spin_unlock_bh(&conn->cmd_lock);
1564 if (cmd->data_direction == DMA_TO_DEVICE) {
1565 iscsit_stop_dataout_timer(cmd);
1567 * Check for special case during comp_err where
1568 * WRITE_PENDING has been handed off from core,
1569 * but requires an extra target_put_sess_cmd()
1570 * before transport_generic_free_cmd() below.
1572 if (comp_err &&
1573 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1574 struct se_cmd *se_cmd = &cmd->se_cmd;
1576 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1580 device->unreg_rdma_mem(isert_cmd, isert_conn);
1581 transport_generic_free_cmd(&cmd->se_cmd, 0);
1582 break;
1583 case ISCSI_OP_SCSI_TMFUNC:
1584 spin_lock_bh(&conn->cmd_lock);
1585 if (!list_empty(&cmd->i_conn_node))
1586 list_del_init(&cmd->i_conn_node);
1587 spin_unlock_bh(&conn->cmd_lock);
1589 transport_generic_free_cmd(&cmd->se_cmd, 0);
1590 break;
1591 case ISCSI_OP_REJECT:
1592 case ISCSI_OP_NOOP_OUT:
1593 case ISCSI_OP_TEXT:
1594 spin_lock_bh(&conn->cmd_lock);
1595 if (!list_empty(&cmd->i_conn_node))
1596 list_del_init(&cmd->i_conn_node);
1597 spin_unlock_bh(&conn->cmd_lock);
1600 * Handle special case for REJECT when iscsi_add_reject*() has
1601 * overwritten the original iscsi_opcode assignment, and the
1602 * associated cmd->se_cmd needs to be released.
1604 if (cmd->se_cmd.se_tfo != NULL) {
1605 pr_debug("Calling transport_generic_free_cmd from"
1606 " isert_put_cmd for 0x%02x\n",
1607 cmd->iscsi_opcode);
1608 transport_generic_free_cmd(&cmd->se_cmd, 0);
1609 break;
1612 * Fall-through
1614 default:
1615 iscsit_release_cmd(cmd);
1616 break;
1620 static void
1621 isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1623 if (tx_desc->dma_addr != 0) {
1624 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1625 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1626 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1627 tx_desc->dma_addr = 0;
1631 static void
1632 isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1633 struct ib_device *ib_dev, bool comp_err)
1635 if (isert_cmd->pdu_buf_dma != 0) {
1636 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1637 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1638 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1639 isert_cmd->pdu_buf_dma = 0;
1642 isert_unmap_tx_desc(tx_desc, ib_dev);
1643 isert_put_cmd(isert_cmd, comp_err);
1646 static void
1647 isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1648 struct isert_cmd *isert_cmd)
1650 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1651 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1652 struct se_cmd *se_cmd = &cmd->se_cmd;
1653 struct isert_conn *isert_conn = isert_cmd->conn;
1654 struct isert_device *device = isert_conn->conn_device;
1656 iscsit_stop_dataout_timer(cmd);
1657 device->unreg_rdma_mem(isert_cmd, isert_conn);
1658 cmd->write_data_done = wr->cur_rdma_length;
1659 wr->send_wr_num = 0;
1661 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1662 spin_lock_bh(&cmd->istate_lock);
1663 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1664 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1665 spin_unlock_bh(&cmd->istate_lock);
1667 target_execute_cmd(se_cmd);
1670 static void
1671 isert_do_control_comp(struct work_struct *work)
1673 struct isert_cmd *isert_cmd = container_of(work,
1674 struct isert_cmd, comp_work);
1675 struct isert_conn *isert_conn = isert_cmd->conn;
1676 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1677 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1679 switch (cmd->i_state) {
1680 case ISTATE_SEND_TASKMGTRSP:
1681 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1683 atomic_dec(&isert_conn->post_send_buf_count);
1684 iscsit_tmr_post_handler(cmd, cmd->conn);
1686 cmd->i_state = ISTATE_SENT_STATUS;
1687 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1688 break;
1689 case ISTATE_SEND_REJECT:
1690 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1691 atomic_dec(&isert_conn->post_send_buf_count);
1693 cmd->i_state = ISTATE_SENT_STATUS;
1694 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1695 break;
1696 case ISTATE_SEND_LOGOUTRSP:
1697 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1699 atomic_dec(&isert_conn->post_send_buf_count);
1700 iscsit_logout_post_handler(cmd, cmd->conn);
1701 break;
1702 case ISTATE_SEND_TEXTRSP:
1703 atomic_dec(&isert_conn->post_send_buf_count);
1704 cmd->i_state = ISTATE_SENT_STATUS;
1705 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
1706 break;
1707 default:
1708 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1709 dump_stack();
1710 break;
1714 static void
1715 isert_response_completion(struct iser_tx_desc *tx_desc,
1716 struct isert_cmd *isert_cmd,
1717 struct isert_conn *isert_conn,
1718 struct ib_device *ib_dev)
1720 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1721 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1723 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
1724 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1725 cmd->i_state == ISTATE_SEND_REJECT ||
1726 cmd->i_state == ISTATE_SEND_TEXTRSP) {
1727 isert_unmap_tx_desc(tx_desc, ib_dev);
1729 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1730 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1731 return;
1733 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1735 cmd->i_state = ISTATE_SENT_STATUS;
1736 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
1739 static void
1740 __isert_send_completion(struct iser_tx_desc *tx_desc,
1741 struct isert_conn *isert_conn)
1743 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1744 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1745 struct isert_rdma_wr *wr;
1747 if (!isert_cmd) {
1748 atomic_dec(&isert_conn->post_send_buf_count);
1749 isert_unmap_tx_desc(tx_desc, ib_dev);
1750 return;
1752 wr = &isert_cmd->rdma_wr;
1754 switch (wr->iser_ib_op) {
1755 case ISER_IB_RECV:
1756 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1757 dump_stack();
1758 break;
1759 case ISER_IB_SEND:
1760 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1761 isert_response_completion(tx_desc, isert_cmd,
1762 isert_conn, ib_dev);
1763 break;
1764 case ISER_IB_RDMA_WRITE:
1765 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1766 dump_stack();
1767 break;
1768 case ISER_IB_RDMA_READ:
1769 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1771 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1772 isert_completion_rdma_read(tx_desc, isert_cmd);
1773 break;
1774 default:
1775 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1776 dump_stack();
1777 break;
1781 static void
1782 isert_send_completion(struct iser_tx_desc *tx_desc,
1783 struct isert_conn *isert_conn)
1785 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1786 struct iser_tx_desc *t;
1788 * Drain coalesced completion llist starting from comp_llnode_batch
1789 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1791 while (llnode) {
1792 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1793 llnode = llist_next(llnode);
1794 __isert_send_completion(t, isert_conn);
1796 __isert_send_completion(tx_desc, isert_conn);
1799 static void
1800 isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1802 struct llist_node *llnode;
1803 struct isert_rdma_wr *wr;
1804 struct iser_tx_desc *t;
1806 mutex_lock(&isert_conn->conn_mutex);
1807 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1808 isert_conn->conn_comp_batch = 0;
1809 mutex_unlock(&isert_conn->conn_mutex);
1811 while (llnode) {
1812 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1813 llnode = llist_next(llnode);
1814 wr = &t->isert_cmd->rdma_wr;
1816 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1817 isert_completion_put(t, t->isert_cmd, ib_dev, true);
1821 static void
1822 isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1824 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1825 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1826 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1827 struct isert_rdma_wr *wr;
1828 struct iser_tx_desc *t;
1830 while (llnode) {
1831 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1832 llnode = llist_next(llnode);
1833 wr = &t->isert_cmd->rdma_wr;
1835 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1836 isert_completion_put(t, t->isert_cmd, ib_dev, true);
1838 tx_desc->comp_llnode_batch = NULL;
1840 if (!isert_cmd)
1841 isert_unmap_tx_desc(tx_desc, ib_dev);
1842 else
1843 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
1846 static void
1847 isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1849 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1850 struct iscsi_conn *conn = isert_conn->conn;
1852 if (isert_conn->post_recv_buf_count)
1853 return;
1855 isert_cq_drain_comp_llist(isert_conn, ib_dev);
1857 if (conn->sess) {
1858 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1859 target_wait_for_sess_cmds(conn->sess->se_sess);
1862 while (atomic_read(&isert_conn->post_send_buf_count))
1863 msleep(3000);
1865 mutex_lock(&isert_conn->conn_mutex);
1866 isert_conn_terminate(isert_conn);
1867 mutex_unlock(&isert_conn->conn_mutex);
1869 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1871 complete(&isert_conn->conn_wait_comp_err);
1874 static void
1875 isert_cq_tx_work(struct work_struct *work)
1877 struct isert_cq_desc *cq_desc = container_of(work,
1878 struct isert_cq_desc, cq_tx_work);
1879 struct isert_device *device = cq_desc->device;
1880 int cq_index = cq_desc->cq_index;
1881 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1882 struct isert_conn *isert_conn;
1883 struct iser_tx_desc *tx_desc;
1884 struct ib_wc wc;
1886 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1887 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1888 isert_conn = wc.qp->qp_context;
1890 if (wc.status == IB_WC_SUCCESS) {
1891 isert_send_completion(tx_desc, isert_conn);
1892 } else {
1893 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1894 pr_debug("TX wc.status: 0x%08x\n", wc.status);
1895 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1897 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
1898 if (tx_desc->llnode_active)
1899 continue;
1901 atomic_dec(&isert_conn->post_send_buf_count);
1902 isert_cq_tx_comp_err(tx_desc, isert_conn);
1907 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1910 static void
1911 isert_cq_tx_callback(struct ib_cq *cq, void *context)
1913 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1915 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1918 static void
1919 isert_cq_rx_work(struct work_struct *work)
1921 struct isert_cq_desc *cq_desc = container_of(work,
1922 struct isert_cq_desc, cq_rx_work);
1923 struct isert_device *device = cq_desc->device;
1924 int cq_index = cq_desc->cq_index;
1925 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1926 struct isert_conn *isert_conn;
1927 struct iser_rx_desc *rx_desc;
1928 struct ib_wc wc;
1929 unsigned long xfer_len;
1931 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1932 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1933 isert_conn = wc.qp->qp_context;
1935 if (wc.status == IB_WC_SUCCESS) {
1936 xfer_len = (unsigned long)wc.byte_len;
1937 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1938 } else {
1939 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1940 if (wc.status != IB_WC_WR_FLUSH_ERR) {
1941 pr_debug("RX wc.status: 0x%08x\n", wc.status);
1942 pr_debug("RX wc.vendor_err: 0x%08x\n",
1943 wc.vendor_err);
1945 isert_conn->post_recv_buf_count--;
1946 isert_cq_rx_comp_err(isert_conn);
1950 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1953 static void
1954 isert_cq_rx_callback(struct ib_cq *cq, void *context)
1956 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1958 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1961 static int
1962 isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1964 struct ib_send_wr *wr_failed;
1965 int ret;
1967 atomic_inc(&isert_conn->post_send_buf_count);
1969 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1970 &wr_failed);
1971 if (ret) {
1972 pr_err("ib_post_send failed with %d\n", ret);
1973 atomic_dec(&isert_conn->post_send_buf_count);
1974 return ret;
1976 return ret;
1979 static int
1980 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1982 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1983 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1984 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1985 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1986 &isert_cmd->tx_desc.iscsi_header;
1988 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1989 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1990 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1992 * Attach SENSE DATA payload to iSCSI Response PDU
1994 if (cmd->se_cmd.sense_buffer &&
1995 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1996 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1997 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1998 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1999 u32 padding, pdu_len;
2001 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2002 cmd->sense_buffer);
2003 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2005 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2006 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2007 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2009 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2010 (void *)cmd->sense_buffer, pdu_len,
2011 DMA_TO_DEVICE);
2013 isert_cmd->pdu_buf_len = pdu_len;
2014 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2015 tx_dsg->length = pdu_len;
2016 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2017 isert_cmd->tx_desc.num_sge = 2;
2020 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2022 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2024 return isert_post_response(isert_conn, isert_cmd);
2027 static int
2028 isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2029 bool nopout_response)
2031 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2032 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2033 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2035 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2036 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2037 &isert_cmd->tx_desc.iscsi_header,
2038 nopout_response);
2039 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2040 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2042 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2044 return isert_post_response(isert_conn, isert_cmd);
2047 static int
2048 isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2050 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2051 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2052 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2054 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2055 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2056 &isert_cmd->tx_desc.iscsi_header);
2057 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2058 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2060 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2062 return isert_post_response(isert_conn, isert_cmd);
2065 static int
2066 isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2068 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2069 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2070 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2072 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2073 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2074 &isert_cmd->tx_desc.iscsi_header);
2075 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2076 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2078 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2080 return isert_post_response(isert_conn, isert_cmd);
2083 static int
2084 isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2086 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2087 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2088 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2089 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2090 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2091 struct iscsi_reject *hdr =
2092 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2094 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2095 iscsit_build_reject(cmd, conn, hdr);
2096 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2098 hton24(hdr->dlength, ISCSI_HDR_LEN);
2099 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2100 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2101 DMA_TO_DEVICE);
2102 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2103 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2104 tx_dsg->length = ISCSI_HDR_LEN;
2105 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2106 isert_cmd->tx_desc.num_sge = 2;
2108 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2110 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2112 return isert_post_response(isert_conn, isert_cmd);
2115 static int
2116 isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2118 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2119 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2120 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2121 struct iscsi_text_rsp *hdr =
2122 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2123 u32 txt_rsp_len;
2124 int rc;
2126 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2127 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
2128 if (rc < 0)
2129 return rc;
2131 txt_rsp_len = rc;
2132 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2134 if (txt_rsp_len) {
2135 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2136 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2137 void *txt_rsp_buf = cmd->buf_ptr;
2139 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2140 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2142 isert_cmd->pdu_buf_len = txt_rsp_len;
2143 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2144 tx_dsg->length = txt_rsp_len;
2145 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2146 isert_cmd->tx_desc.num_sge = 2;
2148 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2150 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2152 return isert_post_response(isert_conn, isert_cmd);
2155 static int
2156 isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2157 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2158 u32 data_left, u32 offset)
2160 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2161 struct scatterlist *sg_start, *tmp_sg;
2162 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2163 u32 sg_off, page_off;
2164 int i = 0, sg_nents;
2166 sg_off = offset / PAGE_SIZE;
2167 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2168 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2169 page_off = offset % PAGE_SIZE;
2171 send_wr->sg_list = ib_sge;
2172 send_wr->num_sge = sg_nents;
2173 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2175 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2177 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2178 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2179 (unsigned long long)tmp_sg->dma_address,
2180 tmp_sg->length, page_off);
2182 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2183 ib_sge->length = min_t(u32, data_left,
2184 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2185 ib_sge->lkey = isert_conn->conn_mr->lkey;
2187 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2188 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2189 page_off = 0;
2190 data_left -= ib_sge->length;
2191 ib_sge++;
2192 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2195 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2196 send_wr->sg_list, send_wr->num_sge);
2198 return sg_nents;
2201 static int
2202 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2203 struct isert_rdma_wr *wr)
2205 struct se_cmd *se_cmd = &cmd->se_cmd;
2206 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2207 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2208 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2209 struct ib_send_wr *send_wr;
2210 struct ib_sge *ib_sge;
2211 struct scatterlist *sg_start;
2212 u32 sg_off = 0, sg_nents;
2213 u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
2214 int ret = 0, count, i, ib_sge_cnt;
2216 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2217 data_left = se_cmd->data_length;
2218 } else {
2219 sg_off = cmd->write_data_done / PAGE_SIZE;
2220 data_left = se_cmd->data_length - cmd->write_data_done;
2221 offset = cmd->write_data_done;
2222 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2225 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2226 sg_nents = se_cmd->t_data_nents - sg_off;
2228 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2229 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2230 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2231 if (unlikely(!count)) {
2232 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2233 return -EINVAL;
2235 wr->sge = sg_start;
2236 wr->num_sge = sg_nents;
2237 wr->cur_rdma_length = data_left;
2238 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2239 isert_cmd, count, sg_start, sg_nents, data_left);
2241 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2242 if (!ib_sge) {
2243 pr_warn("Unable to allocate ib_sge\n");
2244 ret = -ENOMEM;
2245 goto unmap_sg;
2247 wr->ib_sge = ib_sge;
2249 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2250 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2251 GFP_KERNEL);
2252 if (!wr->send_wr) {
2253 pr_debug("Unable to allocate wr->send_wr\n");
2254 ret = -ENOMEM;
2255 goto unmap_sg;
2258 wr->isert_cmd = isert_cmd;
2259 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2261 for (i = 0; i < wr->send_wr_num; i++) {
2262 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2263 data_len = min(data_left, rdma_write_max);
2265 send_wr->send_flags = 0;
2266 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2267 send_wr->opcode = IB_WR_RDMA_WRITE;
2268 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2269 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2270 if (i + 1 == wr->send_wr_num)
2271 send_wr->next = &isert_cmd->tx_desc.send_wr;
2272 else
2273 send_wr->next = &wr->send_wr[i + 1];
2274 } else {
2275 send_wr->opcode = IB_WR_RDMA_READ;
2276 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2277 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2278 if (i + 1 == wr->send_wr_num)
2279 send_wr->send_flags = IB_SEND_SIGNALED;
2280 else
2281 send_wr->next = &wr->send_wr[i + 1];
2284 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2285 send_wr, data_len, offset);
2286 ib_sge += ib_sge_cnt;
2288 offset += data_len;
2289 va_offset += data_len;
2290 data_left -= data_len;
2293 return 0;
2294 unmap_sg:
2295 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2296 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2297 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2298 return ret;
2301 static int
2302 isert_map_fr_pagelist(struct ib_device *ib_dev,
2303 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2305 u64 start_addr, end_addr, page, chunk_start = 0;
2306 struct scatterlist *tmp_sg;
2307 int i = 0, new_chunk, last_ent, n_pages;
2309 n_pages = 0;
2310 new_chunk = 1;
2311 last_ent = sg_nents - 1;
2312 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2313 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2314 if (new_chunk)
2315 chunk_start = start_addr;
2316 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2318 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2319 i, (unsigned long long)tmp_sg->dma_address,
2320 tmp_sg->length);
2322 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2323 new_chunk = 0;
2324 continue;
2326 new_chunk = 1;
2328 page = chunk_start & PAGE_MASK;
2329 do {
2330 fr_pl[n_pages++] = page;
2331 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2332 n_pages - 1, page);
2333 page += PAGE_SIZE;
2334 } while (page < end_addr);
2337 return n_pages;
2340 static int
2341 isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
2342 struct isert_conn *isert_conn, struct scatterlist *sg_start,
2343 struct ib_sge *ib_sge, u32 sg_nents, u32 offset,
2344 unsigned int data_len)
2346 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2347 struct ib_send_wr fr_wr, inv_wr;
2348 struct ib_send_wr *bad_wr, *wr = NULL;
2349 int ret, pagelist_len;
2350 u32 page_off;
2351 u8 key;
2353 sg_nents = min_t(unsigned int, sg_nents, ISCSI_ISER_SG_TABLESIZE);
2354 page_off = offset % PAGE_SIZE;
2356 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2357 fr_desc, sg_nents, offset);
2359 pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
2360 &fr_desc->data_frpl->page_list[0]);
2362 if (!fr_desc->valid) {
2363 memset(&inv_wr, 0, sizeof(inv_wr));
2364 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
2365 inv_wr.opcode = IB_WR_LOCAL_INV;
2366 inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
2367 wr = &inv_wr;
2368 /* Bump the key */
2369 key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
2370 ib_update_fast_reg_key(fr_desc->data_mr, ++key);
2373 /* Prepare FASTREG WR */
2374 memset(&fr_wr, 0, sizeof(fr_wr));
2375 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
2376 fr_wr.opcode = IB_WR_FAST_REG_MR;
2377 fr_wr.wr.fast_reg.iova_start =
2378 fr_desc->data_frpl->page_list[0] + page_off;
2379 fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
2380 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2381 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2382 fr_wr.wr.fast_reg.length = data_len;
2383 fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
2384 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2386 if (!wr)
2387 wr = &fr_wr;
2388 else
2389 wr->next = &fr_wr;
2391 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2392 if (ret) {
2393 pr_err("fast registration failed, ret:%d\n", ret);
2394 return ret;
2396 fr_desc->valid = false;
2398 ib_sge->lkey = fr_desc->data_mr->lkey;
2399 ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2400 ib_sge->length = data_len;
2402 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2403 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2405 return ret;
2408 static int
2409 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2410 struct isert_rdma_wr *wr)
2412 struct se_cmd *se_cmd = &cmd->se_cmd;
2413 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2414 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2415 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2416 struct ib_send_wr *send_wr;
2417 struct ib_sge *ib_sge;
2418 struct scatterlist *sg_start;
2419 struct fast_reg_descriptor *fr_desc;
2420 u32 sg_off = 0, sg_nents;
2421 u32 offset = 0, data_len, data_left, rdma_write_max;
2422 int ret = 0, count;
2423 unsigned long flags;
2425 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2426 data_left = se_cmd->data_length;
2427 } else {
2428 offset = cmd->write_data_done;
2429 sg_off = offset / PAGE_SIZE;
2430 data_left = se_cmd->data_length - cmd->write_data_done;
2431 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2434 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2435 sg_nents = se_cmd->t_data_nents - sg_off;
2437 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2438 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2439 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2440 if (unlikely(!count)) {
2441 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2442 return -EINVAL;
2444 wr->sge = sg_start;
2445 wr->num_sge = sg_nents;
2446 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2447 isert_cmd, count, sg_start, sg_nents, data_left);
2449 memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
2450 ib_sge = &wr->s_ib_sge;
2451 wr->ib_sge = ib_sge;
2453 wr->send_wr_num = 1;
2454 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2455 wr->send_wr = &wr->s_send_wr;
2457 wr->isert_cmd = isert_cmd;
2458 rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
2460 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2461 send_wr->sg_list = ib_sge;
2462 send_wr->num_sge = 1;
2463 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2464 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2465 send_wr->opcode = IB_WR_RDMA_WRITE;
2466 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2467 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2468 send_wr->send_flags = 0;
2469 send_wr->next = &isert_cmd->tx_desc.send_wr;
2470 } else {
2471 send_wr->opcode = IB_WR_RDMA_READ;
2472 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2473 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2474 send_wr->send_flags = IB_SEND_SIGNALED;
2477 data_len = min(data_left, rdma_write_max);
2478 wr->cur_rdma_length = data_len;
2480 /* if there is a single dma entry, dma mr is sufficient */
2481 if (count == 1) {
2482 ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
2483 ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
2484 ib_sge->lkey = isert_conn->conn_mr->lkey;
2485 wr->fr_desc = NULL;
2486 } else {
2487 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2488 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2489 struct fast_reg_descriptor, list);
2490 list_del(&fr_desc->list);
2491 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2492 wr->fr_desc = fr_desc;
2494 ret = isert_fast_reg_mr(fr_desc, isert_conn, sg_start,
2495 ib_sge, sg_nents, offset, data_len);
2496 if (ret) {
2497 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2498 goto unmap_sg;
2502 return 0;
2504 unmap_sg:
2505 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2506 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2507 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2508 return ret;
2511 static int
2512 isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2514 struct se_cmd *se_cmd = &cmd->se_cmd;
2515 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2516 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2517 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2518 struct isert_device *device = isert_conn->conn_device;
2519 struct ib_send_wr *wr_failed;
2520 int rc;
2522 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2523 isert_cmd, se_cmd->data_length);
2524 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2525 rc = device->reg_rdma_mem(conn, cmd, wr);
2526 if (rc) {
2527 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2528 return rc;
2532 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2534 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2535 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2536 &isert_cmd->tx_desc.iscsi_header);
2537 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2538 isert_init_send_wr(isert_conn, isert_cmd,
2539 &isert_cmd->tx_desc.send_wr, false);
2541 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2543 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2544 if (rc) {
2545 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2546 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2548 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
2549 isert_cmd);
2551 return 1;
2554 static int
2555 isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2557 struct se_cmd *se_cmd = &cmd->se_cmd;
2558 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2559 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2560 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2561 struct isert_device *device = isert_conn->conn_device;
2562 struct ib_send_wr *wr_failed;
2563 int rc;
2565 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2566 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2567 wr->iser_ib_op = ISER_IB_RDMA_READ;
2568 rc = device->reg_rdma_mem(conn, cmd, wr);
2569 if (rc) {
2570 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2571 return rc;
2574 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2576 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2577 if (rc) {
2578 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2579 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2581 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2582 isert_cmd);
2584 return 0;
2587 static int
2588 isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2590 int ret;
2592 switch (state) {
2593 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2594 ret = isert_put_nopin(cmd, conn, false);
2595 break;
2596 default:
2597 pr_err("Unknown immediate state: 0x%02x\n", state);
2598 ret = -EINVAL;
2599 break;
2602 return ret;
2605 static int
2606 isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2608 int ret;
2610 switch (state) {
2611 case ISTATE_SEND_LOGOUTRSP:
2612 ret = isert_put_logout_rsp(cmd, conn);
2613 if (!ret) {
2614 pr_debug("Returning iSER Logout -EAGAIN\n");
2615 ret = -EAGAIN;
2617 break;
2618 case ISTATE_SEND_NOPIN:
2619 ret = isert_put_nopin(cmd, conn, true);
2620 break;
2621 case ISTATE_SEND_TASKMGTRSP:
2622 ret = isert_put_tm_rsp(cmd, conn);
2623 break;
2624 case ISTATE_SEND_REJECT:
2625 ret = isert_put_reject(cmd, conn);
2626 break;
2627 case ISTATE_SEND_TEXTRSP:
2628 ret = isert_put_text_rsp(cmd, conn);
2629 break;
2630 case ISTATE_SEND_STATUS:
2632 * Special case for sending non GOOD SCSI status from TX thread
2633 * context during pre se_cmd excecution failure.
2635 ret = isert_put_response(conn, cmd);
2636 break;
2637 default:
2638 pr_err("Unknown response state: 0x%02x\n", state);
2639 ret = -EINVAL;
2640 break;
2643 return ret;
2646 static int
2647 isert_setup_np(struct iscsi_np *np,
2648 struct __kernel_sockaddr_storage *ksockaddr)
2650 struct isert_np *isert_np;
2651 struct rdma_cm_id *isert_lid;
2652 struct sockaddr *sa;
2653 int ret;
2655 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2656 if (!isert_np) {
2657 pr_err("Unable to allocate struct isert_np\n");
2658 return -ENOMEM;
2660 sema_init(&isert_np->np_sem, 0);
2661 mutex_init(&isert_np->np_accept_mutex);
2662 INIT_LIST_HEAD(&isert_np->np_accept_list);
2663 init_completion(&isert_np->np_login_comp);
2665 sa = (struct sockaddr *)ksockaddr;
2666 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2668 * Setup the np->np_sockaddr from the passed sockaddr setup
2669 * in iscsi_target_configfs.c code..
2671 memcpy(&np->np_sockaddr, ksockaddr,
2672 sizeof(struct __kernel_sockaddr_storage));
2674 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2675 IB_QPT_RC);
2676 if (IS_ERR(isert_lid)) {
2677 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2678 PTR_ERR(isert_lid));
2679 ret = PTR_ERR(isert_lid);
2680 goto out;
2683 ret = rdma_bind_addr(isert_lid, sa);
2684 if (ret) {
2685 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2686 goto out_lid;
2689 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2690 if (ret) {
2691 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2692 goto out_lid;
2695 isert_np->np_cm_id = isert_lid;
2696 np->np_context = isert_np;
2697 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2699 return 0;
2701 out_lid:
2702 rdma_destroy_id(isert_lid);
2703 out:
2704 kfree(isert_np);
2705 return ret;
2708 static int
2709 isert_rdma_accept(struct isert_conn *isert_conn)
2711 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2712 struct rdma_conn_param cp;
2713 int ret;
2715 memset(&cp, 0, sizeof(struct rdma_conn_param));
2716 cp.responder_resources = isert_conn->responder_resources;
2717 cp.initiator_depth = isert_conn->initiator_depth;
2718 cp.retry_count = 7;
2719 cp.rnr_retry_count = 7;
2721 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2723 ret = rdma_accept(cm_id, &cp);
2724 if (ret) {
2725 pr_err("rdma_accept() failed with: %d\n", ret);
2726 return ret;
2729 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2731 return 0;
2734 static int
2735 isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2737 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2738 int ret;
2740 pr_info("before login_req comp conn: %p\n", isert_conn);
2741 ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
2742 if (ret) {
2743 pr_err("isert_conn %p interrupted before got login req\n",
2744 isert_conn);
2745 return ret;
2747 reinit_completion(&isert_conn->login_req_comp);
2750 * For login requests after the first PDU, isert_rx_login_req() will
2751 * kick schedule_delayed_work(&conn->login_work) as the packet is
2752 * received, which turns this callback from iscsi_target_do_login_rx()
2753 * into a NOP.
2755 if (!login->first_request)
2756 return 0;
2758 isert_rx_login_req(isert_conn);
2760 pr_info("before conn_login_comp conn: %p\n", conn);
2761 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2762 if (ret)
2763 return ret;
2765 pr_info("processing login->req: %p\n", login->req);
2767 return 0;
2770 static void
2771 isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2772 struct isert_conn *isert_conn)
2774 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2775 struct rdma_route *cm_route = &cm_id->route;
2776 struct sockaddr_in *sock_in;
2777 struct sockaddr_in6 *sock_in6;
2779 conn->login_family = np->np_sockaddr.ss_family;
2781 if (np->np_sockaddr.ss_family == AF_INET6) {
2782 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2783 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2784 &sock_in6->sin6_addr.in6_u);
2785 conn->login_port = ntohs(sock_in6->sin6_port);
2787 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2788 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2789 &sock_in6->sin6_addr.in6_u);
2790 conn->local_port = ntohs(sock_in6->sin6_port);
2791 } else {
2792 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2793 sprintf(conn->login_ip, "%pI4",
2794 &sock_in->sin_addr.s_addr);
2795 conn->login_port = ntohs(sock_in->sin_port);
2797 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2798 sprintf(conn->local_ip, "%pI4",
2799 &sock_in->sin_addr.s_addr);
2800 conn->local_port = ntohs(sock_in->sin_port);
2804 static int
2805 isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2807 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2808 struct isert_conn *isert_conn;
2809 int max_accept = 0, ret;
2811 accept_wait:
2812 ret = down_interruptible(&isert_np->np_sem);
2813 if (max_accept > 5)
2814 return -ENODEV;
2816 spin_lock_bh(&np->np_thread_lock);
2817 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
2818 spin_unlock_bh(&np->np_thread_lock);
2819 pr_debug("np_thread_state %d for isert_accept_np\n",
2820 np->np_thread_state);
2822 * No point in stalling here when np_thread
2823 * is in state RESET/SHUTDOWN/EXIT - bail
2825 return -ENODEV;
2827 spin_unlock_bh(&np->np_thread_lock);
2829 mutex_lock(&isert_np->np_accept_mutex);
2830 if (list_empty(&isert_np->np_accept_list)) {
2831 mutex_unlock(&isert_np->np_accept_mutex);
2832 max_accept++;
2833 goto accept_wait;
2835 isert_conn = list_first_entry(&isert_np->np_accept_list,
2836 struct isert_conn, conn_accept_node);
2837 list_del_init(&isert_conn->conn_accept_node);
2838 mutex_unlock(&isert_np->np_accept_mutex);
2840 conn->context = isert_conn;
2841 isert_conn->conn = conn;
2842 max_accept = 0;
2844 isert_set_conn_info(np, conn, isert_conn);
2846 pr_debug("Processing isert_conn: %p\n", isert_conn);
2848 return 0;
2851 static void
2852 isert_free_np(struct iscsi_np *np)
2854 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2856 if (isert_np->np_cm_id)
2857 rdma_destroy_id(isert_np->np_cm_id);
2859 np->np_context = NULL;
2860 kfree(isert_np);
2863 static void isert_wait_conn(struct iscsi_conn *conn)
2865 struct isert_conn *isert_conn = conn->context;
2867 pr_debug("isert_wait_conn: Starting \n");
2869 mutex_lock(&isert_conn->conn_mutex);
2871 * Only wait for conn_wait_comp_err if the isert_conn made it
2872 * into full feature phase..
2874 if (isert_conn->state == ISER_CONN_INIT) {
2875 mutex_unlock(&isert_conn->conn_mutex);
2876 return;
2878 isert_conn_terminate(isert_conn);
2879 mutex_unlock(&isert_conn->conn_mutex);
2881 wait_for_completion(&isert_conn->conn_wait_comp_err);
2882 wait_for_completion(&isert_conn->conn_wait);
2884 mutex_lock(&isert_conn->conn_mutex);
2885 isert_conn->state = ISER_CONN_DOWN;
2886 mutex_unlock(&isert_conn->conn_mutex);
2888 pr_info("Destroying conn %p\n", isert_conn);
2889 isert_put_conn(isert_conn);
2892 static void isert_free_conn(struct iscsi_conn *conn)
2894 struct isert_conn *isert_conn = conn->context;
2896 isert_put_conn(isert_conn);
2899 static struct iscsit_transport iser_target_transport = {
2900 .name = "IB/iSER",
2901 .transport_type = ISCSI_INFINIBAND,
2902 .priv_size = sizeof(struct isert_cmd),
2903 .owner = THIS_MODULE,
2904 .iscsit_setup_np = isert_setup_np,
2905 .iscsit_accept_np = isert_accept_np,
2906 .iscsit_free_np = isert_free_np,
2907 .iscsit_wait_conn = isert_wait_conn,
2908 .iscsit_free_conn = isert_free_conn,
2909 .iscsit_get_login_rx = isert_get_login_rx,
2910 .iscsit_put_login_tx = isert_put_login_tx,
2911 .iscsit_immediate_queue = isert_immediate_queue,
2912 .iscsit_response_queue = isert_response_queue,
2913 .iscsit_get_dataout = isert_get_dataout,
2914 .iscsit_queue_data_in = isert_put_datain,
2915 .iscsit_queue_status = isert_put_response,
2918 static int __init isert_init(void)
2920 int ret;
2922 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2923 if (!isert_rx_wq) {
2924 pr_err("Unable to allocate isert_rx_wq\n");
2925 return -ENOMEM;
2928 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2929 if (!isert_comp_wq) {
2930 pr_err("Unable to allocate isert_comp_wq\n");
2931 ret = -ENOMEM;
2932 goto destroy_rx_wq;
2935 iscsit_register_transport(&iser_target_transport);
2936 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2937 return 0;
2939 destroy_rx_wq:
2940 destroy_workqueue(isert_rx_wq);
2941 return ret;
2944 static void __exit isert_exit(void)
2946 flush_scheduled_work();
2947 destroy_workqueue(isert_comp_wq);
2948 destroy_workqueue(isert_rx_wq);
2949 iscsit_unregister_transport(&iser_target_transport);
2950 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2953 MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2954 MODULE_VERSION("0.1");
2955 MODULE_AUTHOR("nab@Linux-iSCSI.org");
2956 MODULE_LICENSE("GPL");
2958 module_init(isert_init);
2959 module_exit(isert_exit);