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