blob: 2746bb857e572fc825666a7a27df65219942f970 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2013 Datera, Inc.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08005 *
6 * Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ****************************************************************************/
18
19#include <linux/string.h>
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/socket.h>
23#include <linux/in.h>
24#include <linux/in6.h>
Nicholas Bellinger95b60f02013-11-05 13:16:12 -080025#include <linux/llist.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080026#include <rdma/ib_verbs.h>
27#include <rdma/rdma_cm.h>
28#include <target/target_core_base.h>
29#include <target/target_core_fabric.h>
30#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg531b7bf2014-04-29 13:13:45 +030031#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080032
33#include "isert_proto.h"
34#include "ib_isert.h"
35
36#define ISERT_MAX_CONN 8
37#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
38#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
39
40static DEFINE_MUTEX(device_list_mutex);
41static LIST_HEAD(device_list);
42static struct workqueue_struct *isert_rx_wq;
43static struct workqueue_struct *isert_comp_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080044
45static void
Vu Phamd40945d2013-08-28 23:23:34 +030046isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
47static int
48isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
49 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030050static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020051isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030052static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020053isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
54 struct isert_rdma_wr *wr);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +020055static int
56isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
Vu Phamd40945d2013-08-28 23:23:34 +030057
58static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080059isert_qp_event_callback(struct ib_event *e, void *context)
60{
61 struct isert_conn *isert_conn = (struct isert_conn *)context;
62
63 pr_err("isert_qp_event_callback event: %d\n", e->event);
64 switch (e->event) {
65 case IB_EVENT_COMM_EST:
66 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
67 break;
68 case IB_EVENT_QP_LAST_WQE_REACHED:
69 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
70 break;
71 default:
72 break;
73 }
74}
75
76static int
77isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
78{
79 int ret;
80
81 ret = ib_query_device(ib_dev, devattr);
82 if (ret) {
83 pr_err("ib_query_device() failed: %d\n", ret);
84 return ret;
85 }
86 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
87 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
88
89 return 0;
90}
91
92static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +020093isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
94 u8 protection)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080095{
96 struct isert_device *device = isert_conn->conn_device;
97 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080098 int ret, index, min_index = 0;
99
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800100 mutex_lock(&device_list_mutex);
101 for (index = 0; index < device->cqs_used; index++)
102 if (device->cq_active_qps[index] <
103 device->cq_active_qps[min_index])
104 min_index = index;
105 device->cq_active_qps[min_index]++;
106 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
107 mutex_unlock(&device_list_mutex);
108
109 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
110 attr.event_handler = isert_qp_event_callback;
111 attr.qp_context = isert_conn;
112 attr.send_cq = device->dev_tx_cq[min_index];
113 attr.recv_cq = device->dev_rx_cq[min_index];
114 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
115 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
116 /*
117 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
Or Gerlitzf57915c2014-10-22 14:55:49 -0700118 * work-around for RDMA_READs with ConnectX-2.
119 *
120 * Also, still make sure to have at least two SGEs for
121 * outgoing control PDU responses.
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800122 */
Or Gerlitzf57915c2014-10-22 14:55:49 -0700123 attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800124 isert_conn->max_sge = attr.cap.max_send_sge;
125
126 attr.cap.max_recv_sge = 1;
127 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
128 attr.qp_type = IB_QPT_RC;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200129 if (protection)
130 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800131
132 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
133 cma_id->device);
134 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
135 isert_conn->conn_pd->device);
136
137 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
138 if (ret) {
139 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
140 return ret;
141 }
142 isert_conn->conn_qp = cma_id->qp;
143 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
144
145 return 0;
146}
147
148static void
149isert_cq_event_callback(struct ib_event *e, void *context)
150{
151 pr_debug("isert_cq_event_callback event: %d\n", e->event);
152}
153
154static int
155isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
156{
157 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
158 struct iser_rx_desc *rx_desc;
159 struct ib_sge *rx_sg;
160 u64 dma_addr;
161 int i, j;
162
163 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
164 sizeof(struct iser_rx_desc), GFP_KERNEL);
165 if (!isert_conn->conn_rx_descs)
166 goto fail;
167
168 rx_desc = isert_conn->conn_rx_descs;
169
170 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
171 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
172 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
173 if (ib_dma_mapping_error(ib_dev, dma_addr))
174 goto dma_map_fail;
175
176 rx_desc->dma_addr = dma_addr;
177
178 rx_sg = &rx_desc->rx_sg;
179 rx_sg->addr = rx_desc->dma_addr;
180 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
181 rx_sg->lkey = isert_conn->conn_mr->lkey;
182 }
183
184 isert_conn->conn_rx_desc_head = 0;
185 return 0;
186
187dma_map_fail:
188 rx_desc = isert_conn->conn_rx_descs;
189 for (j = 0; j < i; j++, rx_desc++) {
190 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
191 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
192 }
193 kfree(isert_conn->conn_rx_descs);
194 isert_conn->conn_rx_descs = NULL;
195fail:
196 return -ENOMEM;
197}
198
199static void
200isert_free_rx_descriptors(struct isert_conn *isert_conn)
201{
202 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
203 struct iser_rx_desc *rx_desc;
204 int i;
205
206 if (!isert_conn->conn_rx_descs)
207 return;
208
209 rx_desc = isert_conn->conn_rx_descs;
210 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
211 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
212 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
213 }
214
215 kfree(isert_conn->conn_rx_descs);
216 isert_conn->conn_rx_descs = NULL;
217}
218
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800219static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800220static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800221static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800222static void isert_cq_rx_callback(struct ib_cq *, void *);
223
224static int
225isert_create_device_ib_res(struct isert_device *device)
226{
227 struct ib_device *ib_dev = device->ib_device;
228 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300229 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800230 int ret = 0, i, j;
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000231 int max_rx_cqe, max_tx_cqe;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800232
Vu Pham59464ef2013-08-28 23:23:35 +0300233 dev_attr = &device->dev_attr;
234 ret = isert_query_device(ib_dev, dev_attr);
235 if (ret)
236 return ret;
237
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000238 max_rx_cqe = min(ISER_MAX_RX_CQ_LEN, dev_attr->max_cqe);
239 max_tx_cqe = min(ISER_MAX_TX_CQ_LEN, dev_attr->max_cqe);
240
Vu Phamd40945d2013-08-28 23:23:34 +0300241 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200242 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
243 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200244 device->use_fastreg = 1;
245 device->reg_rdma_mem = isert_reg_rdma;
246 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300247 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200248 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300249 device->reg_rdma_mem = isert_map_rdma;
250 device->unreg_rdma_mem = isert_unmap_cmd;
251 }
Vu Phamd40945d2013-08-28 23:23:34 +0300252
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200253 /* Check signature cap */
254 device->pi_capable = dev_attr->device_cap_flags &
255 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
256
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800257 device->cqs_used = min_t(int, num_online_cpus(),
258 device->ib_device->num_comp_vectors);
259 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200260 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200261 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800262 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200263 device->ib_device->num_comp_vectors, device->use_fastreg,
264 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800265 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
266 device->cqs_used, GFP_KERNEL);
267 if (!device->cq_desc) {
268 pr_err("Unable to allocate device->cq_desc\n");
269 return -ENOMEM;
270 }
271 cq_desc = device->cq_desc;
272
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800273 for (i = 0; i < device->cqs_used; i++) {
274 cq_desc[i].device = device;
275 cq_desc[i].cq_index = i;
276
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800277 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800278 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
279 isert_cq_rx_callback,
280 isert_cq_event_callback,
281 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000282 max_rx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800283 if (IS_ERR(device->dev_rx_cq[i])) {
284 ret = PTR_ERR(device->dev_rx_cq[i]);
285 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800286 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800287 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800288
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800289 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800290 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
291 isert_cq_tx_callback,
292 isert_cq_event_callback,
293 (void *)&cq_desc[i],
Chris Mooreb1a5ad02014-11-04 16:28:29 +0000294 max_tx_cqe, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800295 if (IS_ERR(device->dev_tx_cq[i])) {
296 ret = PTR_ERR(device->dev_tx_cq[i]);
297 device->dev_tx_cq[i] = NULL;
298 goto out_cq;
299 }
300
301 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
302 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800303 goto out_cq;
304
Wei Yongjun94a71112013-10-29 09:56:34 +0800305 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
306 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800307 goto out_cq;
308 }
309
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800310 return 0;
311
312out_cq:
313 for (j = 0; j < i; j++) {
314 cq_desc = &device->cq_desc[j];
315
316 if (device->dev_rx_cq[j]) {
317 cancel_work_sync(&cq_desc->cq_rx_work);
318 ib_destroy_cq(device->dev_rx_cq[j]);
319 }
320 if (device->dev_tx_cq[j]) {
321 cancel_work_sync(&cq_desc->cq_tx_work);
322 ib_destroy_cq(device->dev_tx_cq[j]);
323 }
324 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800325 kfree(device->cq_desc);
326
327 return ret;
328}
329
330static void
331isert_free_device_ib_res(struct isert_device *device)
332{
333 struct isert_cq_desc *cq_desc;
334 int i;
335
336 for (i = 0; i < device->cqs_used; i++) {
337 cq_desc = &device->cq_desc[i];
338
339 cancel_work_sync(&cq_desc->cq_rx_work);
340 cancel_work_sync(&cq_desc->cq_tx_work);
341 ib_destroy_cq(device->dev_rx_cq[i]);
342 ib_destroy_cq(device->dev_tx_cq[i]);
343 device->dev_rx_cq[i] = NULL;
344 device->dev_tx_cq[i] = NULL;
345 }
346
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800347 kfree(device->cq_desc);
348}
349
350static void
351isert_device_try_release(struct isert_device *device)
352{
353 mutex_lock(&device_list_mutex);
354 device->refcount--;
355 if (!device->refcount) {
356 isert_free_device_ib_res(device);
357 list_del(&device->dev_node);
358 kfree(device);
359 }
360 mutex_unlock(&device_list_mutex);
361}
362
363static struct isert_device *
364isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
365{
366 struct isert_device *device;
367 int ret;
368
369 mutex_lock(&device_list_mutex);
370 list_for_each_entry(device, &device_list, dev_node) {
371 if (device->ib_device->node_guid == cma_id->device->node_guid) {
372 device->refcount++;
373 mutex_unlock(&device_list_mutex);
374 return device;
375 }
376 }
377
378 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
379 if (!device) {
380 mutex_unlock(&device_list_mutex);
381 return ERR_PTR(-ENOMEM);
382 }
383
384 INIT_LIST_HEAD(&device->dev_node);
385
386 device->ib_device = cma_id->device;
387 ret = isert_create_device_ib_res(device);
388 if (ret) {
389 kfree(device);
390 mutex_unlock(&device_list_mutex);
391 return ERR_PTR(ret);
392 }
393
394 device->refcount++;
395 list_add_tail(&device->dev_node, &device_list);
396 mutex_unlock(&device_list_mutex);
397
398 return device;
399}
400
Vu Pham59464ef2013-08-28 23:23:35 +0300401static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200402isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300403{
404 struct fast_reg_descriptor *fr_desc, *tmp;
405 int i = 0;
406
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200407 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300408 return;
409
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200410 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300411
412 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200413 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300414 list_del(&fr_desc->list);
415 ib_free_fast_reg_page_list(fr_desc->data_frpl);
416 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200417 if (fr_desc->pi_ctx) {
418 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
419 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
420 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
421 kfree(fr_desc->pi_ctx);
422 }
Vu Pham59464ef2013-08-28 23:23:35 +0300423 kfree(fr_desc);
424 ++i;
425 }
426
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200427 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300428 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200429 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300430}
431
432static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200433isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200434 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200435{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200436 int ret;
437
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200438 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
439 ISCSI_ISER_SG_TABLESIZE);
440 if (IS_ERR(fr_desc->data_frpl)) {
441 pr_err("Failed to allocate data frpl err=%ld\n",
442 PTR_ERR(fr_desc->data_frpl));
443 return PTR_ERR(fr_desc->data_frpl);
444 }
445
446 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
447 if (IS_ERR(fr_desc->data_mr)) {
448 pr_err("Failed to allocate data frmr err=%ld\n",
449 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200450 ret = PTR_ERR(fr_desc->data_mr);
451 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200452 }
453 pr_debug("Create fr_desc %p page_list %p\n",
454 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200455 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200456
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200457 if (protection) {
458 struct ib_mr_init_attr mr_init_attr = {0};
459 struct pi_context *pi_ctx;
460
461 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
462 if (!fr_desc->pi_ctx) {
463 pr_err("Failed to allocate pi context\n");
464 ret = -ENOMEM;
465 goto err_data_mr;
466 }
467 pi_ctx = fr_desc->pi_ctx;
468
469 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
470 ISCSI_ISER_SG_TABLESIZE);
471 if (IS_ERR(pi_ctx->prot_frpl)) {
472 pr_err("Failed to allocate prot frpl err=%ld\n",
473 PTR_ERR(pi_ctx->prot_frpl));
474 ret = PTR_ERR(pi_ctx->prot_frpl);
475 goto err_pi_ctx;
476 }
477
478 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
479 if (IS_ERR(pi_ctx->prot_mr)) {
480 pr_err("Failed to allocate prot frmr err=%ld\n",
481 PTR_ERR(pi_ctx->prot_mr));
482 ret = PTR_ERR(pi_ctx->prot_mr);
483 goto err_prot_frpl;
484 }
485 fr_desc->ind |= ISERT_PROT_KEY_VALID;
486
487 mr_init_attr.max_reg_descriptors = 2;
488 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
489 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
490 if (IS_ERR(pi_ctx->sig_mr)) {
491 pr_err("Failed to allocate signature enabled mr err=%ld\n",
492 PTR_ERR(pi_ctx->sig_mr));
493 ret = PTR_ERR(pi_ctx->sig_mr);
494 goto err_prot_mr;
495 }
496 fr_desc->ind |= ISERT_SIG_KEY_VALID;
497 }
498 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200499
500 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200501err_prot_mr:
502 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
503err_prot_frpl:
504 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
505err_pi_ctx:
506 kfree(fr_desc->pi_ctx);
507err_data_mr:
508 ib_dereg_mr(fr_desc->data_mr);
509err_data_frpl:
510 ib_free_fast_reg_page_list(fr_desc->data_frpl);
511
512 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200513}
514
515static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200516isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
Vu Pham59464ef2013-08-28 23:23:35 +0300517{
518 struct fast_reg_descriptor *fr_desc;
519 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700520 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
521 struct se_node_acl *se_nacl = se_sess->se_node_acl;
522 int i, ret, tag_num;
523 /*
524 * Setup the number of FRMRs based upon the number of tags
525 * available to session in iscsi_target_locate_portal().
526 */
527 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
528 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300529
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200530 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700531 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300532 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
533 if (!fr_desc) {
534 pr_err("Failed to allocate fast_reg descriptor\n");
535 ret = -ENOMEM;
536 goto err;
537 }
538
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200539 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200540 isert_conn->conn_pd, fr_desc,
541 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200542 if (ret) {
543 pr_err("Failed to create fastreg descriptor err=%d\n",
544 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800545 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300546 goto err;
547 }
548
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200549 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
550 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300551 }
552
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200553 pr_debug("Creating conn %p fastreg pool size=%d",
554 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300555
556 return 0;
557
558err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200559 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300560 return ret;
561}
562
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800563static int
564isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
565{
566 struct iscsi_np *np = cma_id->context;
567 struct isert_np *isert_np = np->np_context;
568 struct isert_conn *isert_conn;
569 struct isert_device *device;
570 struct ib_device *ib_dev = cma_id->device;
571 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300572 u8 pi_support;
573
574 spin_lock_bh(&np->np_thread_lock);
575 if (!np->enabled) {
576 spin_unlock_bh(&np->np_thread_lock);
577 pr_debug("iscsi_np is not enabled, reject connect request\n");
578 return rdma_reject(cma_id, NULL, 0);
579 }
580 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800581
582 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
583 cma_id, cma_id->context);
584
585 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
586 if (!isert_conn) {
587 pr_err("Unable to allocate isert_conn\n");
588 return -ENOMEM;
589 }
590 isert_conn->state = ISER_CONN_INIT;
591 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
592 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800593 init_completion(&isert_conn->conn_wait);
594 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800595 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700596 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300597 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700598 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800599
600 cma_id->context = isert_conn;
601 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800602
603 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
604 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
605 if (!isert_conn->login_buf) {
606 pr_err("Unable to allocate isert_conn->login_buf\n");
607 ret = -ENOMEM;
608 goto out;
609 }
610
611 isert_conn->login_req_buf = isert_conn->login_buf;
612 isert_conn->login_rsp_buf = isert_conn->login_buf +
613 ISCSI_DEF_MAX_RECV_SEG_LEN;
614 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
615 isert_conn->login_buf, isert_conn->login_req_buf,
616 isert_conn->login_rsp_buf);
617
618 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
619 (void *)isert_conn->login_req_buf,
620 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
621
622 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
623 if (ret) {
624 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
625 ret);
626 isert_conn->login_req_dma = 0;
627 goto out_login_buf;
628 }
629
630 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
631 (void *)isert_conn->login_rsp_buf,
632 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
633
634 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
635 if (ret) {
636 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
637 ret);
638 isert_conn->login_rsp_dma = 0;
639 goto out_req_dma_map;
640 }
641
642 device = isert_device_find_by_ib_dev(cma_id);
643 if (IS_ERR(device)) {
644 ret = PTR_ERR(device);
645 goto out_rsp_dma_map;
646 }
647
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300648 /* Set max inflight RDMA READ requests */
649 isert_conn->initiator_depth = min_t(u8,
650 event->param.conn.initiator_depth,
651 device->dev_attr.max_qp_init_rd_atom);
652 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
653
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800654 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200655 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
656 if (IS_ERR(isert_conn->conn_pd)) {
657 ret = PTR_ERR(isert_conn->conn_pd);
658 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
659 isert_conn, ret);
660 goto out_pd;
661 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800662
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200663 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
664 IB_ACCESS_LOCAL_WRITE);
665 if (IS_ERR(isert_conn->conn_mr)) {
666 ret = PTR_ERR(isert_conn->conn_mr);
667 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
668 isert_conn, ret);
669 goto out_mr;
670 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800671
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300672 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200673 if (pi_support && !device->pi_capable) {
Sagi Grimberg5f80ff82014-05-20 13:28:11 +0300674 pr_err("Protection information requested but not supported, "
675 "rejecting connect request\n");
676 ret = rdma_reject(cma_id, NULL, 0);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200677 goto out_mr;
678 }
679
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200680 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800681 if (ret)
682 goto out_conn_dev;
683
684 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300685 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 mutex_unlock(&isert_np->np_accept_mutex);
687
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300688 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
689 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800690 return 0;
691
692out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200693 ib_dereg_mr(isert_conn->conn_mr);
694out_mr:
695 ib_dealloc_pd(isert_conn->conn_pd);
696out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800697 isert_device_try_release(device);
698out_rsp_dma_map:
699 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
700 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
701out_req_dma_map:
702 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
703 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
704out_login_buf:
705 kfree(isert_conn->login_buf);
706out:
707 kfree(isert_conn);
708 return ret;
709}
710
711static void
712isert_connect_release(struct isert_conn *isert_conn)
713{
714 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
715 struct isert_device *device = isert_conn->conn_device;
716 int cq_index;
717
718 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
719
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200720 if (device && device->use_fastreg)
721 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300722
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800723 if (isert_conn->conn_qp) {
724 cq_index = ((struct isert_cq_desc *)
725 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
726 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
727 isert_conn->conn_device->cq_active_qps[cq_index]--;
728
729 rdma_destroy_qp(isert_conn->conn_cm_id);
730 }
731
732 isert_free_rx_descriptors(isert_conn);
733 rdma_destroy_id(isert_conn->conn_cm_id);
734
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200735 ib_dereg_mr(isert_conn->conn_mr);
736 ib_dealloc_pd(isert_conn->conn_pd);
737
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800738 if (isert_conn->login_buf) {
739 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
740 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
741 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
742 ISCSI_DEF_MAX_RECV_SEG_LEN,
743 DMA_FROM_DEVICE);
744 kfree(isert_conn->login_buf);
745 }
746 kfree(isert_conn);
747
748 if (device)
749 isert_device_try_release(device);
750
751 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
752}
753
754static void
755isert_connected_handler(struct rdma_cm_id *cma_id)
756{
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300757 struct isert_conn *isert_conn = cma_id->context;
758
759 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800760}
761
762static void
763isert_release_conn_kref(struct kref *kref)
764{
765 struct isert_conn *isert_conn = container_of(kref,
766 struct isert_conn, conn_kref);
767
768 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
769 current->comm, current->pid);
770
771 isert_connect_release(isert_conn);
772}
773
774static void
775isert_put_conn(struct isert_conn *isert_conn)
776{
777 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
778}
779
Sagi Grimberg954f2372014-12-02 16:57:17 +0200780/**
781 * isert_conn_terminate() - Initiate connection termination
782 * @isert_conn: isert connection struct
783 *
784 * Notes:
785 * In case the connection state is UP, move state
786 * to TEMINATING and start teardown sequence (rdma_disconnect).
787 *
788 * This routine must be called with conn_mutex held. Thus it is
789 * safe to call multiple times.
790 */
791static void
792isert_conn_terminate(struct isert_conn *isert_conn)
793{
794 int err;
795
796 if (isert_conn->state == ISER_CONN_UP) {
797 isert_conn->state = ISER_CONN_TERMINATING;
798 pr_info("Terminating conn %p state %d\n",
799 isert_conn, isert_conn->state);
800 err = rdma_disconnect(isert_conn->conn_cm_id);
801 if (err)
802 pr_warn("Failed rdma_disconnect isert_conn %p\n",
803 isert_conn);
804 }
805}
806
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800807static void
808isert_disconnect_work(struct work_struct *work)
809{
810 struct isert_conn *isert_conn = container_of(work,
811 struct isert_conn, conn_logout_work);
812
813 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700814 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +0200815 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700816 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800817
Sagi Grimberg954f2372014-12-02 16:57:17 +0200818 pr_info("conn %p completing conn_wait\n", isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800819 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800820}
821
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700822static int
Sagi Grimberg954f2372014-12-02 16:57:17 +0200823isert_disconnected_handler(struct rdma_cm_id *cma_id)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800824{
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700825 struct isert_conn *isert_conn;
826
827 if (!cma_id->qp) {
828 struct isert_np *isert_np = cma_id->context;
829
830 isert_np->np_cm_id = NULL;
831 return -1;
832 }
833
834 isert_conn = (struct isert_conn *)cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800835
836 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
837 schedule_work(&isert_conn->conn_logout_work);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700838
839 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800840}
841
Sagi Grimberg954f2372014-12-02 16:57:17 +0200842static void
843isert_connect_error(struct rdma_cm_id *cma_id)
844{
845 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
846
847 isert_put_conn(isert_conn);
848}
849
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800850static int
851isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
852{
853 int ret = 0;
854
855 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
856 event->event, event->status, cma_id->context, cma_id);
857
858 switch (event->event) {
859 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800860 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700861 if (ret)
862 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
863 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800864 break;
865 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800866 isert_connected_handler(cma_id);
867 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300868 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
869 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
870 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
Sagi Grimberg88c40152014-05-19 17:44:24 +0300871 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimberg954f2372014-12-02 16:57:17 +0200872 ret = isert_disconnected_handler(cma_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800873 break;
Sagi Grimberg954f2372014-12-02 16:57:17 +0200874 case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
875 case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800876 case RDMA_CM_EVENT_CONNECT_ERROR:
Sagi Grimberg954f2372014-12-02 16:57:17 +0200877 isert_connect_error(cma_id);
878 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800879 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300880 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800881 break;
882 }
883
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800884 return ret;
885}
886
887static int
888isert_post_recv(struct isert_conn *isert_conn, u32 count)
889{
890 struct ib_recv_wr *rx_wr, *rx_wr_failed;
891 int i, ret;
892 unsigned int rx_head = isert_conn->conn_rx_desc_head;
893 struct iser_rx_desc *rx_desc;
894
895 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
896 rx_desc = &isert_conn->conn_rx_descs[rx_head];
897 rx_wr->wr_id = (unsigned long)rx_desc;
898 rx_wr->sg_list = &rx_desc->rx_sg;
899 rx_wr->num_sge = 1;
900 rx_wr->next = rx_wr + 1;
901 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
902 }
903
904 rx_wr--;
905 rx_wr->next = NULL; /* mark end of work requests list */
906
907 isert_conn->post_recv_buf_count += count;
908 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
909 &rx_wr_failed);
910 if (ret) {
911 pr_err("ib_post_recv() failed with ret: %d\n", ret);
912 isert_conn->post_recv_buf_count -= count;
913 } else {
914 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
915 isert_conn->conn_rx_desc_head = rx_head;
916 }
917 return ret;
918}
919
920static int
921isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
922{
923 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
924 struct ib_send_wr send_wr, *send_wr_failed;
925 int ret;
926
927 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
928 ISER_HEADERS_LEN, DMA_TO_DEVICE);
929
930 send_wr.next = NULL;
931 send_wr.wr_id = (unsigned long)tx_desc;
932 send_wr.sg_list = tx_desc->tx_sg;
933 send_wr.num_sge = tx_desc->num_sge;
934 send_wr.opcode = IB_WR_SEND;
935 send_wr.send_flags = IB_SEND_SIGNALED;
936
937 atomic_inc(&isert_conn->post_send_buf_count);
938
939 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
940 if (ret) {
941 pr_err("ib_post_send() failed, ret: %d\n", ret);
942 atomic_dec(&isert_conn->post_send_buf_count);
943 }
944
945 return ret;
946}
947
948static void
949isert_create_send_desc(struct isert_conn *isert_conn,
950 struct isert_cmd *isert_cmd,
951 struct iser_tx_desc *tx_desc)
952{
953 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
954
955 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
956 ISER_HEADERS_LEN, DMA_TO_DEVICE);
957
958 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
959 tx_desc->iser_header.flags = ISER_VER;
960
961 tx_desc->num_sge = 1;
962 tx_desc->isert_cmd = isert_cmd;
963
964 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
965 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
966 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
967 }
968}
969
970static int
971isert_init_tx_hdrs(struct isert_conn *isert_conn,
972 struct iser_tx_desc *tx_desc)
973{
974 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
975 u64 dma_addr;
976
977 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
978 ISER_HEADERS_LEN, DMA_TO_DEVICE);
979 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
980 pr_err("ib_dma_mapping_error() failed\n");
981 return -ENOMEM;
982 }
983
984 tx_desc->dma_addr = dma_addr;
985 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
986 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
987 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
988
989 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
990 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
991 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
992
993 return 0;
994}
995
996static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800997isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
998 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800999{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001000 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
1001
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001002 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
1003 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1004 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001005 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001006 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001007 /*
1008 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
1009 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
1010 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001011 mutex_lock(&isert_conn->conn_mutex);
1012 if (coalesce && isert_conn->state == ISER_CONN_UP &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001013 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001014 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001015 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001016 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001017 return;
1018 }
1019 isert_conn->conn_comp_batch = 0;
1020 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001021 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001022
1023 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001024}
1025
1026static int
1027isert_rdma_post_recvl(struct isert_conn *isert_conn)
1028{
1029 struct ib_recv_wr rx_wr, *rx_wr_fail;
1030 struct ib_sge sge;
1031 int ret;
1032
1033 memset(&sge, 0, sizeof(struct ib_sge));
1034 sge.addr = isert_conn->login_req_dma;
1035 sge.length = ISER_RX_LOGIN_SIZE;
1036 sge.lkey = isert_conn->conn_mr->lkey;
1037
1038 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1039 sge.addr, sge.length, sge.lkey);
1040
1041 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1042 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1043 rx_wr.sg_list = &sge;
1044 rx_wr.num_sge = 1;
1045
1046 isert_conn->post_recv_buf_count++;
1047 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1048 if (ret) {
1049 pr_err("ib_post_recv() failed: %d\n", ret);
1050 isert_conn->post_recv_buf_count--;
1051 }
1052
1053 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1054 return ret;
1055}
1056
1057static int
1058isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1059 u32 length)
1060{
1061 struct isert_conn *isert_conn = conn->context;
1062 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1063 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1064 int ret;
1065
1066 isert_create_send_desc(isert_conn, NULL, tx_desc);
1067
1068 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1069 sizeof(struct iscsi_hdr));
1070
1071 isert_init_tx_hdrs(isert_conn, tx_desc);
1072
1073 if (length > 0) {
1074 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1075
1076 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1077 length, DMA_TO_DEVICE);
1078
1079 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1080
1081 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1082 length, DMA_TO_DEVICE);
1083
1084 tx_dsg->addr = isert_conn->login_rsp_dma;
1085 tx_dsg->length = length;
1086 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1087 tx_desc->num_sge = 2;
1088 }
1089 if (!login->login_failed) {
1090 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001091 if (!conn->sess->sess_ops->SessionType &&
1092 isert_conn->conn_device->use_fastreg) {
1093 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001094 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1095
1096 ret = isert_conn_create_fastreg_pool(isert_conn,
1097 pi_support);
1098 if (ret) {
1099 pr_err("Conn: %p failed to create"
1100 " fastreg pool\n", isert_conn);
1101 return ret;
1102 }
1103 }
1104
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001105 ret = isert_alloc_rx_descriptors(isert_conn);
1106 if (ret)
1107 return ret;
1108
1109 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1110 if (ret)
1111 return ret;
1112
1113 isert_conn->state = ISER_CONN_UP;
1114 goto post_send;
1115 }
1116
1117 ret = isert_rdma_post_recvl(isert_conn);
1118 if (ret)
1119 return ret;
1120 }
1121post_send:
1122 ret = isert_post_send(isert_conn, tx_desc);
1123 if (ret)
1124 return ret;
1125
1126 return 0;
1127}
1128
1129static void
1130isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1131 struct isert_conn *isert_conn)
1132{
1133 struct iscsi_conn *conn = isert_conn->conn;
1134 struct iscsi_login *login = conn->conn_login;
1135 int size;
1136
1137 if (!login) {
1138 pr_err("conn->conn_login is NULL\n");
1139 dump_stack();
1140 return;
1141 }
1142
1143 if (login->first_request) {
1144 struct iscsi_login_req *login_req =
1145 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1146 /*
1147 * Setup the initial iscsi_login values from the leading
1148 * login request PDU.
1149 */
1150 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1151 login->current_stage =
1152 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1153 >> 2;
1154 login->version_min = login_req->min_version;
1155 login->version_max = login_req->max_version;
1156 memcpy(login->isid, login_req->isid, 6);
1157 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1158 login->init_task_tag = login_req->itt;
1159 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1160 login->cid = be16_to_cpu(login_req->cid);
1161 login->tsih = be16_to_cpu(login_req->tsih);
1162 }
1163
1164 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1165
1166 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1167 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1168 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1169 memcpy(login->req_buf, &rx_desc->data[0], size);
1170
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001171 if (login->first_request) {
1172 complete(&isert_conn->conn_login_comp);
1173 return;
1174 }
1175 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001176}
1177
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001178static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001179*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001180{
1181 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1182 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001183 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001184
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001185 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001186 if (!cmd) {
1187 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001188 return NULL;
1189 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001190 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001192 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001193
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001194 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001195}
1196
1197static int
1198isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001199 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1200 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001201{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001202 struct iscsi_conn *conn = isert_conn->conn;
1203 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1204 struct scatterlist *sg;
1205 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1206 bool dump_payload = false;
1207
1208 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1209 if (rc < 0)
1210 return rc;
1211
1212 imm_data = cmd->immediate_data;
1213 imm_data_len = cmd->first_burst_len;
1214 unsol_data = cmd->unsolicited_data;
1215
1216 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1217 if (rc < 0) {
1218 return 0;
1219 } else if (rc > 0) {
1220 dump_payload = true;
1221 goto sequence_cmd;
1222 }
1223
1224 if (!imm_data)
1225 return 0;
1226
1227 sg = &cmd->se_cmd.t_data_sg[0];
1228 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1229
1230 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1231 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1232
1233 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1234
1235 cmd->write_data_done += imm_data_len;
1236
1237 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1238 spin_lock_bh(&cmd->istate_lock);
1239 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1240 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1241 spin_unlock_bh(&cmd->istate_lock);
1242 }
1243
1244sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001245 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001246
1247 if (!rc && dump_payload == false && unsol_data)
1248 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001249 else if (dump_payload && imm_data)
1250 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252 return 0;
1253}
1254
1255static int
1256isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1257 struct iser_rx_desc *rx_desc, unsigned char *buf)
1258{
1259 struct scatterlist *sg_start;
1260 struct iscsi_conn *conn = isert_conn->conn;
1261 struct iscsi_cmd *cmd = NULL;
1262 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1263 u32 unsol_data_len = ntoh24(hdr->dlength);
1264 int rc, sg_nents, sg_off, page_off;
1265
1266 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1267 if (rc < 0)
1268 return rc;
1269 else if (!cmd)
1270 return 0;
1271 /*
1272 * FIXME: Unexpected unsolicited_data out
1273 */
1274 if (!cmd->unsolicited_data) {
1275 pr_err("Received unexpected solicited data payload\n");
1276 dump_stack();
1277 return -1;
1278 }
1279
1280 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1281 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1282
1283 sg_off = cmd->write_data_done / PAGE_SIZE;
1284 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1285 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1286 page_off = cmd->write_data_done % PAGE_SIZE;
1287 /*
1288 * FIXME: Non page-aligned unsolicited_data out
1289 */
1290 if (page_off) {
1291 pr_err("Received unexpected non-page aligned data payload\n");
1292 dump_stack();
1293 return -1;
1294 }
1295 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1296 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1297
1298 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1299 unsol_data_len);
1300
1301 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1302 if (rc < 0)
1303 return rc;
1304
1305 return 0;
1306}
1307
1308static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001309isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001310 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1311 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001312{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001313 struct iscsi_conn *conn = isert_conn->conn;
1314 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1315 int rc;
1316
1317 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1318 if (rc < 0)
1319 return rc;
1320 /*
1321 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1322 */
1323
1324 return iscsit_process_nop_out(conn, cmd, hdr);
1325}
1326
1327static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001328isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001329 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1330 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001331{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001332 struct iscsi_conn *conn = isert_conn->conn;
1333 u32 payload_length = ntoh24(hdr->dlength);
1334 int rc;
1335 unsigned char *text_in;
1336
1337 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1338 if (rc < 0)
1339 return rc;
1340
1341 text_in = kzalloc(payload_length, GFP_KERNEL);
1342 if (!text_in) {
1343 pr_err("Unable to allocate text_in of payload_length: %u\n",
1344 payload_length);
1345 return -ENOMEM;
1346 }
1347 cmd->text_in_ptr = text_in;
1348
1349 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1350
1351 return iscsit_process_text_cmd(conn, cmd, hdr);
1352}
1353
1354static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001355isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1356 uint32_t read_stag, uint64_t read_va,
1357 uint32_t write_stag, uint64_t write_va)
1358{
1359 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1360 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001361 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001362 struct iscsi_cmd *cmd;
1363 struct isert_cmd *isert_cmd;
1364 int ret = -EINVAL;
1365 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1366
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001367 if (sess->sess_ops->SessionType &&
1368 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1369 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1370 " ignoring\n", opcode);
1371 return 0;
1372 }
1373
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001374 switch (opcode) {
1375 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001376 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001377 if (!cmd)
1378 break;
1379
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001380 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001381 isert_cmd->read_stag = read_stag;
1382 isert_cmd->read_va = read_va;
1383 isert_cmd->write_stag = write_stag;
1384 isert_cmd->write_va = write_va;
1385
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001386 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001387 rx_desc, (unsigned char *)hdr);
1388 break;
1389 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001390 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001391 if (!cmd)
1392 break;
1393
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001394 isert_cmd = iscsit_priv_cmd(cmd);
1395 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001396 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001397 break;
1398 case ISCSI_OP_SCSI_DATA_OUT:
1399 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1400 (unsigned char *)hdr);
1401 break;
1402 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001403 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001404 if (!cmd)
1405 break;
1406
1407 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1408 (unsigned char *)hdr);
1409 break;
1410 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001411 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001412 if (!cmd)
1413 break;
1414
1415 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1416 if (ret > 0)
1417 wait_for_completion_timeout(&conn->conn_logout_comp,
1418 SECONDS_FOR_LOGOUT_COMP *
1419 HZ);
1420 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001421 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001422 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001423 if (!cmd)
1424 break;
1425
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001426 isert_cmd = iscsit_priv_cmd(cmd);
1427 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001428 rx_desc, (struct iscsi_text *)hdr);
1429 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001430 default:
1431 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1432 dump_stack();
1433 break;
1434 }
1435
1436 return ret;
1437}
1438
1439static void
1440isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1441{
1442 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1443 uint64_t read_va = 0, write_va = 0;
1444 uint32_t read_stag = 0, write_stag = 0;
1445 int rc;
1446
1447 switch (iser_hdr->flags & 0xF0) {
1448 case ISCSI_CTRL:
1449 if (iser_hdr->flags & ISER_RSV) {
1450 read_stag = be32_to_cpu(iser_hdr->read_stag);
1451 read_va = be64_to_cpu(iser_hdr->read_va);
1452 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1453 read_stag, (unsigned long long)read_va);
1454 }
1455 if (iser_hdr->flags & ISER_WSV) {
1456 write_stag = be32_to_cpu(iser_hdr->write_stag);
1457 write_va = be64_to_cpu(iser_hdr->write_va);
1458 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1459 write_stag, (unsigned long long)write_va);
1460 }
1461
1462 pr_debug("ISER ISCSI_CTRL PDU\n");
1463 break;
1464 case ISER_HELLO:
1465 pr_err("iSER Hello message\n");
1466 break;
1467 default:
1468 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1469 break;
1470 }
1471
1472 rc = isert_rx_opcode(isert_conn, rx_desc,
1473 read_stag, read_va, write_stag, write_va);
1474}
1475
1476static void
1477isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1478 unsigned long xfer_len)
1479{
1480 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1481 struct iscsi_hdr *hdr;
1482 u64 rx_dma;
1483 int rx_buflen, outstanding;
1484
1485 if ((char *)desc == isert_conn->login_req_buf) {
1486 rx_dma = isert_conn->login_req_dma;
1487 rx_buflen = ISER_RX_LOGIN_SIZE;
1488 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1489 rx_dma, rx_buflen);
1490 } else {
1491 rx_dma = desc->dma_addr;
1492 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1493 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1494 rx_dma, rx_buflen);
1495 }
1496
1497 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1498
1499 hdr = &desc->iscsi_header;
1500 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1501 hdr->opcode, hdr->itt, hdr->flags,
1502 (int)(xfer_len - ISER_HEADERS_LEN));
1503
1504 if ((char *)desc == isert_conn->login_req_buf)
1505 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1506 isert_conn);
1507 else
1508 isert_rx_do_work(desc, isert_conn);
1509
1510 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1511 DMA_FROM_DEVICE);
1512
1513 isert_conn->post_recv_buf_count--;
1514 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1515 isert_conn->post_recv_buf_count);
1516
1517 if ((char *)desc == isert_conn->login_req_buf)
1518 return;
1519
1520 outstanding = isert_conn->post_recv_buf_count;
1521 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1522 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1523 ISERT_MIN_POSTED_RX);
1524 err = isert_post_recv(isert_conn, count);
1525 if (err) {
1526 pr_err("isert_post_recv() count: %d failed, %d\n",
1527 count, err);
1528 }
1529 }
1530}
1531
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001532static int
1533isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1534 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1535 enum iser_ib_op_code op, struct isert_data_buf *data)
1536{
1537 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1538
1539 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1540 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1541
1542 data->len = length - offset;
1543 data->offset = offset;
1544 data->sg_off = data->offset / PAGE_SIZE;
1545
1546 data->sg = &sg[data->sg_off];
1547 data->nents = min_t(unsigned int, nents - data->sg_off,
1548 ISCSI_ISER_SG_TABLESIZE);
1549 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1550 PAGE_SIZE);
1551
1552 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1553 data->dma_dir);
1554 if (unlikely(!data->dma_nents)) {
1555 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1556 return -EINVAL;
1557 }
1558
1559 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1560 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1561
1562 return 0;
1563}
1564
1565static void
1566isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1567{
1568 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1569
1570 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1571 memset(data, 0, sizeof(*data));
1572}
1573
1574
1575
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001576static void
1577isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1578{
1579 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001580
Vu Pham90ecc6e2013-08-28 23:23:33 +03001581 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001582
1583 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001584 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001585 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001586 }
1587
Vu Pham90ecc6e2013-08-28 23:23:33 +03001588 if (wr->send_wr) {
1589 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1590 kfree(wr->send_wr);
1591 wr->send_wr = NULL;
1592 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593
Vu Pham90ecc6e2013-08-28 23:23:33 +03001594 if (wr->ib_sge) {
1595 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1596 kfree(wr->ib_sge);
1597 wr->ib_sge = NULL;
1598 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599}
1600
1601static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001602isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001603{
1604 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001605 LIST_HEAD(unmap_list);
1606
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001607 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001608
1609 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001610 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001611 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001612 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1613 isert_unmap_data_buf(isert_conn, &wr->prot);
1614 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1615 }
Vu Pham59464ef2013-08-28 23:23:35 +03001616 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001617 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001618 spin_unlock_bh(&isert_conn->conn_lock);
1619 wr->fr_desc = NULL;
1620 }
1621
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001622 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001623 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001624 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001625 }
1626
1627 wr->ib_sge = NULL;
1628 wr->send_wr = NULL;
1629}
1630
1631static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001632isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001633{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001634 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001635 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001636 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001637 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638
1639 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1640
1641 switch (cmd->iscsi_opcode) {
1642 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001643 spin_lock_bh(&conn->cmd_lock);
1644 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001645 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001646 spin_unlock_bh(&conn->cmd_lock);
1647
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001648 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001649 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001650 /*
1651 * Check for special case during comp_err where
1652 * WRITE_PENDING has been handed off from core,
1653 * but requires an extra target_put_sess_cmd()
1654 * before transport_generic_free_cmd() below.
1655 */
1656 if (comp_err &&
1657 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1658 struct se_cmd *se_cmd = &cmd->se_cmd;
1659
1660 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1661 }
1662 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001663
Vu Phamd40945d2013-08-28 23:23:34 +03001664 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001665 transport_generic_free_cmd(&cmd->se_cmd, 0);
1666 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001667 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001668 spin_lock_bh(&conn->cmd_lock);
1669 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001670 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001671 spin_unlock_bh(&conn->cmd_lock);
1672
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001673 transport_generic_free_cmd(&cmd->se_cmd, 0);
1674 break;
1675 case ISCSI_OP_REJECT:
1676 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001677 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001678 spin_lock_bh(&conn->cmd_lock);
1679 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001680 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001681 spin_unlock_bh(&conn->cmd_lock);
1682
1683 /*
1684 * Handle special case for REJECT when iscsi_add_reject*() has
1685 * overwritten the original iscsi_opcode assignment, and the
1686 * associated cmd->se_cmd needs to be released.
1687 */
1688 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001689 pr_debug("Calling transport_generic_free_cmd from"
1690 " isert_put_cmd for 0x%02x\n",
1691 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001692 transport_generic_free_cmd(&cmd->se_cmd, 0);
1693 break;
1694 }
1695 /*
1696 * Fall-through
1697 */
1698 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001699 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001700 break;
1701 }
1702}
1703
1704static void
1705isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1706{
1707 if (tx_desc->dma_addr != 0) {
1708 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1709 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1710 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1711 tx_desc->dma_addr = 0;
1712 }
1713}
1714
1715static void
1716isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001717 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001718{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001719 if (isert_cmd->pdu_buf_dma != 0) {
1720 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1721 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1722 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1723 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001724 }
1725
1726 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001727 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001728}
1729
Sagi Grimberg96b79732014-03-17 12:52:18 +02001730static int
1731isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1732{
1733 struct ib_mr_status mr_status;
1734 int ret;
1735
1736 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1737 if (ret) {
1738 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1739 goto fail_mr_status;
1740 }
1741
1742 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1743 u64 sec_offset_err;
1744 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1745
1746 switch (mr_status.sig_err.err_type) {
1747 case IB_SIG_BAD_GUARD:
1748 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1749 break;
1750 case IB_SIG_BAD_REFTAG:
1751 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1752 break;
1753 case IB_SIG_BAD_APPTAG:
1754 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1755 break;
1756 }
1757 sec_offset_err = mr_status.sig_err.sig_err_offset;
1758 do_div(sec_offset_err, block_size);
1759 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1760
1761 pr_err("isert: PI error found type %d at sector 0x%llx "
1762 "expected 0x%x vs actual 0x%x\n",
1763 mr_status.sig_err.err_type,
1764 (unsigned long long)se_cmd->bad_sector,
1765 mr_status.sig_err.expected,
1766 mr_status.sig_err.actual);
1767 ret = 1;
1768 }
1769
1770fail_mr_status:
1771 return ret;
1772}
1773
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001774static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001775isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1776 struct isert_cmd *isert_cmd)
1777{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001778 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001779 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001780 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001781 struct isert_conn *isert_conn = isert_cmd->conn;
1782 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001783 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001784
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001785 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001786 ret = isert_check_pi_status(se_cmd,
1787 wr->fr_desc->pi_ctx->sig_mr);
1788 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001789 }
1790
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001791 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001792 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001793 if (ret)
1794 transport_send_check_condition_and_sense(se_cmd,
1795 se_cmd->pi_err, 0);
1796 else
1797 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001798}
1799
1800static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001801isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1802 struct isert_cmd *isert_cmd)
1803{
1804 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001805 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001806 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001807 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001808 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001809 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001810
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001811 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001812 ret = isert_check_pi_status(se_cmd,
1813 wr->fr_desc->pi_ctx->sig_mr);
1814 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001815 }
1816
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001817 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001818 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001819 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001820 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001821
Vu Pham90ecc6e2013-08-28 23:23:33 +03001822 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001823 spin_lock_bh(&cmd->istate_lock);
1824 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1825 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1826 spin_unlock_bh(&cmd->istate_lock);
1827
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001828 if (ret)
1829 transport_send_check_condition_and_sense(se_cmd,
1830 se_cmd->pi_err, 0);
1831 else
1832 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001833}
1834
1835static void
1836isert_do_control_comp(struct work_struct *work)
1837{
1838 struct isert_cmd *isert_cmd = container_of(work,
1839 struct isert_cmd, comp_work);
1840 struct isert_conn *isert_conn = isert_cmd->conn;
1841 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001842 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001843
1844 switch (cmd->i_state) {
1845 case ISTATE_SEND_TASKMGTRSP:
1846 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1847
1848 atomic_dec(&isert_conn->post_send_buf_count);
1849 iscsit_tmr_post_handler(cmd, cmd->conn);
1850
1851 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001852 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001853 break;
1854 case ISTATE_SEND_REJECT:
1855 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1856 atomic_dec(&isert_conn->post_send_buf_count);
1857
1858 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001859 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001860 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861 case ISTATE_SEND_LOGOUTRSP:
1862 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001863
1864 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001865 iscsit_logout_post_handler(cmd, cmd->conn);
1866 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001867 case ISTATE_SEND_TEXTRSP:
1868 atomic_dec(&isert_conn->post_send_buf_count);
1869 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001870 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001871 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001872 default:
1873 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1874 dump_stack();
1875 break;
1876 }
1877}
1878
1879static void
1880isert_response_completion(struct iser_tx_desc *tx_desc,
1881 struct isert_cmd *isert_cmd,
1882 struct isert_conn *isert_conn,
1883 struct ib_device *ib_dev)
1884{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001885 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001886 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001887
1888 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001889 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001890 cmd->i_state == ISTATE_SEND_REJECT ||
1891 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001892 isert_unmap_tx_desc(tx_desc, ib_dev);
1893
1894 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1895 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1896 return;
1897 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001898
1899 /**
1900 * If send_wr_num is 0 this means that we got
1901 * RDMA completion and we cleared it and we should
1902 * simply decrement the response post. else the
1903 * response is incorporated in send_wr_num, just
1904 * sub it.
1905 **/
1906 if (wr->send_wr_num)
1907 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1908 else
1909 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001910
1911 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001912 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913}
1914
1915static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001916__isert_send_completion(struct iser_tx_desc *tx_desc,
1917 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001918{
1919 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1920 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1921 struct isert_rdma_wr *wr;
1922
1923 if (!isert_cmd) {
1924 atomic_dec(&isert_conn->post_send_buf_count);
1925 isert_unmap_tx_desc(tx_desc, ib_dev);
1926 return;
1927 }
1928 wr = &isert_cmd->rdma_wr;
1929
1930 switch (wr->iser_ib_op) {
1931 case ISER_IB_RECV:
1932 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1933 dump_stack();
1934 break;
1935 case ISER_IB_SEND:
1936 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1937 isert_response_completion(tx_desc, isert_cmd,
1938 isert_conn, ib_dev);
1939 break;
1940 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001941 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001942 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001943 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001944 break;
1945 case ISER_IB_RDMA_READ:
1946 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1947
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001948 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001949 isert_completion_rdma_read(tx_desc, isert_cmd);
1950 break;
1951 default:
1952 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1953 dump_stack();
1954 break;
1955 }
1956}
1957
1958static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001959isert_send_completion(struct iser_tx_desc *tx_desc,
1960 struct isert_conn *isert_conn)
1961{
1962 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1963 struct iser_tx_desc *t;
1964 /*
1965 * Drain coalesced completion llist starting from comp_llnode_batch
1966 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1967 */
1968 while (llnode) {
1969 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1970 llnode = llist_next(llnode);
1971 __isert_send_completion(t, isert_conn);
1972 }
1973 __isert_send_completion(tx_desc, isert_conn);
1974}
1975
1976static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001977isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1978{
1979 struct llist_node *llnode;
1980 struct isert_rdma_wr *wr;
1981 struct iser_tx_desc *t;
1982
1983 mutex_lock(&isert_conn->conn_mutex);
1984 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1985 isert_conn->conn_comp_batch = 0;
1986 mutex_unlock(&isert_conn->conn_mutex);
1987
1988 while (llnode) {
1989 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1990 llnode = llist_next(llnode);
1991 wr = &t->isert_cmd->rdma_wr;
1992
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001993 /**
1994 * If send_wr_num is 0 this means that we got
1995 * RDMA completion and we cleared it and we should
1996 * simply decrement the response post. else the
1997 * response is incorporated in send_wr_num, just
1998 * sub it.
1999 **/
2000 if (wr->send_wr_num)
2001 atomic_sub(wr->send_wr_num,
2002 &isert_conn->post_send_buf_count);
2003 else
2004 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002005
2006 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002007 }
2008}
2009
2010static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002011isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002012{
2013 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002014 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002015 struct llist_node *llnode = tx_desc->comp_llnode_batch;
2016 struct isert_rdma_wr *wr;
2017 struct iser_tx_desc *t;
2018
2019 while (llnode) {
2020 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
2021 llnode = llist_next(llnode);
2022 wr = &t->isert_cmd->rdma_wr;
2023
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002024 /**
2025 * If send_wr_num is 0 this means that we got
2026 * RDMA completion and we cleared it and we should
2027 * simply decrement the response post. else the
2028 * response is incorporated in send_wr_num, just
2029 * sub it.
2030 **/
2031 if (wr->send_wr_num)
2032 atomic_sub(wr->send_wr_num,
2033 &isert_conn->post_send_buf_count);
2034 else
2035 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002036
2037 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002038 }
2039 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002040
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002041 if (!isert_cmd)
2042 isert_unmap_tx_desc(tx_desc, ib_dev);
2043 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002044 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002045}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002046
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002047static void
2048isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2049{
2050 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2051 struct iscsi_conn *conn = isert_conn->conn;
2052
2053 if (isert_conn->post_recv_buf_count)
2054 return;
2055
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002056 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2057
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002058 if (conn->sess) {
2059 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2060 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002061 }
2062
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002063 while (atomic_read(&isert_conn->post_send_buf_count))
2064 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002066 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg954f2372014-12-02 16:57:17 +02002067 isert_conn_terminate(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002068 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002069
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002070 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2071
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002072 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002073}
2074
2075static void
2076isert_cq_tx_work(struct work_struct *work)
2077{
2078 struct isert_cq_desc *cq_desc = container_of(work,
2079 struct isert_cq_desc, cq_tx_work);
2080 struct isert_device *device = cq_desc->device;
2081 int cq_index = cq_desc->cq_index;
2082 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2083 struct isert_conn *isert_conn;
2084 struct iser_tx_desc *tx_desc;
2085 struct ib_wc wc;
2086
2087 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2088 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2089 isert_conn = wc.qp->qp_context;
2090
2091 if (wc.status == IB_WC_SUCCESS) {
2092 isert_send_completion(tx_desc, isert_conn);
2093 } else {
2094 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2095 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002096 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002097
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002098 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002099 if (tx_desc->llnode_active)
2100 continue;
2101
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002102 atomic_dec(&isert_conn->post_send_buf_count);
2103 isert_cq_tx_comp_err(tx_desc, isert_conn);
2104 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002105 }
2106 }
2107
2108 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2109}
2110
2111static void
2112isert_cq_tx_callback(struct ib_cq *cq, void *context)
2113{
2114 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2115
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002116 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2117}
2118
2119static void
2120isert_cq_rx_work(struct work_struct *work)
2121{
2122 struct isert_cq_desc *cq_desc = container_of(work,
2123 struct isert_cq_desc, cq_rx_work);
2124 struct isert_device *device = cq_desc->device;
2125 int cq_index = cq_desc->cq_index;
2126 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2127 struct isert_conn *isert_conn;
2128 struct iser_rx_desc *rx_desc;
2129 struct ib_wc wc;
2130 unsigned long xfer_len;
2131
2132 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2133 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2134 isert_conn = wc.qp->qp_context;
2135
2136 if (wc.status == IB_WC_SUCCESS) {
2137 xfer_len = (unsigned long)wc.byte_len;
2138 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2139 } else {
2140 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002141 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002142 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002143 pr_debug("RX wc.vendor_err: 0x%08x\n",
2144 wc.vendor_err);
2145 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002146 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002147 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002148 }
2149 }
2150
2151 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2152}
2153
2154static void
2155isert_cq_rx_callback(struct ib_cq *cq, void *context)
2156{
2157 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2158
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002159 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2160}
2161
2162static int
2163isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2164{
2165 struct ib_send_wr *wr_failed;
2166 int ret;
2167
2168 atomic_inc(&isert_conn->post_send_buf_count);
2169
2170 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2171 &wr_failed);
2172 if (ret) {
2173 pr_err("ib_post_send failed with %d\n", ret);
2174 atomic_dec(&isert_conn->post_send_buf_count);
2175 return ret;
2176 }
2177 return ret;
2178}
2179
2180static int
2181isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2182{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002183 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002184 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2185 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2186 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2187 &isert_cmd->tx_desc.iscsi_header;
2188
2189 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2190 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2191 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2192 /*
2193 * Attach SENSE DATA payload to iSCSI Response PDU
2194 */
2195 if (cmd->se_cmd.sense_buffer &&
2196 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2197 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2198 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2199 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002200 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002201
2202 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2203 cmd->sense_buffer);
2204 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2205
2206 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2207 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002208 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002209
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002210 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2211 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002212 DMA_TO_DEVICE);
2213
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002214 isert_cmd->pdu_buf_len = pdu_len;
2215 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2216 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002217 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2218 isert_cmd->tx_desc.num_sge = 2;
2219 }
2220
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002221 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002222
2223 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2224
2225 return isert_post_response(isert_conn, isert_cmd);
2226}
2227
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002228static void
2229isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2230{
2231 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2232 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2233 struct isert_device *device = isert_conn->conn_device;
2234
2235 spin_lock_bh(&conn->cmd_lock);
2236 if (!list_empty(&cmd->i_conn_node))
2237 list_del_init(&cmd->i_conn_node);
2238 spin_unlock_bh(&conn->cmd_lock);
2239
2240 if (cmd->data_direction == DMA_TO_DEVICE)
2241 iscsit_stop_dataout_timer(cmd);
2242
2243 device->unreg_rdma_mem(isert_cmd, isert_conn);
2244}
2245
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002246static enum target_prot_op
2247isert_get_sup_prot_ops(struct iscsi_conn *conn)
2248{
2249 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2250 struct isert_device *device = isert_conn->conn_device;
2251
2252 if (device->pi_capable)
2253 return TARGET_PROT_ALL;
2254
2255 return TARGET_PROT_NORMAL;
2256}
2257
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002258static int
2259isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2260 bool nopout_response)
2261{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002262 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002263 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2264 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2265
2266 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2267 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2268 &isert_cmd->tx_desc.iscsi_header,
2269 nopout_response);
2270 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002271 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002272
Masanari Iida8b513d02013-05-21 23:13:12 +09002273 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002274
2275 return isert_post_response(isert_conn, isert_cmd);
2276}
2277
2278static int
2279isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2280{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002281 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002282 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2283 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2284
2285 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2286 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2287 &isert_cmd->tx_desc.iscsi_header);
2288 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002289 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002290
2291 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2292
2293 return isert_post_response(isert_conn, isert_cmd);
2294}
2295
2296static int
2297isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2298{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002299 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002300 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2301 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2302
2303 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2304 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2305 &isert_cmd->tx_desc.iscsi_header);
2306 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002307 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002308
2309 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2310
2311 return isert_post_response(isert_conn, isert_cmd);
2312}
2313
2314static int
2315isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2316{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002317 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002318 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2319 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002320 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2321 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2322 struct iscsi_reject *hdr =
2323 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002324
2325 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002326 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002327 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002328
2329 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002330 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002331 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2332 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002333 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2334 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002335 tx_dsg->length = ISCSI_HDR_LEN;
2336 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2337 isert_cmd->tx_desc.num_sge = 2;
2338
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002339 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002340
2341 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2342
2343 return isert_post_response(isert_conn, isert_cmd);
2344}
2345
2346static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002347isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2348{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002349 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002350 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2351 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2352 struct iscsi_text_rsp *hdr =
2353 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2354 u32 txt_rsp_len;
2355 int rc;
2356
2357 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002358 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002359 if (rc < 0)
2360 return rc;
2361
2362 txt_rsp_len = rc;
2363 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2364
2365 if (txt_rsp_len) {
2366 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2367 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2368 void *txt_rsp_buf = cmd->buf_ptr;
2369
2370 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2371 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2372
2373 isert_cmd->pdu_buf_len = txt_rsp_len;
2374 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2375 tx_dsg->length = txt_rsp_len;
2376 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2377 isert_cmd->tx_desc.num_sge = 2;
2378 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002379 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002380
2381 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2382
2383 return isert_post_response(isert_conn, isert_cmd);
2384}
2385
2386static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002387isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2388 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2389 u32 data_left, u32 offset)
2390{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002391 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002392 struct scatterlist *sg_start, *tmp_sg;
2393 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2394 u32 sg_off, page_off;
2395 int i = 0, sg_nents;
2396
2397 sg_off = offset / PAGE_SIZE;
2398 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2399 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2400 page_off = offset % PAGE_SIZE;
2401
2402 send_wr->sg_list = ib_sge;
2403 send_wr->num_sge = sg_nents;
2404 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2405 /*
2406 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2407 */
2408 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2409 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2410 (unsigned long long)tmp_sg->dma_address,
2411 tmp_sg->length, page_off);
2412
2413 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2414 ib_sge->length = min_t(u32, data_left,
2415 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2416 ib_sge->lkey = isert_conn->conn_mr->lkey;
2417
Vu Pham90ecc6e2013-08-28 23:23:33 +03002418 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2419 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002420 page_off = 0;
2421 data_left -= ib_sge->length;
2422 ib_sge++;
2423 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2424 }
2425
2426 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2427 send_wr->sg_list, send_wr->num_sge);
2428
2429 return sg_nents;
2430}
2431
2432static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002433isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2434 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435{
2436 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002437 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002439 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002440 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002442 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2443 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002444
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002445 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002446
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002447 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2448 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2449 se_cmd->t_data_nents, se_cmd->data_length,
2450 offset, wr->iser_ib_op, &wr->data);
2451 if (ret)
2452 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002453
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002454 data_left = data->len;
2455 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002456
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002457 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002459 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002460 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002461 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002462 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002463 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002464
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002465 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002466 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2467 GFP_KERNEL);
2468 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002469 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002470 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002471 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002472 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002473
2474 wr->isert_cmd = isert_cmd;
2475 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002476
2477 for (i = 0; i < wr->send_wr_num; i++) {
2478 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2479 data_len = min(data_left, rdma_write_max);
2480
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002481 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002482 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2483 send_wr->opcode = IB_WR_RDMA_WRITE;
2484 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2485 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2486 if (i + 1 == wr->send_wr_num)
2487 send_wr->next = &isert_cmd->tx_desc.send_wr;
2488 else
2489 send_wr->next = &wr->send_wr[i + 1];
2490 } else {
2491 send_wr->opcode = IB_WR_RDMA_READ;
2492 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2493 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2494 if (i + 1 == wr->send_wr_num)
2495 send_wr->send_flags = IB_SEND_SIGNALED;
2496 else
2497 send_wr->next = &wr->send_wr[i + 1];
2498 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002499
2500 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2501 send_wr, data_len, offset);
2502 ib_sge += ib_sge_cnt;
2503
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002504 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002505 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002506 data_left -= data_len;
2507 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002508
2509 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002510unmap_cmd:
2511 isert_unmap_data_buf(isert_conn, data);
2512
Vu Pham90ecc6e2013-08-28 23:23:33 +03002513 return ret;
2514}
2515
2516static int
Vu Pham59464ef2013-08-28 23:23:35 +03002517isert_map_fr_pagelist(struct ib_device *ib_dev,
2518 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2519{
2520 u64 start_addr, end_addr, page, chunk_start = 0;
2521 struct scatterlist *tmp_sg;
2522 int i = 0, new_chunk, last_ent, n_pages;
2523
2524 n_pages = 0;
2525 new_chunk = 1;
2526 last_ent = sg_nents - 1;
2527 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2528 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2529 if (new_chunk)
2530 chunk_start = start_addr;
2531 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2532
2533 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2534 i, (unsigned long long)tmp_sg->dma_address,
2535 tmp_sg->length);
2536
2537 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2538 new_chunk = 0;
2539 continue;
2540 }
2541 new_chunk = 1;
2542
2543 page = chunk_start & PAGE_MASK;
2544 do {
2545 fr_pl[n_pages++] = page;
2546 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2547 n_pages - 1, page);
2548 page += PAGE_SIZE;
2549 } while (page < end_addr);
2550 }
2551
2552 return n_pages;
2553}
2554
2555static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002556isert_fast_reg_mr(struct isert_conn *isert_conn,
2557 struct fast_reg_descriptor *fr_desc,
2558 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002559 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002560 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002561{
Vu Pham59464ef2013-08-28 23:23:35 +03002562 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002563 struct ib_mr *mr;
2564 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002565 struct ib_send_wr fr_wr, inv_wr;
2566 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002567 int ret, pagelist_len;
2568 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002569 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002570
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002571 if (mem->dma_nents == 1) {
2572 sge->lkey = isert_conn->conn_mr->lkey;
2573 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2574 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002575 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2576 __func__, __LINE__, sge->addr, sge->length,
2577 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002578 return 0;
2579 }
2580
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002581 if (ind == ISERT_DATA_KEY_VALID) {
2582 /* Registering data buffer */
2583 mr = fr_desc->data_mr;
2584 frpl = fr_desc->data_frpl;
2585 } else {
2586 /* Registering protection buffer */
2587 mr = fr_desc->pi_ctx->prot_mr;
2588 frpl = fr_desc->pi_ctx->prot_frpl;
2589 }
2590
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002591 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002592
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002593 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002594 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002595
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002596 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002597 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002598
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002599 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002600 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002601 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002602 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002603 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002604 wr = &inv_wr;
2605 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002606 key = (u8)(mr->rkey & 0x000000FF);
2607 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002608 }
2609
2610 /* Prepare FASTREG WR */
2611 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002612 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002613 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002614 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2615 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002616 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2617 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002618 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002619 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002620 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2621
2622 if (!wr)
2623 wr = &fr_wr;
2624 else
2625 wr->next = &fr_wr;
2626
2627 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2628 if (ret) {
2629 pr_err("fast registration failed, ret:%d\n", ret);
2630 return ret;
2631 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002632 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002633
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002634 sge->lkey = mr->lkey;
2635 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002636 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002637
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002638 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2639 __func__, __LINE__, sge->addr, sge->length,
2640 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002641
2642 return ret;
2643}
2644
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002645static inline void
2646isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2647 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002648{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002649 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002650 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2651 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2652 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002653 /*
2654 * At the moment we hard code those, but if in the future
2655 * the target core would like to use it, we will take it
2656 * from se_cmd.
2657 */
2658 domain->sig.dif.apptag_check_mask = 0xffff;
2659 domain->sig.dif.app_escape = true;
2660 domain->sig.dif.ref_escape = true;
2661 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2662 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2663 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002664};
2665
2666static int
2667isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2668{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002669 switch (se_cmd->prot_op) {
2670 case TARGET_PROT_DIN_INSERT:
2671 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002672 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002673 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002674 break;
2675 case TARGET_PROT_DOUT_INSERT:
2676 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002677 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002678 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002679 break;
2680 case TARGET_PROT_DIN_PASS:
2681 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002682 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2683 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002684 break;
2685 default:
2686 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2687 return -EINVAL;
2688 }
2689
2690 return 0;
2691}
2692
2693static inline u8
2694isert_set_prot_checks(u8 prot_checks)
2695{
2696 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2697 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2698 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2699}
2700
2701static int
2702isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2703 struct fast_reg_descriptor *fr_desc,
2704 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2705 struct ib_sge *sig_sge)
2706{
2707 struct ib_send_wr sig_wr, inv_wr;
2708 struct ib_send_wr *bad_wr, *wr = NULL;
2709 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2710 struct ib_sig_attrs sig_attrs;
2711 int ret;
2712 u32 key;
2713
2714 memset(&sig_attrs, 0, sizeof(sig_attrs));
2715 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2716 if (ret)
2717 goto err;
2718
2719 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2720
2721 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2722 memset(&inv_wr, 0, sizeof(inv_wr));
2723 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002724 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002725 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2726 wr = &inv_wr;
2727 /* Bump the key */
2728 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2729 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2730 }
2731
2732 memset(&sig_wr, 0, sizeof(sig_wr));
2733 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002734 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002735 sig_wr.sg_list = data_sge;
2736 sig_wr.num_sge = 1;
2737 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2738 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2739 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2740 if (se_cmd->t_prot_sg)
2741 sig_wr.wr.sig_handover.prot = prot_sge;
2742
2743 if (!wr)
2744 wr = &sig_wr;
2745 else
2746 wr->next = &sig_wr;
2747
2748 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2749 if (ret) {
2750 pr_err("fast registration failed, ret:%d\n", ret);
2751 goto err;
2752 }
2753 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2754
2755 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2756 sig_sge->addr = 0;
2757 sig_sge->length = se_cmd->data_length;
2758 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2759 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2760 /*
2761 * We have protection guards on the wire
2762 * so we need to set a larget transfer
2763 */
2764 sig_sge->length += se_cmd->prot_length;
2765
2766 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2767 sig_sge->addr, sig_sge->length,
2768 sig_sge->lkey);
2769err:
2770 return ret;
2771}
2772
Vu Pham59464ef2013-08-28 23:23:35 +03002773static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002774isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2775 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002776{
2777 struct se_cmd *se_cmd = &cmd->se_cmd;
2778 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002779 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002780 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002781 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002782 struct fast_reg_descriptor *fr_desc = NULL;
2783 u32 offset;
2784 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002785 unsigned long flags;
2786
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002787 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2788
2789 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2790 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2791 se_cmd->t_data_nents, se_cmd->data_length,
2792 offset, wr->iser_ib_op, &wr->data);
2793 if (ret)
2794 return ret;
2795
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002796 if (wr->data.dma_nents != 1 ||
2797 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002798 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2799 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2800 struct fast_reg_descriptor, list);
2801 list_del(&fr_desc->list);
2802 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2803 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002804 }
2805
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002806 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2807 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002808 if (ret)
2809 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002810
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002811 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2812 struct ib_sge prot_sge, sig_sge;
2813
2814 if (se_cmd->t_prot_sg) {
2815 ret = isert_map_data_buf(isert_conn, isert_cmd,
2816 se_cmd->t_prot_sg,
2817 se_cmd->t_prot_nents,
2818 se_cmd->prot_length,
2819 0, wr->iser_ib_op, &wr->prot);
2820 if (ret)
2821 goto unmap_cmd;
2822
2823 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2824 ISERT_PROT_KEY_VALID, &prot_sge);
2825 if (ret)
2826 goto unmap_prot_cmd;
2827 }
2828
2829 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2830 &data_sge, &prot_sge, &sig_sge);
2831 if (ret)
2832 goto unmap_prot_cmd;
2833
2834 fr_desc->ind |= ISERT_PROTECTED;
2835 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2836 } else
2837 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2838
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002839 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002840 wr->send_wr_num = 1;
2841 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2842 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002843 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002844
2845 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002846 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002847 send_wr->num_sge = 1;
2848 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2849 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2850 send_wr->opcode = IB_WR_RDMA_WRITE;
2851 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2852 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002853 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2854 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002855 } else {
2856 send_wr->opcode = IB_WR_RDMA_READ;
2857 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2858 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2859 send_wr->send_flags = IB_SEND_SIGNALED;
2860 }
2861
Vu Pham59464ef2013-08-28 23:23:35 +03002862 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002863unmap_prot_cmd:
2864 if (se_cmd->t_prot_sg)
2865 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002866unmap_cmd:
2867 if (fr_desc) {
2868 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2869 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2870 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2871 }
2872 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002873
Vu Pham59464ef2013-08-28 23:23:35 +03002874 return ret;
2875}
2876
2877static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002878isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2879{
2880 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002881 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002882 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2883 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002884 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002885 struct ib_send_wr *wr_failed;
2886 int rc;
2887
2888 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2889 isert_cmd, se_cmd->data_length);
2890 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002891 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002892 if (rc) {
2893 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2894 return rc;
2895 }
2896
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002897 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2898 /*
2899 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2900 */
2901 isert_create_send_desc(isert_conn, isert_cmd,
2902 &isert_cmd->tx_desc);
2903 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2904 &isert_cmd->tx_desc.iscsi_header);
2905 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2906 isert_init_send_wr(isert_conn, isert_cmd,
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002907 &isert_cmd->tx_desc.send_wr, false);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002908 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002909 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002910 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002911
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002912 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913
2914 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2915 if (rc) {
2916 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002917 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002919
2920 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2921 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2922 "READ\n", isert_cmd);
2923 else
2924 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2925 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926
Vu Pham90ecc6e2013-08-28 23:23:33 +03002927 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002928}
2929
2930static int
2931isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2932{
2933 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002934 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002935 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2936 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002937 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002938 struct ib_send_wr *wr_failed;
2939 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002940
Vu Pham90ecc6e2013-08-28 23:23:33 +03002941 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2942 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002943 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002944 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002945 if (rc) {
2946 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2947 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002948 }
2949
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002950 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002951
2952 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2953 if (rc) {
2954 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002955 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002956 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002957 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2958 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002959
Vu Pham90ecc6e2013-08-28 23:23:33 +03002960 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002961}
2962
2963static int
2964isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2965{
2966 int ret;
2967
2968 switch (state) {
2969 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2970 ret = isert_put_nopin(cmd, conn, false);
2971 break;
2972 default:
2973 pr_err("Unknown immediate state: 0x%02x\n", state);
2974 ret = -EINVAL;
2975 break;
2976 }
2977
2978 return ret;
2979}
2980
2981static int
2982isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2983{
2984 int ret;
2985
2986 switch (state) {
2987 case ISTATE_SEND_LOGOUTRSP:
2988 ret = isert_put_logout_rsp(cmd, conn);
2989 if (!ret) {
2990 pr_debug("Returning iSER Logout -EAGAIN\n");
2991 ret = -EAGAIN;
2992 }
2993 break;
2994 case ISTATE_SEND_NOPIN:
2995 ret = isert_put_nopin(cmd, conn, true);
2996 break;
2997 case ISTATE_SEND_TASKMGTRSP:
2998 ret = isert_put_tm_rsp(cmd, conn);
2999 break;
3000 case ISTATE_SEND_REJECT:
3001 ret = isert_put_reject(cmd, conn);
3002 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07003003 case ISTATE_SEND_TEXTRSP:
3004 ret = isert_put_text_rsp(cmd, conn);
3005 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003006 case ISTATE_SEND_STATUS:
3007 /*
3008 * Special case for sending non GOOD SCSI status from TX thread
3009 * context during pre se_cmd excecution failure.
3010 */
3011 ret = isert_put_response(conn, cmd);
3012 break;
3013 default:
3014 pr_err("Unknown response state: 0x%02x\n", state);
3015 ret = -EINVAL;
3016 break;
3017 }
3018
3019 return ret;
3020}
3021
3022static int
3023isert_setup_np(struct iscsi_np *np,
3024 struct __kernel_sockaddr_storage *ksockaddr)
3025{
3026 struct isert_np *isert_np;
3027 struct rdma_cm_id *isert_lid;
3028 struct sockaddr *sa;
3029 int ret;
3030
3031 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3032 if (!isert_np) {
3033 pr_err("Unable to allocate struct isert_np\n");
3034 return -ENOMEM;
3035 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003036 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003037 mutex_init(&isert_np->np_accept_mutex);
3038 INIT_LIST_HEAD(&isert_np->np_accept_list);
3039 init_completion(&isert_np->np_login_comp);
3040
3041 sa = (struct sockaddr *)ksockaddr;
3042 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3043 /*
3044 * Setup the np->np_sockaddr from the passed sockaddr setup
3045 * in iscsi_target_configfs.c code..
3046 */
3047 memcpy(&np->np_sockaddr, ksockaddr,
3048 sizeof(struct __kernel_sockaddr_storage));
3049
3050 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3051 IB_QPT_RC);
3052 if (IS_ERR(isert_lid)) {
3053 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3054 PTR_ERR(isert_lid));
3055 ret = PTR_ERR(isert_lid);
3056 goto out;
3057 }
3058
3059 ret = rdma_bind_addr(isert_lid, sa);
3060 if (ret) {
3061 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3062 goto out_lid;
3063 }
3064
3065 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3066 if (ret) {
3067 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3068 goto out_lid;
3069 }
3070
3071 isert_np->np_cm_id = isert_lid;
3072 np->np_context = isert_np;
3073 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3074
3075 return 0;
3076
3077out_lid:
3078 rdma_destroy_id(isert_lid);
3079out:
3080 kfree(isert_np);
3081 return ret;
3082}
3083
3084static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003085isert_rdma_accept(struct isert_conn *isert_conn)
3086{
3087 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3088 struct rdma_conn_param cp;
3089 int ret;
3090
3091 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003092 cp.initiator_depth = isert_conn->initiator_depth;
3093 cp.retry_count = 7;
3094 cp.rnr_retry_count = 7;
3095
3096 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3097
3098 ret = rdma_accept(cm_id, &cp);
3099 if (ret) {
3100 pr_err("rdma_accept() failed with: %d\n", ret);
3101 return ret;
3102 }
3103
3104 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3105
3106 return 0;
3107}
3108
3109static int
3110isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3111{
3112 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3113 int ret;
3114
3115 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003116 /*
3117 * For login requests after the first PDU, isert_rx_login_req() will
3118 * kick schedule_delayed_work(&conn->login_work) as the packet is
3119 * received, which turns this callback from iscsi_target_do_login_rx()
3120 * into a NOP.
3121 */
3122 if (!login->first_request)
3123 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003124
3125 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3126 if (ret)
3127 return ret;
3128
3129 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3130 return 0;
3131}
3132
3133static void
3134isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3135 struct isert_conn *isert_conn)
3136{
3137 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3138 struct rdma_route *cm_route = &cm_id->route;
3139 struct sockaddr_in *sock_in;
3140 struct sockaddr_in6 *sock_in6;
3141
3142 conn->login_family = np->np_sockaddr.ss_family;
3143
3144 if (np->np_sockaddr.ss_family == AF_INET6) {
3145 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3146 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3147 &sock_in6->sin6_addr.in6_u);
3148 conn->login_port = ntohs(sock_in6->sin6_port);
3149
3150 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3151 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3152 &sock_in6->sin6_addr.in6_u);
3153 conn->local_port = ntohs(sock_in6->sin6_port);
3154 } else {
3155 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3156 sprintf(conn->login_ip, "%pI4",
3157 &sock_in->sin_addr.s_addr);
3158 conn->login_port = ntohs(sock_in->sin_port);
3159
3160 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3161 sprintf(conn->local_ip, "%pI4",
3162 &sock_in->sin_addr.s_addr);
3163 conn->local_port = ntohs(sock_in->sin_port);
3164 }
3165}
3166
3167static int
3168isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3169{
3170 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3171 struct isert_conn *isert_conn;
3172 int max_accept = 0, ret;
3173
3174accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003175 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003176 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003177 return -ENODEV;
3178
3179 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003180 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003181 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003182 pr_debug("np_thread_state %d for isert_accept_np\n",
3183 np->np_thread_state);
3184 /**
3185 * No point in stalling here when np_thread
3186 * is in state RESET/SHUTDOWN/EXIT - bail
3187 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003188 return -ENODEV;
3189 }
3190 spin_unlock_bh(&np->np_thread_lock);
3191
3192 mutex_lock(&isert_np->np_accept_mutex);
3193 if (list_empty(&isert_np->np_accept_list)) {
3194 mutex_unlock(&isert_np->np_accept_mutex);
3195 max_accept++;
3196 goto accept_wait;
3197 }
3198 isert_conn = list_first_entry(&isert_np->np_accept_list,
3199 struct isert_conn, conn_accept_node);
3200 list_del_init(&isert_conn->conn_accept_node);
3201 mutex_unlock(&isert_np->np_accept_mutex);
3202
3203 conn->context = isert_conn;
3204 isert_conn->conn = conn;
3205 max_accept = 0;
3206
3207 ret = isert_rdma_post_recvl(isert_conn);
3208 if (ret)
3209 return ret;
3210
3211 ret = isert_rdma_accept(isert_conn);
3212 if (ret)
3213 return ret;
3214
3215 isert_set_conn_info(np, conn, isert_conn);
3216
3217 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3218 return 0;
3219}
3220
3221static void
3222isert_free_np(struct iscsi_np *np)
3223{
3224 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3225
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003226 if (isert_np->np_cm_id)
3227 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003228
3229 np->np_context = NULL;
3230 kfree(isert_np);
3231}
3232
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003233static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003234{
3235 struct isert_conn *isert_conn = conn->context;
3236
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003237 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003238
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003239 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003240 /*
3241 * Only wait for conn_wait_comp_err if the isert_conn made it
3242 * into full feature phase..
3243 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003244 if (isert_conn->state == ISER_CONN_INIT) {
3245 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003246 return;
3247 }
Sagi Grimberg954f2372014-12-02 16:57:17 +02003248 isert_conn_terminate(isert_conn);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003249 mutex_unlock(&isert_conn->conn_mutex);
3250
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003251 wait_for_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003252 wait_for_completion(&isert_conn->conn_wait);
Sagi Grimberg954f2372014-12-02 16:57:17 +02003253
3254 mutex_lock(&isert_conn->conn_mutex);
3255 isert_conn->state = ISER_CONN_DOWN;
3256 mutex_unlock(&isert_conn->conn_mutex);
3257
3258 pr_info("Destroying conn %p\n", isert_conn);
Sagi Grimberg0fc4ea72014-07-02 16:19:25 +03003259 isert_put_conn(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003260}
3261
3262static void isert_free_conn(struct iscsi_conn *conn)
3263{
3264 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265
3266 isert_put_conn(isert_conn);
3267}
3268
3269static struct iscsit_transport iser_target_transport = {
3270 .name = "IB/iSER",
3271 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003272 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003273 .owner = THIS_MODULE,
3274 .iscsit_setup_np = isert_setup_np,
3275 .iscsit_accept_np = isert_accept_np,
3276 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003277 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003278 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003279 .iscsit_get_login_rx = isert_get_login_rx,
3280 .iscsit_put_login_tx = isert_put_login_tx,
3281 .iscsit_immediate_queue = isert_immediate_queue,
3282 .iscsit_response_queue = isert_response_queue,
3283 .iscsit_get_dataout = isert_get_dataout,
3284 .iscsit_queue_data_in = isert_put_datain,
3285 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003286 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003287 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003288};
3289
3290static int __init isert_init(void)
3291{
3292 int ret;
3293
3294 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3295 if (!isert_rx_wq) {
3296 pr_err("Unable to allocate isert_rx_wq\n");
3297 return -ENOMEM;
3298 }
3299
3300 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3301 if (!isert_comp_wq) {
3302 pr_err("Unable to allocate isert_comp_wq\n");
3303 ret = -ENOMEM;
3304 goto destroy_rx_wq;
3305 }
3306
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003307 iscsit_register_transport(&iser_target_transport);
3308 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3309 return 0;
3310
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003311destroy_rx_wq:
3312 destroy_workqueue(isert_rx_wq);
3313 return ret;
3314}
3315
3316static void __exit isert_exit(void)
3317{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003318 flush_scheduled_work();
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003319 destroy_workqueue(isert_comp_wq);
3320 destroy_workqueue(isert_rx_wq);
3321 iscsit_unregister_transport(&iser_target_transport);
3322 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3323}
3324
3325MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3326MODULE_VERSION("0.1");
3327MODULE_AUTHOR("nab@Linux-iSCSI.org");
3328MODULE_LICENSE("GPL");
3329
3330module_init(isert_init);
3331module_exit(isert_exit);