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