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