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