blob: a6c7b395c61d8fe7b229542e786065b9eb8b8273 [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;
231
Vu Pham59464ef2013-08-28 23:23:35 +0300232 dev_attr = &device->dev_attr;
233 ret = isert_query_device(ib_dev, dev_attr);
234 if (ret)
235 return ret;
236
Vu Phamd40945d2013-08-28 23:23:34 +0300237 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200238 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
239 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200240 device->use_fastreg = 1;
241 device->reg_rdma_mem = isert_reg_rdma;
242 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300243 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200244 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300245 device->reg_rdma_mem = isert_map_rdma;
246 device->unreg_rdma_mem = isert_unmap_cmd;
247 }
Vu Phamd40945d2013-08-28 23:23:34 +0300248
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200249 /* Check signature cap */
250 device->pi_capable = dev_attr->device_cap_flags &
251 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
252
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800253 device->cqs_used = min_t(int, num_online_cpus(),
254 device->ib_device->num_comp_vectors);
255 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200256 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200257 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800258 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200259 device->ib_device->num_comp_vectors, device->use_fastreg,
260 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800261 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
262 device->cqs_used, GFP_KERNEL);
263 if (!device->cq_desc) {
264 pr_err("Unable to allocate device->cq_desc\n");
265 return -ENOMEM;
266 }
267 cq_desc = device->cq_desc;
268
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800269 for (i = 0; i < device->cqs_used; i++) {
270 cq_desc[i].device = device;
271 cq_desc[i].cq_index = i;
272
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800273 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800274 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
275 isert_cq_rx_callback,
276 isert_cq_event_callback,
277 (void *)&cq_desc[i],
278 ISER_MAX_RX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800279 if (IS_ERR(device->dev_rx_cq[i])) {
280 ret = PTR_ERR(device->dev_rx_cq[i]);
281 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800282 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800283 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800284
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800285 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800286 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
287 isert_cq_tx_callback,
288 isert_cq_event_callback,
289 (void *)&cq_desc[i],
290 ISER_MAX_TX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800291 if (IS_ERR(device->dev_tx_cq[i])) {
292 ret = PTR_ERR(device->dev_tx_cq[i]);
293 device->dev_tx_cq[i] = NULL;
294 goto out_cq;
295 }
296
297 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
298 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800299 goto out_cq;
300
Wei Yongjun94a71112013-10-29 09:56:34 +0800301 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
302 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800303 goto out_cq;
304 }
305
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800306 return 0;
307
308out_cq:
309 for (j = 0; j < i; j++) {
310 cq_desc = &device->cq_desc[j];
311
312 if (device->dev_rx_cq[j]) {
313 cancel_work_sync(&cq_desc->cq_rx_work);
314 ib_destroy_cq(device->dev_rx_cq[j]);
315 }
316 if (device->dev_tx_cq[j]) {
317 cancel_work_sync(&cq_desc->cq_tx_work);
318 ib_destroy_cq(device->dev_tx_cq[j]);
319 }
320 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800321 kfree(device->cq_desc);
322
323 return ret;
324}
325
326static void
327isert_free_device_ib_res(struct isert_device *device)
328{
329 struct isert_cq_desc *cq_desc;
330 int i;
331
332 for (i = 0; i < device->cqs_used; i++) {
333 cq_desc = &device->cq_desc[i];
334
335 cancel_work_sync(&cq_desc->cq_rx_work);
336 cancel_work_sync(&cq_desc->cq_tx_work);
337 ib_destroy_cq(device->dev_rx_cq[i]);
338 ib_destroy_cq(device->dev_tx_cq[i]);
339 device->dev_rx_cq[i] = NULL;
340 device->dev_tx_cq[i] = NULL;
341 }
342
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800343 kfree(device->cq_desc);
344}
345
346static void
347isert_device_try_release(struct isert_device *device)
348{
349 mutex_lock(&device_list_mutex);
350 device->refcount--;
351 if (!device->refcount) {
352 isert_free_device_ib_res(device);
353 list_del(&device->dev_node);
354 kfree(device);
355 }
356 mutex_unlock(&device_list_mutex);
357}
358
359static struct isert_device *
360isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
361{
362 struct isert_device *device;
363 int ret;
364
365 mutex_lock(&device_list_mutex);
366 list_for_each_entry(device, &device_list, dev_node) {
367 if (device->ib_device->node_guid == cma_id->device->node_guid) {
368 device->refcount++;
369 mutex_unlock(&device_list_mutex);
370 return device;
371 }
372 }
373
374 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
375 if (!device) {
376 mutex_unlock(&device_list_mutex);
377 return ERR_PTR(-ENOMEM);
378 }
379
380 INIT_LIST_HEAD(&device->dev_node);
381
382 device->ib_device = cma_id->device;
383 ret = isert_create_device_ib_res(device);
384 if (ret) {
385 kfree(device);
386 mutex_unlock(&device_list_mutex);
387 return ERR_PTR(ret);
388 }
389
390 device->refcount++;
391 list_add_tail(&device->dev_node, &device_list);
392 mutex_unlock(&device_list_mutex);
393
394 return device;
395}
396
Vu Pham59464ef2013-08-28 23:23:35 +0300397static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200398isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300399{
400 struct fast_reg_descriptor *fr_desc, *tmp;
401 int i = 0;
402
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200403 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300404 return;
405
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200406 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300407
408 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200409 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300410 list_del(&fr_desc->list);
411 ib_free_fast_reg_page_list(fr_desc->data_frpl);
412 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200413 if (fr_desc->pi_ctx) {
414 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
415 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
416 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
417 kfree(fr_desc->pi_ctx);
418 }
Vu Pham59464ef2013-08-28 23:23:35 +0300419 kfree(fr_desc);
420 ++i;
421 }
422
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200423 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300424 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200425 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300426}
427
428static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200429isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200430 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200431{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200432 int ret;
433
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200434 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
435 ISCSI_ISER_SG_TABLESIZE);
436 if (IS_ERR(fr_desc->data_frpl)) {
437 pr_err("Failed to allocate data frpl err=%ld\n",
438 PTR_ERR(fr_desc->data_frpl));
439 return PTR_ERR(fr_desc->data_frpl);
440 }
441
442 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
443 if (IS_ERR(fr_desc->data_mr)) {
444 pr_err("Failed to allocate data frmr err=%ld\n",
445 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200446 ret = PTR_ERR(fr_desc->data_mr);
447 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200448 }
449 pr_debug("Create fr_desc %p page_list %p\n",
450 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200451 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200452
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200453 if (protection) {
454 struct ib_mr_init_attr mr_init_attr = {0};
455 struct pi_context *pi_ctx;
456
457 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
458 if (!fr_desc->pi_ctx) {
459 pr_err("Failed to allocate pi context\n");
460 ret = -ENOMEM;
461 goto err_data_mr;
462 }
463 pi_ctx = fr_desc->pi_ctx;
464
465 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
466 ISCSI_ISER_SG_TABLESIZE);
467 if (IS_ERR(pi_ctx->prot_frpl)) {
468 pr_err("Failed to allocate prot frpl err=%ld\n",
469 PTR_ERR(pi_ctx->prot_frpl));
470 ret = PTR_ERR(pi_ctx->prot_frpl);
471 goto err_pi_ctx;
472 }
473
474 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
475 if (IS_ERR(pi_ctx->prot_mr)) {
476 pr_err("Failed to allocate prot frmr err=%ld\n",
477 PTR_ERR(pi_ctx->prot_mr));
478 ret = PTR_ERR(pi_ctx->prot_mr);
479 goto err_prot_frpl;
480 }
481 fr_desc->ind |= ISERT_PROT_KEY_VALID;
482
483 mr_init_attr.max_reg_descriptors = 2;
484 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
485 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
486 if (IS_ERR(pi_ctx->sig_mr)) {
487 pr_err("Failed to allocate signature enabled mr err=%ld\n",
488 PTR_ERR(pi_ctx->sig_mr));
489 ret = PTR_ERR(pi_ctx->sig_mr);
490 goto err_prot_mr;
491 }
492 fr_desc->ind |= ISERT_SIG_KEY_VALID;
493 }
494 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200495
496 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200497err_prot_mr:
498 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
499err_prot_frpl:
500 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
501err_pi_ctx:
502 kfree(fr_desc->pi_ctx);
503err_data_mr:
504 ib_dereg_mr(fr_desc->data_mr);
505err_data_frpl:
506 ib_free_fast_reg_page_list(fr_desc->data_frpl);
507
508 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200509}
510
511static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200512isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
Vu Pham59464ef2013-08-28 23:23:35 +0300513{
514 struct fast_reg_descriptor *fr_desc;
515 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700516 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
517 struct se_node_acl *se_nacl = se_sess->se_node_acl;
518 int i, ret, tag_num;
519 /*
520 * Setup the number of FRMRs based upon the number of tags
521 * available to session in iscsi_target_locate_portal().
522 */
523 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
524 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300525
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200526 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700527 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300528 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
529 if (!fr_desc) {
530 pr_err("Failed to allocate fast_reg descriptor\n");
531 ret = -ENOMEM;
532 goto err;
533 }
534
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200535 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200536 isert_conn->conn_pd, fr_desc,
537 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200538 if (ret) {
539 pr_err("Failed to create fastreg descriptor err=%d\n",
540 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800541 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300542 goto err;
543 }
544
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200545 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
546 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300547 }
548
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200549 pr_debug("Creating conn %p fastreg pool size=%d",
550 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300551
552 return 0;
553
554err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200555 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300556 return ret;
557}
558
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800559static int
560isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
561{
562 struct iscsi_np *np = cma_id->context;
563 struct isert_np *isert_np = np->np_context;
564 struct isert_conn *isert_conn;
565 struct isert_device *device;
566 struct ib_device *ib_dev = cma_id->device;
567 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300568 u8 pi_support;
569
570 spin_lock_bh(&np->np_thread_lock);
571 if (!np->enabled) {
572 spin_unlock_bh(&np->np_thread_lock);
573 pr_debug("iscsi_np is not enabled, reject connect request\n");
574 return rdma_reject(cma_id, NULL, 0);
575 }
576 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800577
578 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
579 cma_id, cma_id->context);
580
581 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
582 if (!isert_conn) {
583 pr_err("Unable to allocate isert_conn\n");
584 return -ENOMEM;
585 }
586 isert_conn->state = ISER_CONN_INIT;
587 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
588 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800589 init_completion(&isert_conn->conn_wait);
590 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800591 kref_init(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700592 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300593 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700594 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800595
596 cma_id->context = isert_conn;
597 isert_conn->conn_cm_id = cma_id;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800598
599 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
600 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
601 if (!isert_conn->login_buf) {
602 pr_err("Unable to allocate isert_conn->login_buf\n");
603 ret = -ENOMEM;
604 goto out;
605 }
606
607 isert_conn->login_req_buf = isert_conn->login_buf;
608 isert_conn->login_rsp_buf = isert_conn->login_buf +
609 ISCSI_DEF_MAX_RECV_SEG_LEN;
610 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
611 isert_conn->login_buf, isert_conn->login_req_buf,
612 isert_conn->login_rsp_buf);
613
614 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
615 (void *)isert_conn->login_req_buf,
616 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
617
618 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
619 if (ret) {
620 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
621 ret);
622 isert_conn->login_req_dma = 0;
623 goto out_login_buf;
624 }
625
626 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
627 (void *)isert_conn->login_rsp_buf,
628 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
629
630 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
631 if (ret) {
632 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
633 ret);
634 isert_conn->login_rsp_dma = 0;
635 goto out_req_dma_map;
636 }
637
638 device = isert_device_find_by_ib_dev(cma_id);
639 if (IS_ERR(device)) {
640 ret = PTR_ERR(device);
641 goto out_rsp_dma_map;
642 }
643
Sagi Grimberg1a92e172014-06-19 13:54:19 +0300644 /* Set max inflight RDMA READ requests */
645 isert_conn->initiator_depth = min_t(u8,
646 event->param.conn.initiator_depth,
647 device->dev_attr.max_qp_init_rd_atom);
648 pr_debug("Using initiator_depth: %u\n", isert_conn->initiator_depth);
649
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800650 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200651 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
652 if (IS_ERR(isert_conn->conn_pd)) {
653 ret = PTR_ERR(isert_conn->conn_pd);
654 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
655 isert_conn, ret);
656 goto out_pd;
657 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800658
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200659 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
660 IB_ACCESS_LOCAL_WRITE);
661 if (IS_ERR(isert_conn->conn_mr)) {
662 ret = PTR_ERR(isert_conn->conn_mr);
663 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
664 isert_conn, ret);
665 goto out_mr;
666 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800667
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300668 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200669 if (pi_support && !device->pi_capable) {
Sagi Grimberg5f80ff82014-05-20 13:28:11 +0300670 pr_err("Protection information requested but not supported, "
671 "rejecting connect request\n");
672 ret = rdma_reject(cma_id, NULL, 0);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200673 goto out_mr;
674 }
675
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200676 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800677 if (ret)
678 goto out_conn_dev;
679
680 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300681 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800682 mutex_unlock(&isert_np->np_accept_mutex);
683
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300684 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
685 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686 return 0;
687
688out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200689 ib_dereg_mr(isert_conn->conn_mr);
690out_mr:
691 ib_dealloc_pd(isert_conn->conn_pd);
692out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800693 isert_device_try_release(device);
694out_rsp_dma_map:
695 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
696 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
697out_req_dma_map:
698 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
699 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
700out_login_buf:
701 kfree(isert_conn->login_buf);
702out:
703 kfree(isert_conn);
704 return ret;
705}
706
707static void
708isert_connect_release(struct isert_conn *isert_conn)
709{
710 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
711 struct isert_device *device = isert_conn->conn_device;
712 int cq_index;
713
714 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
715
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200716 if (device && device->use_fastreg)
717 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300718
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800719 if (isert_conn->conn_qp) {
720 cq_index = ((struct isert_cq_desc *)
721 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
722 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
723 isert_conn->conn_device->cq_active_qps[cq_index]--;
724
725 rdma_destroy_qp(isert_conn->conn_cm_id);
726 }
727
728 isert_free_rx_descriptors(isert_conn);
729 rdma_destroy_id(isert_conn->conn_cm_id);
730
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200731 ib_dereg_mr(isert_conn->conn_mr);
732 ib_dealloc_pd(isert_conn->conn_pd);
733
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800734 if (isert_conn->login_buf) {
735 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
736 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
737 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
738 ISCSI_DEF_MAX_RECV_SEG_LEN,
739 DMA_FROM_DEVICE);
740 kfree(isert_conn->login_buf);
741 }
742 kfree(isert_conn);
743
744 if (device)
745 isert_device_try_release(device);
746
747 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
748}
749
750static void
751isert_connected_handler(struct rdma_cm_id *cma_id)
752{
Sagi Grimbergc2f88b12014-07-02 16:19:24 +0300753 struct isert_conn *isert_conn = cma_id->context;
754
755 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800756}
757
758static void
759isert_release_conn_kref(struct kref *kref)
760{
761 struct isert_conn *isert_conn = container_of(kref,
762 struct isert_conn, conn_kref);
763
764 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
765 current->comm, current->pid);
766
767 isert_connect_release(isert_conn);
768}
769
770static void
771isert_put_conn(struct isert_conn *isert_conn)
772{
773 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
774}
775
776static void
777isert_disconnect_work(struct work_struct *work)
778{
779 struct isert_conn *isert_conn = container_of(work,
780 struct isert_conn, conn_logout_work);
781
782 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700783 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800784 if (isert_conn->state == ISER_CONN_UP)
785 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800786
787 if (isert_conn->post_recv_buf_count == 0 &&
788 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700789 mutex_unlock(&isert_conn->conn_mutex);
790 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800791 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700792 if (!isert_conn->conn_cm_id) {
793 mutex_unlock(&isert_conn->conn_mutex);
794 isert_put_conn(isert_conn);
795 return;
796 }
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +0300797
Sagi Grimberg88c40152014-05-19 17:44:24 +0300798 if (isert_conn->disconnect) {
799 /* Send DREQ/DREP towards our initiator */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700800 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700801 }
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +0300802
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700803 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800804
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700805wake_up:
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800806 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800807}
808
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700809static int
Sagi Grimberg88c40152014-05-19 17:44:24 +0300810isert_disconnected_handler(struct rdma_cm_id *cma_id, bool disconnect)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800811{
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700812 struct isert_conn *isert_conn;
813
814 if (!cma_id->qp) {
815 struct isert_np *isert_np = cma_id->context;
816
817 isert_np->np_cm_id = NULL;
818 return -1;
819 }
820
821 isert_conn = (struct isert_conn *)cma_id->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800822
Sagi Grimberg88c40152014-05-19 17:44:24 +0300823 isert_conn->disconnect = disconnect;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800824 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
825 schedule_work(&isert_conn->conn_logout_work);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700826
827 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800828}
829
830static int
831isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
832{
833 int ret = 0;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300834 bool disconnect = false;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800835
836 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
837 event->event, event->status, cma_id->context, cma_id);
838
839 switch (event->event) {
840 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800841 ret = isert_connect_request(cma_id, event);
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700842 if (ret)
843 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
844 event->event, ret);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800845 break;
846 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800847 isert_connected_handler(cma_id);
848 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300849 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
850 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
851 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
852 disconnect = true;
853 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
Sagi Grimberg3b726ae2014-10-28 13:45:03 -0700854 ret = isert_disconnected_handler(cma_id, disconnect);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800855 break;
856 case RDMA_CM_EVENT_CONNECT_ERROR:
857 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300858 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800859 break;
860 }
861
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800862 return ret;
863}
864
865static int
866isert_post_recv(struct isert_conn *isert_conn, u32 count)
867{
868 struct ib_recv_wr *rx_wr, *rx_wr_failed;
869 int i, ret;
870 unsigned int rx_head = isert_conn->conn_rx_desc_head;
871 struct iser_rx_desc *rx_desc;
872
873 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
874 rx_desc = &isert_conn->conn_rx_descs[rx_head];
875 rx_wr->wr_id = (unsigned long)rx_desc;
876 rx_wr->sg_list = &rx_desc->rx_sg;
877 rx_wr->num_sge = 1;
878 rx_wr->next = rx_wr + 1;
879 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
880 }
881
882 rx_wr--;
883 rx_wr->next = NULL; /* mark end of work requests list */
884
885 isert_conn->post_recv_buf_count += count;
886 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
887 &rx_wr_failed);
888 if (ret) {
889 pr_err("ib_post_recv() failed with ret: %d\n", ret);
890 isert_conn->post_recv_buf_count -= count;
891 } else {
892 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
893 isert_conn->conn_rx_desc_head = rx_head;
894 }
895 return ret;
896}
897
898static int
899isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
900{
901 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
902 struct ib_send_wr send_wr, *send_wr_failed;
903 int ret;
904
905 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
906 ISER_HEADERS_LEN, DMA_TO_DEVICE);
907
908 send_wr.next = NULL;
909 send_wr.wr_id = (unsigned long)tx_desc;
910 send_wr.sg_list = tx_desc->tx_sg;
911 send_wr.num_sge = tx_desc->num_sge;
912 send_wr.opcode = IB_WR_SEND;
913 send_wr.send_flags = IB_SEND_SIGNALED;
914
915 atomic_inc(&isert_conn->post_send_buf_count);
916
917 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
918 if (ret) {
919 pr_err("ib_post_send() failed, ret: %d\n", ret);
920 atomic_dec(&isert_conn->post_send_buf_count);
921 }
922
923 return ret;
924}
925
926static void
927isert_create_send_desc(struct isert_conn *isert_conn,
928 struct isert_cmd *isert_cmd,
929 struct iser_tx_desc *tx_desc)
930{
931 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
932
933 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
934 ISER_HEADERS_LEN, DMA_TO_DEVICE);
935
936 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
937 tx_desc->iser_header.flags = ISER_VER;
938
939 tx_desc->num_sge = 1;
940 tx_desc->isert_cmd = isert_cmd;
941
942 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
943 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
944 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
945 }
946}
947
948static int
949isert_init_tx_hdrs(struct isert_conn *isert_conn,
950 struct iser_tx_desc *tx_desc)
951{
952 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
953 u64 dma_addr;
954
955 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
956 ISER_HEADERS_LEN, DMA_TO_DEVICE);
957 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
958 pr_err("ib_dma_mapping_error() failed\n");
959 return -ENOMEM;
960 }
961
962 tx_desc->dma_addr = dma_addr;
963 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
964 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
965 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
966
967 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
968 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
969 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
970
971 return 0;
972}
973
974static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800975isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
976 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800977{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800978 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
979
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800980 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
981 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
982 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800983 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800984 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800985 /*
986 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
987 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
988 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800989 mutex_lock(&isert_conn->conn_mutex);
990 if (coalesce && isert_conn->state == ISER_CONN_UP &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800991 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800992 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800993 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800994 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800995 return;
996 }
997 isert_conn->conn_comp_batch = 0;
998 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800999 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001000
1001 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001002}
1003
1004static int
1005isert_rdma_post_recvl(struct isert_conn *isert_conn)
1006{
1007 struct ib_recv_wr rx_wr, *rx_wr_fail;
1008 struct ib_sge sge;
1009 int ret;
1010
1011 memset(&sge, 0, sizeof(struct ib_sge));
1012 sge.addr = isert_conn->login_req_dma;
1013 sge.length = ISER_RX_LOGIN_SIZE;
1014 sge.lkey = isert_conn->conn_mr->lkey;
1015
1016 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1017 sge.addr, sge.length, sge.lkey);
1018
1019 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1020 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1021 rx_wr.sg_list = &sge;
1022 rx_wr.num_sge = 1;
1023
1024 isert_conn->post_recv_buf_count++;
1025 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1026 if (ret) {
1027 pr_err("ib_post_recv() failed: %d\n", ret);
1028 isert_conn->post_recv_buf_count--;
1029 }
1030
1031 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1032 return ret;
1033}
1034
1035static int
1036isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1037 u32 length)
1038{
1039 struct isert_conn *isert_conn = conn->context;
1040 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1041 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1042 int ret;
1043
1044 isert_create_send_desc(isert_conn, NULL, tx_desc);
1045
1046 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1047 sizeof(struct iscsi_hdr));
1048
1049 isert_init_tx_hdrs(isert_conn, tx_desc);
1050
1051 if (length > 0) {
1052 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1053
1054 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1055 length, DMA_TO_DEVICE);
1056
1057 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1058
1059 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1060 length, DMA_TO_DEVICE);
1061
1062 tx_dsg->addr = isert_conn->login_rsp_dma;
1063 tx_dsg->length = length;
1064 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1065 tx_desc->num_sge = 2;
1066 }
1067 if (!login->login_failed) {
1068 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001069 if (!conn->sess->sess_ops->SessionType &&
1070 isert_conn->conn_device->use_fastreg) {
1071 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001072 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1073
1074 ret = isert_conn_create_fastreg_pool(isert_conn,
1075 pi_support);
1076 if (ret) {
1077 pr_err("Conn: %p failed to create"
1078 " fastreg pool\n", isert_conn);
1079 return ret;
1080 }
1081 }
1082
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001083 ret = isert_alloc_rx_descriptors(isert_conn);
1084 if (ret)
1085 return ret;
1086
1087 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1088 if (ret)
1089 return ret;
1090
1091 isert_conn->state = ISER_CONN_UP;
1092 goto post_send;
1093 }
1094
1095 ret = isert_rdma_post_recvl(isert_conn);
1096 if (ret)
1097 return ret;
1098 }
1099post_send:
1100 ret = isert_post_send(isert_conn, tx_desc);
1101 if (ret)
1102 return ret;
1103
1104 return 0;
1105}
1106
1107static void
1108isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1109 struct isert_conn *isert_conn)
1110{
1111 struct iscsi_conn *conn = isert_conn->conn;
1112 struct iscsi_login *login = conn->conn_login;
1113 int size;
1114
1115 if (!login) {
1116 pr_err("conn->conn_login is NULL\n");
1117 dump_stack();
1118 return;
1119 }
1120
1121 if (login->first_request) {
1122 struct iscsi_login_req *login_req =
1123 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1124 /*
1125 * Setup the initial iscsi_login values from the leading
1126 * login request PDU.
1127 */
1128 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1129 login->current_stage =
1130 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1131 >> 2;
1132 login->version_min = login_req->min_version;
1133 login->version_max = login_req->max_version;
1134 memcpy(login->isid, login_req->isid, 6);
1135 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1136 login->init_task_tag = login_req->itt;
1137 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1138 login->cid = be16_to_cpu(login_req->cid);
1139 login->tsih = be16_to_cpu(login_req->tsih);
1140 }
1141
1142 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1143
1144 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1145 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1146 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1147 memcpy(login->req_buf, &rx_desc->data[0], size);
1148
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001149 if (login->first_request) {
1150 complete(&isert_conn->conn_login_comp);
1151 return;
1152 }
1153 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001154}
1155
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001156static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001157*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001158{
1159 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1160 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001161 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001162
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001163 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001164 if (!cmd) {
1165 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001166 return NULL;
1167 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001168 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001169 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001170 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001171
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001172 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001173}
1174
1175static int
1176isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001177 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1178 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001179{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001180 struct iscsi_conn *conn = isert_conn->conn;
1181 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1182 struct scatterlist *sg;
1183 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1184 bool dump_payload = false;
1185
1186 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1187 if (rc < 0)
1188 return rc;
1189
1190 imm_data = cmd->immediate_data;
1191 imm_data_len = cmd->first_burst_len;
1192 unsol_data = cmd->unsolicited_data;
1193
1194 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1195 if (rc < 0) {
1196 return 0;
1197 } else if (rc > 0) {
1198 dump_payload = true;
1199 goto sequence_cmd;
1200 }
1201
1202 if (!imm_data)
1203 return 0;
1204
1205 sg = &cmd->se_cmd.t_data_sg[0];
1206 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1207
1208 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1209 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1210
1211 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1212
1213 cmd->write_data_done += imm_data_len;
1214
1215 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1216 spin_lock_bh(&cmd->istate_lock);
1217 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1218 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1219 spin_unlock_bh(&cmd->istate_lock);
1220 }
1221
1222sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001223 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001224
1225 if (!rc && dump_payload == false && unsol_data)
1226 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001227 else if (dump_payload && imm_data)
1228 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001229
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001230 return 0;
1231}
1232
1233static int
1234isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1235 struct iser_rx_desc *rx_desc, unsigned char *buf)
1236{
1237 struct scatterlist *sg_start;
1238 struct iscsi_conn *conn = isert_conn->conn;
1239 struct iscsi_cmd *cmd = NULL;
1240 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1241 u32 unsol_data_len = ntoh24(hdr->dlength);
1242 int rc, sg_nents, sg_off, page_off;
1243
1244 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1245 if (rc < 0)
1246 return rc;
1247 else if (!cmd)
1248 return 0;
1249 /*
1250 * FIXME: Unexpected unsolicited_data out
1251 */
1252 if (!cmd->unsolicited_data) {
1253 pr_err("Received unexpected solicited data payload\n");
1254 dump_stack();
1255 return -1;
1256 }
1257
1258 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1259 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1260
1261 sg_off = cmd->write_data_done / PAGE_SIZE;
1262 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1263 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1264 page_off = cmd->write_data_done % PAGE_SIZE;
1265 /*
1266 * FIXME: Non page-aligned unsolicited_data out
1267 */
1268 if (page_off) {
1269 pr_err("Received unexpected non-page aligned data payload\n");
1270 dump_stack();
1271 return -1;
1272 }
1273 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1274 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1275
1276 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1277 unsol_data_len);
1278
1279 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1280 if (rc < 0)
1281 return rc;
1282
1283 return 0;
1284}
1285
1286static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001287isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001288 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1289 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001290{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001291 struct iscsi_conn *conn = isert_conn->conn;
1292 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1293 int rc;
1294
1295 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1296 if (rc < 0)
1297 return rc;
1298 /*
1299 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1300 */
1301
1302 return iscsit_process_nop_out(conn, cmd, hdr);
1303}
1304
1305static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001306isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001307 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1308 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001309{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001310 struct iscsi_conn *conn = isert_conn->conn;
1311 u32 payload_length = ntoh24(hdr->dlength);
1312 int rc;
1313 unsigned char *text_in;
1314
1315 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1316 if (rc < 0)
1317 return rc;
1318
1319 text_in = kzalloc(payload_length, GFP_KERNEL);
1320 if (!text_in) {
1321 pr_err("Unable to allocate text_in of payload_length: %u\n",
1322 payload_length);
1323 return -ENOMEM;
1324 }
1325 cmd->text_in_ptr = text_in;
1326
1327 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1328
1329 return iscsit_process_text_cmd(conn, cmd, hdr);
1330}
1331
1332static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001333isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1334 uint32_t read_stag, uint64_t read_va,
1335 uint32_t write_stag, uint64_t write_va)
1336{
1337 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1338 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001339 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001340 struct iscsi_cmd *cmd;
1341 struct isert_cmd *isert_cmd;
1342 int ret = -EINVAL;
1343 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1344
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001345 if (sess->sess_ops->SessionType &&
1346 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1347 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1348 " ignoring\n", opcode);
1349 return 0;
1350 }
1351
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001352 switch (opcode) {
1353 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001354 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001355 if (!cmd)
1356 break;
1357
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001358 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001359 isert_cmd->read_stag = read_stag;
1360 isert_cmd->read_va = read_va;
1361 isert_cmd->write_stag = write_stag;
1362 isert_cmd->write_va = write_va;
1363
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001364 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001365 rx_desc, (unsigned char *)hdr);
1366 break;
1367 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001368 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001369 if (!cmd)
1370 break;
1371
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001372 isert_cmd = iscsit_priv_cmd(cmd);
1373 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001374 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001375 break;
1376 case ISCSI_OP_SCSI_DATA_OUT:
1377 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1378 (unsigned char *)hdr);
1379 break;
1380 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001381 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001382 if (!cmd)
1383 break;
1384
1385 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1386 (unsigned char *)hdr);
1387 break;
1388 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001389 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001390 if (!cmd)
1391 break;
1392
1393 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1394 if (ret > 0)
1395 wait_for_completion_timeout(&conn->conn_logout_comp,
1396 SECONDS_FOR_LOGOUT_COMP *
1397 HZ);
1398 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001399 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001400 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001401 if (!cmd)
1402 break;
1403
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001404 isert_cmd = iscsit_priv_cmd(cmd);
1405 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001406 rx_desc, (struct iscsi_text *)hdr);
1407 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001408 default:
1409 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1410 dump_stack();
1411 break;
1412 }
1413
1414 return ret;
1415}
1416
1417static void
1418isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1419{
1420 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1421 uint64_t read_va = 0, write_va = 0;
1422 uint32_t read_stag = 0, write_stag = 0;
1423 int rc;
1424
1425 switch (iser_hdr->flags & 0xF0) {
1426 case ISCSI_CTRL:
1427 if (iser_hdr->flags & ISER_RSV) {
1428 read_stag = be32_to_cpu(iser_hdr->read_stag);
1429 read_va = be64_to_cpu(iser_hdr->read_va);
1430 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1431 read_stag, (unsigned long long)read_va);
1432 }
1433 if (iser_hdr->flags & ISER_WSV) {
1434 write_stag = be32_to_cpu(iser_hdr->write_stag);
1435 write_va = be64_to_cpu(iser_hdr->write_va);
1436 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1437 write_stag, (unsigned long long)write_va);
1438 }
1439
1440 pr_debug("ISER ISCSI_CTRL PDU\n");
1441 break;
1442 case ISER_HELLO:
1443 pr_err("iSER Hello message\n");
1444 break;
1445 default:
1446 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1447 break;
1448 }
1449
1450 rc = isert_rx_opcode(isert_conn, rx_desc,
1451 read_stag, read_va, write_stag, write_va);
1452}
1453
1454static void
1455isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1456 unsigned long xfer_len)
1457{
1458 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1459 struct iscsi_hdr *hdr;
1460 u64 rx_dma;
1461 int rx_buflen, outstanding;
1462
1463 if ((char *)desc == isert_conn->login_req_buf) {
1464 rx_dma = isert_conn->login_req_dma;
1465 rx_buflen = ISER_RX_LOGIN_SIZE;
1466 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1467 rx_dma, rx_buflen);
1468 } else {
1469 rx_dma = desc->dma_addr;
1470 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1471 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1472 rx_dma, rx_buflen);
1473 }
1474
1475 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1476
1477 hdr = &desc->iscsi_header;
1478 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1479 hdr->opcode, hdr->itt, hdr->flags,
1480 (int)(xfer_len - ISER_HEADERS_LEN));
1481
1482 if ((char *)desc == isert_conn->login_req_buf)
1483 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1484 isert_conn);
1485 else
1486 isert_rx_do_work(desc, isert_conn);
1487
1488 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1489 DMA_FROM_DEVICE);
1490
1491 isert_conn->post_recv_buf_count--;
1492 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1493 isert_conn->post_recv_buf_count);
1494
1495 if ((char *)desc == isert_conn->login_req_buf)
1496 return;
1497
1498 outstanding = isert_conn->post_recv_buf_count;
1499 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1500 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1501 ISERT_MIN_POSTED_RX);
1502 err = isert_post_recv(isert_conn, count);
1503 if (err) {
1504 pr_err("isert_post_recv() count: %d failed, %d\n",
1505 count, err);
1506 }
1507 }
1508}
1509
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001510static int
1511isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1512 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1513 enum iser_ib_op_code op, struct isert_data_buf *data)
1514{
1515 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1516
1517 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1518 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1519
1520 data->len = length - offset;
1521 data->offset = offset;
1522 data->sg_off = data->offset / PAGE_SIZE;
1523
1524 data->sg = &sg[data->sg_off];
1525 data->nents = min_t(unsigned int, nents - data->sg_off,
1526 ISCSI_ISER_SG_TABLESIZE);
1527 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1528 PAGE_SIZE);
1529
1530 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1531 data->dma_dir);
1532 if (unlikely(!data->dma_nents)) {
1533 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1534 return -EINVAL;
1535 }
1536
1537 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1538 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1539
1540 return 0;
1541}
1542
1543static void
1544isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1545{
1546 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1547
1548 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1549 memset(data, 0, sizeof(*data));
1550}
1551
1552
1553
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001554static void
1555isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1556{
1557 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558
Vu Pham90ecc6e2013-08-28 23:23:33 +03001559 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001560
1561 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001562 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001563 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001564 }
1565
Vu Pham90ecc6e2013-08-28 23:23:33 +03001566 if (wr->send_wr) {
1567 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1568 kfree(wr->send_wr);
1569 wr->send_wr = NULL;
1570 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001571
Vu Pham90ecc6e2013-08-28 23:23:33 +03001572 if (wr->ib_sge) {
1573 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1574 kfree(wr->ib_sge);
1575 wr->ib_sge = NULL;
1576 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001577}
1578
1579static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001580isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001581{
1582 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001583 LIST_HEAD(unmap_list);
1584
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001585 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001586
1587 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001588 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001589 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001590 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1591 isert_unmap_data_buf(isert_conn, &wr->prot);
1592 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1593 }
Vu Pham59464ef2013-08-28 23:23:35 +03001594 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001595 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001596 spin_unlock_bh(&isert_conn->conn_lock);
1597 wr->fr_desc = NULL;
1598 }
1599
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001600 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001601 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001602 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001603 }
1604
1605 wr->ib_sge = NULL;
1606 wr->send_wr = NULL;
1607}
1608
1609static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001610isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001612 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001613 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001614 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001615 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616
1617 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1618
1619 switch (cmd->iscsi_opcode) {
1620 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621 spin_lock_bh(&conn->cmd_lock);
1622 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001623 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001624 spin_unlock_bh(&conn->cmd_lock);
1625
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001626 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001627 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001628 /*
1629 * Check for special case during comp_err where
1630 * WRITE_PENDING has been handed off from core,
1631 * but requires an extra target_put_sess_cmd()
1632 * before transport_generic_free_cmd() below.
1633 */
1634 if (comp_err &&
1635 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1636 struct se_cmd *se_cmd = &cmd->se_cmd;
1637
1638 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1639 }
1640 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001641
Vu Phamd40945d2013-08-28 23:23:34 +03001642 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001643 transport_generic_free_cmd(&cmd->se_cmd, 0);
1644 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001645 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001646 spin_lock_bh(&conn->cmd_lock);
1647 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001648 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001649 spin_unlock_bh(&conn->cmd_lock);
1650
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651 transport_generic_free_cmd(&cmd->se_cmd, 0);
1652 break;
1653 case ISCSI_OP_REJECT:
1654 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001655 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001656 spin_lock_bh(&conn->cmd_lock);
1657 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001658 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001659 spin_unlock_bh(&conn->cmd_lock);
1660
1661 /*
1662 * Handle special case for REJECT when iscsi_add_reject*() has
1663 * overwritten the original iscsi_opcode assignment, and the
1664 * associated cmd->se_cmd needs to be released.
1665 */
1666 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001667 pr_debug("Calling transport_generic_free_cmd from"
1668 " isert_put_cmd for 0x%02x\n",
1669 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001670 transport_generic_free_cmd(&cmd->se_cmd, 0);
1671 break;
1672 }
1673 /*
1674 * Fall-through
1675 */
1676 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001677 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001678 break;
1679 }
1680}
1681
1682static void
1683isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1684{
1685 if (tx_desc->dma_addr != 0) {
1686 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1687 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1688 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1689 tx_desc->dma_addr = 0;
1690 }
1691}
1692
1693static void
1694isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001695 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001697 if (isert_cmd->pdu_buf_dma != 0) {
1698 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1699 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1700 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1701 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702 }
1703
1704 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001705 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706}
1707
Sagi Grimberg96b79732014-03-17 12:52:18 +02001708static int
1709isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1710{
1711 struct ib_mr_status mr_status;
1712 int ret;
1713
1714 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1715 if (ret) {
1716 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1717 goto fail_mr_status;
1718 }
1719
1720 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1721 u64 sec_offset_err;
1722 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1723
1724 switch (mr_status.sig_err.err_type) {
1725 case IB_SIG_BAD_GUARD:
1726 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1727 break;
1728 case IB_SIG_BAD_REFTAG:
1729 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1730 break;
1731 case IB_SIG_BAD_APPTAG:
1732 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1733 break;
1734 }
1735 sec_offset_err = mr_status.sig_err.sig_err_offset;
1736 do_div(sec_offset_err, block_size);
1737 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1738
1739 pr_err("isert: PI error found type %d at sector 0x%llx "
1740 "expected 0x%x vs actual 0x%x\n",
1741 mr_status.sig_err.err_type,
1742 (unsigned long long)se_cmd->bad_sector,
1743 mr_status.sig_err.expected,
1744 mr_status.sig_err.actual);
1745 ret = 1;
1746 }
1747
1748fail_mr_status:
1749 return ret;
1750}
1751
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001752static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001753isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1754 struct isert_cmd *isert_cmd)
1755{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001756 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001757 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001758 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001759 struct isert_conn *isert_conn = isert_cmd->conn;
1760 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001761 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001762
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001763 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001764 ret = isert_check_pi_status(se_cmd,
1765 wr->fr_desc->pi_ctx->sig_mr);
1766 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001767 }
1768
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001769 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001770 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001771 if (ret)
1772 transport_send_check_condition_and_sense(se_cmd,
1773 se_cmd->pi_err, 0);
1774 else
1775 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001776}
1777
1778static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001779isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1780 struct isert_cmd *isert_cmd)
1781{
1782 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001783 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001784 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001785 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001786 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001787 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001788
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001789 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001790 ret = isert_check_pi_status(se_cmd,
1791 wr->fr_desc->pi_ctx->sig_mr);
1792 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001793 }
1794
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001795 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001796 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001797 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001798 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001799
Vu Pham90ecc6e2013-08-28 23:23:33 +03001800 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001801 spin_lock_bh(&cmd->istate_lock);
1802 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1803 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1804 spin_unlock_bh(&cmd->istate_lock);
1805
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001806 if (ret)
1807 transport_send_check_condition_and_sense(se_cmd,
1808 se_cmd->pi_err, 0);
1809 else
1810 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001811}
1812
1813static void
1814isert_do_control_comp(struct work_struct *work)
1815{
1816 struct isert_cmd *isert_cmd = container_of(work,
1817 struct isert_cmd, comp_work);
1818 struct isert_conn *isert_conn = isert_cmd->conn;
1819 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001820 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001821
1822 switch (cmd->i_state) {
1823 case ISTATE_SEND_TASKMGTRSP:
1824 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1825
1826 atomic_dec(&isert_conn->post_send_buf_count);
1827 iscsit_tmr_post_handler(cmd, cmd->conn);
1828
1829 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001830 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001831 break;
1832 case ISTATE_SEND_REJECT:
1833 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1834 atomic_dec(&isert_conn->post_send_buf_count);
1835
1836 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001837 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001838 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001839 case ISTATE_SEND_LOGOUTRSP:
1840 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001841
1842 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001843 iscsit_logout_post_handler(cmd, cmd->conn);
1844 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001845 case ISTATE_SEND_TEXTRSP:
1846 atomic_dec(&isert_conn->post_send_buf_count);
1847 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001848 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001849 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001850 default:
1851 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1852 dump_stack();
1853 break;
1854 }
1855}
1856
1857static void
1858isert_response_completion(struct iser_tx_desc *tx_desc,
1859 struct isert_cmd *isert_cmd,
1860 struct isert_conn *isert_conn,
1861 struct ib_device *ib_dev)
1862{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001863 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001864 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001865
1866 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001867 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001868 cmd->i_state == ISTATE_SEND_REJECT ||
1869 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001870 isert_unmap_tx_desc(tx_desc, ib_dev);
1871
1872 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1873 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1874 return;
1875 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001876
1877 /**
1878 * If send_wr_num is 0 this means that we got
1879 * RDMA completion and we cleared it and we should
1880 * simply decrement the response post. else the
1881 * response is incorporated in send_wr_num, just
1882 * sub it.
1883 **/
1884 if (wr->send_wr_num)
1885 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1886 else
1887 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001888
1889 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001890 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001891}
1892
1893static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001894__isert_send_completion(struct iser_tx_desc *tx_desc,
1895 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001896{
1897 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1898 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1899 struct isert_rdma_wr *wr;
1900
1901 if (!isert_cmd) {
1902 atomic_dec(&isert_conn->post_send_buf_count);
1903 isert_unmap_tx_desc(tx_desc, ib_dev);
1904 return;
1905 }
1906 wr = &isert_cmd->rdma_wr;
1907
1908 switch (wr->iser_ib_op) {
1909 case ISER_IB_RECV:
1910 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1911 dump_stack();
1912 break;
1913 case ISER_IB_SEND:
1914 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1915 isert_response_completion(tx_desc, isert_cmd,
1916 isert_conn, ib_dev);
1917 break;
1918 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001919 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001920 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001921 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001922 break;
1923 case ISER_IB_RDMA_READ:
1924 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1925
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001926 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001927 isert_completion_rdma_read(tx_desc, isert_cmd);
1928 break;
1929 default:
1930 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1931 dump_stack();
1932 break;
1933 }
1934}
1935
1936static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001937isert_send_completion(struct iser_tx_desc *tx_desc,
1938 struct isert_conn *isert_conn)
1939{
1940 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1941 struct iser_tx_desc *t;
1942 /*
1943 * Drain coalesced completion llist starting from comp_llnode_batch
1944 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1945 */
1946 while (llnode) {
1947 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1948 llnode = llist_next(llnode);
1949 __isert_send_completion(t, isert_conn);
1950 }
1951 __isert_send_completion(tx_desc, isert_conn);
1952}
1953
1954static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001955isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1956{
1957 struct llist_node *llnode;
1958 struct isert_rdma_wr *wr;
1959 struct iser_tx_desc *t;
1960
1961 mutex_lock(&isert_conn->conn_mutex);
1962 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1963 isert_conn->conn_comp_batch = 0;
1964 mutex_unlock(&isert_conn->conn_mutex);
1965
1966 while (llnode) {
1967 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1968 llnode = llist_next(llnode);
1969 wr = &t->isert_cmd->rdma_wr;
1970
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001971 /**
1972 * If send_wr_num is 0 this means that we got
1973 * RDMA completion and we cleared it and we should
1974 * simply decrement the response post. else the
1975 * response is incorporated in send_wr_num, just
1976 * sub it.
1977 **/
1978 if (wr->send_wr_num)
1979 atomic_sub(wr->send_wr_num,
1980 &isert_conn->post_send_buf_count);
1981 else
1982 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001983
1984 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001985 }
1986}
1987
1988static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001989isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990{
1991 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001992 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001993 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1994 struct isert_rdma_wr *wr;
1995 struct iser_tx_desc *t;
1996
1997 while (llnode) {
1998 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1999 llnode = llist_next(llnode);
2000 wr = &t->isert_cmd->rdma_wr;
2001
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002002 /**
2003 * If send_wr_num is 0 this means that we got
2004 * RDMA completion and we cleared it and we should
2005 * simply decrement the response post. else the
2006 * response is incorporated in send_wr_num, just
2007 * sub it.
2008 **/
2009 if (wr->send_wr_num)
2010 atomic_sub(wr->send_wr_num,
2011 &isert_conn->post_send_buf_count);
2012 else
2013 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002014
2015 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002016 }
2017 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002018
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002019 if (!isert_cmd)
2020 isert_unmap_tx_desc(tx_desc, ib_dev);
2021 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002022 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002023}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002024
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002025static void
2026isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2027{
2028 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2029 struct iscsi_conn *conn = isert_conn->conn;
2030
2031 if (isert_conn->post_recv_buf_count)
2032 return;
2033
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002034 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2035
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002036 if (conn->sess) {
2037 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2038 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039 }
2040
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002041 while (atomic_read(&isert_conn->post_send_buf_count))
2042 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002044 mutex_lock(&isert_conn->conn_mutex);
2045 isert_conn->state = ISER_CONN_DOWN;
2046 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002047
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002048 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2049
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002050 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002051}
2052
2053static void
2054isert_cq_tx_work(struct work_struct *work)
2055{
2056 struct isert_cq_desc *cq_desc = container_of(work,
2057 struct isert_cq_desc, cq_tx_work);
2058 struct isert_device *device = cq_desc->device;
2059 int cq_index = cq_desc->cq_index;
2060 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2061 struct isert_conn *isert_conn;
2062 struct iser_tx_desc *tx_desc;
2063 struct ib_wc wc;
2064
2065 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2066 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2067 isert_conn = wc.qp->qp_context;
2068
2069 if (wc.status == IB_WC_SUCCESS) {
2070 isert_send_completion(tx_desc, isert_conn);
2071 } else {
2072 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2073 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002074 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002075
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002076 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002077 if (tx_desc->llnode_active)
2078 continue;
2079
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002080 atomic_dec(&isert_conn->post_send_buf_count);
2081 isert_cq_tx_comp_err(tx_desc, isert_conn);
2082 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083 }
2084 }
2085
2086 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2087}
2088
2089static void
2090isert_cq_tx_callback(struct ib_cq *cq, void *context)
2091{
2092 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2093
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002094 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2095}
2096
2097static void
2098isert_cq_rx_work(struct work_struct *work)
2099{
2100 struct isert_cq_desc *cq_desc = container_of(work,
2101 struct isert_cq_desc, cq_rx_work);
2102 struct isert_device *device = cq_desc->device;
2103 int cq_index = cq_desc->cq_index;
2104 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2105 struct isert_conn *isert_conn;
2106 struct iser_rx_desc *rx_desc;
2107 struct ib_wc wc;
2108 unsigned long xfer_len;
2109
2110 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2111 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2112 isert_conn = wc.qp->qp_context;
2113
2114 if (wc.status == IB_WC_SUCCESS) {
2115 xfer_len = (unsigned long)wc.byte_len;
2116 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2117 } else {
2118 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002119 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002120 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002121 pr_debug("RX wc.vendor_err: 0x%08x\n",
2122 wc.vendor_err);
2123 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002124 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002125 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002126 }
2127 }
2128
2129 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2130}
2131
2132static void
2133isert_cq_rx_callback(struct ib_cq *cq, void *context)
2134{
2135 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2136
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002137 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2138}
2139
2140static int
2141isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2142{
2143 struct ib_send_wr *wr_failed;
2144 int ret;
2145
2146 atomic_inc(&isert_conn->post_send_buf_count);
2147
2148 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2149 &wr_failed);
2150 if (ret) {
2151 pr_err("ib_post_send failed with %d\n", ret);
2152 atomic_dec(&isert_conn->post_send_buf_count);
2153 return ret;
2154 }
2155 return ret;
2156}
2157
2158static int
2159isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2160{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002161 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002162 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2163 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2164 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2165 &isert_cmd->tx_desc.iscsi_header;
2166
2167 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2168 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2169 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2170 /*
2171 * Attach SENSE DATA payload to iSCSI Response PDU
2172 */
2173 if (cmd->se_cmd.sense_buffer &&
2174 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2175 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2176 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2177 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002178 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179
2180 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2181 cmd->sense_buffer);
2182 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2183
2184 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2185 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002186 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002188 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2189 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002190 DMA_TO_DEVICE);
2191
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002192 isert_cmd->pdu_buf_len = pdu_len;
2193 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2194 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002195 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2196 isert_cmd->tx_desc.num_sge = 2;
2197 }
2198
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002199 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002200
2201 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2202
2203 return isert_post_response(isert_conn, isert_cmd);
2204}
2205
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002206static void
2207isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2208{
2209 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2210 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2211 struct isert_device *device = isert_conn->conn_device;
2212
2213 spin_lock_bh(&conn->cmd_lock);
2214 if (!list_empty(&cmd->i_conn_node))
2215 list_del_init(&cmd->i_conn_node);
2216 spin_unlock_bh(&conn->cmd_lock);
2217
2218 if (cmd->data_direction == DMA_TO_DEVICE)
2219 iscsit_stop_dataout_timer(cmd);
2220
2221 device->unreg_rdma_mem(isert_cmd, isert_conn);
2222}
2223
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002224static enum target_prot_op
2225isert_get_sup_prot_ops(struct iscsi_conn *conn)
2226{
2227 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2228 struct isert_device *device = isert_conn->conn_device;
2229
2230 if (device->pi_capable)
2231 return TARGET_PROT_ALL;
2232
2233 return TARGET_PROT_NORMAL;
2234}
2235
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002236static int
2237isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2238 bool nopout_response)
2239{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002240 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002241 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2242 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2243
2244 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2245 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2246 &isert_cmd->tx_desc.iscsi_header,
2247 nopout_response);
2248 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002249 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002250
Masanari Iida8b513d02013-05-21 23:13:12 +09002251 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002252
2253 return isert_post_response(isert_conn, isert_cmd);
2254}
2255
2256static int
2257isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2258{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002259 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002260 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2261 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2262
2263 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2264 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2265 &isert_cmd->tx_desc.iscsi_header);
2266 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002267 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002268
2269 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2270
2271 return isert_post_response(isert_conn, isert_cmd);
2272}
2273
2274static int
2275isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2276{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002277 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2279 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2280
2281 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2282 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2283 &isert_cmd->tx_desc.iscsi_header);
2284 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002285 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002286
2287 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2288
2289 return isert_post_response(isert_conn, isert_cmd);
2290}
2291
2292static int
2293isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2294{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002295 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002296 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2297 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002298 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2299 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2300 struct iscsi_reject *hdr =
2301 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002302
2303 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002304 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002306
2307 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002308 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002309 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2310 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002311 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2312 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002313 tx_dsg->length = ISCSI_HDR_LEN;
2314 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2315 isert_cmd->tx_desc.num_sge = 2;
2316
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002317 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002318
2319 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2320
2321 return isert_post_response(isert_conn, isert_cmd);
2322}
2323
2324static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002325isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2326{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002327 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002328 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2329 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2330 struct iscsi_text_rsp *hdr =
2331 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2332 u32 txt_rsp_len;
2333 int rc;
2334
2335 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002336 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002337 if (rc < 0)
2338 return rc;
2339
2340 txt_rsp_len = rc;
2341 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2342
2343 if (txt_rsp_len) {
2344 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2345 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2346 void *txt_rsp_buf = cmd->buf_ptr;
2347
2348 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2349 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2350
2351 isert_cmd->pdu_buf_len = txt_rsp_len;
2352 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2353 tx_dsg->length = txt_rsp_len;
2354 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2355 isert_cmd->tx_desc.num_sge = 2;
2356 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002357 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002358
2359 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2360
2361 return isert_post_response(isert_conn, isert_cmd);
2362}
2363
2364static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002365isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2366 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2367 u32 data_left, u32 offset)
2368{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002369 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002370 struct scatterlist *sg_start, *tmp_sg;
2371 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2372 u32 sg_off, page_off;
2373 int i = 0, sg_nents;
2374
2375 sg_off = offset / PAGE_SIZE;
2376 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2377 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2378 page_off = offset % PAGE_SIZE;
2379
2380 send_wr->sg_list = ib_sge;
2381 send_wr->num_sge = sg_nents;
2382 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2383 /*
2384 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2385 */
2386 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2387 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2388 (unsigned long long)tmp_sg->dma_address,
2389 tmp_sg->length, page_off);
2390
2391 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2392 ib_sge->length = min_t(u32, data_left,
2393 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2394 ib_sge->lkey = isert_conn->conn_mr->lkey;
2395
Vu Pham90ecc6e2013-08-28 23:23:33 +03002396 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2397 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398 page_off = 0;
2399 data_left -= ib_sge->length;
2400 ib_sge++;
2401 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2402 }
2403
2404 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2405 send_wr->sg_list, send_wr->num_sge);
2406
2407 return sg_nents;
2408}
2409
2410static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002411isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2412 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002413{
2414 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002415 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002417 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002418 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002419 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002420 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2421 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002422
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002423 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002424
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002425 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2426 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2427 se_cmd->t_data_nents, se_cmd->data_length,
2428 offset, wr->iser_ib_op, &wr->data);
2429 if (ret)
2430 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002431
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002432 data_left = data->len;
2433 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002434
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002435 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002437 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002439 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002440 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002441 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002443 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002444 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2445 GFP_KERNEL);
2446 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002447 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002448 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002449 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002450 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451
2452 wr->isert_cmd = isert_cmd;
2453 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002454
2455 for (i = 0; i < wr->send_wr_num; i++) {
2456 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2457 data_len = min(data_left, rdma_write_max);
2458
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002459 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002460 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2461 send_wr->opcode = IB_WR_RDMA_WRITE;
2462 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2463 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2464 if (i + 1 == wr->send_wr_num)
2465 send_wr->next = &isert_cmd->tx_desc.send_wr;
2466 else
2467 send_wr->next = &wr->send_wr[i + 1];
2468 } else {
2469 send_wr->opcode = IB_WR_RDMA_READ;
2470 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2471 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2472 if (i + 1 == wr->send_wr_num)
2473 send_wr->send_flags = IB_SEND_SIGNALED;
2474 else
2475 send_wr->next = &wr->send_wr[i + 1];
2476 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002477
2478 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2479 send_wr, data_len, offset);
2480 ib_sge += ib_sge_cnt;
2481
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002482 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002483 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002484 data_left -= data_len;
2485 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002486
2487 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002488unmap_cmd:
2489 isert_unmap_data_buf(isert_conn, data);
2490
Vu Pham90ecc6e2013-08-28 23:23:33 +03002491 return ret;
2492}
2493
2494static int
Vu Pham59464ef2013-08-28 23:23:35 +03002495isert_map_fr_pagelist(struct ib_device *ib_dev,
2496 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2497{
2498 u64 start_addr, end_addr, page, chunk_start = 0;
2499 struct scatterlist *tmp_sg;
2500 int i = 0, new_chunk, last_ent, n_pages;
2501
2502 n_pages = 0;
2503 new_chunk = 1;
2504 last_ent = sg_nents - 1;
2505 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2506 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2507 if (new_chunk)
2508 chunk_start = start_addr;
2509 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2510
2511 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2512 i, (unsigned long long)tmp_sg->dma_address,
2513 tmp_sg->length);
2514
2515 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2516 new_chunk = 0;
2517 continue;
2518 }
2519 new_chunk = 1;
2520
2521 page = chunk_start & PAGE_MASK;
2522 do {
2523 fr_pl[n_pages++] = page;
2524 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2525 n_pages - 1, page);
2526 page += PAGE_SIZE;
2527 } while (page < end_addr);
2528 }
2529
2530 return n_pages;
2531}
2532
2533static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002534isert_fast_reg_mr(struct isert_conn *isert_conn,
2535 struct fast_reg_descriptor *fr_desc,
2536 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002537 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002538 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002539{
Vu Pham59464ef2013-08-28 23:23:35 +03002540 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002541 struct ib_mr *mr;
2542 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002543 struct ib_send_wr fr_wr, inv_wr;
2544 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002545 int ret, pagelist_len;
2546 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002547 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002548
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002549 if (mem->dma_nents == 1) {
2550 sge->lkey = isert_conn->conn_mr->lkey;
2551 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2552 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002553 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2554 __func__, __LINE__, sge->addr, sge->length,
2555 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002556 return 0;
2557 }
2558
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002559 if (ind == ISERT_DATA_KEY_VALID) {
2560 /* Registering data buffer */
2561 mr = fr_desc->data_mr;
2562 frpl = fr_desc->data_frpl;
2563 } else {
2564 /* Registering protection buffer */
2565 mr = fr_desc->pi_ctx->prot_mr;
2566 frpl = fr_desc->pi_ctx->prot_frpl;
2567 }
2568
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002569 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002570
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002571 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002572 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002573
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002574 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002575 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002576
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002577 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002578 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002579 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002580 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002581 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002582 wr = &inv_wr;
2583 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002584 key = (u8)(mr->rkey & 0x000000FF);
2585 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002586 }
2587
2588 /* Prepare FASTREG WR */
2589 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002590 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002591 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002592 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2593 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002594 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2595 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002596 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002597 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002598 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2599
2600 if (!wr)
2601 wr = &fr_wr;
2602 else
2603 wr->next = &fr_wr;
2604
2605 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2606 if (ret) {
2607 pr_err("fast registration failed, ret:%d\n", ret);
2608 return ret;
2609 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002610 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002611
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002612 sge->lkey = mr->lkey;
2613 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002614 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002615
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002616 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2617 __func__, __LINE__, sge->addr, sge->length,
2618 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002619
2620 return ret;
2621}
2622
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002623static inline void
2624isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2625 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002626{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002627 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002628 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2629 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2630 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002631 /*
2632 * At the moment we hard code those, but if in the future
2633 * the target core would like to use it, we will take it
2634 * from se_cmd.
2635 */
2636 domain->sig.dif.apptag_check_mask = 0xffff;
2637 domain->sig.dif.app_escape = true;
2638 domain->sig.dif.ref_escape = true;
2639 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2640 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2641 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002642};
2643
2644static int
2645isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2646{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002647 switch (se_cmd->prot_op) {
2648 case TARGET_PROT_DIN_INSERT:
2649 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002650 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002651 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002652 break;
2653 case TARGET_PROT_DOUT_INSERT:
2654 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002655 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002656 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002657 break;
2658 case TARGET_PROT_DIN_PASS:
2659 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002660 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2661 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002662 break;
2663 default:
2664 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2665 return -EINVAL;
2666 }
2667
2668 return 0;
2669}
2670
2671static inline u8
2672isert_set_prot_checks(u8 prot_checks)
2673{
2674 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2675 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2676 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2677}
2678
2679static int
2680isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2681 struct fast_reg_descriptor *fr_desc,
2682 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2683 struct ib_sge *sig_sge)
2684{
2685 struct ib_send_wr sig_wr, inv_wr;
2686 struct ib_send_wr *bad_wr, *wr = NULL;
2687 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2688 struct ib_sig_attrs sig_attrs;
2689 int ret;
2690 u32 key;
2691
2692 memset(&sig_attrs, 0, sizeof(sig_attrs));
2693 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2694 if (ret)
2695 goto err;
2696
2697 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2698
2699 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2700 memset(&inv_wr, 0, sizeof(inv_wr));
2701 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002702 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002703 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2704 wr = &inv_wr;
2705 /* Bump the key */
2706 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2707 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2708 }
2709
2710 memset(&sig_wr, 0, sizeof(sig_wr));
2711 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002712 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002713 sig_wr.sg_list = data_sge;
2714 sig_wr.num_sge = 1;
2715 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2716 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2717 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2718 if (se_cmd->t_prot_sg)
2719 sig_wr.wr.sig_handover.prot = prot_sge;
2720
2721 if (!wr)
2722 wr = &sig_wr;
2723 else
2724 wr->next = &sig_wr;
2725
2726 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2727 if (ret) {
2728 pr_err("fast registration failed, ret:%d\n", ret);
2729 goto err;
2730 }
2731 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2732
2733 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2734 sig_sge->addr = 0;
2735 sig_sge->length = se_cmd->data_length;
2736 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2737 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2738 /*
2739 * We have protection guards on the wire
2740 * so we need to set a larget transfer
2741 */
2742 sig_sge->length += se_cmd->prot_length;
2743
2744 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2745 sig_sge->addr, sig_sge->length,
2746 sig_sge->lkey);
2747err:
2748 return ret;
2749}
2750
Vu Pham59464ef2013-08-28 23:23:35 +03002751static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002752isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2753 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002754{
2755 struct se_cmd *se_cmd = &cmd->se_cmd;
2756 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002757 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002758 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002759 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002760 struct fast_reg_descriptor *fr_desc = NULL;
2761 u32 offset;
2762 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002763 unsigned long flags;
2764
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002765 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2766
2767 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2768 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2769 se_cmd->t_data_nents, se_cmd->data_length,
2770 offset, wr->iser_ib_op, &wr->data);
2771 if (ret)
2772 return ret;
2773
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002774 if (wr->data.dma_nents != 1 ||
2775 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002776 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2777 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2778 struct fast_reg_descriptor, list);
2779 list_del(&fr_desc->list);
2780 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2781 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002782 }
2783
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002784 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2785 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002786 if (ret)
2787 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002788
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002789 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2790 struct ib_sge prot_sge, sig_sge;
2791
2792 if (se_cmd->t_prot_sg) {
2793 ret = isert_map_data_buf(isert_conn, isert_cmd,
2794 se_cmd->t_prot_sg,
2795 se_cmd->t_prot_nents,
2796 se_cmd->prot_length,
2797 0, wr->iser_ib_op, &wr->prot);
2798 if (ret)
2799 goto unmap_cmd;
2800
2801 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2802 ISERT_PROT_KEY_VALID, &prot_sge);
2803 if (ret)
2804 goto unmap_prot_cmd;
2805 }
2806
2807 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2808 &data_sge, &prot_sge, &sig_sge);
2809 if (ret)
2810 goto unmap_prot_cmd;
2811
2812 fr_desc->ind |= ISERT_PROTECTED;
2813 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2814 } else
2815 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2816
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002817 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002818 wr->send_wr_num = 1;
2819 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2820 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002821 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002822
2823 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002824 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002825 send_wr->num_sge = 1;
2826 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2827 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2828 send_wr->opcode = IB_WR_RDMA_WRITE;
2829 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2830 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002831 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2832 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002833 } else {
2834 send_wr->opcode = IB_WR_RDMA_READ;
2835 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2836 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2837 send_wr->send_flags = IB_SEND_SIGNALED;
2838 }
2839
Vu Pham59464ef2013-08-28 23:23:35 +03002840 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002841unmap_prot_cmd:
2842 if (se_cmd->t_prot_sg)
2843 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002844unmap_cmd:
2845 if (fr_desc) {
2846 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2847 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2848 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2849 }
2850 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002851
Vu Pham59464ef2013-08-28 23:23:35 +03002852 return ret;
2853}
2854
2855static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002856isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2857{
2858 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002859 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002860 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2861 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002862 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002863 struct ib_send_wr *wr_failed;
2864 int rc;
2865
2866 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2867 isert_cmd, se_cmd->data_length);
2868 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002869 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002870 if (rc) {
2871 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2872 return rc;
2873 }
2874
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002875 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2876 /*
2877 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2878 */
2879 isert_create_send_desc(isert_conn, isert_cmd,
2880 &isert_cmd->tx_desc);
2881 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2882 &isert_cmd->tx_desc.iscsi_header);
2883 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2884 isert_init_send_wr(isert_conn, isert_cmd,
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002885 &isert_cmd->tx_desc.send_wr, false);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002886 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002887 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002888 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002889
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002890 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002891
2892 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2893 if (rc) {
2894 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002895 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002897
2898 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2899 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2900 "READ\n", isert_cmd);
2901 else
2902 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2903 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002904
Vu Pham90ecc6e2013-08-28 23:23:33 +03002905 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002906}
2907
2908static int
2909isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2910{
2911 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002912 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2914 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002915 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002916 struct ib_send_wr *wr_failed;
2917 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918
Vu Pham90ecc6e2013-08-28 23:23:33 +03002919 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2920 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002921 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002922 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002923 if (rc) {
2924 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2925 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926 }
2927
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002928 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002929
2930 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2931 if (rc) {
2932 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002933 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002934 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002935 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2936 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002937
Vu Pham90ecc6e2013-08-28 23:23:33 +03002938 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002939}
2940
2941static int
2942isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2943{
2944 int ret;
2945
2946 switch (state) {
2947 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2948 ret = isert_put_nopin(cmd, conn, false);
2949 break;
2950 default:
2951 pr_err("Unknown immediate state: 0x%02x\n", state);
2952 ret = -EINVAL;
2953 break;
2954 }
2955
2956 return ret;
2957}
2958
2959static int
2960isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2961{
2962 int ret;
2963
2964 switch (state) {
2965 case ISTATE_SEND_LOGOUTRSP:
2966 ret = isert_put_logout_rsp(cmd, conn);
2967 if (!ret) {
2968 pr_debug("Returning iSER Logout -EAGAIN\n");
2969 ret = -EAGAIN;
2970 }
2971 break;
2972 case ISTATE_SEND_NOPIN:
2973 ret = isert_put_nopin(cmd, conn, true);
2974 break;
2975 case ISTATE_SEND_TASKMGTRSP:
2976 ret = isert_put_tm_rsp(cmd, conn);
2977 break;
2978 case ISTATE_SEND_REJECT:
2979 ret = isert_put_reject(cmd, conn);
2980 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002981 case ISTATE_SEND_TEXTRSP:
2982 ret = isert_put_text_rsp(cmd, conn);
2983 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002984 case ISTATE_SEND_STATUS:
2985 /*
2986 * Special case for sending non GOOD SCSI status from TX thread
2987 * context during pre se_cmd excecution failure.
2988 */
2989 ret = isert_put_response(conn, cmd);
2990 break;
2991 default:
2992 pr_err("Unknown response state: 0x%02x\n", state);
2993 ret = -EINVAL;
2994 break;
2995 }
2996
2997 return ret;
2998}
2999
3000static int
3001isert_setup_np(struct iscsi_np *np,
3002 struct __kernel_sockaddr_storage *ksockaddr)
3003{
3004 struct isert_np *isert_np;
3005 struct rdma_cm_id *isert_lid;
3006 struct sockaddr *sa;
3007 int ret;
3008
3009 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3010 if (!isert_np) {
3011 pr_err("Unable to allocate struct isert_np\n");
3012 return -ENOMEM;
3013 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003014 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003015 mutex_init(&isert_np->np_accept_mutex);
3016 INIT_LIST_HEAD(&isert_np->np_accept_list);
3017 init_completion(&isert_np->np_login_comp);
3018
3019 sa = (struct sockaddr *)ksockaddr;
3020 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3021 /*
3022 * Setup the np->np_sockaddr from the passed sockaddr setup
3023 * in iscsi_target_configfs.c code..
3024 */
3025 memcpy(&np->np_sockaddr, ksockaddr,
3026 sizeof(struct __kernel_sockaddr_storage));
3027
3028 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3029 IB_QPT_RC);
3030 if (IS_ERR(isert_lid)) {
3031 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3032 PTR_ERR(isert_lid));
3033 ret = PTR_ERR(isert_lid);
3034 goto out;
3035 }
3036
3037 ret = rdma_bind_addr(isert_lid, sa);
3038 if (ret) {
3039 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3040 goto out_lid;
3041 }
3042
3043 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3044 if (ret) {
3045 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3046 goto out_lid;
3047 }
3048
3049 isert_np->np_cm_id = isert_lid;
3050 np->np_context = isert_np;
3051 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3052
3053 return 0;
3054
3055out_lid:
3056 rdma_destroy_id(isert_lid);
3057out:
3058 kfree(isert_np);
3059 return ret;
3060}
3061
3062static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003063isert_rdma_accept(struct isert_conn *isert_conn)
3064{
3065 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3066 struct rdma_conn_param cp;
3067 int ret;
3068
3069 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003070 cp.initiator_depth = isert_conn->initiator_depth;
3071 cp.retry_count = 7;
3072 cp.rnr_retry_count = 7;
3073
3074 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3075
3076 ret = rdma_accept(cm_id, &cp);
3077 if (ret) {
3078 pr_err("rdma_accept() failed with: %d\n", ret);
3079 return ret;
3080 }
3081
3082 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3083
3084 return 0;
3085}
3086
3087static int
3088isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3089{
3090 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3091 int ret;
3092
3093 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003094 /*
3095 * For login requests after the first PDU, isert_rx_login_req() will
3096 * kick schedule_delayed_work(&conn->login_work) as the packet is
3097 * received, which turns this callback from iscsi_target_do_login_rx()
3098 * into a NOP.
3099 */
3100 if (!login->first_request)
3101 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003102
3103 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3104 if (ret)
3105 return ret;
3106
3107 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3108 return 0;
3109}
3110
3111static void
3112isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3113 struct isert_conn *isert_conn)
3114{
3115 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3116 struct rdma_route *cm_route = &cm_id->route;
3117 struct sockaddr_in *sock_in;
3118 struct sockaddr_in6 *sock_in6;
3119
3120 conn->login_family = np->np_sockaddr.ss_family;
3121
3122 if (np->np_sockaddr.ss_family == AF_INET6) {
3123 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3124 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3125 &sock_in6->sin6_addr.in6_u);
3126 conn->login_port = ntohs(sock_in6->sin6_port);
3127
3128 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3129 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3130 &sock_in6->sin6_addr.in6_u);
3131 conn->local_port = ntohs(sock_in6->sin6_port);
3132 } else {
3133 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3134 sprintf(conn->login_ip, "%pI4",
3135 &sock_in->sin_addr.s_addr);
3136 conn->login_port = ntohs(sock_in->sin_port);
3137
3138 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3139 sprintf(conn->local_ip, "%pI4",
3140 &sock_in->sin_addr.s_addr);
3141 conn->local_port = ntohs(sock_in->sin_port);
3142 }
3143}
3144
3145static int
3146isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3147{
3148 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3149 struct isert_conn *isert_conn;
3150 int max_accept = 0, ret;
3151
3152accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003153 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003154 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003155 return -ENODEV;
3156
3157 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003158 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003159 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003160 pr_debug("np_thread_state %d for isert_accept_np\n",
3161 np->np_thread_state);
3162 /**
3163 * No point in stalling here when np_thread
3164 * is in state RESET/SHUTDOWN/EXIT - bail
3165 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003166 return -ENODEV;
3167 }
3168 spin_unlock_bh(&np->np_thread_lock);
3169
3170 mutex_lock(&isert_np->np_accept_mutex);
3171 if (list_empty(&isert_np->np_accept_list)) {
3172 mutex_unlock(&isert_np->np_accept_mutex);
3173 max_accept++;
3174 goto accept_wait;
3175 }
3176 isert_conn = list_first_entry(&isert_np->np_accept_list,
3177 struct isert_conn, conn_accept_node);
3178 list_del_init(&isert_conn->conn_accept_node);
3179 mutex_unlock(&isert_np->np_accept_mutex);
3180
3181 conn->context = isert_conn;
3182 isert_conn->conn = conn;
3183 max_accept = 0;
3184
3185 ret = isert_rdma_post_recvl(isert_conn);
3186 if (ret)
3187 return ret;
3188
3189 ret = isert_rdma_accept(isert_conn);
3190 if (ret)
3191 return ret;
3192
3193 isert_set_conn_info(np, conn, isert_conn);
3194
3195 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3196 return 0;
3197}
3198
3199static void
3200isert_free_np(struct iscsi_np *np)
3201{
3202 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3203
Sagi Grimberg3b726ae2014-10-28 13:45:03 -07003204 if (isert_np->np_cm_id)
3205 rdma_destroy_id(isert_np->np_cm_id);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003206
3207 np->np_context = NULL;
3208 kfree(isert_np);
3209}
3210
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003211static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003212{
3213 struct isert_conn *isert_conn = conn->context;
3214
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003215 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003216
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003217 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg38a83162014-07-02 16:19:26 +03003218 if (isert_conn->conn_cm_id && !isert_conn->disconnect) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003219 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003220 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003221 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003222 /*
3223 * Only wait for conn_wait_comp_err if the isert_conn made it
3224 * into full feature phase..
3225 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003226 if (isert_conn->state == ISER_CONN_INIT) {
3227 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003228 return;
3229 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003230 if (isert_conn->state == ISER_CONN_UP)
3231 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003232 mutex_unlock(&isert_conn->conn_mutex);
3233
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003234 wait_for_completion(&isert_conn->conn_wait_comp_err);
3235
3236 wait_for_completion(&isert_conn->conn_wait);
Sagi Grimberg0fc4ea72014-07-02 16:19:25 +03003237 isert_put_conn(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003238}
3239
3240static void isert_free_conn(struct iscsi_conn *conn)
3241{
3242 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003243
3244 isert_put_conn(isert_conn);
3245}
3246
3247static struct iscsit_transport iser_target_transport = {
3248 .name = "IB/iSER",
3249 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003250 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003251 .owner = THIS_MODULE,
3252 .iscsit_setup_np = isert_setup_np,
3253 .iscsit_accept_np = isert_accept_np,
3254 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003255 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003256 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003257 .iscsit_get_login_rx = isert_get_login_rx,
3258 .iscsit_put_login_tx = isert_put_login_tx,
3259 .iscsit_immediate_queue = isert_immediate_queue,
3260 .iscsit_response_queue = isert_response_queue,
3261 .iscsit_get_dataout = isert_get_dataout,
3262 .iscsit_queue_data_in = isert_put_datain,
3263 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003264 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003265 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003266};
3267
3268static int __init isert_init(void)
3269{
3270 int ret;
3271
3272 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3273 if (!isert_rx_wq) {
3274 pr_err("Unable to allocate isert_rx_wq\n");
3275 return -ENOMEM;
3276 }
3277
3278 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3279 if (!isert_comp_wq) {
3280 pr_err("Unable to allocate isert_comp_wq\n");
3281 ret = -ENOMEM;
3282 goto destroy_rx_wq;
3283 }
3284
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003285 iscsit_register_transport(&iser_target_transport);
3286 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3287 return 0;
3288
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003289destroy_rx_wq:
3290 destroy_workqueue(isert_rx_wq);
3291 return ret;
3292}
3293
3294static void __exit isert_exit(void)
3295{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003296 flush_scheduled_work();
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003297 destroy_workqueue(isert_comp_wq);
3298 destroy_workqueue(isert_rx_wq);
3299 iscsit_unregister_transport(&iser_target_transport);
3300 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3301}
3302
3303MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3304MODULE_VERSION("0.1");
3305MODULE_AUTHOR("nab@Linux-iSCSI.org");
3306MODULE_LICENSE("GPL");
3307
3308module_init(isert_init);
3309module_exit(isert_exit);