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