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