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