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