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_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
52 isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53 struct isert_rdma_wr *wr);
56 isert_qp_event_callback(struct ib_event *e, void *context)
58 struct isert_conn *isert_conn = (struct isert_conn *)context;
60 pr_err("isert_qp_event_callback event: %d\n", e->event);
62 case IB_EVENT_COMM_EST:
63 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
65 case IB_EVENT_QP_LAST_WQE_REACHED:
66 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
74 isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
78 ret = ib_query_device(ib_dev, devattr);
80 pr_err("ib_query_device() failed: %d\n", ret);
83 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
84 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
90 isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
92 struct isert_device *device = isert_conn->conn_device;
93 struct ib_qp_init_attr attr;
94 int ret, index, min_index = 0;
96 mutex_lock(&device_list_mutex);
97 for (index = 0; index < device->cqs_used; index++)
98 if (device->cq_active_qps[index] <
99 device->cq_active_qps[min_index])
101 device->cq_active_qps[min_index]++;
102 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
103 mutex_unlock(&device_list_mutex);
105 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
106 attr.event_handler = isert_qp_event_callback;
107 attr.qp_context = isert_conn;
108 attr.send_cq = device->dev_tx_cq[min_index];
109 attr.recv_cq = device->dev_rx_cq[min_index];
110 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
111 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
113 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
114 * work-around for RDMA_READ..
116 attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
117 isert_conn->max_sge = attr.cap.max_send_sge;
119 attr.cap.max_recv_sge = 1;
120 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
121 attr.qp_type = IB_QPT_RC;
123 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
125 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
126 isert_conn->conn_pd->device);
128 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
130 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
133 isert_conn->conn_qp = cma_id->qp;
134 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
140 isert_cq_event_callback(struct ib_event *e, void *context)
142 pr_debug("isert_cq_event_callback event: %d\n", e->event);
146 isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
148 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
149 struct iser_rx_desc *rx_desc;
150 struct ib_sge *rx_sg;
154 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
155 sizeof(struct iser_rx_desc), GFP_KERNEL);
156 if (!isert_conn->conn_rx_descs)
159 rx_desc = isert_conn->conn_rx_descs;
161 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
162 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
163 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
164 if (ib_dma_mapping_error(ib_dev, dma_addr))
167 rx_desc->dma_addr = dma_addr;
169 rx_sg = &rx_desc->rx_sg;
170 rx_sg->addr = rx_desc->dma_addr;
171 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
172 rx_sg->lkey = isert_conn->conn_mr->lkey;
175 isert_conn->conn_rx_desc_head = 0;
179 rx_desc = isert_conn->conn_rx_descs;
180 for (j = 0; j < i; j++, rx_desc++) {
181 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
182 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
184 kfree(isert_conn->conn_rx_descs);
185 isert_conn->conn_rx_descs = NULL;
191 isert_free_rx_descriptors(struct isert_conn *isert_conn)
193 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
194 struct iser_rx_desc *rx_desc;
197 if (!isert_conn->conn_rx_descs)
200 rx_desc = isert_conn->conn_rx_descs;
201 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
202 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
203 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
206 kfree(isert_conn->conn_rx_descs);
207 isert_conn->conn_rx_descs = NULL;
210 static void isert_cq_tx_callback(struct ib_cq *, void *);
211 static void isert_cq_rx_callback(struct ib_cq *, void *);
214 isert_create_device_ib_res(struct isert_device *device)
216 struct ib_device *ib_dev = device->ib_device;
217 struct isert_cq_desc *cq_desc;
218 struct ib_device_attr *dev_attr;
221 dev_attr = &device->dev_attr;
222 ret = isert_query_device(ib_dev, dev_attr);
226 /* asign function handlers */
227 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
228 device->use_frwr = 1;
229 device->reg_rdma_mem = isert_reg_rdma_frwr;
230 device->unreg_rdma_mem = isert_unreg_rdma_frwr;
232 device->use_frwr = 0;
233 device->reg_rdma_mem = isert_map_rdma;
234 device->unreg_rdma_mem = isert_unmap_cmd;
237 device->cqs_used = min_t(int, num_online_cpus(),
238 device->ib_device->num_comp_vectors);
239 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
240 pr_debug("Using %d CQs, device %s supports %d vectors support FRWR %d\n",
241 device->cqs_used, device->ib_device->name,
242 device->ib_device->num_comp_vectors, device->use_frwr);
243 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
244 device->cqs_used, GFP_KERNEL);
245 if (!device->cq_desc) {
246 pr_err("Unable to allocate device->cq_desc\n");
249 cq_desc = device->cq_desc;
251 device->dev_pd = ib_alloc_pd(ib_dev);
252 if (IS_ERR(device->dev_pd)) {
253 ret = PTR_ERR(device->dev_pd);
254 pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
258 for (i = 0; i < device->cqs_used; i++) {
259 cq_desc[i].device = device;
260 cq_desc[i].cq_index = i;
262 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
263 isert_cq_rx_callback,
264 isert_cq_event_callback,
266 ISER_MAX_RX_CQ_LEN, i);
267 if (IS_ERR(device->dev_rx_cq[i]))
270 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
271 isert_cq_tx_callback,
272 isert_cq_event_callback,
274 ISER_MAX_TX_CQ_LEN, i);
275 if (IS_ERR(device->dev_tx_cq[i]))
278 if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
281 if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
285 device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
286 if (IS_ERR(device->dev_mr)) {
287 ret = PTR_ERR(device->dev_mr);
288 pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
295 for (j = 0; j < i; j++) {
296 cq_desc = &device->cq_desc[j];
298 if (device->dev_rx_cq[j]) {
299 cancel_work_sync(&cq_desc->cq_rx_work);
300 ib_destroy_cq(device->dev_rx_cq[j]);
302 if (device->dev_tx_cq[j]) {
303 cancel_work_sync(&cq_desc->cq_tx_work);
304 ib_destroy_cq(device->dev_tx_cq[j]);
307 ib_dealloc_pd(device->dev_pd);
310 kfree(device->cq_desc);
316 isert_free_device_ib_res(struct isert_device *device)
318 struct isert_cq_desc *cq_desc;
321 for (i = 0; i < device->cqs_used; i++) {
322 cq_desc = &device->cq_desc[i];
324 cancel_work_sync(&cq_desc->cq_rx_work);
325 cancel_work_sync(&cq_desc->cq_tx_work);
326 ib_destroy_cq(device->dev_rx_cq[i]);
327 ib_destroy_cq(device->dev_tx_cq[i]);
328 device->dev_rx_cq[i] = NULL;
329 device->dev_tx_cq[i] = NULL;
332 ib_dereg_mr(device->dev_mr);
333 ib_dealloc_pd(device->dev_pd);
334 kfree(device->cq_desc);
338 isert_device_try_release(struct isert_device *device)
340 mutex_lock(&device_list_mutex);
342 if (!device->refcount) {
343 isert_free_device_ib_res(device);
344 list_del(&device->dev_node);
347 mutex_unlock(&device_list_mutex);
350 static struct isert_device *
351 isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
353 struct isert_device *device;
356 mutex_lock(&device_list_mutex);
357 list_for_each_entry(device, &device_list, dev_node) {
358 if (device->ib_device->node_guid == cma_id->device->node_guid) {
360 mutex_unlock(&device_list_mutex);
365 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
367 mutex_unlock(&device_list_mutex);
368 return ERR_PTR(-ENOMEM);
371 INIT_LIST_HEAD(&device->dev_node);
373 device->ib_device = cma_id->device;
374 ret = isert_create_device_ib_res(device);
377 mutex_unlock(&device_list_mutex);
382 list_add_tail(&device->dev_node, &device_list);
383 mutex_unlock(&device_list_mutex);
389 isert_conn_free_frwr_pool(struct isert_conn *isert_conn)
391 struct fast_reg_descriptor *fr_desc, *tmp;
394 if (list_empty(&isert_conn->conn_frwr_pool))
397 pr_debug("Freeing conn %p frwr pool", isert_conn);
399 list_for_each_entry_safe(fr_desc, tmp,
400 &isert_conn->conn_frwr_pool, list) {
401 list_del(&fr_desc->list);
402 ib_free_fast_reg_page_list(fr_desc->data_frpl);
403 ib_dereg_mr(fr_desc->data_mr);
408 if (i < isert_conn->conn_frwr_pool_size)
409 pr_warn("Pool still has %d regions registered\n",
410 isert_conn->conn_frwr_pool_size - i);
414 isert_conn_create_frwr_pool(struct isert_conn *isert_conn)
416 struct fast_reg_descriptor *fr_desc;
417 struct isert_device *device = isert_conn->conn_device;
420 INIT_LIST_HEAD(&isert_conn->conn_frwr_pool);
421 isert_conn->conn_frwr_pool_size = 0;
422 for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
423 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
425 pr_err("Failed to allocate fast_reg descriptor\n");
431 ib_alloc_fast_reg_page_list(device->ib_device,
432 ISCSI_ISER_SG_TABLESIZE);
433 if (IS_ERR(fr_desc->data_frpl)) {
434 pr_err("Failed to allocate fr_pg_list err=%ld\n",
435 PTR_ERR(fr_desc->data_frpl));
436 ret = PTR_ERR(fr_desc->data_frpl);
440 fr_desc->data_mr = ib_alloc_fast_reg_mr(device->dev_pd,
441 ISCSI_ISER_SG_TABLESIZE);
442 if (IS_ERR(fr_desc->data_mr)) {
443 pr_err("Failed to allocate frmr err=%ld\n",
444 PTR_ERR(fr_desc->data_mr));
445 ret = PTR_ERR(fr_desc->data_mr);
446 ib_free_fast_reg_page_list(fr_desc->data_frpl);
449 pr_debug("Create fr_desc %p page_list %p\n",
450 fr_desc, fr_desc->data_frpl->page_list);
452 fr_desc->valid = true;
453 list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
454 isert_conn->conn_frwr_pool_size++;
457 pr_debug("Creating conn %p frwr pool size=%d",
458 isert_conn, isert_conn->conn_frwr_pool_size);
463 isert_conn_free_frwr_pool(isert_conn);
468 isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
470 struct iscsi_np *np = cma_id->context;
471 struct isert_np *isert_np = np->np_context;
472 struct isert_conn *isert_conn;
473 struct isert_device *device;
474 struct ib_device *ib_dev = cma_id->device;
477 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
478 cma_id, cma_id->context);
480 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
482 pr_err("Unable to allocate isert_conn\n");
485 isert_conn->state = ISER_CONN_INIT;
486 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
487 init_completion(&isert_conn->conn_login_comp);
488 init_waitqueue_head(&isert_conn->conn_wait);
489 init_waitqueue_head(&isert_conn->conn_wait_comp_err);
490 kref_init(&isert_conn->conn_kref);
491 kref_get(&isert_conn->conn_kref);
492 mutex_init(&isert_conn->conn_mutex);
493 mutex_init(&isert_conn->conn_comp_mutex);
494 spin_lock_init(&isert_conn->conn_lock);
496 cma_id->context = isert_conn;
497 isert_conn->conn_cm_id = cma_id;
498 isert_conn->responder_resources = event->param.conn.responder_resources;
499 isert_conn->initiator_depth = event->param.conn.initiator_depth;
500 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
501 isert_conn->responder_resources, isert_conn->initiator_depth);
503 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
504 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
505 if (!isert_conn->login_buf) {
506 pr_err("Unable to allocate isert_conn->login_buf\n");
511 isert_conn->login_req_buf = isert_conn->login_buf;
512 isert_conn->login_rsp_buf = isert_conn->login_buf +
513 ISCSI_DEF_MAX_RECV_SEG_LEN;
514 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
515 isert_conn->login_buf, isert_conn->login_req_buf,
516 isert_conn->login_rsp_buf);
518 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
519 (void *)isert_conn->login_req_buf,
520 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
522 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
524 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
526 isert_conn->login_req_dma = 0;
530 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
531 (void *)isert_conn->login_rsp_buf,
532 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
534 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
536 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
538 isert_conn->login_rsp_dma = 0;
539 goto out_req_dma_map;
542 device = isert_device_find_by_ib_dev(cma_id);
543 if (IS_ERR(device)) {
544 ret = PTR_ERR(device);
545 goto out_rsp_dma_map;
548 isert_conn->conn_device = device;
549 isert_conn->conn_pd = device->dev_pd;
550 isert_conn->conn_mr = device->dev_mr;
552 if (device->use_frwr) {
553 ret = isert_conn_create_frwr_pool(isert_conn);
555 pr_err("Conn: %p failed to create frwr_pool\n", isert_conn);
560 ret = isert_conn_setup_qp(isert_conn, cma_id);
564 mutex_lock(&isert_np->np_accept_mutex);
565 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
566 mutex_unlock(&isert_np->np_accept_mutex);
568 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
569 wake_up(&isert_np->np_accept_wq);
573 if (device->use_frwr)
574 isert_conn_free_frwr_pool(isert_conn);
576 isert_device_try_release(device);
578 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
579 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
581 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
582 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
584 kfree(isert_conn->login_buf);
591 isert_connect_release(struct isert_conn *isert_conn)
593 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
594 struct isert_device *device = isert_conn->conn_device;
597 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
599 if (device && device->use_frwr)
600 isert_conn_free_frwr_pool(isert_conn);
602 if (isert_conn->conn_qp) {
603 cq_index = ((struct isert_cq_desc *)
604 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
605 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
606 isert_conn->conn_device->cq_active_qps[cq_index]--;
608 rdma_destroy_qp(isert_conn->conn_cm_id);
611 isert_free_rx_descriptors(isert_conn);
612 rdma_destroy_id(isert_conn->conn_cm_id);
614 if (isert_conn->login_buf) {
615 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
616 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
617 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
618 ISCSI_DEF_MAX_RECV_SEG_LEN,
620 kfree(isert_conn->login_buf);
625 isert_device_try_release(device);
627 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
631 isert_connected_handler(struct rdma_cm_id *cma_id)
637 isert_release_conn_kref(struct kref *kref)
639 struct isert_conn *isert_conn = container_of(kref,
640 struct isert_conn, conn_kref);
642 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
643 current->comm, current->pid);
645 isert_connect_release(isert_conn);
649 isert_put_conn(struct isert_conn *isert_conn)
651 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
655 isert_disconnect_work(struct work_struct *work)
657 struct isert_conn *isert_conn = container_of(work,
658 struct isert_conn, conn_logout_work);
660 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
661 mutex_lock(&isert_conn->conn_mutex);
662 isert_conn->state = ISER_CONN_DOWN;
664 if (isert_conn->post_recv_buf_count == 0 &&
665 atomic_read(&isert_conn->post_send_buf_count) == 0) {
666 pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
667 mutex_unlock(&isert_conn->conn_mutex);
670 if (!isert_conn->conn_cm_id) {
671 mutex_unlock(&isert_conn->conn_mutex);
672 isert_put_conn(isert_conn);
675 if (!isert_conn->logout_posted) {
676 pr_debug("Calling rdma_disconnect for !logout_posted from"
677 " isert_disconnect_work\n");
678 rdma_disconnect(isert_conn->conn_cm_id);
679 mutex_unlock(&isert_conn->conn_mutex);
680 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
683 mutex_unlock(&isert_conn->conn_mutex);
686 wake_up(&isert_conn->conn_wait);
687 isert_put_conn(isert_conn);
691 isert_disconnected_handler(struct rdma_cm_id *cma_id)
693 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
695 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
696 schedule_work(&isert_conn->conn_logout_work);
700 isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
704 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
705 event->event, event->status, cma_id->context, cma_id);
707 switch (event->event) {
708 case RDMA_CM_EVENT_CONNECT_REQUEST:
709 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
710 ret = isert_connect_request(cma_id, event);
712 case RDMA_CM_EVENT_ESTABLISHED:
713 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
714 isert_connected_handler(cma_id);
716 case RDMA_CM_EVENT_DISCONNECTED:
717 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
718 isert_disconnected_handler(cma_id);
720 case RDMA_CM_EVENT_DEVICE_REMOVAL:
721 case RDMA_CM_EVENT_ADDR_CHANGE:
723 case RDMA_CM_EVENT_CONNECT_ERROR:
725 pr_err("Unknown RDMA CMA event: %d\n", event->event);
730 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
739 isert_post_recv(struct isert_conn *isert_conn, u32 count)
741 struct ib_recv_wr *rx_wr, *rx_wr_failed;
743 unsigned int rx_head = isert_conn->conn_rx_desc_head;
744 struct iser_rx_desc *rx_desc;
746 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
747 rx_desc = &isert_conn->conn_rx_descs[rx_head];
748 rx_wr->wr_id = (unsigned long)rx_desc;
749 rx_wr->sg_list = &rx_desc->rx_sg;
751 rx_wr->next = rx_wr + 1;
752 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
756 rx_wr->next = NULL; /* mark end of work requests list */
758 isert_conn->post_recv_buf_count += count;
759 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
762 pr_err("ib_post_recv() failed with ret: %d\n", ret);
763 isert_conn->post_recv_buf_count -= count;
765 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
766 isert_conn->conn_rx_desc_head = rx_head;
772 isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
774 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
775 struct ib_send_wr send_wr, *send_wr_failed;
778 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
779 ISER_HEADERS_LEN, DMA_TO_DEVICE);
782 send_wr.wr_id = (unsigned long)tx_desc;
783 send_wr.sg_list = tx_desc->tx_sg;
784 send_wr.num_sge = tx_desc->num_sge;
785 send_wr.opcode = IB_WR_SEND;
786 send_wr.send_flags = IB_SEND_SIGNALED;
788 atomic_inc(&isert_conn->post_send_buf_count);
790 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
792 pr_err("ib_post_send() failed, ret: %d\n", ret);
793 atomic_dec(&isert_conn->post_send_buf_count);
800 isert_create_send_desc(struct isert_conn *isert_conn,
801 struct isert_cmd *isert_cmd,
802 struct iser_tx_desc *tx_desc)
804 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
806 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
807 ISER_HEADERS_LEN, DMA_TO_DEVICE);
809 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
810 tx_desc->iser_header.flags = ISER_VER;
812 tx_desc->num_sge = 1;
813 tx_desc->isert_cmd = isert_cmd;
815 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
816 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
817 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
822 isert_init_tx_hdrs(struct isert_conn *isert_conn,
823 struct iser_tx_desc *tx_desc)
825 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
828 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
829 ISER_HEADERS_LEN, DMA_TO_DEVICE);
830 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
831 pr_err("ib_dma_mapping_error() failed\n");
835 tx_desc->dma_addr = dma_addr;
836 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
837 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
838 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
840 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
841 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
842 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
848 isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
849 struct ib_send_wr *send_wr, bool coalesce)
851 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
853 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
854 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
855 send_wr->opcode = IB_WR_SEND;
856 send_wr->sg_list = &tx_desc->tx_sg[0];
857 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
859 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
860 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
862 mutex_lock(&isert_conn->conn_comp_mutex);
864 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
865 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
866 mutex_unlock(&isert_conn->conn_comp_mutex);
869 isert_conn->conn_comp_batch = 0;
870 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
871 mutex_unlock(&isert_conn->conn_comp_mutex);
873 send_wr->send_flags = IB_SEND_SIGNALED;
877 isert_rdma_post_recvl(struct isert_conn *isert_conn)
879 struct ib_recv_wr rx_wr, *rx_wr_fail;
883 memset(&sge, 0, sizeof(struct ib_sge));
884 sge.addr = isert_conn->login_req_dma;
885 sge.length = ISER_RX_LOGIN_SIZE;
886 sge.lkey = isert_conn->conn_mr->lkey;
888 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
889 sge.addr, sge.length, sge.lkey);
891 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
892 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
893 rx_wr.sg_list = &sge;
896 isert_conn->post_recv_buf_count++;
897 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
899 pr_err("ib_post_recv() failed: %d\n", ret);
900 isert_conn->post_recv_buf_count--;
903 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
908 isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
911 struct isert_conn *isert_conn = conn->context;
912 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
913 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
916 isert_create_send_desc(isert_conn, NULL, tx_desc);
918 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
919 sizeof(struct iscsi_hdr));
921 isert_init_tx_hdrs(isert_conn, tx_desc);
924 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
926 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
927 length, DMA_TO_DEVICE);
929 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
931 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
932 length, DMA_TO_DEVICE);
934 tx_dsg->addr = isert_conn->login_rsp_dma;
935 tx_dsg->length = length;
936 tx_dsg->lkey = isert_conn->conn_mr->lkey;
937 tx_desc->num_sge = 2;
939 if (!login->login_failed) {
940 if (login->login_complete) {
941 ret = isert_alloc_rx_descriptors(isert_conn);
945 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
949 isert_conn->state = ISER_CONN_UP;
953 ret = isert_rdma_post_recvl(isert_conn);
958 ret = isert_post_send(isert_conn, tx_desc);
966 isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
967 struct isert_conn *isert_conn)
969 struct iscsi_conn *conn = isert_conn->conn;
970 struct iscsi_login *login = conn->conn_login;
974 pr_err("conn->conn_login is NULL\n");
979 if (login->first_request) {
980 struct iscsi_login_req *login_req =
981 (struct iscsi_login_req *)&rx_desc->iscsi_header;
983 * Setup the initial iscsi_login values from the leading
986 login->leading_connection = (!login_req->tsih) ? 1 : 0;
987 login->current_stage =
988 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
990 login->version_min = login_req->min_version;
991 login->version_max = login_req->max_version;
992 memcpy(login->isid, login_req->isid, 6);
993 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
994 login->init_task_tag = login_req->itt;
995 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
996 login->cid = be16_to_cpu(login_req->cid);
997 login->tsih = be16_to_cpu(login_req->tsih);
1000 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1002 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1003 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1004 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1005 memcpy(login->req_buf, &rx_desc->data[0], size);
1007 if (login->first_request) {
1008 complete(&isert_conn->conn_login_comp);
1011 schedule_delayed_work(&conn->login_work, 0);
1014 static struct iscsi_cmd
1015 *isert_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp)
1017 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1018 struct isert_cmd *isert_cmd;
1019 struct iscsi_cmd *cmd;
1021 cmd = iscsit_allocate_cmd(conn, gfp);
1023 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
1026 isert_cmd = iscsit_priv_cmd(cmd);
1027 isert_cmd->conn = isert_conn;
1028 isert_cmd->iscsi_cmd = cmd;
1034 isert_handle_scsi_cmd(struct isert_conn *isert_conn,
1035 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1036 struct iser_rx_desc *rx_desc, unsigned char *buf)
1038 struct iscsi_conn *conn = isert_conn->conn;
1039 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1040 struct scatterlist *sg;
1041 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1042 bool dump_payload = false;
1044 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1048 imm_data = cmd->immediate_data;
1049 imm_data_len = cmd->first_burst_len;
1050 unsol_data = cmd->unsolicited_data;
1052 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1055 } else if (rc > 0) {
1056 dump_payload = true;
1063 sg = &cmd->se_cmd.t_data_sg[0];
1064 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1066 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1067 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1069 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1071 cmd->write_data_done += imm_data_len;
1073 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1074 spin_lock_bh(&cmd->istate_lock);
1075 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1076 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1077 spin_unlock_bh(&cmd->istate_lock);
1081 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
1083 if (!rc && dump_payload == false && unsol_data)
1084 iscsit_set_unsoliticed_dataout(cmd);
1090 isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1091 struct iser_rx_desc *rx_desc, unsigned char *buf)
1093 struct scatterlist *sg_start;
1094 struct iscsi_conn *conn = isert_conn->conn;
1095 struct iscsi_cmd *cmd = NULL;
1096 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1097 u32 unsol_data_len = ntoh24(hdr->dlength);
1098 int rc, sg_nents, sg_off, page_off;
1100 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1106 * FIXME: Unexpected unsolicited_data out
1108 if (!cmd->unsolicited_data) {
1109 pr_err("Received unexpected solicited data payload\n");
1114 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1115 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1117 sg_off = cmd->write_data_done / PAGE_SIZE;
1118 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1119 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1120 page_off = cmd->write_data_done % PAGE_SIZE;
1122 * FIXME: Non page-aligned unsolicited_data out
1125 pr_err("Received unexpected non-page aligned data payload\n");
1129 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1130 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1132 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1135 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1143 isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1144 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1147 struct iscsi_conn *conn = isert_conn->conn;
1148 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1151 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1155 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1158 return iscsit_process_nop_out(conn, cmd, hdr);
1162 isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1163 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1164 struct iscsi_text *hdr)
1166 struct iscsi_conn *conn = isert_conn->conn;
1167 u32 payload_length = ntoh24(hdr->dlength);
1169 unsigned char *text_in;
1171 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1175 text_in = kzalloc(payload_length, GFP_KERNEL);
1177 pr_err("Unable to allocate text_in of payload_length: %u\n",
1181 cmd->text_in_ptr = text_in;
1183 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1185 return iscsit_process_text_cmd(conn, cmd, hdr);
1189 isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1190 uint32_t read_stag, uint64_t read_va,
1191 uint32_t write_stag, uint64_t write_va)
1193 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1194 struct iscsi_conn *conn = isert_conn->conn;
1195 struct iscsi_session *sess = conn->sess;
1196 struct iscsi_cmd *cmd;
1197 struct isert_cmd *isert_cmd;
1199 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1201 if (sess->sess_ops->SessionType &&
1202 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1203 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1204 " ignoring\n", opcode);
1209 case ISCSI_OP_SCSI_CMD:
1210 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1214 isert_cmd = iscsit_priv_cmd(cmd);
1215 isert_cmd->read_stag = read_stag;
1216 isert_cmd->read_va = read_va;
1217 isert_cmd->write_stag = write_stag;
1218 isert_cmd->write_va = write_va;
1220 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
1221 rx_desc, (unsigned char *)hdr);
1223 case ISCSI_OP_NOOP_OUT:
1224 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1228 isert_cmd = iscsit_priv_cmd(cmd);
1229 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
1230 rx_desc, (unsigned char *)hdr);
1232 case ISCSI_OP_SCSI_DATA_OUT:
1233 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1234 (unsigned char *)hdr);
1236 case ISCSI_OP_SCSI_TMFUNC:
1237 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1241 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1242 (unsigned char *)hdr);
1244 case ISCSI_OP_LOGOUT:
1245 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1249 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1251 wait_for_completion_timeout(&conn->conn_logout_comp,
1252 SECONDS_FOR_LOGOUT_COMP *
1256 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
1260 isert_cmd = iscsit_priv_cmd(cmd);
1261 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
1262 rx_desc, (struct iscsi_text *)hdr);
1265 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1274 isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1276 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1277 uint64_t read_va = 0, write_va = 0;
1278 uint32_t read_stag = 0, write_stag = 0;
1281 switch (iser_hdr->flags & 0xF0) {
1283 if (iser_hdr->flags & ISER_RSV) {
1284 read_stag = be32_to_cpu(iser_hdr->read_stag);
1285 read_va = be64_to_cpu(iser_hdr->read_va);
1286 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1287 read_stag, (unsigned long long)read_va);
1289 if (iser_hdr->flags & ISER_WSV) {
1290 write_stag = be32_to_cpu(iser_hdr->write_stag);
1291 write_va = be64_to_cpu(iser_hdr->write_va);
1292 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1293 write_stag, (unsigned long long)write_va);
1296 pr_debug("ISER ISCSI_CTRL PDU\n");
1299 pr_err("iSER Hello message\n");
1302 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1306 rc = isert_rx_opcode(isert_conn, rx_desc,
1307 read_stag, read_va, write_stag, write_va);
1311 isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1312 unsigned long xfer_len)
1314 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1315 struct iscsi_hdr *hdr;
1317 int rx_buflen, outstanding;
1319 if ((char *)desc == isert_conn->login_req_buf) {
1320 rx_dma = isert_conn->login_req_dma;
1321 rx_buflen = ISER_RX_LOGIN_SIZE;
1322 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1325 rx_dma = desc->dma_addr;
1326 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1327 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1331 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1333 hdr = &desc->iscsi_header;
1334 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1335 hdr->opcode, hdr->itt, hdr->flags,
1336 (int)(xfer_len - ISER_HEADERS_LEN));
1338 if ((char *)desc == isert_conn->login_req_buf)
1339 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1342 isert_rx_do_work(desc, isert_conn);
1344 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1347 isert_conn->post_recv_buf_count--;
1348 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1349 isert_conn->post_recv_buf_count);
1351 if ((char *)desc == isert_conn->login_req_buf)
1354 outstanding = isert_conn->post_recv_buf_count;
1355 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1356 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1357 ISERT_MIN_POSTED_RX);
1358 err = isert_post_recv(isert_conn, count);
1360 pr_err("isert_post_recv() count: %d failed, %d\n",
1367 isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1369 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1370 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1372 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
1374 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1375 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1376 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1377 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1382 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1388 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1395 isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1397 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1398 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1399 LIST_HEAD(unmap_list);
1401 pr_debug("unreg_frwr_cmd: %p\n", isert_cmd);
1404 pr_debug("unreg_frwr_cmd: %p free fr_desc %p\n",
1405 isert_cmd, wr->fr_desc);
1406 spin_lock_bh(&isert_conn->conn_lock);
1407 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_frwr_pool);
1408 spin_unlock_bh(&isert_conn->conn_lock);
1413 pr_debug("unreg_frwr_cmd: %p unmap_sg op\n", isert_cmd);
1414 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1415 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1416 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1425 isert_put_cmd(struct isert_cmd *isert_cmd)
1427 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1428 struct isert_conn *isert_conn = isert_cmd->conn;
1429 struct iscsi_conn *conn = isert_conn->conn;
1430 struct isert_device *device = isert_conn->conn_device;
1432 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1434 switch (cmd->iscsi_opcode) {
1435 case ISCSI_OP_SCSI_CMD:
1436 spin_lock_bh(&conn->cmd_lock);
1437 if (!list_empty(&cmd->i_conn_node))
1438 list_del(&cmd->i_conn_node);
1439 spin_unlock_bh(&conn->cmd_lock);
1441 if (cmd->data_direction == DMA_TO_DEVICE)
1442 iscsit_stop_dataout_timer(cmd);
1444 device->unreg_rdma_mem(isert_cmd, isert_conn);
1445 transport_generic_free_cmd(&cmd->se_cmd, 0);
1447 case ISCSI_OP_SCSI_TMFUNC:
1448 spin_lock_bh(&conn->cmd_lock);
1449 if (!list_empty(&cmd->i_conn_node))
1450 list_del(&cmd->i_conn_node);
1451 spin_unlock_bh(&conn->cmd_lock);
1453 transport_generic_free_cmd(&cmd->se_cmd, 0);
1455 case ISCSI_OP_REJECT:
1456 case ISCSI_OP_NOOP_OUT:
1458 spin_lock_bh(&conn->cmd_lock);
1459 if (!list_empty(&cmd->i_conn_node))
1460 list_del(&cmd->i_conn_node);
1461 spin_unlock_bh(&conn->cmd_lock);
1464 * Handle special case for REJECT when iscsi_add_reject*() has
1465 * overwritten the original iscsi_opcode assignment, and the
1466 * associated cmd->se_cmd needs to be released.
1468 if (cmd->se_cmd.se_tfo != NULL) {
1469 pr_debug("Calling transport_generic_free_cmd from"
1470 " isert_put_cmd for 0x%02x\n",
1472 transport_generic_free_cmd(&cmd->se_cmd, 0);
1479 iscsit_release_cmd(cmd);
1485 isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1487 if (tx_desc->dma_addr != 0) {
1488 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1489 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1490 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1491 tx_desc->dma_addr = 0;
1496 isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1497 struct ib_device *ib_dev)
1499 if (isert_cmd->pdu_buf_dma != 0) {
1500 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1501 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1502 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1503 isert_cmd->pdu_buf_dma = 0;
1506 isert_unmap_tx_desc(tx_desc, ib_dev);
1507 isert_put_cmd(isert_cmd);
1511 isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1512 struct isert_cmd *isert_cmd)
1514 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1515 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1516 struct se_cmd *se_cmd = &cmd->se_cmd;
1517 struct isert_conn *isert_conn = isert_cmd->conn;
1518 struct isert_device *device = isert_conn->conn_device;
1520 iscsit_stop_dataout_timer(cmd);
1521 device->unreg_rdma_mem(isert_cmd, isert_conn);
1522 cmd->write_data_done = wr->cur_rdma_length;
1524 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
1525 spin_lock_bh(&cmd->istate_lock);
1526 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1527 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1528 spin_unlock_bh(&cmd->istate_lock);
1530 target_execute_cmd(se_cmd);
1534 isert_do_control_comp(struct work_struct *work)
1536 struct isert_cmd *isert_cmd = container_of(work,
1537 struct isert_cmd, comp_work);
1538 struct isert_conn *isert_conn = isert_cmd->conn;
1539 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1540 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1542 switch (cmd->i_state) {
1543 case ISTATE_SEND_TASKMGTRSP:
1544 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1546 atomic_dec(&isert_conn->post_send_buf_count);
1547 iscsit_tmr_post_handler(cmd, cmd->conn);
1549 cmd->i_state = ISTATE_SENT_STATUS;
1550 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1552 case ISTATE_SEND_REJECT:
1553 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1554 atomic_dec(&isert_conn->post_send_buf_count);
1556 cmd->i_state = ISTATE_SENT_STATUS;
1557 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1559 case ISTATE_SEND_LOGOUTRSP:
1560 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1562 * Call atomic_dec(&isert_conn->post_send_buf_count)
1563 * from isert_free_conn()
1565 isert_conn->logout_posted = true;
1566 iscsit_logout_post_handler(cmd, cmd->conn);
1568 case ISTATE_SEND_TEXTRSP:
1569 atomic_dec(&isert_conn->post_send_buf_count);
1570 cmd->i_state = ISTATE_SENT_STATUS;
1571 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1574 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1581 isert_response_completion(struct iser_tx_desc *tx_desc,
1582 struct isert_cmd *isert_cmd,
1583 struct isert_conn *isert_conn,
1584 struct ib_device *ib_dev)
1586 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1588 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
1589 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1590 cmd->i_state == ISTATE_SEND_REJECT ||
1591 cmd->i_state == ISTATE_SEND_TEXTRSP) {
1592 isert_unmap_tx_desc(tx_desc, ib_dev);
1594 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1595 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1598 atomic_dec(&isert_conn->post_send_buf_count);
1600 cmd->i_state = ISTATE_SENT_STATUS;
1601 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1605 __isert_send_completion(struct iser_tx_desc *tx_desc,
1606 struct isert_conn *isert_conn)
1608 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1609 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1610 struct isert_rdma_wr *wr;
1613 atomic_dec(&isert_conn->post_send_buf_count);
1614 isert_unmap_tx_desc(tx_desc, ib_dev);
1617 wr = &isert_cmd->rdma_wr;
1619 switch (wr->iser_ib_op) {
1621 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1625 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1626 isert_response_completion(tx_desc, isert_cmd,
1627 isert_conn, ib_dev);
1629 case ISER_IB_RDMA_WRITE:
1630 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1633 case ISER_IB_RDMA_READ:
1634 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1636 atomic_dec(&isert_conn->post_send_buf_count);
1637 isert_completion_rdma_read(tx_desc, isert_cmd);
1640 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1647 isert_send_completion(struct iser_tx_desc *tx_desc,
1648 struct isert_conn *isert_conn)
1650 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1651 struct iser_tx_desc *t;
1653 * Drain coalesced completion llist starting from comp_llnode_batch
1654 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1657 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1658 llnode = llist_next(llnode);
1659 __isert_send_completion(t, isert_conn);
1661 __isert_send_completion(tx_desc, isert_conn);
1665 isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1667 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1670 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1673 isert_unmap_tx_desc(tx_desc, ib_dev);
1675 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1678 if (isert_conn->post_recv_buf_count == 0 &&
1679 atomic_read(&isert_conn->post_send_buf_count) == 0) {
1680 pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1681 pr_debug("Calling wake_up from isert_cq_comp_err\n");
1683 mutex_lock(&isert_conn->conn_mutex);
1684 if (isert_conn->state != ISER_CONN_DOWN)
1685 isert_conn->state = ISER_CONN_TERMINATING;
1686 mutex_unlock(&isert_conn->conn_mutex);
1688 wake_up(&isert_conn->conn_wait_comp_err);
1693 isert_cq_tx_work(struct work_struct *work)
1695 struct isert_cq_desc *cq_desc = container_of(work,
1696 struct isert_cq_desc, cq_tx_work);
1697 struct isert_device *device = cq_desc->device;
1698 int cq_index = cq_desc->cq_index;
1699 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1700 struct isert_conn *isert_conn;
1701 struct iser_tx_desc *tx_desc;
1704 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1705 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1706 isert_conn = wc.qp->qp_context;
1708 if (wc.status == IB_WC_SUCCESS) {
1709 isert_send_completion(tx_desc, isert_conn);
1711 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1712 pr_debug("TX wc.status: 0x%08x\n", wc.status);
1713 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
1714 atomic_dec(&isert_conn->post_send_buf_count);
1715 isert_cq_comp_err(tx_desc, isert_conn);
1719 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1723 isert_cq_tx_callback(struct ib_cq *cq, void *context)
1725 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1727 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1728 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1732 isert_cq_rx_work(struct work_struct *work)
1734 struct isert_cq_desc *cq_desc = container_of(work,
1735 struct isert_cq_desc, cq_rx_work);
1736 struct isert_device *device = cq_desc->device;
1737 int cq_index = cq_desc->cq_index;
1738 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1739 struct isert_conn *isert_conn;
1740 struct iser_rx_desc *rx_desc;
1742 unsigned long xfer_len;
1744 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1745 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1746 isert_conn = wc.qp->qp_context;
1748 if (wc.status == IB_WC_SUCCESS) {
1749 xfer_len = (unsigned long)wc.byte_len;
1750 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1752 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1753 if (wc.status != IB_WC_WR_FLUSH_ERR) {
1754 pr_debug("RX wc.status: 0x%08x\n", wc.status);
1755 pr_debug("RX wc.vendor_err: 0x%08x\n",
1758 isert_conn->post_recv_buf_count--;
1759 isert_cq_comp_err(NULL, isert_conn);
1763 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1767 isert_cq_rx_callback(struct ib_cq *cq, void *context)
1769 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1771 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1772 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1776 isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1778 struct ib_send_wr *wr_failed;
1781 atomic_inc(&isert_conn->post_send_buf_count);
1783 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1786 pr_err("ib_post_send failed with %d\n", ret);
1787 atomic_dec(&isert_conn->post_send_buf_count);
1794 isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1796 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1797 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1798 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1799 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1800 &isert_cmd->tx_desc.iscsi_header;
1802 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1803 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1804 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1806 * Attach SENSE DATA payload to iSCSI Response PDU
1808 if (cmd->se_cmd.sense_buffer &&
1809 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1810 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1811 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1812 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1813 u32 padding, pdu_len;
1815 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1817 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1819 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1820 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1821 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
1823 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1824 (void *)cmd->sense_buffer, pdu_len,
1827 isert_cmd->pdu_buf_len = pdu_len;
1828 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1829 tx_dsg->length = pdu_len;
1830 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1831 isert_cmd->tx_desc.num_sge = 2;
1834 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
1836 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1838 return isert_post_response(isert_conn, isert_cmd);
1842 isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1843 bool nopout_response)
1845 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1846 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1847 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1849 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1850 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1851 &isert_cmd->tx_desc.iscsi_header,
1853 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1854 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1856 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1858 return isert_post_response(isert_conn, isert_cmd);
1862 isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1864 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1865 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1866 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1868 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1869 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1870 &isert_cmd->tx_desc.iscsi_header);
1871 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1872 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1874 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1876 return isert_post_response(isert_conn, isert_cmd);
1880 isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1882 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1883 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1884 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1886 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1887 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1888 &isert_cmd->tx_desc.iscsi_header);
1889 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1890 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1892 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1894 return isert_post_response(isert_conn, isert_cmd);
1898 isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1900 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1901 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1902 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1903 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1904 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1905 struct iscsi_reject *hdr =
1906 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
1908 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1909 iscsit_build_reject(cmd, conn, hdr);
1910 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1912 hton24(hdr->dlength, ISCSI_HDR_LEN);
1913 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1914 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1916 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1917 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1918 tx_dsg->length = ISCSI_HDR_LEN;
1919 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1920 isert_cmd->tx_desc.num_sge = 2;
1922 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1924 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1926 return isert_post_response(isert_conn, isert_cmd);
1930 isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1932 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
1933 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1934 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1935 struct iscsi_text_rsp *hdr =
1936 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1940 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1941 rc = iscsit_build_text_rsp(cmd, conn, hdr);
1946 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1949 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1950 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1951 void *txt_rsp_buf = cmd->buf_ptr;
1953 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1954 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1956 isert_cmd->pdu_buf_len = txt_rsp_len;
1957 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1958 tx_dsg->length = txt_rsp_len;
1959 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1960 isert_cmd->tx_desc.num_sge = 2;
1962 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
1964 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1966 return isert_post_response(isert_conn, isert_cmd);
1970 isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1971 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1972 u32 data_left, u32 offset)
1974 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
1975 struct scatterlist *sg_start, *tmp_sg;
1976 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1977 u32 sg_off, page_off;
1978 int i = 0, sg_nents;
1980 sg_off = offset / PAGE_SIZE;
1981 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1982 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1983 page_off = offset % PAGE_SIZE;
1985 send_wr->sg_list = ib_sge;
1986 send_wr->num_sge = sg_nents;
1987 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1989 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1991 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1992 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1993 (unsigned long long)tmp_sg->dma_address,
1994 tmp_sg->length, page_off);
1996 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1997 ib_sge->length = min_t(u32, data_left,
1998 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1999 ib_sge->lkey = isert_conn->conn_mr->lkey;
2001 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2002 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2004 data_left -= ib_sge->length;
2006 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2009 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2010 send_wr->sg_list, send_wr->num_sge);
2016 isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2017 struct isert_rdma_wr *wr)
2019 struct se_cmd *se_cmd = &cmd->se_cmd;
2020 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2021 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2022 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2023 struct ib_send_wr *send_wr;
2024 struct ib_sge *ib_sge;
2025 struct scatterlist *sg_start;
2026 u32 sg_off = 0, sg_nents;
2027 u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
2028 int ret = 0, count, i, ib_sge_cnt;
2030 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2031 data_left = se_cmd->data_length;
2033 sg_off = cmd->write_data_done / PAGE_SIZE;
2034 data_left = se_cmd->data_length - cmd->write_data_done;
2035 offset = cmd->write_data_done;
2036 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2039 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2040 sg_nents = se_cmd->t_data_nents - sg_off;
2042 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2043 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2044 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2045 if (unlikely(!count)) {
2046 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2050 wr->num_sge = sg_nents;
2051 wr->cur_rdma_length = data_left;
2052 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2053 isert_cmd, count, sg_start, sg_nents, data_left);
2055 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2057 pr_warn("Unable to allocate ib_sge\n");
2061 wr->ib_sge = ib_sge;
2063 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2064 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2067 pr_debug("Unable to allocate wr->send_wr\n");
2072 wr->isert_cmd = isert_cmd;
2073 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
2075 for (i = 0; i < wr->send_wr_num; i++) {
2076 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2077 data_len = min(data_left, rdma_write_max);
2079 send_wr->send_flags = 0;
2080 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2081 send_wr->opcode = IB_WR_RDMA_WRITE;
2082 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2083 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2084 if (i + 1 == wr->send_wr_num)
2085 send_wr->next = &isert_cmd->tx_desc.send_wr;
2087 send_wr->next = &wr->send_wr[i + 1];
2089 send_wr->opcode = IB_WR_RDMA_READ;
2090 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2091 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2092 if (i + 1 == wr->send_wr_num)
2093 send_wr->send_flags = IB_SEND_SIGNALED;
2095 send_wr->next = &wr->send_wr[i + 1];
2098 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2099 send_wr, data_len, offset);
2100 ib_sge += ib_sge_cnt;
2103 va_offset += data_len;
2104 data_left -= data_len;
2109 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2110 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2111 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2116 isert_map_fr_pagelist(struct ib_device *ib_dev,
2117 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2119 u64 start_addr, end_addr, page, chunk_start = 0;
2120 struct scatterlist *tmp_sg;
2121 int i = 0, new_chunk, last_ent, n_pages;
2125 last_ent = sg_nents - 1;
2126 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2127 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2129 chunk_start = start_addr;
2130 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2132 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2133 i, (unsigned long long)tmp_sg->dma_address,
2136 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2142 page = chunk_start & PAGE_MASK;
2144 fr_pl[n_pages++] = page;
2145 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2148 } while (page < end_addr);
2155 isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
2156 struct isert_cmd *isert_cmd, struct isert_conn *isert_conn,
2157 struct ib_sge *ib_sge, u32 offset, unsigned int data_len)
2159 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2160 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2161 struct scatterlist *sg_start;
2162 u32 sg_off, page_off;
2163 struct ib_send_wr fr_wr, inv_wr;
2164 struct ib_send_wr *bad_wr, *wr = NULL;
2166 int ret, sg_nents, pagelist_len;
2168 sg_off = offset / PAGE_SIZE;
2169 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2170 sg_nents = min_t(unsigned int, cmd->se_cmd.t_data_nents - sg_off,
2171 ISCSI_ISER_SG_TABLESIZE);
2172 page_off = offset % PAGE_SIZE;
2174 pr_debug("Cmd: %p use fr_desc %p sg_nents %d sg_off %d offset %u\n",
2175 isert_cmd, fr_desc, sg_nents, sg_off, offset);
2177 pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
2178 &fr_desc->data_frpl->page_list[0]);
2180 if (!fr_desc->valid) {
2181 memset(&inv_wr, 0, sizeof(inv_wr));
2182 inv_wr.opcode = IB_WR_LOCAL_INV;
2183 inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
2186 key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
2187 ib_update_fast_reg_key(fr_desc->data_mr, ++key);
2190 /* Prepare FASTREG WR */
2191 memset(&fr_wr, 0, sizeof(fr_wr));
2192 fr_wr.opcode = IB_WR_FAST_REG_MR;
2193 fr_wr.wr.fast_reg.iova_start =
2194 fr_desc->data_frpl->page_list[0] + page_off;
2195 fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
2196 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2197 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2198 fr_wr.wr.fast_reg.length = data_len;
2199 fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
2200 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2207 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2209 pr_err("fast registration failed, ret:%d\n", ret);
2212 fr_desc->valid = false;
2214 ib_sge->lkey = fr_desc->data_mr->lkey;
2215 ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2216 ib_sge->length = data_len;
2218 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2219 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2225 isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2226 struct isert_rdma_wr *wr)
2228 struct se_cmd *se_cmd = &cmd->se_cmd;
2229 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2230 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2231 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2232 struct ib_send_wr *send_wr;
2233 struct ib_sge *ib_sge;
2234 struct scatterlist *sg_start;
2235 struct fast_reg_descriptor *fr_desc;
2236 u32 sg_off = 0, sg_nents;
2237 u32 offset = 0, data_len, data_left, rdma_write_max;
2239 unsigned long flags;
2241 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2242 data_left = se_cmd->data_length;
2244 sg_off = cmd->write_data_done / PAGE_SIZE;
2245 data_left = se_cmd->data_length - cmd->write_data_done;
2246 offset = cmd->write_data_done;
2247 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2250 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2251 sg_nents = se_cmd->t_data_nents - sg_off;
2253 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2254 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2255 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2256 if (unlikely(!count)) {
2257 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2261 wr->num_sge = sg_nents;
2262 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2263 isert_cmd, count, sg_start, sg_nents, data_left);
2265 memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
2266 ib_sge = &wr->s_ib_sge;
2267 wr->ib_sge = ib_sge;
2269 wr->send_wr_num = 1;
2270 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2271 wr->send_wr = &wr->s_send_wr;
2273 wr->isert_cmd = isert_cmd;
2274 rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
2276 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2277 send_wr->sg_list = ib_sge;
2278 send_wr->num_sge = 1;
2279 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2280 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2281 send_wr->opcode = IB_WR_RDMA_WRITE;
2282 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2283 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2284 send_wr->send_flags = 0;
2285 send_wr->next = &isert_cmd->tx_desc.send_wr;
2287 send_wr->opcode = IB_WR_RDMA_READ;
2288 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2289 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2290 send_wr->send_flags = IB_SEND_SIGNALED;
2293 data_len = min(data_left, rdma_write_max);
2294 wr->cur_rdma_length = data_len;
2296 /* if there is a single dma entry, dma mr is sufficient */
2298 ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
2299 ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
2300 ib_sge->lkey = isert_conn->conn_mr->lkey;
2303 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2304 fr_desc = list_first_entry(&isert_conn->conn_frwr_pool,
2305 struct fast_reg_descriptor, list);
2306 list_del(&fr_desc->list);
2307 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2308 wr->fr_desc = fr_desc;
2310 ret = isert_fast_reg_mr(fr_desc, isert_cmd, isert_conn,
2311 ib_sge, offset, data_len);
2313 list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
2321 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2322 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2323 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2328 isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2330 struct se_cmd *se_cmd = &cmd->se_cmd;
2331 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2332 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2333 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2334 struct isert_device *device = isert_conn->conn_device;
2335 struct ib_send_wr *wr_failed;
2338 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2339 isert_cmd, se_cmd->data_length);
2340 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
2341 rc = device->reg_rdma_mem(conn, cmd, wr);
2343 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2348 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2350 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2351 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2352 &isert_cmd->tx_desc.iscsi_header);
2353 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2354 isert_init_send_wr(isert_conn, isert_cmd,
2355 &isert_cmd->tx_desc.send_wr, true);
2357 atomic_inc(&isert_conn->post_send_buf_count);
2359 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2361 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2362 atomic_dec(&isert_conn->post_send_buf_count);
2364 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
2371 isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2373 struct se_cmd *se_cmd = &cmd->se_cmd;
2374 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2375 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2376 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2377 struct isert_device *device = isert_conn->conn_device;
2378 struct ib_send_wr *wr_failed;
2381 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2382 isert_cmd, se_cmd->data_length, cmd->write_data_done);
2383 wr->iser_ib_op = ISER_IB_RDMA_READ;
2384 rc = device->reg_rdma_mem(conn, cmd, wr);
2386 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2390 atomic_inc(&isert_conn->post_send_buf_count);
2392 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2394 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2395 atomic_dec(&isert_conn->post_send_buf_count);
2397 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2404 isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2409 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2410 ret = isert_put_nopin(cmd, conn, false);
2413 pr_err("Unknown immediate state: 0x%02x\n", state);
2422 isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2427 case ISTATE_SEND_LOGOUTRSP:
2428 ret = isert_put_logout_rsp(cmd, conn);
2430 pr_debug("Returning iSER Logout -EAGAIN\n");
2434 case ISTATE_SEND_NOPIN:
2435 ret = isert_put_nopin(cmd, conn, true);
2437 case ISTATE_SEND_TASKMGTRSP:
2438 ret = isert_put_tm_rsp(cmd, conn);
2440 case ISTATE_SEND_REJECT:
2441 ret = isert_put_reject(cmd, conn);
2443 case ISTATE_SEND_TEXTRSP:
2444 ret = isert_put_text_rsp(cmd, conn);
2446 case ISTATE_SEND_STATUS:
2448 * Special case for sending non GOOD SCSI status from TX thread
2449 * context during pre se_cmd excecution failure.
2451 ret = isert_put_response(conn, cmd);
2454 pr_err("Unknown response state: 0x%02x\n", state);
2463 isert_setup_np(struct iscsi_np *np,
2464 struct __kernel_sockaddr_storage *ksockaddr)
2466 struct isert_np *isert_np;
2467 struct rdma_cm_id *isert_lid;
2468 struct sockaddr *sa;
2471 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2473 pr_err("Unable to allocate struct isert_np\n");
2476 init_waitqueue_head(&isert_np->np_accept_wq);
2477 mutex_init(&isert_np->np_accept_mutex);
2478 INIT_LIST_HEAD(&isert_np->np_accept_list);
2479 init_completion(&isert_np->np_login_comp);
2481 sa = (struct sockaddr *)ksockaddr;
2482 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2484 * Setup the np->np_sockaddr from the passed sockaddr setup
2485 * in iscsi_target_configfs.c code..
2487 memcpy(&np->np_sockaddr, ksockaddr,
2488 sizeof(struct __kernel_sockaddr_storage));
2490 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2492 if (IS_ERR(isert_lid)) {
2493 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2494 PTR_ERR(isert_lid));
2495 ret = PTR_ERR(isert_lid);
2499 ret = rdma_bind_addr(isert_lid, sa);
2501 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2505 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2507 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2511 isert_np->np_cm_id = isert_lid;
2512 np->np_context = isert_np;
2513 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2518 rdma_destroy_id(isert_lid);
2525 isert_check_accept_queue(struct isert_np *isert_np)
2529 mutex_lock(&isert_np->np_accept_mutex);
2530 empty = list_empty(&isert_np->np_accept_list);
2531 mutex_unlock(&isert_np->np_accept_mutex);
2537 isert_rdma_accept(struct isert_conn *isert_conn)
2539 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2540 struct rdma_conn_param cp;
2543 memset(&cp, 0, sizeof(struct rdma_conn_param));
2544 cp.responder_resources = isert_conn->responder_resources;
2545 cp.initiator_depth = isert_conn->initiator_depth;
2547 cp.rnr_retry_count = 7;
2549 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2551 ret = rdma_accept(cm_id, &cp);
2553 pr_err("rdma_accept() failed with: %d\n", ret);
2557 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2563 isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2565 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2568 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2570 * For login requests after the first PDU, isert_rx_login_req() will
2571 * kick schedule_delayed_work(&conn->login_work) as the packet is
2572 * received, which turns this callback from iscsi_target_do_login_rx()
2575 if (!login->first_request)
2578 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2582 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2587 isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2588 struct isert_conn *isert_conn)
2590 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2591 struct rdma_route *cm_route = &cm_id->route;
2592 struct sockaddr_in *sock_in;
2593 struct sockaddr_in6 *sock_in6;
2595 conn->login_family = np->np_sockaddr.ss_family;
2597 if (np->np_sockaddr.ss_family == AF_INET6) {
2598 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2599 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2600 &sock_in6->sin6_addr.in6_u);
2601 conn->login_port = ntohs(sock_in6->sin6_port);
2603 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2604 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2605 &sock_in6->sin6_addr.in6_u);
2606 conn->local_port = ntohs(sock_in6->sin6_port);
2608 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2609 sprintf(conn->login_ip, "%pI4",
2610 &sock_in->sin_addr.s_addr);
2611 conn->login_port = ntohs(sock_in->sin_port);
2613 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2614 sprintf(conn->local_ip, "%pI4",
2615 &sock_in->sin_addr.s_addr);
2616 conn->local_port = ntohs(sock_in->sin_port);
2621 isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2623 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2624 struct isert_conn *isert_conn;
2625 int max_accept = 0, ret;
2628 ret = wait_event_interruptible(isert_np->np_accept_wq,
2629 !isert_check_accept_queue(isert_np) ||
2630 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2634 spin_lock_bh(&np->np_thread_lock);
2635 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2636 spin_unlock_bh(&np->np_thread_lock);
2637 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2640 spin_unlock_bh(&np->np_thread_lock);
2642 mutex_lock(&isert_np->np_accept_mutex);
2643 if (list_empty(&isert_np->np_accept_list)) {
2644 mutex_unlock(&isert_np->np_accept_mutex);
2648 isert_conn = list_first_entry(&isert_np->np_accept_list,
2649 struct isert_conn, conn_accept_node);
2650 list_del_init(&isert_conn->conn_accept_node);
2651 mutex_unlock(&isert_np->np_accept_mutex);
2653 conn->context = isert_conn;
2654 isert_conn->conn = conn;
2657 ret = isert_rdma_post_recvl(isert_conn);
2661 ret = isert_rdma_accept(isert_conn);
2665 isert_set_conn_info(np, conn, isert_conn);
2667 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2672 isert_free_np(struct iscsi_np *np)
2674 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2676 rdma_destroy_id(isert_np->np_cm_id);
2678 np->np_context = NULL;
2682 static int isert_check_state(struct isert_conn *isert_conn, int state)
2686 mutex_lock(&isert_conn->conn_mutex);
2687 ret = (isert_conn->state == state);
2688 mutex_unlock(&isert_conn->conn_mutex);
2693 static void isert_free_conn(struct iscsi_conn *conn)
2695 struct isert_conn *isert_conn = conn->context;
2697 pr_debug("isert_free_conn: Starting \n");
2699 * Decrement post_send_buf_count for special case when called
2700 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2702 mutex_lock(&isert_conn->conn_mutex);
2703 if (isert_conn->logout_posted)
2704 atomic_dec(&isert_conn->post_send_buf_count);
2706 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2707 pr_debug("Calling rdma_disconnect from isert_free_conn\n");
2708 rdma_disconnect(isert_conn->conn_cm_id);
2711 * Only wait for conn_wait_comp_err if the isert_conn made it
2712 * into full feature phase..
2714 if (isert_conn->state == ISER_CONN_UP) {
2715 pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2717 mutex_unlock(&isert_conn->conn_mutex);
2719 wait_event(isert_conn->conn_wait_comp_err,
2720 (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
2722 wait_event(isert_conn->conn_wait,
2723 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2725 isert_put_conn(isert_conn);
2728 if (isert_conn->state == ISER_CONN_INIT) {
2729 mutex_unlock(&isert_conn->conn_mutex);
2730 isert_put_conn(isert_conn);
2733 pr_debug("isert_free_conn: wait_event conn_wait %d\n",
2735 mutex_unlock(&isert_conn->conn_mutex);
2737 wait_event(isert_conn->conn_wait,
2738 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2740 isert_put_conn(isert_conn);
2743 static struct iscsit_transport iser_target_transport = {
2745 .transport_type = ISCSI_INFINIBAND,
2746 .priv_size = sizeof(struct isert_cmd),
2747 .owner = THIS_MODULE,
2748 .iscsit_setup_np = isert_setup_np,
2749 .iscsit_accept_np = isert_accept_np,
2750 .iscsit_free_np = isert_free_np,
2751 .iscsit_free_conn = isert_free_conn,
2752 .iscsit_get_login_rx = isert_get_login_rx,
2753 .iscsit_put_login_tx = isert_put_login_tx,
2754 .iscsit_immediate_queue = isert_immediate_queue,
2755 .iscsit_response_queue = isert_response_queue,
2756 .iscsit_get_dataout = isert_get_dataout,
2757 .iscsit_queue_data_in = isert_put_datain,
2758 .iscsit_queue_status = isert_put_response,
2761 static int __init isert_init(void)
2765 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2767 pr_err("Unable to allocate isert_rx_wq\n");
2771 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2772 if (!isert_comp_wq) {
2773 pr_err("Unable to allocate isert_comp_wq\n");
2778 iscsit_register_transport(&iser_target_transport);
2779 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2783 destroy_workqueue(isert_rx_wq);
2787 static void __exit isert_exit(void)
2789 destroy_workqueue(isert_comp_wq);
2790 destroy_workqueue(isert_rx_wq);
2791 iscsit_unregister_transport(&iser_target_transport);
2792 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2795 MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2796 MODULE_VERSION("0.1");
2797 MODULE_AUTHOR("nab@Linux-iSCSI.org");
2798 MODULE_LICENSE("GPL");
2800 module_init(isert_init);
2801 module_exit(isert_exit);