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>
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>
32 #include "isert_proto.h"
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)
39 static DEFINE_MUTEX(device_list_mutex);
40 static LIST_HEAD(device_list);
41 static struct workqueue_struct *isert_rx_wq;
42 static struct workqueue_struct *isert_comp_wq;
45 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
48 struct isert_rdma_wr *wr);
50 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53 struct isert_rdma_wr *wr);
55 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
58 isert_qp_event_callback(struct ib_event *e, void *context)
60 struct isert_conn *isert_conn = (struct isert_conn *)context;
62 pr_err("isert_qp_event_callback event: %d\n", e->event);
64 case IB_EVENT_COMM_EST:
65 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
67 case IB_EVENT_QP_LAST_WQE_REACHED:
68 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
76 isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
80 ret = ib_query_device(ib_dev, devattr);
82 pr_err("ib_query_device() failed: %d\n", ret);
85 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
86 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
92 isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
95 struct isert_device *device = isert_conn->conn_device;
96 struct ib_qp_init_attr attr;
97 int ret, index, min_index = 0;
99 mutex_lock(&device_list_mutex);
100 for (index = 0; index < device->cqs_used; index++)
101 if (device->cq_active_qps[index] <
102 device->cq_active_qps[min_index])
104 device->cq_active_qps[min_index]++;
105 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
106 mutex_unlock(&device_list_mutex);
108 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
109 attr.event_handler = isert_qp_event_callback;
110 attr.qp_context = isert_conn;
111 attr.send_cq = device->dev_tx_cq[min_index];
112 attr.recv_cq = device->dev_rx_cq[min_index];
113 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
114 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
116 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
117 * work-around for RDMA_READ..
119 attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
120 isert_conn->max_sge = attr.cap.max_send_sge;
122 attr.cap.max_recv_sge = 1;
123 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
124 attr.qp_type = IB_QPT_RC;
126 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
128 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
130 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
131 isert_conn->conn_pd->device);
133 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
135 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
138 isert_conn->conn_qp = cma_id->qp;
139 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
145 isert_cq_event_callback(struct ib_event *e, void *context)
147 pr_debug("isert_cq_event_callback event: %d\n", e->event);
151 isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
153 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
154 struct iser_rx_desc *rx_desc;
155 struct ib_sge *rx_sg;
159 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
160 sizeof(struct iser_rx_desc), GFP_KERNEL);
161 if (!isert_conn->conn_rx_descs)
164 rx_desc = isert_conn->conn_rx_descs;
166 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
167 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
168 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
169 if (ib_dma_mapping_error(ib_dev, dma_addr))
172 rx_desc->dma_addr = dma_addr;
174 rx_sg = &rx_desc->rx_sg;
175 rx_sg->addr = rx_desc->dma_addr;
176 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
177 rx_sg->lkey = isert_conn->conn_mr->lkey;
180 isert_conn->conn_rx_desc_head = 0;
184 rx_desc = isert_conn->conn_rx_descs;
185 for (j = 0; j < i; j++, rx_desc++) {
186 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
187 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
189 kfree(isert_conn->conn_rx_descs);
190 isert_conn->conn_rx_descs = NULL;
196 isert_free_rx_descriptors(struct isert_conn *isert_conn)
198 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
199 struct iser_rx_desc *rx_desc;
202 if (!isert_conn->conn_rx_descs)
205 rx_desc = isert_conn->conn_rx_descs;
206 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
207 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
208 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
211 kfree(isert_conn->conn_rx_descs);
212 isert_conn->conn_rx_descs = NULL;
215 static void isert_cq_tx_work(struct work_struct *);
216 static void isert_cq_tx_callback(struct ib_cq *, void *);
217 static void isert_cq_rx_work(struct work_struct *);
218 static void isert_cq_rx_callback(struct ib_cq *, void *);
221 isert_create_device_ib_res(struct isert_device *device)
223 struct ib_device *ib_dev = device->ib_device;
224 struct isert_cq_desc *cq_desc;
225 struct ib_device_attr *dev_attr;
228 dev_attr = &device->dev_attr;
229 ret = isert_query_device(ib_dev, dev_attr);
233 /* asign function handlers */
234 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
235 device->use_fastreg = 1;
236 device->reg_rdma_mem = isert_reg_rdma;
237 device->unreg_rdma_mem = isert_unreg_rdma;
239 device->use_fastreg = 0;
240 device->reg_rdma_mem = isert_map_rdma;
241 device->unreg_rdma_mem = isert_unmap_cmd;
244 /* Check signature cap */
245 device->pi_capable = dev_attr->device_cap_flags &
246 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
248 device->cqs_used = min_t(int, num_online_cpus(),
249 device->ib_device->num_comp_vectors);
250 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
251 pr_debug("Using %d CQs, device %s supports %d vectors support "
252 "Fast registration %d pi_capable %d\n",
253 device->cqs_used, device->ib_device->name,
254 device->ib_device->num_comp_vectors, device->use_fastreg,
256 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
257 device->cqs_used, GFP_KERNEL);
258 if (!device->cq_desc) {
259 pr_err("Unable to allocate device->cq_desc\n");
262 cq_desc = device->cq_desc;
264 for (i = 0; i < device->cqs_used; i++) {
265 cq_desc[i].device = device;
266 cq_desc[i].cq_index = i;
268 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
269 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
270 isert_cq_rx_callback,
271 isert_cq_event_callback,
273 ISER_MAX_RX_CQ_LEN, i);
274 if (IS_ERR(device->dev_rx_cq[i])) {
275 ret = PTR_ERR(device->dev_rx_cq[i]);
276 device->dev_rx_cq[i] = NULL;
280 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
281 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
282 isert_cq_tx_callback,
283 isert_cq_event_callback,
285 ISER_MAX_TX_CQ_LEN, i);
286 if (IS_ERR(device->dev_tx_cq[i])) {
287 ret = PTR_ERR(device->dev_tx_cq[i]);
288 device->dev_tx_cq[i] = NULL;
292 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
296 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
304 for (j = 0; j < i; j++) {
305 cq_desc = &device->cq_desc[j];
307 if (device->dev_rx_cq[j]) {
308 cancel_work_sync(&cq_desc->cq_rx_work);
309 ib_destroy_cq(device->dev_rx_cq[j]);
311 if (device->dev_tx_cq[j]) {
312 cancel_work_sync(&cq_desc->cq_tx_work);
313 ib_destroy_cq(device->dev_tx_cq[j]);
316 kfree(device->cq_desc);
322 isert_free_device_ib_res(struct isert_device *device)
324 struct isert_cq_desc *cq_desc;
327 for (i = 0; i < device->cqs_used; i++) {
328 cq_desc = &device->cq_desc[i];
330 cancel_work_sync(&cq_desc->cq_rx_work);
331 cancel_work_sync(&cq_desc->cq_tx_work);
332 ib_destroy_cq(device->dev_rx_cq[i]);
333 ib_destroy_cq(device->dev_tx_cq[i]);
334 device->dev_rx_cq[i] = NULL;
335 device->dev_tx_cq[i] = NULL;
338 kfree(device->cq_desc);
342 isert_device_try_release(struct isert_device *device)
344 mutex_lock(&device_list_mutex);
346 if (!device->refcount) {
347 isert_free_device_ib_res(device);
348 list_del(&device->dev_node);
351 mutex_unlock(&device_list_mutex);
354 static struct isert_device *
355 isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
357 struct isert_device *device;
360 mutex_lock(&device_list_mutex);
361 list_for_each_entry(device, &device_list, dev_node) {
362 if (device->ib_device->node_guid == cma_id->device->node_guid) {
364 mutex_unlock(&device_list_mutex);
369 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
371 mutex_unlock(&device_list_mutex);
372 return ERR_PTR(-ENOMEM);
375 INIT_LIST_HEAD(&device->dev_node);
377 device->ib_device = cma_id->device;
378 ret = isert_create_device_ib_res(device);
381 mutex_unlock(&device_list_mutex);
386 list_add_tail(&device->dev_node, &device_list);
387 mutex_unlock(&device_list_mutex);
393 isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
395 struct fast_reg_descriptor *fr_desc, *tmp;
398 if (list_empty(&isert_conn->conn_fr_pool))
401 pr_debug("Freeing conn %p fastreg pool", isert_conn);
403 list_for_each_entry_safe(fr_desc, tmp,
404 &isert_conn->conn_fr_pool, list) {
405 list_del(&fr_desc->list);
406 ib_free_fast_reg_page_list(fr_desc->data_frpl);
407 ib_dereg_mr(fr_desc->data_mr);
408 if (fr_desc->pi_ctx) {
409 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
410 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
411 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
412 kfree(fr_desc->pi_ctx);
418 if (i < isert_conn->conn_fr_pool_size)
419 pr_warn("Pool still has %d regions registered\n",
420 isert_conn->conn_fr_pool_size - i);
424 isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
425 struct fast_reg_descriptor *fr_desc, u8 protection)
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 ret = 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);
446 fr_desc->ind |= ISERT_DATA_KEY_VALID;
449 struct ib_mr_init_attr mr_init_attr = {0};
450 struct pi_context *pi_ctx;
452 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
453 if (!fr_desc->pi_ctx) {
454 pr_err("Failed to allocate pi context\n");
458 pi_ctx = fr_desc->pi_ctx;
460 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
461 ISCSI_ISER_SG_TABLESIZE);
462 if (IS_ERR(pi_ctx->prot_frpl)) {
463 pr_err("Failed to allocate prot frpl err=%ld\n",
464 PTR_ERR(pi_ctx->prot_frpl));
465 ret = PTR_ERR(pi_ctx->prot_frpl);
469 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
470 if (IS_ERR(pi_ctx->prot_mr)) {
471 pr_err("Failed to allocate prot frmr err=%ld\n",
472 PTR_ERR(pi_ctx->prot_mr));
473 ret = PTR_ERR(pi_ctx->prot_mr);
476 fr_desc->ind |= ISERT_PROT_KEY_VALID;
478 mr_init_attr.max_reg_descriptors = 2;
479 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
480 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
481 if (IS_ERR(pi_ctx->sig_mr)) {
482 pr_err("Failed to allocate signature enabled mr err=%ld\n",
483 PTR_ERR(pi_ctx->sig_mr));
484 ret = PTR_ERR(pi_ctx->sig_mr);
487 fr_desc->ind |= ISERT_SIG_KEY_VALID;
489 fr_desc->ind &= ~ISERT_PROTECTED;
493 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
495 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
497 kfree(fr_desc->pi_ctx);
499 ib_dereg_mr(fr_desc->data_mr);
501 ib_free_fast_reg_page_list(fr_desc->data_frpl);
507 isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
509 struct fast_reg_descriptor *fr_desc;
510 struct isert_device *device = isert_conn->conn_device;
513 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
514 isert_conn->conn_fr_pool_size = 0;
515 for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
516 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
518 pr_err("Failed to allocate fast_reg descriptor\n");
523 ret = isert_create_fr_desc(device->ib_device,
524 isert_conn->conn_pd, fr_desc,
527 pr_err("Failed to create fastreg descriptor err=%d\n",
533 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
534 isert_conn->conn_fr_pool_size++;
537 pr_debug("Creating conn %p fastreg pool size=%d",
538 isert_conn, isert_conn->conn_fr_pool_size);
543 isert_conn_free_fastreg_pool(isert_conn);
548 isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
550 struct iscsi_np *np = cma_id->context;
551 struct isert_np *isert_np = np->np_context;
552 struct isert_conn *isert_conn;
553 struct isert_device *device;
554 struct ib_device *ib_dev = cma_id->device;
556 u8 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
558 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
559 cma_id, cma_id->context);
561 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
563 pr_err("Unable to allocate isert_conn\n");
566 isert_conn->state = ISER_CONN_INIT;
567 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
568 init_completion(&isert_conn->conn_login_comp);
569 init_completion(&isert_conn->conn_wait);
570 init_completion(&isert_conn->conn_wait_comp_err);
571 kref_init(&isert_conn->conn_kref);
572 kref_get(&isert_conn->conn_kref);
573 mutex_init(&isert_conn->conn_mutex);
574 spin_lock_init(&isert_conn->conn_lock);
576 cma_id->context = isert_conn;
577 isert_conn->conn_cm_id = cma_id;
578 isert_conn->responder_resources = event->param.conn.responder_resources;
579 isert_conn->initiator_depth = event->param.conn.initiator_depth;
580 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
581 isert_conn->responder_resources, isert_conn->initiator_depth);
583 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
584 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
585 if (!isert_conn->login_buf) {
586 pr_err("Unable to allocate isert_conn->login_buf\n");
591 isert_conn->login_req_buf = isert_conn->login_buf;
592 isert_conn->login_rsp_buf = isert_conn->login_buf +
593 ISCSI_DEF_MAX_RECV_SEG_LEN;
594 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
595 isert_conn->login_buf, isert_conn->login_req_buf,
596 isert_conn->login_rsp_buf);
598 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
599 (void *)isert_conn->login_req_buf,
600 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
602 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
604 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
606 isert_conn->login_req_dma = 0;
610 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
611 (void *)isert_conn->login_rsp_buf,
612 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
614 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
616 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
618 isert_conn->login_rsp_dma = 0;
619 goto out_req_dma_map;
622 device = isert_device_find_by_ib_dev(cma_id);
623 if (IS_ERR(device)) {
624 ret = PTR_ERR(device);
625 goto out_rsp_dma_map;
628 isert_conn->conn_device = device;
629 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
630 if (IS_ERR(isert_conn->conn_pd)) {
631 ret = PTR_ERR(isert_conn->conn_pd);
632 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
637 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
638 IB_ACCESS_LOCAL_WRITE);
639 if (IS_ERR(isert_conn->conn_mr)) {
640 ret = PTR_ERR(isert_conn->conn_mr);
641 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
646 if (pi_support && !device->pi_capable) {
647 pr_err("Protection information requested but not supported\n");
652 if (device->use_fastreg) {
653 ret = isert_conn_create_fastreg_pool(isert_conn, pi_support);
655 pr_err("Conn: %p failed to create fastreg pool\n",
661 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
665 mutex_lock(&isert_np->np_accept_mutex);
666 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
667 mutex_unlock(&isert_np->np_accept_mutex);
669 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
670 wake_up(&isert_np->np_accept_wq);
674 if (device->use_fastreg)
675 isert_conn_free_fastreg_pool(isert_conn);
677 ib_dereg_mr(isert_conn->conn_mr);
679 ib_dealloc_pd(isert_conn->conn_pd);
681 isert_device_try_release(device);
683 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
684 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
686 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
687 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
689 kfree(isert_conn->login_buf);
696 isert_connect_release(struct isert_conn *isert_conn)
698 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
699 struct isert_device *device = isert_conn->conn_device;
702 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
704 if (device && device->use_fastreg)
705 isert_conn_free_fastreg_pool(isert_conn);
707 if (isert_conn->conn_qp) {
708 cq_index = ((struct isert_cq_desc *)
709 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
710 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
711 isert_conn->conn_device->cq_active_qps[cq_index]--;
713 rdma_destroy_qp(isert_conn->conn_cm_id);
716 isert_free_rx_descriptors(isert_conn);
717 rdma_destroy_id(isert_conn->conn_cm_id);
719 ib_dereg_mr(isert_conn->conn_mr);
720 ib_dealloc_pd(isert_conn->conn_pd);
722 if (isert_conn->login_buf) {
723 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
724 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
725 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
726 ISCSI_DEF_MAX_RECV_SEG_LEN,
728 kfree(isert_conn->login_buf);
733 isert_device_try_release(device);
735 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
739 isert_connected_handler(struct rdma_cm_id *cma_id)
745 isert_release_conn_kref(struct kref *kref)
747 struct isert_conn *isert_conn = container_of(kref,
748 struct isert_conn, conn_kref);
750 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
751 current->comm, current->pid);
753 isert_connect_release(isert_conn);
757 isert_put_conn(struct isert_conn *isert_conn)
759 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
763 isert_disconnect_work(struct work_struct *work)
765 struct isert_conn *isert_conn = container_of(work,
766 struct isert_conn, conn_logout_work);
768 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
769 mutex_lock(&isert_conn->conn_mutex);
770 if (isert_conn->state == ISER_CONN_UP)
771 isert_conn->state = ISER_CONN_TERMINATING;
773 if (isert_conn->post_recv_buf_count == 0 &&
774 atomic_read(&isert_conn->post_send_buf_count) == 0) {
775 mutex_unlock(&isert_conn->conn_mutex);
778 if (!isert_conn->conn_cm_id) {
779 mutex_unlock(&isert_conn->conn_mutex);
780 isert_put_conn(isert_conn);
783 if (!isert_conn->logout_posted) {
784 pr_debug("Calling rdma_disconnect for !logout_posted from"
785 " isert_disconnect_work\n");
786 rdma_disconnect(isert_conn->conn_cm_id);
787 mutex_unlock(&isert_conn->conn_mutex);
788 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
791 mutex_unlock(&isert_conn->conn_mutex);
794 complete(&isert_conn->conn_wait);
795 isert_put_conn(isert_conn);
799 isert_disconnected_handler(struct rdma_cm_id *cma_id)
801 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
803 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
804 schedule_work(&isert_conn->conn_logout_work);
808 isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
812 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
813 event->event, event->status, cma_id->context, cma_id);
815 switch (event->event) {
816 case RDMA_CM_EVENT_CONNECT_REQUEST:
817 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
818 ret = isert_connect_request(cma_id, event);
820 case RDMA_CM_EVENT_ESTABLISHED:
821 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
822 isert_connected_handler(cma_id);
824 case RDMA_CM_EVENT_DISCONNECTED:
825 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
826 isert_disconnected_handler(cma_id);
828 case RDMA_CM_EVENT_DEVICE_REMOVAL:
829 case RDMA_CM_EVENT_ADDR_CHANGE:
831 case RDMA_CM_EVENT_CONNECT_ERROR:
833 pr_err("Unknown RDMA CMA event: %d\n", event->event);
838 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
847 isert_post_recv(struct isert_conn *isert_conn, u32 count)
849 struct ib_recv_wr *rx_wr, *rx_wr_failed;
851 unsigned int rx_head = isert_conn->conn_rx_desc_head;
852 struct iser_rx_desc *rx_desc;
854 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
855 rx_desc = &isert_conn->conn_rx_descs[rx_head];
856 rx_wr->wr_id = (unsigned long)rx_desc;
857 rx_wr->sg_list = &rx_desc->rx_sg;
859 rx_wr->next = rx_wr + 1;
860 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
864 rx_wr->next = NULL; /* mark end of work requests list */
866 isert_conn->post_recv_buf_count += count;
867 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
870 pr_err("ib_post_recv() failed with ret: %d\n", ret);
871 isert_conn->post_recv_buf_count -= count;
873 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
874 isert_conn->conn_rx_desc_head = rx_head;
880 isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
882 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
883 struct ib_send_wr send_wr, *send_wr_failed;
886 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
887 ISER_HEADERS_LEN, DMA_TO_DEVICE);
890 send_wr.wr_id = (unsigned long)tx_desc;
891 send_wr.sg_list = tx_desc->tx_sg;
892 send_wr.num_sge = tx_desc->num_sge;
893 send_wr.opcode = IB_WR_SEND;
894 send_wr.send_flags = IB_SEND_SIGNALED;
896 atomic_inc(&isert_conn->post_send_buf_count);
898 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
900 pr_err("ib_post_send() failed, ret: %d\n", ret);
901 atomic_dec(&isert_conn->post_send_buf_count);
908 isert_create_send_desc(struct isert_conn *isert_conn,
909 struct isert_cmd *isert_cmd,
910 struct iser_tx_desc *tx_desc)
912 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
914 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
915 ISER_HEADERS_LEN, DMA_TO_DEVICE);
917 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
918 tx_desc->iser_header.flags = ISER_VER;
920 tx_desc->num_sge = 1;
921 tx_desc->isert_cmd = isert_cmd;
923 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
924 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
925 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
930 isert_init_tx_hdrs(struct isert_conn *isert_conn,
931 struct iser_tx_desc *tx_desc)
933 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
936 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
937 ISER_HEADERS_LEN, DMA_TO_DEVICE);
938 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
939 pr_err("ib_dma_mapping_error() failed\n");
943 tx_desc->dma_addr = dma_addr;
944 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
945 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
946 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
948 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
949 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
950 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
956 isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
957 struct ib_send_wr *send_wr, bool coalesce)
959 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
961 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
962 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
963 send_wr->opcode = IB_WR_SEND;
964 send_wr->sg_list = &tx_desc->tx_sg[0];
965 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
967 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
968 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
970 mutex_lock(&isert_conn->conn_mutex);
971 if (coalesce && isert_conn->state == ISER_CONN_UP &&
972 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
973 tx_desc->llnode_active = true;
974 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
975 mutex_unlock(&isert_conn->conn_mutex);
978 isert_conn->conn_comp_batch = 0;
979 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
980 mutex_unlock(&isert_conn->conn_mutex);
982 send_wr->send_flags = IB_SEND_SIGNALED;
986 isert_rdma_post_recvl(struct isert_conn *isert_conn)
988 struct ib_recv_wr rx_wr, *rx_wr_fail;
992 memset(&sge, 0, sizeof(struct ib_sge));
993 sge.addr = isert_conn->login_req_dma;
994 sge.length = ISER_RX_LOGIN_SIZE;
995 sge.lkey = isert_conn->conn_mr->lkey;
997 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
998 sge.addr, sge.length, sge.lkey);
1000 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1001 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1002 rx_wr.sg_list = &sge;
1005 isert_conn->post_recv_buf_count++;
1006 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1008 pr_err("ib_post_recv() failed: %d\n", ret);
1009 isert_conn->post_recv_buf_count--;
1012 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1017 isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1020 struct isert_conn *isert_conn = conn->context;
1021 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1022 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1025 isert_create_send_desc(isert_conn, NULL, tx_desc);
1027 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1028 sizeof(struct iscsi_hdr));
1030 isert_init_tx_hdrs(isert_conn, tx_desc);
1033 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1035 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1036 length, DMA_TO_DEVICE);
1038 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1040 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1041 length, DMA_TO_DEVICE);
1043 tx_dsg->addr = isert_conn->login_rsp_dma;
1044 tx_dsg->length = length;
1045 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1046 tx_desc->num_sge = 2;
1048 if (!login->login_failed) {
1049 if (login->login_complete) {
1050 ret = isert_alloc_rx_descriptors(isert_conn);
1054 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1058 isert_conn->state = ISER_CONN_UP;
1062 ret = isert_rdma_post_recvl(isert_conn);
1067 ret = isert_post_send(isert_conn, tx_desc);
1075 isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1076 struct isert_conn *isert_conn)
1078 struct iscsi_conn *conn = isert_conn->conn;
1079 struct iscsi_login *login = conn->conn_login;
1083 pr_err("conn->conn_login is NULL\n");
1088 if (login->first_request) {
1089 struct iscsi_login_req *login_req =
1090 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1092 * Setup the initial iscsi_login values from the leading
1093 * login request PDU.
1095 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1096 login->current_stage =
1097 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1099 login->version_min = login_req->min_version;
1100 login->version_max = login_req->max_version;
1101 memcpy(login->isid, login_req->isid, 6);
1102 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1103 login->init_task_tag = login_req->itt;
1104 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1105 login->cid = be16_to_cpu(login_req->cid);
1106 login->tsih = be16_to_cpu(login_req->tsih);
1109 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1111 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1112 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1113 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1114 memcpy(login->req_buf, &rx_desc->data[0], size);
1116 if (login->first_request) {
1117 complete(&isert_conn->conn_login_comp);
1120 schedule_delayed_work(&conn->login_work, 0);
1123 static struct iscsi_cmd
1124 *isert_allocate_cmd(struct iscsi_conn *conn)
1126 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1127 struct isert_cmd *isert_cmd;
1128 struct iscsi_cmd *cmd;
1130 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
1132 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1135 isert_cmd = iscsit_priv_cmd(cmd);
1136 isert_cmd->conn = isert_conn;
1137 isert_cmd->iscsi_cmd = cmd;
1143 isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1144 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1145 struct iser_rx_desc *rx_desc, unsigned char *buf)
1147 struct iscsi_conn *conn = isert_conn->conn;
1148 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1149 struct scatterlist *sg;
1150 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1151 bool dump_payload = false;
1153 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1157 imm_data = cmd->immediate_data;
1158 imm_data_len = cmd->first_burst_len;
1159 unsol_data = cmd->unsolicited_data;
1161 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1164 } else if (rc > 0) {
1165 dump_payload = true;
1172 sg = &cmd->se_cmd.t_data_sg[0];
1173 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1175 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1176 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1178 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1180 cmd->write_data_done += imm_data_len;
1182 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1183 spin_lock_bh(&cmd->istate_lock);
1184 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1185 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1186 spin_unlock_bh(&cmd->istate_lock);
1190 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1192 if (!rc && dump_payload == false && unsol_data)
1193 iscsit_set_unsoliticed_dataout(cmd);
1199 isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1200 struct iser_rx_desc *rx_desc, unsigned char *buf)
1202 struct scatterlist *sg_start;
1203 struct iscsi_conn *conn = isert_conn->conn;
1204 struct iscsi_cmd *cmd = NULL;
1205 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1206 u32 unsol_data_len = ntoh24(hdr->dlength);
1207 int rc, sg_nents, sg_off, page_off;
1209 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1215 * FIXME: Unexpected unsolicited_data out
1217 if (!cmd->unsolicited_data) {
1218 pr_err("Received unexpected solicited data payload\n");
1223 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1224 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1226 sg_off = cmd->write_data_done / PAGE_SIZE;
1227 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1228 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1229 page_off = cmd->write_data_done % PAGE_SIZE;
1231 * FIXME: Non page-aligned unsolicited_data out
1234 pr_err("Received unexpected non-page aligned data payload\n");
1238 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1239 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1241 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1244 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1252 isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1253 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1256 struct iscsi_conn *conn = isert_conn->conn;
1257 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1260 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1264 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1267 return iscsit_process_nop_out(conn, cmd, hdr);
1271 isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1272 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1273 struct iscsi_text *hdr)
1275 struct iscsi_conn *conn = isert_conn->conn;
1276 u32 payload_length = ntoh24(hdr->dlength);
1278 unsigned char *text_in;
1280 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1284 text_in = kzalloc(payload_length, GFP_KERNEL);
1286 pr_err("Unable to allocate text_in of payload_length: %u\n",
1290 cmd->text_in_ptr = text_in;
1292 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1294 return iscsit_process_text_cmd(conn, cmd, hdr);
1298 isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1299 uint32_t read_stag, uint64_t read_va,
1300 uint32_t write_stag, uint64_t write_va)
1302 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1303 struct iscsi_conn *conn = isert_conn->conn;
1304 struct iscsi_session *sess = conn->sess;
1305 struct iscsi_cmd *cmd;
1306 struct isert_cmd *isert_cmd;
1308 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1310 if (sess->sess_ops->SessionType &&
1311 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1312 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1313 " ignoring\n", opcode);
1318 case ISCSI_OP_SCSI_CMD:
1319 cmd = isert_allocate_cmd(conn);
1323 isert_cmd = iscsit_priv_cmd(cmd);
1324 isert_cmd->read_stag = read_stag;
1325 isert_cmd->read_va = read_va;
1326 isert_cmd->write_stag = write_stag;
1327 isert_cmd->write_va = write_va;
1329 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1330 rx_desc, (unsigned char *)hdr);
1332 case ISCSI_OP_NOOP_OUT:
1333 cmd = isert_allocate_cmd(conn);
1337 isert_cmd = iscsit_priv_cmd(cmd);
1338 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1339 rx_desc, (unsigned char *)hdr);
1341 case ISCSI_OP_SCSI_DATA_OUT:
1342 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1343 (unsigned char *)hdr);
1345 case ISCSI_OP_SCSI_TMFUNC:
1346 cmd = isert_allocate_cmd(conn);
1350 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1351 (unsigned char *)hdr);
1353 case ISCSI_OP_LOGOUT:
1354 cmd = isert_allocate_cmd(conn);
1358 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1360 wait_for_completion_timeout(&conn->conn_logout_comp,
1361 SECONDS_FOR_LOGOUT_COMP *
1365 cmd = isert_allocate_cmd(conn);
1369 isert_cmd = iscsit_priv_cmd(cmd);
1370 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1371 rx_desc, (struct iscsi_text *)hdr);
1374 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1383 isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1385 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1386 uint64_t read_va = 0, write_va = 0;
1387 uint32_t read_stag = 0, write_stag = 0;
1390 switch (iser_hdr->flags & 0xF0) {
1392 if (iser_hdr->flags & ISER_RSV) {
1393 read_stag = be32_to_cpu(iser_hdr->read_stag);
1394 read_va = be64_to_cpu(iser_hdr->read_va);
1395 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1396 read_stag, (unsigned long long)read_va);
1398 if (iser_hdr->flags & ISER_WSV) {
1399 write_stag = be32_to_cpu(iser_hdr->write_stag);
1400 write_va = be64_to_cpu(iser_hdr->write_va);
1401 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1402 write_stag, (unsigned long long)write_va);
1405 pr_debug("ISER ISCSI_CTRL PDU\n");
1408 pr_err("iSER Hello message\n");
1411 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1415 rc = isert_rx_opcode(isert_conn, rx_desc,
1416 read_stag, read_va, write_stag, write_va);
1420 isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1421 unsigned long xfer_len)
1423 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1424 struct iscsi_hdr *hdr;
1426 int rx_buflen, outstanding;
1428 if ((char *)desc == isert_conn->login_req_buf) {
1429 rx_dma = isert_conn->login_req_dma;
1430 rx_buflen = ISER_RX_LOGIN_SIZE;
1431 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1434 rx_dma = desc->dma_addr;
1435 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1436 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1440 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1442 hdr = &desc->iscsi_header;
1443 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1444 hdr->opcode, hdr->itt, hdr->flags,
1445 (int)(xfer_len - ISER_HEADERS_LEN));
1447 if ((char *)desc == isert_conn->login_req_buf)
1448 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1451 isert_rx_do_work(desc, isert_conn);
1453 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1456 isert_conn->post_recv_buf_count--;
1457 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1458 isert_conn->post_recv_buf_count);
1460 if ((char *)desc == isert_conn->login_req_buf)
1463 outstanding = isert_conn->post_recv_buf_count;
1464 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1465 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1466 ISERT_MIN_POSTED_RX);
1467 err = isert_post_recv(isert_conn, count);
1469 pr_err("isert_post_recv() count: %d failed, %d\n",
1476 isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1477 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1478 enum iser_ib_op_code op, struct isert_data_buf *data)
1480 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1482 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1483 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1485 data->len = length - offset;
1486 data->offset = offset;
1487 data->sg_off = data->offset / PAGE_SIZE;
1489 data->sg = &sg[data->sg_off];
1490 data->nents = min_t(unsigned int, nents - data->sg_off,
1491 ISCSI_ISER_SG_TABLESIZE);
1492 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1495 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1497 if (unlikely(!data->dma_nents)) {
1498 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1502 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1503 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1509 isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1511 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1513 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1514 memset(data, 0, sizeof(*data));
1520 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1522 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1524 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1527 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1528 isert_unmap_data_buf(isert_conn, &wr->data);
1532 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1538 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1545 isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1547 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1548 LIST_HEAD(unmap_list);
1550 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
1553 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
1554 isert_cmd, wr->fr_desc);
1555 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1556 isert_unmap_data_buf(isert_conn, &wr->prot);
1557 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1559 spin_lock_bh(&isert_conn->conn_lock);
1560 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
1561 spin_unlock_bh(&isert_conn->conn_lock);
1566 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
1567 isert_unmap_data_buf(isert_conn, &wr->data);
1575 isert_put_cmd(struct isert_cmd *isert_cmd)
1577 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1578 struct isert_conn *isert_conn = isert_cmd->conn;
1579 struct iscsi_conn *conn = isert_conn->conn;
1580 struct isert_device *device = isert_conn->conn_device;
1582 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1584 switch (cmd->iscsi_opcode) {
1585 case ISCSI_OP_SCSI_CMD:
1586 spin_lock_bh(&conn->cmd_lock);
1587 if (!list_empty(&cmd->i_conn_node))
1588 list_del_init(&cmd->i_conn_node);
1589 spin_unlock_bh(&conn->cmd_lock);
1591 if (cmd->data_direction == DMA_TO_DEVICE)
1592 iscsit_stop_dataout_timer(cmd);
1594 device->unreg_rdma_mem(isert_cmd, isert_conn);
1595 transport_generic_free_cmd(&cmd->se_cmd, 0);
1597 case ISCSI_OP_SCSI_TMFUNC:
1598 spin_lock_bh(&conn->cmd_lock);
1599 if (!list_empty(&cmd->i_conn_node))
1600 list_del_init(&cmd->i_conn_node);
1601 spin_unlock_bh(&conn->cmd_lock);
1603 transport_generic_free_cmd(&cmd->se_cmd, 0);
1605 case ISCSI_OP_REJECT:
1606 case ISCSI_OP_NOOP_OUT:
1608 spin_lock_bh(&conn->cmd_lock);
1609 if (!list_empty(&cmd->i_conn_node))
1610 list_del_init(&cmd->i_conn_node);
1611 spin_unlock_bh(&conn->cmd_lock);
1614 * Handle special case for REJECT when iscsi_add_reject*() has
1615 * overwritten the original iscsi_opcode assignment, and the
1616 * associated cmd->se_cmd needs to be released.
1618 if (cmd->se_cmd.se_tfo != NULL) {
1619 pr_debug("Calling transport_generic_free_cmd from"
1620 " isert_put_cmd for 0x%02x\n",
1622 transport_generic_free_cmd(&cmd->se_cmd, 0);
1629 iscsit_release_cmd(cmd);
1635 isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1637 if (tx_desc->dma_addr != 0) {
1638 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1639 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1640 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1641 tx_desc->dma_addr = 0;
1646 isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1647 struct ib_device *ib_dev)
1649 if (isert_cmd->pdu_buf_dma != 0) {
1650 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1651 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1652 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1653 isert_cmd->pdu_buf_dma = 0;
1656 isert_unmap_tx_desc(tx_desc, ib_dev);
1657 isert_put_cmd(isert_cmd);
1661 isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1662 struct isert_cmd *isert_cmd)
1664 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1665 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1666 struct se_cmd *se_cmd = &cmd->se_cmd;
1667 struct isert_conn *isert_conn = isert_cmd->conn;
1668 struct isert_device *device = isert_conn->conn_device;
1669 struct ib_mr_status mr_status;
1672 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
1673 ret = ib_check_mr_status(wr->fr_desc->pi_ctx->sig_mr,
1674 IB_MR_CHECK_SIG_STATUS, &mr_status);
1676 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1677 goto fail_mr_status;
1679 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1680 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1682 switch (mr_status.sig_err.err_type) {
1683 case IB_SIG_BAD_GUARD:
1684 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1686 case IB_SIG_BAD_REFTAG:
1687 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1689 case IB_SIG_BAD_APPTAG:
1690 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1693 se_cmd->bad_sector = mr_status.sig_err.sig_err_offset;
1694 do_div(se_cmd->bad_sector, block_size);
1696 pr_err("isert: PI error found type %d at sector 0x%llx "
1697 "expected 0x%x vs actual 0x%x\n",
1698 mr_status.sig_err.err_type,
1699 (unsigned long long)se_cmd->bad_sector,
1700 mr_status.sig_err.expected,
1701 mr_status.sig_err.actual);
1707 device->unreg_rdma_mem(isert_cmd, isert_conn);
1709 transport_send_check_condition_and_sense(se_cmd,
1712 isert_put_response(isert_conn->conn, cmd);
1716 isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1717 struct isert_cmd *isert_cmd)
1719 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1720 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1721 struct se_cmd *se_cmd = &cmd->se_cmd;
1722 struct isert_conn *isert_conn = isert_cmd->conn;
1723 struct isert_device *device = isert_conn->conn_device;
1724 struct ib_mr_status mr_status;
1727 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
1728 ret = ib_check_mr_status(wr->fr_desc->pi_ctx->sig_mr,
1729 IB_MR_CHECK_SIG_STATUS, &mr_status);
1731 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1732 goto fail_mr_status;
1734 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1735 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1737 switch (mr_status.sig_err.err_type) {
1738 case IB_SIG_BAD_GUARD:
1739 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1741 case IB_SIG_BAD_REFTAG:
1742 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1744 case IB_SIG_BAD_APPTAG:
1745 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1748 se_cmd->bad_sector = mr_status.sig_err.sig_err_offset;
1749 do_div(se_cmd->bad_sector, block_size);
1751 pr_err("isert: PI error found type %d at sector 0x%llx "
1752 "expected 0x%x vs actual 0x%x\n",
1753 mr_status.sig_err.err_type,
1754 (unsigned long long)se_cmd->bad_sector,
1755 mr_status.sig_err.expected,
1756 mr_status.sig_err.actual);
1761 iscsit_stop_dataout_timer(cmd);
1762 device->unreg_rdma_mem(isert_cmd, isert_conn);
1763 cmd->write_data_done = wr->data.len;
1764 wr->send_wr_num = 0;
1766 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1767 spin_lock_bh(&cmd->istate_lock);
1768 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1769 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1770 spin_unlock_bh(&cmd->istate_lock);
1772 target_execute_cmd(se_cmd);
1776 isert_do_control_comp(struct work_struct *work)
1778 struct isert_cmd *isert_cmd = container_of(work,
1779 struct isert_cmd, comp_work);
1780 struct isert_conn *isert_conn = isert_cmd->conn;
1781 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1782 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1784 switch (cmd->i_state) {
1785 case ISTATE_SEND_TASKMGTRSP:
1786 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1788 atomic_dec(&isert_conn->post_send_buf_count);
1789 iscsit_tmr_post_handler(cmd, cmd->conn);
1791 cmd->i_state = ISTATE_SENT_STATUS;
1792 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1794 case ISTATE_SEND_REJECT:
1795 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1796 atomic_dec(&isert_conn->post_send_buf_count);
1798 cmd->i_state = ISTATE_SENT_STATUS;
1799 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1801 case ISTATE_SEND_LOGOUTRSP:
1802 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1804 * Call atomic_dec(&isert_conn->post_send_buf_count)
1805 * from isert_wait_conn()
1807 isert_conn->logout_posted = true;
1808 iscsit_logout_post_handler(cmd, cmd->conn);
1810 case ISTATE_SEND_TEXTRSP:
1811 atomic_dec(&isert_conn->post_send_buf_count);
1812 cmd->i_state = ISTATE_SENT_STATUS;
1813 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1816 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1823 isert_response_completion(struct iser_tx_desc *tx_desc,
1824 struct isert_cmd *isert_cmd,
1825 struct isert_conn *isert_conn,
1826 struct ib_device *ib_dev)
1828 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1829 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1831 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
1832 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1833 cmd->i_state == ISTATE_SEND_REJECT ||
1834 cmd->i_state == ISTATE_SEND_TEXTRSP) {
1835 isert_unmap_tx_desc(tx_desc, ib_dev);
1837 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1838 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1841 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1843 cmd->i_state = ISTATE_SENT_STATUS;
1844 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1848 __isert_send_completion(struct iser_tx_desc *tx_desc,
1849 struct isert_conn *isert_conn)
1851 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1852 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1853 struct isert_rdma_wr *wr;
1856 atomic_dec(&isert_conn->post_send_buf_count);
1857 isert_unmap_tx_desc(tx_desc, ib_dev);
1860 wr = &isert_cmd->rdma_wr;
1862 switch (wr->iser_ib_op) {
1864 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1868 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1869 isert_response_completion(tx_desc, isert_cmd,
1870 isert_conn, ib_dev);
1872 case ISER_IB_RDMA_WRITE:
1873 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1874 atomic_dec(&isert_conn->post_send_buf_count);
1875 isert_completion_rdma_write(tx_desc, isert_cmd);
1877 case ISER_IB_RDMA_READ:
1878 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1880 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1881 isert_completion_rdma_read(tx_desc, isert_cmd);
1884 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1891 isert_send_completion(struct iser_tx_desc *tx_desc,
1892 struct isert_conn *isert_conn)
1894 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1895 struct iser_tx_desc *t;
1897 * Drain coalesced completion llist starting from comp_llnode_batch
1898 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1901 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1902 llnode = llist_next(llnode);
1903 __isert_send_completion(t, isert_conn);
1905 __isert_send_completion(tx_desc, isert_conn);
1909 isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1911 struct llist_node *llnode;
1912 struct isert_rdma_wr *wr;
1913 struct iser_tx_desc *t;
1915 mutex_lock(&isert_conn->conn_mutex);
1916 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1917 isert_conn->conn_comp_batch = 0;
1918 mutex_unlock(&isert_conn->conn_mutex);
1921 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1922 llnode = llist_next(llnode);
1923 wr = &t->isert_cmd->rdma_wr;
1925 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1926 isert_completion_put(t, t->isert_cmd, ib_dev);
1931 isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1933 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1934 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1935 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1936 struct isert_rdma_wr *wr;
1937 struct iser_tx_desc *t;
1940 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1941 llnode = llist_next(llnode);
1942 wr = &t->isert_cmd->rdma_wr;
1944 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1945 isert_completion_put(t, t->isert_cmd, ib_dev);
1947 tx_desc->comp_llnode_batch = NULL;
1950 isert_unmap_tx_desc(tx_desc, ib_dev);
1952 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1956 isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1958 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1959 struct iscsi_conn *conn = isert_conn->conn;
1961 if (isert_conn->post_recv_buf_count)
1964 isert_cq_drain_comp_llist(isert_conn, ib_dev);
1967 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1968 target_wait_for_sess_cmds(conn->sess->se_sess);
1971 while (atomic_read(&isert_conn->post_send_buf_count))
1974 mutex_lock(&isert_conn->conn_mutex);
1975 isert_conn->state = ISER_CONN_DOWN;
1976 mutex_unlock(&isert_conn->conn_mutex);
1978 complete(&isert_conn->conn_wait_comp_err);
1982 isert_cq_tx_work(struct work_struct *work)
1984 struct isert_cq_desc *cq_desc = container_of(work,
1985 struct isert_cq_desc, cq_tx_work);
1986 struct isert_device *device = cq_desc->device;
1987 int cq_index = cq_desc->cq_index;
1988 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1989 struct isert_conn *isert_conn;
1990 struct iser_tx_desc *tx_desc;
1993 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1994 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1995 isert_conn = wc.qp->qp_context;
1997 if (wc.status == IB_WC_SUCCESS) {
1998 isert_send_completion(tx_desc, isert_conn);
2000 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2001 pr_debug("TX wc.status: 0x%08x\n", wc.status);
2002 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
2004 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
2005 if (tx_desc->llnode_active)
2008 atomic_dec(&isert_conn->post_send_buf_count);
2009 isert_cq_tx_comp_err(tx_desc, isert_conn);
2014 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2018 isert_cq_tx_callback(struct ib_cq *cq, void *context)
2020 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2022 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2026 isert_cq_rx_work(struct work_struct *work)
2028 struct isert_cq_desc *cq_desc = container_of(work,
2029 struct isert_cq_desc, cq_rx_work);
2030 struct isert_device *device = cq_desc->device;
2031 int cq_index = cq_desc->cq_index;
2032 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2033 struct isert_conn *isert_conn;
2034 struct iser_rx_desc *rx_desc;
2036 unsigned long xfer_len;
2038 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2039 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2040 isert_conn = wc.qp->qp_context;
2042 if (wc.status == IB_WC_SUCCESS) {
2043 xfer_len = (unsigned long)wc.byte_len;
2044 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2046 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2047 if (wc.status != IB_WC_WR_FLUSH_ERR) {
2048 pr_debug("RX wc.status: 0x%08x\n", wc.status);
2049 pr_debug("RX wc.vendor_err: 0x%08x\n",
2052 isert_conn->post_recv_buf_count--;
2053 isert_cq_rx_comp_err(isert_conn);
2057 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2061 isert_cq_rx_callback(struct ib_cq *cq, void *context)
2063 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2065 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2069 isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2071 struct ib_send_wr *wr_failed;
2074 atomic_inc(&isert_conn->post_send_buf_count);
2076 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2079 pr_err("ib_post_send failed with %d\n", ret);
2080 atomic_dec(&isert_conn->post_send_buf_count);
2087 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2089 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2090 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2091 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2092 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2093 &isert_cmd->tx_desc.iscsi_header;
2095 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2096 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2097 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2099 * Attach SENSE DATA payload to iSCSI Response PDU
2101 if (cmd->se_cmd.sense_buffer &&
2102 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2103 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2104 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2105 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2106 u32 padding, pdu_len;
2108 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2110 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2112 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2113 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
2114 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
2116 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2117 (void *)cmd->sense_buffer, pdu_len,
2120 isert_cmd->pdu_buf_len = pdu_len;
2121 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2122 tx_dsg->length = pdu_len;
2123 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2124 isert_cmd->tx_desc.num_sge = 2;
2127 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
2129 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2131 return isert_post_response(isert_conn, isert_cmd);
2135 isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2136 bool nopout_response)
2138 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2139 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2140 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2142 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2143 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2144 &isert_cmd->tx_desc.iscsi_header,
2146 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2147 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2149 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2151 return isert_post_response(isert_conn, isert_cmd);
2155 isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2157 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2158 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2159 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2161 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2162 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2163 &isert_cmd->tx_desc.iscsi_header);
2164 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2165 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2167 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2169 return isert_post_response(isert_conn, isert_cmd);
2173 isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2175 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2176 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2177 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2179 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2180 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2181 &isert_cmd->tx_desc.iscsi_header);
2182 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2183 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2185 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2187 return isert_post_response(isert_conn, isert_cmd);
2191 isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2193 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2194 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2195 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2196 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2197 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2198 struct iscsi_reject *hdr =
2199 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
2201 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2202 iscsit_build_reject(cmd, conn, hdr);
2203 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2205 hton24(hdr->dlength, ISCSI_HDR_LEN);
2206 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2207 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2209 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2210 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2211 tx_dsg->length = ISCSI_HDR_LEN;
2212 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2213 isert_cmd->tx_desc.num_sge = 2;
2215 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2217 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2219 return isert_post_response(isert_conn, isert_cmd);
2223 isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2225 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2226 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2227 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2228 struct iscsi_text_rsp *hdr =
2229 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2233 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2234 rc = iscsit_build_text_rsp(cmd, conn, hdr);
2239 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2242 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2243 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2244 void *txt_rsp_buf = cmd->buf_ptr;
2246 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2247 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2249 isert_cmd->pdu_buf_len = txt_rsp_len;
2250 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2251 tx_dsg->length = txt_rsp_len;
2252 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2253 isert_cmd->tx_desc.num_sge = 2;
2255 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
2257 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2259 return isert_post_response(isert_conn, isert_cmd);
2263 isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2264 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2265 u32 data_left, u32 offset)
2267 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2268 struct scatterlist *sg_start, *tmp_sg;
2269 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2270 u32 sg_off, page_off;
2271 int i = 0, sg_nents;
2273 sg_off = offset / PAGE_SIZE;
2274 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2275 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2276 page_off = offset % PAGE_SIZE;
2278 send_wr->sg_list = ib_sge;
2279 send_wr->num_sge = sg_nents;
2280 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2282 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2284 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2285 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2286 (unsigned long long)tmp_sg->dma_address,
2287 tmp_sg->length, page_off);
2289 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2290 ib_sge->length = min_t(u32, data_left,
2291 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2292 ib_sge->lkey = isert_conn->conn_mr->lkey;
2294 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2295 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2297 data_left -= ib_sge->length;
2299 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2302 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2303 send_wr->sg_list, send_wr->num_sge);
2309 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2310 struct isert_rdma_wr *wr)
2312 struct se_cmd *se_cmd = &cmd->se_cmd;
2313 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2314 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2315 struct isert_data_buf *data = &wr->data;
2316 struct ib_send_wr *send_wr;
2317 struct ib_sge *ib_sge;
2318 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2319 int ret = 0, i, ib_sge_cnt;
2321 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2323 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2324 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2325 se_cmd->t_data_nents, se_cmd->data_length,
2326 offset, wr->iser_ib_op, &wr->data);
2330 data_left = data->len;
2331 offset = data->offset;
2333 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
2335 pr_warn("Unable to allocate ib_sge\n");
2339 wr->ib_sge = ib_sge;
2341 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
2342 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2345 pr_debug("Unable to allocate wr->send_wr\n");
2350 wr->isert_cmd = isert_cmd;
2351 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2353 for (i = 0; i < wr->send_wr_num; i++) {
2354 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2355 data_len = min(data_left, rdma_write_max);
2357 send_wr->send_flags = 0;
2358 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2359 send_wr->opcode = IB_WR_RDMA_WRITE;
2360 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2361 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2362 if (i + 1 == wr->send_wr_num)
2363 send_wr->next = &isert_cmd->tx_desc.send_wr;
2365 send_wr->next = &wr->send_wr[i + 1];
2367 send_wr->opcode = IB_WR_RDMA_READ;
2368 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2369 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2370 if (i + 1 == wr->send_wr_num)
2371 send_wr->send_flags = IB_SEND_SIGNALED;
2373 send_wr->next = &wr->send_wr[i + 1];
2376 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2377 send_wr, data_len, offset);
2378 ib_sge += ib_sge_cnt;
2381 va_offset += data_len;
2382 data_left -= data_len;
2387 isert_unmap_data_buf(isert_conn, data);
2393 isert_map_fr_pagelist(struct ib_device *ib_dev,
2394 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2396 u64 start_addr, end_addr, page, chunk_start = 0;
2397 struct scatterlist *tmp_sg;
2398 int i = 0, new_chunk, last_ent, n_pages;
2402 last_ent = sg_nents - 1;
2403 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2404 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2406 chunk_start = start_addr;
2407 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2409 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2410 i, (unsigned long long)tmp_sg->dma_address,
2413 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2419 page = chunk_start & PAGE_MASK;
2421 fr_pl[n_pages++] = page;
2422 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2425 } while (page < end_addr);
2432 isert_fast_reg_mr(struct isert_conn *isert_conn,
2433 struct fast_reg_descriptor *fr_desc,
2434 struct isert_data_buf *mem,
2435 enum isert_indicator ind,
2438 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2440 struct ib_fast_reg_page_list *frpl;
2441 struct ib_send_wr fr_wr, inv_wr;
2442 struct ib_send_wr *bad_wr, *wr = NULL;
2443 int ret, pagelist_len;
2447 if (mem->dma_nents == 1) {
2448 sge->lkey = isert_conn->conn_mr->lkey;
2449 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2450 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
2451 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2452 __func__, __LINE__, sge->addr, sge->length,
2457 if (ind == ISERT_DATA_KEY_VALID) {
2458 /* Registering data buffer */
2459 mr = fr_desc->data_mr;
2460 frpl = fr_desc->data_frpl;
2462 /* Registering protection buffer */
2463 mr = fr_desc->pi_ctx->prot_mr;
2464 frpl = fr_desc->pi_ctx->prot_frpl;
2467 page_off = mem->offset % PAGE_SIZE;
2469 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2470 fr_desc, mem->nents, mem->offset);
2472 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
2473 &frpl->page_list[0]);
2475 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
2476 memset(&inv_wr, 0, sizeof(inv_wr));
2477 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
2478 inv_wr.opcode = IB_WR_LOCAL_INV;
2479 inv_wr.ex.invalidate_rkey = mr->rkey;
2482 key = (u8)(mr->rkey & 0x000000FF);
2483 ib_update_fast_reg_key(mr, ++key);
2486 /* Prepare FASTREG WR */
2487 memset(&fr_wr, 0, sizeof(fr_wr));
2488 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
2489 fr_wr.opcode = IB_WR_FAST_REG_MR;
2490 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2491 fr_wr.wr.fast_reg.page_list = frpl;
2492 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2493 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2494 fr_wr.wr.fast_reg.length = mem->len;
2495 fr_wr.wr.fast_reg.rkey = mr->rkey;
2496 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2503 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2505 pr_err("fast registration failed, ret:%d\n", ret);
2508 fr_desc->ind &= ~ind;
2510 sge->lkey = mr->lkey;
2511 sge->addr = frpl->page_list[0] + page_off;
2512 sge->length = mem->len;
2514 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2515 __func__, __LINE__, sge->addr, sge->length,
2521 static inline enum ib_t10_dif_type
2522 se2ib_prot_type(enum target_prot_type prot_type)
2524 switch (prot_type) {
2525 case TARGET_DIF_TYPE0_PROT:
2526 return IB_T10DIF_NONE;
2527 case TARGET_DIF_TYPE1_PROT:
2528 return IB_T10DIF_TYPE1;
2529 case TARGET_DIF_TYPE2_PROT:
2530 return IB_T10DIF_TYPE2;
2531 case TARGET_DIF_TYPE3_PROT:
2532 return IB_T10DIF_TYPE3;
2534 return IB_T10DIF_NONE;
2539 isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2541 enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
2543 sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
2544 sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
2545 sig_attrs->mem.sig.dif.pi_interval =
2546 se_cmd->se_dev->dev_attrib.block_size;
2547 sig_attrs->wire.sig.dif.pi_interval =
2548 se_cmd->se_dev->dev_attrib.block_size;
2550 switch (se_cmd->prot_op) {
2551 case TARGET_PROT_DIN_INSERT:
2552 case TARGET_PROT_DOUT_STRIP:
2553 sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
2554 sig_attrs->wire.sig.dif.type = ib_prot_type;
2555 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2556 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2558 case TARGET_PROT_DOUT_INSERT:
2559 case TARGET_PROT_DIN_STRIP:
2560 sig_attrs->mem.sig.dif.type = ib_prot_type;
2561 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2562 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2563 sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
2565 case TARGET_PROT_DIN_PASS:
2566 case TARGET_PROT_DOUT_PASS:
2567 sig_attrs->mem.sig.dif.type = ib_prot_type;
2568 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2569 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2570 sig_attrs->wire.sig.dif.type = ib_prot_type;
2571 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2572 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2575 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2583 isert_set_prot_checks(u8 prot_checks)
2585 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2586 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2587 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2591 isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2592 struct fast_reg_descriptor *fr_desc,
2593 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2594 struct ib_sge *sig_sge)
2596 struct ib_send_wr sig_wr, inv_wr;
2597 struct ib_send_wr *bad_wr, *wr = NULL;
2598 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2599 struct ib_sig_attrs sig_attrs;
2603 memset(&sig_attrs, 0, sizeof(sig_attrs));
2604 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2608 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2610 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2611 memset(&inv_wr, 0, sizeof(inv_wr));
2612 inv_wr.opcode = IB_WR_LOCAL_INV;
2613 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2616 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2617 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2620 memset(&sig_wr, 0, sizeof(sig_wr));
2621 sig_wr.opcode = IB_WR_REG_SIG_MR;
2622 sig_wr.sg_list = data_sge;
2624 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2625 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2626 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2627 if (se_cmd->t_prot_sg)
2628 sig_wr.wr.sig_handover.prot = prot_sge;
2635 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2637 pr_err("fast registration failed, ret:%d\n", ret);
2640 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2642 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2644 sig_sge->length = se_cmd->data_length;
2645 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2646 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2648 * We have protection guards on the wire
2649 * so we need to set a larget transfer
2651 sig_sge->length += se_cmd->prot_length;
2653 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2654 sig_sge->addr, sig_sge->length,
2661 isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2662 struct isert_rdma_wr *wr)
2664 struct se_cmd *se_cmd = &cmd->se_cmd;
2665 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2666 struct isert_conn *isert_conn = conn->context;
2667 struct ib_sge data_sge;
2668 struct ib_send_wr *send_wr;
2669 struct fast_reg_descriptor *fr_desc = NULL;
2672 unsigned long flags;
2674 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2676 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2677 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2678 se_cmd->t_data_nents, se_cmd->data_length,
2679 offset, wr->iser_ib_op, &wr->data);
2683 if (wr->data.dma_nents != 1 ||
2684 se_cmd->prot_op != TARGET_PROT_NORMAL) {
2685 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2686 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2687 struct fast_reg_descriptor, list);
2688 list_del(&fr_desc->list);
2689 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2690 wr->fr_desc = fr_desc;
2693 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2694 ISERT_DATA_KEY_VALID, &data_sge);
2698 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2699 struct ib_sge prot_sge, sig_sge;
2701 if (se_cmd->t_prot_sg) {
2702 ret = isert_map_data_buf(isert_conn, isert_cmd,
2704 se_cmd->t_prot_nents,
2705 se_cmd->prot_length,
2706 0, wr->iser_ib_op, &wr->prot);
2710 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2711 ISERT_PROT_KEY_VALID, &prot_sge);
2713 goto unmap_prot_cmd;
2716 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2717 &data_sge, &prot_sge, &sig_sge);
2719 goto unmap_prot_cmd;
2721 fr_desc->ind |= ISERT_PROTECTED;
2722 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2724 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2726 wr->ib_sge = &wr->s_ib_sge;
2727 wr->send_wr_num = 1;
2728 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2729 wr->send_wr = &wr->s_send_wr;
2730 wr->isert_cmd = isert_cmd;
2732 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2733 send_wr->sg_list = &wr->s_ib_sge;
2734 send_wr->num_sge = 1;
2735 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2736 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2737 send_wr->opcode = IB_WR_RDMA_WRITE;
2738 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2739 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2740 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2741 0 : IB_SEND_SIGNALED;
2743 send_wr->opcode = IB_WR_RDMA_READ;
2744 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2745 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2746 send_wr->send_flags = IB_SEND_SIGNALED;
2751 if (se_cmd->t_prot_sg)
2752 isert_unmap_data_buf(isert_conn, &wr->prot);
2755 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2756 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2757 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2759 isert_unmap_data_buf(isert_conn, &wr->data);
2765 isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2767 struct se_cmd *se_cmd = &cmd->se_cmd;
2768 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2769 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2770 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2771 struct isert_device *device = isert_conn->conn_device;
2772 struct ib_send_wr *wr_failed;
2775 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2776 isert_cmd, se_cmd->data_length);
2777 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2778 rc = device->reg_rdma_mem(conn, cmd, wr);
2780 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2784 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2786 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2788 isert_create_send_desc(isert_conn, isert_cmd,
2789 &isert_cmd->tx_desc);
2790 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2791 &isert_cmd->tx_desc.iscsi_header);
2792 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2793 isert_init_send_wr(isert_conn, isert_cmd,
2794 &isert_cmd->tx_desc.send_wr, true);
2795 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
2798 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2800 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2802 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2803 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
2806 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2807 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2808 "READ\n", isert_cmd);
2810 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2817 isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2819 struct se_cmd *se_cmd = &cmd->se_cmd;
2820 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2821 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2822 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2823 struct isert_device *device = isert_conn->conn_device;
2824 struct ib_send_wr *wr_failed;
2827 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2828 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2829 wr->iser_ib_op = ISER_IB_RDMA_READ;
2830 rc = device->reg_rdma_mem(conn, cmd, wr);
2832 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2836 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
2838 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2840 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2841 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
2843 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2850 isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2855 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2856 ret = isert_put_nopin(cmd, conn, false);
2859 pr_err("Unknown immediate state: 0x%02x\n", state);
2868 isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2873 case ISTATE_SEND_LOGOUTRSP:
2874 ret = isert_put_logout_rsp(cmd, conn);
2876 pr_debug("Returning iSER Logout -EAGAIN\n");
2880 case ISTATE_SEND_NOPIN:
2881 ret = isert_put_nopin(cmd, conn, true);
2883 case ISTATE_SEND_TASKMGTRSP:
2884 ret = isert_put_tm_rsp(cmd, conn);
2886 case ISTATE_SEND_REJECT:
2887 ret = isert_put_reject(cmd, conn);
2889 case ISTATE_SEND_TEXTRSP:
2890 ret = isert_put_text_rsp(cmd, conn);
2892 case ISTATE_SEND_STATUS:
2894 * Special case for sending non GOOD SCSI status from TX thread
2895 * context during pre se_cmd excecution failure.
2897 ret = isert_put_response(conn, cmd);
2900 pr_err("Unknown response state: 0x%02x\n", state);
2909 isert_setup_np(struct iscsi_np *np,
2910 struct __kernel_sockaddr_storage *ksockaddr)
2912 struct isert_np *isert_np;
2913 struct rdma_cm_id *isert_lid;
2914 struct sockaddr *sa;
2917 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2919 pr_err("Unable to allocate struct isert_np\n");
2922 init_waitqueue_head(&isert_np->np_accept_wq);
2923 mutex_init(&isert_np->np_accept_mutex);
2924 INIT_LIST_HEAD(&isert_np->np_accept_list);
2925 init_completion(&isert_np->np_login_comp);
2927 sa = (struct sockaddr *)ksockaddr;
2928 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2930 * Setup the np->np_sockaddr from the passed sockaddr setup
2931 * in iscsi_target_configfs.c code..
2933 memcpy(&np->np_sockaddr, ksockaddr,
2934 sizeof(struct __kernel_sockaddr_storage));
2936 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2938 if (IS_ERR(isert_lid)) {
2939 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2940 PTR_ERR(isert_lid));
2941 ret = PTR_ERR(isert_lid);
2945 ret = rdma_bind_addr(isert_lid, sa);
2947 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2951 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2953 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2957 isert_np->np_cm_id = isert_lid;
2958 np->np_context = isert_np;
2959 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2964 rdma_destroy_id(isert_lid);
2971 isert_check_accept_queue(struct isert_np *isert_np)
2975 mutex_lock(&isert_np->np_accept_mutex);
2976 empty = list_empty(&isert_np->np_accept_list);
2977 mutex_unlock(&isert_np->np_accept_mutex);
2983 isert_rdma_accept(struct isert_conn *isert_conn)
2985 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2986 struct rdma_conn_param cp;
2989 memset(&cp, 0, sizeof(struct rdma_conn_param));
2990 cp.responder_resources = isert_conn->responder_resources;
2991 cp.initiator_depth = isert_conn->initiator_depth;
2993 cp.rnr_retry_count = 7;
2995 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2997 ret = rdma_accept(cm_id, &cp);
2999 pr_err("rdma_accept() failed with: %d\n", ret);
3003 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3009 isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3011 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3014 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
3016 * For login requests after the first PDU, isert_rx_login_req() will
3017 * kick schedule_delayed_work(&conn->login_work) as the packet is
3018 * received, which turns this callback from iscsi_target_do_login_rx()
3021 if (!login->first_request)
3024 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3028 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3033 isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3034 struct isert_conn *isert_conn)
3036 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3037 struct rdma_route *cm_route = &cm_id->route;
3038 struct sockaddr_in *sock_in;
3039 struct sockaddr_in6 *sock_in6;
3041 conn->login_family = np->np_sockaddr.ss_family;
3043 if (np->np_sockaddr.ss_family == AF_INET6) {
3044 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3045 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3046 &sock_in6->sin6_addr.in6_u);
3047 conn->login_port = ntohs(sock_in6->sin6_port);
3049 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3050 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3051 &sock_in6->sin6_addr.in6_u);
3052 conn->local_port = ntohs(sock_in6->sin6_port);
3054 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3055 sprintf(conn->login_ip, "%pI4",
3056 &sock_in->sin_addr.s_addr);
3057 conn->login_port = ntohs(sock_in->sin_port);
3059 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3060 sprintf(conn->local_ip, "%pI4",
3061 &sock_in->sin_addr.s_addr);
3062 conn->local_port = ntohs(sock_in->sin_port);
3067 isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3069 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3070 struct isert_conn *isert_conn;
3071 int max_accept = 0, ret;
3074 ret = wait_event_interruptible(isert_np->np_accept_wq,
3075 !isert_check_accept_queue(isert_np) ||
3076 np->np_thread_state == ISCSI_NP_THREAD_RESET);
3080 spin_lock_bh(&np->np_thread_lock);
3081 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
3082 spin_unlock_bh(&np->np_thread_lock);
3083 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
3086 spin_unlock_bh(&np->np_thread_lock);
3088 mutex_lock(&isert_np->np_accept_mutex);
3089 if (list_empty(&isert_np->np_accept_list)) {
3090 mutex_unlock(&isert_np->np_accept_mutex);
3094 isert_conn = list_first_entry(&isert_np->np_accept_list,
3095 struct isert_conn, conn_accept_node);
3096 list_del_init(&isert_conn->conn_accept_node);
3097 mutex_unlock(&isert_np->np_accept_mutex);
3099 conn->context = isert_conn;
3100 isert_conn->conn = conn;
3103 ret = isert_rdma_post_recvl(isert_conn);
3107 ret = isert_rdma_accept(isert_conn);
3111 isert_set_conn_info(np, conn, isert_conn);
3113 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3118 isert_free_np(struct iscsi_np *np)
3120 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3122 rdma_destroy_id(isert_np->np_cm_id);
3124 np->np_context = NULL;
3128 static void isert_wait_conn(struct iscsi_conn *conn)
3130 struct isert_conn *isert_conn = conn->context;
3132 pr_debug("isert_wait_conn: Starting \n");
3134 * Decrement post_send_buf_count for special case when called
3135 * from isert_do_control_comp() -> iscsit_logout_post_handler()
3137 mutex_lock(&isert_conn->conn_mutex);
3138 if (isert_conn->logout_posted)
3139 atomic_dec(&isert_conn->post_send_buf_count);
3141 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
3142 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
3143 rdma_disconnect(isert_conn->conn_cm_id);
3146 * Only wait for conn_wait_comp_err if the isert_conn made it
3147 * into full feature phase..
3149 if (isert_conn->state == ISER_CONN_INIT) {
3150 mutex_unlock(&isert_conn->conn_mutex);
3153 if (isert_conn->state == ISER_CONN_UP)
3154 isert_conn->state = ISER_CONN_TERMINATING;
3155 mutex_unlock(&isert_conn->conn_mutex);
3157 wait_for_completion(&isert_conn->conn_wait_comp_err);
3159 wait_for_completion(&isert_conn->conn_wait);
3162 static void isert_free_conn(struct iscsi_conn *conn)
3164 struct isert_conn *isert_conn = conn->context;
3166 isert_put_conn(isert_conn);
3169 static struct iscsit_transport iser_target_transport = {
3171 .transport_type = ISCSI_INFINIBAND,
3172 .priv_size = sizeof(struct isert_cmd),
3173 .owner = THIS_MODULE,
3174 .iscsit_setup_np = isert_setup_np,
3175 .iscsit_accept_np = isert_accept_np,
3176 .iscsit_free_np = isert_free_np,
3177 .iscsit_wait_conn = isert_wait_conn,
3178 .iscsit_free_conn = isert_free_conn,
3179 .iscsit_get_login_rx = isert_get_login_rx,
3180 .iscsit_put_login_tx = isert_put_login_tx,
3181 .iscsit_immediate_queue = isert_immediate_queue,
3182 .iscsit_response_queue = isert_response_queue,
3183 .iscsit_get_dataout = isert_get_dataout,
3184 .iscsit_queue_data_in = isert_put_datain,
3185 .iscsit_queue_status = isert_put_response,
3188 static int __init isert_init(void)
3192 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3194 pr_err("Unable to allocate isert_rx_wq\n");
3198 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3199 if (!isert_comp_wq) {
3200 pr_err("Unable to allocate isert_comp_wq\n");
3205 iscsit_register_transport(&iser_target_transport);
3206 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3210 destroy_workqueue(isert_rx_wq);
3214 static void __exit isert_exit(void)
3216 destroy_workqueue(isert_comp_wq);
3217 destroy_workqueue(isert_rx_wq);
3218 iscsit_unregister_transport(&iser_target_transport);
3219 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3222 MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3223 MODULE_VERSION("0.1");
3224 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3225 MODULE_LICENSE("GPL");
3227 module_init(isert_init);
3228 module_exit(isert_exit);