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