blob: ef5b22b3ea8398bf5388f498617d54f0f0076276 [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
809static void
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{
812 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
813
Sagi Grimberg88c40152014-05-19 17:44:24 +0300814 isert_conn->disconnect = disconnect;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800815 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
816 schedule_work(&isert_conn->conn_logout_work);
817}
818
819static int
820isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
821{
822 int ret = 0;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300823 bool disconnect = false;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800824
825 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
826 event->event, event->status, cma_id->context, cma_id);
827
828 switch (event->event) {
829 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800830 ret = isert_connect_request(cma_id, event);
831 break;
832 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800833 isert_connected_handler(cma_id);
834 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300835 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
836 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
837 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
838 disconnect = true;
839 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
840 isert_disconnected_handler(cma_id, disconnect);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800841 break;
842 case RDMA_CM_EVENT_CONNECT_ERROR:
843 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300844 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800845 break;
846 }
847
848 if (ret != 0) {
849 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
850 event->event, ret);
851 dump_stack();
852 }
853
854 return ret;
855}
856
857static int
858isert_post_recv(struct isert_conn *isert_conn, u32 count)
859{
860 struct ib_recv_wr *rx_wr, *rx_wr_failed;
861 int i, ret;
862 unsigned int rx_head = isert_conn->conn_rx_desc_head;
863 struct iser_rx_desc *rx_desc;
864
865 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
866 rx_desc = &isert_conn->conn_rx_descs[rx_head];
867 rx_wr->wr_id = (unsigned long)rx_desc;
868 rx_wr->sg_list = &rx_desc->rx_sg;
869 rx_wr->num_sge = 1;
870 rx_wr->next = rx_wr + 1;
871 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
872 }
873
874 rx_wr--;
875 rx_wr->next = NULL; /* mark end of work requests list */
876
877 isert_conn->post_recv_buf_count += count;
878 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
879 &rx_wr_failed);
880 if (ret) {
881 pr_err("ib_post_recv() failed with ret: %d\n", ret);
882 isert_conn->post_recv_buf_count -= count;
883 } else {
884 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
885 isert_conn->conn_rx_desc_head = rx_head;
886 }
887 return ret;
888}
889
890static int
891isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
892{
893 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
894 struct ib_send_wr send_wr, *send_wr_failed;
895 int ret;
896
897 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
898 ISER_HEADERS_LEN, DMA_TO_DEVICE);
899
900 send_wr.next = NULL;
901 send_wr.wr_id = (unsigned long)tx_desc;
902 send_wr.sg_list = tx_desc->tx_sg;
903 send_wr.num_sge = tx_desc->num_sge;
904 send_wr.opcode = IB_WR_SEND;
905 send_wr.send_flags = IB_SEND_SIGNALED;
906
907 atomic_inc(&isert_conn->post_send_buf_count);
908
909 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
910 if (ret) {
911 pr_err("ib_post_send() failed, ret: %d\n", ret);
912 atomic_dec(&isert_conn->post_send_buf_count);
913 }
914
915 return ret;
916}
917
918static void
919isert_create_send_desc(struct isert_conn *isert_conn,
920 struct isert_cmd *isert_cmd,
921 struct iser_tx_desc *tx_desc)
922{
923 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
924
925 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
926 ISER_HEADERS_LEN, DMA_TO_DEVICE);
927
928 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
929 tx_desc->iser_header.flags = ISER_VER;
930
931 tx_desc->num_sge = 1;
932 tx_desc->isert_cmd = isert_cmd;
933
934 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
935 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
936 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
937 }
938}
939
940static int
941isert_init_tx_hdrs(struct isert_conn *isert_conn,
942 struct iser_tx_desc *tx_desc)
943{
944 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
945 u64 dma_addr;
946
947 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
948 ISER_HEADERS_LEN, DMA_TO_DEVICE);
949 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
950 pr_err("ib_dma_mapping_error() failed\n");
951 return -ENOMEM;
952 }
953
954 tx_desc->dma_addr = dma_addr;
955 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
956 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
957 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
958
959 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
960 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
961 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
962
963 return 0;
964}
965
966static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800967isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
968 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800970 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
971
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800972 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
973 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
974 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800975 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800976 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800977 /*
978 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
979 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
980 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800981 mutex_lock(&isert_conn->conn_mutex);
982 if (coalesce && isert_conn->state == ISER_CONN_UP &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800983 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800984 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800985 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800986 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800987 return;
988 }
989 isert_conn->conn_comp_batch = 0;
990 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800991 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800992
993 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800994}
995
996static int
997isert_rdma_post_recvl(struct isert_conn *isert_conn)
998{
999 struct ib_recv_wr rx_wr, *rx_wr_fail;
1000 struct ib_sge sge;
1001 int ret;
1002
1003 memset(&sge, 0, sizeof(struct ib_sge));
1004 sge.addr = isert_conn->login_req_dma;
1005 sge.length = ISER_RX_LOGIN_SIZE;
1006 sge.lkey = isert_conn->conn_mr->lkey;
1007
1008 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1009 sge.addr, sge.length, sge.lkey);
1010
1011 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1012 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1013 rx_wr.sg_list = &sge;
1014 rx_wr.num_sge = 1;
1015
1016 isert_conn->post_recv_buf_count++;
1017 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1018 if (ret) {
1019 pr_err("ib_post_recv() failed: %d\n", ret);
1020 isert_conn->post_recv_buf_count--;
1021 }
1022
1023 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1024 return ret;
1025}
1026
1027static int
1028isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1029 u32 length)
1030{
1031 struct isert_conn *isert_conn = conn->context;
1032 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1033 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1034 int ret;
1035
1036 isert_create_send_desc(isert_conn, NULL, tx_desc);
1037
1038 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1039 sizeof(struct iscsi_hdr));
1040
1041 isert_init_tx_hdrs(isert_conn, tx_desc);
1042
1043 if (length > 0) {
1044 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1045
1046 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1047 length, DMA_TO_DEVICE);
1048
1049 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1050
1051 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1052 length, DMA_TO_DEVICE);
1053
1054 tx_dsg->addr = isert_conn->login_rsp_dma;
1055 tx_dsg->length = length;
1056 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1057 tx_desc->num_sge = 2;
1058 }
1059 if (!login->login_failed) {
1060 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001061 if (!conn->sess->sess_ops->SessionType &&
1062 isert_conn->conn_device->use_fastreg) {
1063 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001064 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1065
1066 ret = isert_conn_create_fastreg_pool(isert_conn,
1067 pi_support);
1068 if (ret) {
1069 pr_err("Conn: %p failed to create"
1070 " fastreg pool\n", isert_conn);
1071 return ret;
1072 }
1073 }
1074
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001075 ret = isert_alloc_rx_descriptors(isert_conn);
1076 if (ret)
1077 return ret;
1078
1079 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1080 if (ret)
1081 return ret;
1082
1083 isert_conn->state = ISER_CONN_UP;
1084 goto post_send;
1085 }
1086
1087 ret = isert_rdma_post_recvl(isert_conn);
1088 if (ret)
1089 return ret;
1090 }
1091post_send:
1092 ret = isert_post_send(isert_conn, tx_desc);
1093 if (ret)
1094 return ret;
1095
1096 return 0;
1097}
1098
1099static void
1100isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1101 struct isert_conn *isert_conn)
1102{
1103 struct iscsi_conn *conn = isert_conn->conn;
1104 struct iscsi_login *login = conn->conn_login;
1105 int size;
1106
1107 if (!login) {
1108 pr_err("conn->conn_login is NULL\n");
1109 dump_stack();
1110 return;
1111 }
1112
1113 if (login->first_request) {
1114 struct iscsi_login_req *login_req =
1115 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1116 /*
1117 * Setup the initial iscsi_login values from the leading
1118 * login request PDU.
1119 */
1120 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1121 login->current_stage =
1122 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1123 >> 2;
1124 login->version_min = login_req->min_version;
1125 login->version_max = login_req->max_version;
1126 memcpy(login->isid, login_req->isid, 6);
1127 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1128 login->init_task_tag = login_req->itt;
1129 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1130 login->cid = be16_to_cpu(login_req->cid);
1131 login->tsih = be16_to_cpu(login_req->tsih);
1132 }
1133
1134 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1135
1136 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1137 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1138 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1139 memcpy(login->req_buf, &rx_desc->data[0], size);
1140
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001141 if (login->first_request) {
1142 complete(&isert_conn->conn_login_comp);
1143 return;
1144 }
1145 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001146}
1147
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001148static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001149*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001150{
1151 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1152 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001153 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001154
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001155 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001156 if (!cmd) {
1157 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001158 return NULL;
1159 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001160 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001161 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001162 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001163
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001164 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001165}
1166
1167static int
1168isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001169 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1170 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001171{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001172 struct iscsi_conn *conn = isert_conn->conn;
1173 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1174 struct scatterlist *sg;
1175 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1176 bool dump_payload = false;
1177
1178 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1179 if (rc < 0)
1180 return rc;
1181
1182 imm_data = cmd->immediate_data;
1183 imm_data_len = cmd->first_burst_len;
1184 unsol_data = cmd->unsolicited_data;
1185
1186 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1187 if (rc < 0) {
1188 return 0;
1189 } else if (rc > 0) {
1190 dump_payload = true;
1191 goto sequence_cmd;
1192 }
1193
1194 if (!imm_data)
1195 return 0;
1196
1197 sg = &cmd->se_cmd.t_data_sg[0];
1198 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1199
1200 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1201 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1202
1203 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1204
1205 cmd->write_data_done += imm_data_len;
1206
1207 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1208 spin_lock_bh(&cmd->istate_lock);
1209 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1210 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1211 spin_unlock_bh(&cmd->istate_lock);
1212 }
1213
1214sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001215 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001216
1217 if (!rc && dump_payload == false && unsol_data)
1218 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001219 else if (dump_payload && imm_data)
1220 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001221
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001222 return 0;
1223}
1224
1225static int
1226isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1227 struct iser_rx_desc *rx_desc, unsigned char *buf)
1228{
1229 struct scatterlist *sg_start;
1230 struct iscsi_conn *conn = isert_conn->conn;
1231 struct iscsi_cmd *cmd = NULL;
1232 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1233 u32 unsol_data_len = ntoh24(hdr->dlength);
1234 int rc, sg_nents, sg_off, page_off;
1235
1236 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1237 if (rc < 0)
1238 return rc;
1239 else if (!cmd)
1240 return 0;
1241 /*
1242 * FIXME: Unexpected unsolicited_data out
1243 */
1244 if (!cmd->unsolicited_data) {
1245 pr_err("Received unexpected solicited data payload\n");
1246 dump_stack();
1247 return -1;
1248 }
1249
1250 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1251 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1252
1253 sg_off = cmd->write_data_done / PAGE_SIZE;
1254 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1255 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1256 page_off = cmd->write_data_done % PAGE_SIZE;
1257 /*
1258 * FIXME: Non page-aligned unsolicited_data out
1259 */
1260 if (page_off) {
1261 pr_err("Received unexpected non-page aligned data payload\n");
1262 dump_stack();
1263 return -1;
1264 }
1265 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1266 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1267
1268 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1269 unsol_data_len);
1270
1271 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1272 if (rc < 0)
1273 return rc;
1274
1275 return 0;
1276}
1277
1278static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001279isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001280 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1281 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001282{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001283 struct iscsi_conn *conn = isert_conn->conn;
1284 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1285 int rc;
1286
1287 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1288 if (rc < 0)
1289 return rc;
1290 /*
1291 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1292 */
1293
1294 return iscsit_process_nop_out(conn, cmd, hdr);
1295}
1296
1297static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001298isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001299 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1300 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001301{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001302 struct iscsi_conn *conn = isert_conn->conn;
1303 u32 payload_length = ntoh24(hdr->dlength);
1304 int rc;
1305 unsigned char *text_in;
1306
1307 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1308 if (rc < 0)
1309 return rc;
1310
1311 text_in = kzalloc(payload_length, GFP_KERNEL);
1312 if (!text_in) {
1313 pr_err("Unable to allocate text_in of payload_length: %u\n",
1314 payload_length);
1315 return -ENOMEM;
1316 }
1317 cmd->text_in_ptr = text_in;
1318
1319 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1320
1321 return iscsit_process_text_cmd(conn, cmd, hdr);
1322}
1323
1324static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001325isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1326 uint32_t read_stag, uint64_t read_va,
1327 uint32_t write_stag, uint64_t write_va)
1328{
1329 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1330 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001331 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001332 struct iscsi_cmd *cmd;
1333 struct isert_cmd *isert_cmd;
1334 int ret = -EINVAL;
1335 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1336
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001337 if (sess->sess_ops->SessionType &&
1338 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1339 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1340 " ignoring\n", opcode);
1341 return 0;
1342 }
1343
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001344 switch (opcode) {
1345 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001346 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001347 if (!cmd)
1348 break;
1349
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001350 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001351 isert_cmd->read_stag = read_stag;
1352 isert_cmd->read_va = read_va;
1353 isert_cmd->write_stag = write_stag;
1354 isert_cmd->write_va = write_va;
1355
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001356 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001357 rx_desc, (unsigned char *)hdr);
1358 break;
1359 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001360 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001361 if (!cmd)
1362 break;
1363
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001364 isert_cmd = iscsit_priv_cmd(cmd);
1365 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001366 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001367 break;
1368 case ISCSI_OP_SCSI_DATA_OUT:
1369 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1370 (unsigned char *)hdr);
1371 break;
1372 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001373 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001374 if (!cmd)
1375 break;
1376
1377 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1378 (unsigned char *)hdr);
1379 break;
1380 case ISCSI_OP_LOGOUT:
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_logout_cmd(conn, cmd, (unsigned char *)hdr);
1386 if (ret > 0)
1387 wait_for_completion_timeout(&conn->conn_logout_comp,
1388 SECONDS_FOR_LOGOUT_COMP *
1389 HZ);
1390 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001391 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001392 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001393 if (!cmd)
1394 break;
1395
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001396 isert_cmd = iscsit_priv_cmd(cmd);
1397 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001398 rx_desc, (struct iscsi_text *)hdr);
1399 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400 default:
1401 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1402 dump_stack();
1403 break;
1404 }
1405
1406 return ret;
1407}
1408
1409static void
1410isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1411{
1412 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1413 uint64_t read_va = 0, write_va = 0;
1414 uint32_t read_stag = 0, write_stag = 0;
1415 int rc;
1416
1417 switch (iser_hdr->flags & 0xF0) {
1418 case ISCSI_CTRL:
1419 if (iser_hdr->flags & ISER_RSV) {
1420 read_stag = be32_to_cpu(iser_hdr->read_stag);
1421 read_va = be64_to_cpu(iser_hdr->read_va);
1422 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1423 read_stag, (unsigned long long)read_va);
1424 }
1425 if (iser_hdr->flags & ISER_WSV) {
1426 write_stag = be32_to_cpu(iser_hdr->write_stag);
1427 write_va = be64_to_cpu(iser_hdr->write_va);
1428 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1429 write_stag, (unsigned long long)write_va);
1430 }
1431
1432 pr_debug("ISER ISCSI_CTRL PDU\n");
1433 break;
1434 case ISER_HELLO:
1435 pr_err("iSER Hello message\n");
1436 break;
1437 default:
1438 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1439 break;
1440 }
1441
1442 rc = isert_rx_opcode(isert_conn, rx_desc,
1443 read_stag, read_va, write_stag, write_va);
1444}
1445
1446static void
1447isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1448 unsigned long xfer_len)
1449{
1450 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1451 struct iscsi_hdr *hdr;
1452 u64 rx_dma;
1453 int rx_buflen, outstanding;
1454
1455 if ((char *)desc == isert_conn->login_req_buf) {
1456 rx_dma = isert_conn->login_req_dma;
1457 rx_buflen = ISER_RX_LOGIN_SIZE;
1458 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1459 rx_dma, rx_buflen);
1460 } else {
1461 rx_dma = desc->dma_addr;
1462 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1463 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1464 rx_dma, rx_buflen);
1465 }
1466
1467 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1468
1469 hdr = &desc->iscsi_header;
1470 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1471 hdr->opcode, hdr->itt, hdr->flags,
1472 (int)(xfer_len - ISER_HEADERS_LEN));
1473
1474 if ((char *)desc == isert_conn->login_req_buf)
1475 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1476 isert_conn);
1477 else
1478 isert_rx_do_work(desc, isert_conn);
1479
1480 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1481 DMA_FROM_DEVICE);
1482
1483 isert_conn->post_recv_buf_count--;
1484 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1485 isert_conn->post_recv_buf_count);
1486
1487 if ((char *)desc == isert_conn->login_req_buf)
1488 return;
1489
1490 outstanding = isert_conn->post_recv_buf_count;
1491 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1492 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1493 ISERT_MIN_POSTED_RX);
1494 err = isert_post_recv(isert_conn, count);
1495 if (err) {
1496 pr_err("isert_post_recv() count: %d failed, %d\n",
1497 count, err);
1498 }
1499 }
1500}
1501
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001502static int
1503isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1504 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1505 enum iser_ib_op_code op, struct isert_data_buf *data)
1506{
1507 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1508
1509 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1510 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1511
1512 data->len = length - offset;
1513 data->offset = offset;
1514 data->sg_off = data->offset / PAGE_SIZE;
1515
1516 data->sg = &sg[data->sg_off];
1517 data->nents = min_t(unsigned int, nents - data->sg_off,
1518 ISCSI_ISER_SG_TABLESIZE);
1519 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1520 PAGE_SIZE);
1521
1522 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1523 data->dma_dir);
1524 if (unlikely(!data->dma_nents)) {
1525 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1526 return -EINVAL;
1527 }
1528
1529 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1530 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1531
1532 return 0;
1533}
1534
1535static void
1536isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1537{
1538 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1539
1540 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1541 memset(data, 0, sizeof(*data));
1542}
1543
1544
1545
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001546static void
1547isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1548{
1549 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001550
Vu Pham90ecc6e2013-08-28 23:23:33 +03001551 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001552
1553 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001554 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001555 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001556 }
1557
Vu Pham90ecc6e2013-08-28 23:23:33 +03001558 if (wr->send_wr) {
1559 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1560 kfree(wr->send_wr);
1561 wr->send_wr = NULL;
1562 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001563
Vu Pham90ecc6e2013-08-28 23:23:33 +03001564 if (wr->ib_sge) {
1565 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1566 kfree(wr->ib_sge);
1567 wr->ib_sge = NULL;
1568 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001569}
1570
1571static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001572isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001573{
1574 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001575 LIST_HEAD(unmap_list);
1576
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001577 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001578
1579 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001580 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001581 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001582 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1583 isert_unmap_data_buf(isert_conn, &wr->prot);
1584 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1585 }
Vu Pham59464ef2013-08-28 23:23:35 +03001586 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001587 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001588 spin_unlock_bh(&isert_conn->conn_lock);
1589 wr->fr_desc = NULL;
1590 }
1591
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001592 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001593 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001594 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001595 }
1596
1597 wr->ib_sge = NULL;
1598 wr->send_wr = NULL;
1599}
1600
1601static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001602isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001603{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001604 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001605 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001606 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001607 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001608
1609 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1610
1611 switch (cmd->iscsi_opcode) {
1612 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001613 spin_lock_bh(&conn->cmd_lock);
1614 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001615 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616 spin_unlock_bh(&conn->cmd_lock);
1617
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001618 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001619 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001620 /*
1621 * Check for special case during comp_err where
1622 * WRITE_PENDING has been handed off from core,
1623 * but requires an extra target_put_sess_cmd()
1624 * before transport_generic_free_cmd() below.
1625 */
1626 if (comp_err &&
1627 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1628 struct se_cmd *se_cmd = &cmd->se_cmd;
1629
1630 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1631 }
1632 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001633
Vu Phamd40945d2013-08-28 23:23:34 +03001634 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001635 transport_generic_free_cmd(&cmd->se_cmd, 0);
1636 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001637 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001638 spin_lock_bh(&conn->cmd_lock);
1639 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001640 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001641 spin_unlock_bh(&conn->cmd_lock);
1642
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001643 transport_generic_free_cmd(&cmd->se_cmd, 0);
1644 break;
1645 case ISCSI_OP_REJECT:
1646 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001647 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001648 spin_lock_bh(&conn->cmd_lock);
1649 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001650 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001651 spin_unlock_bh(&conn->cmd_lock);
1652
1653 /*
1654 * Handle special case for REJECT when iscsi_add_reject*() has
1655 * overwritten the original iscsi_opcode assignment, and the
1656 * associated cmd->se_cmd needs to be released.
1657 */
1658 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001659 pr_debug("Calling transport_generic_free_cmd from"
1660 " isert_put_cmd for 0x%02x\n",
1661 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001662 transport_generic_free_cmd(&cmd->se_cmd, 0);
1663 break;
1664 }
1665 /*
1666 * Fall-through
1667 */
1668 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001669 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001670 break;
1671 }
1672}
1673
1674static void
1675isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1676{
1677 if (tx_desc->dma_addr != 0) {
1678 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1679 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1680 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1681 tx_desc->dma_addr = 0;
1682 }
1683}
1684
1685static void
1686isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001687 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001688{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001689 if (isert_cmd->pdu_buf_dma != 0) {
1690 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1691 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1692 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1693 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001694 }
1695
1696 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001697 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001698}
1699
Sagi Grimberg96b79732014-03-17 12:52:18 +02001700static int
1701isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1702{
1703 struct ib_mr_status mr_status;
1704 int ret;
1705
1706 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1707 if (ret) {
1708 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1709 goto fail_mr_status;
1710 }
1711
1712 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1713 u64 sec_offset_err;
1714 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1715
1716 switch (mr_status.sig_err.err_type) {
1717 case IB_SIG_BAD_GUARD:
1718 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1719 break;
1720 case IB_SIG_BAD_REFTAG:
1721 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1722 break;
1723 case IB_SIG_BAD_APPTAG:
1724 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1725 break;
1726 }
1727 sec_offset_err = mr_status.sig_err.sig_err_offset;
1728 do_div(sec_offset_err, block_size);
1729 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1730
1731 pr_err("isert: PI error found type %d at sector 0x%llx "
1732 "expected 0x%x vs actual 0x%x\n",
1733 mr_status.sig_err.err_type,
1734 (unsigned long long)se_cmd->bad_sector,
1735 mr_status.sig_err.expected,
1736 mr_status.sig_err.actual);
1737 ret = 1;
1738 }
1739
1740fail_mr_status:
1741 return ret;
1742}
1743
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001744static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001745isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1746 struct isert_cmd *isert_cmd)
1747{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001748 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001749 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001750 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001751 struct isert_conn *isert_conn = isert_cmd->conn;
1752 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001753 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001754
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001755 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001756 ret = isert_check_pi_status(se_cmd,
1757 wr->fr_desc->pi_ctx->sig_mr);
1758 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001759 }
1760
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001761 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001762 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001763 if (ret)
1764 transport_send_check_condition_and_sense(se_cmd,
1765 se_cmd->pi_err, 0);
1766 else
1767 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001768}
1769
1770static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001771isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1772 struct isert_cmd *isert_cmd)
1773{
1774 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001775 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001776 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001777 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001778 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001779 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001780
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001781 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001782 ret = isert_check_pi_status(se_cmd,
1783 wr->fr_desc->pi_ctx->sig_mr);
1784 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001785 }
1786
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001787 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001788 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001789 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001790 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001791
Vu Pham90ecc6e2013-08-28 23:23:33 +03001792 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001793 spin_lock_bh(&cmd->istate_lock);
1794 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1795 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1796 spin_unlock_bh(&cmd->istate_lock);
1797
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001798 if (ret)
1799 transport_send_check_condition_and_sense(se_cmd,
1800 se_cmd->pi_err, 0);
1801 else
1802 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001803}
1804
1805static void
1806isert_do_control_comp(struct work_struct *work)
1807{
1808 struct isert_cmd *isert_cmd = container_of(work,
1809 struct isert_cmd, comp_work);
1810 struct isert_conn *isert_conn = isert_cmd->conn;
1811 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001812 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001813
1814 switch (cmd->i_state) {
1815 case ISTATE_SEND_TASKMGTRSP:
1816 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1817
1818 atomic_dec(&isert_conn->post_send_buf_count);
1819 iscsit_tmr_post_handler(cmd, cmd->conn);
1820
1821 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001822 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001823 break;
1824 case ISTATE_SEND_REJECT:
1825 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1826 atomic_dec(&isert_conn->post_send_buf_count);
1827
1828 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001829 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001830 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001831 case ISTATE_SEND_LOGOUTRSP:
1832 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001833
1834 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835 iscsit_logout_post_handler(cmd, cmd->conn);
1836 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001837 case ISTATE_SEND_TEXTRSP:
1838 atomic_dec(&isert_conn->post_send_buf_count);
1839 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001840 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001841 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001842 default:
1843 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1844 dump_stack();
1845 break;
1846 }
1847}
1848
1849static void
1850isert_response_completion(struct iser_tx_desc *tx_desc,
1851 struct isert_cmd *isert_cmd,
1852 struct isert_conn *isert_conn,
1853 struct ib_device *ib_dev)
1854{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001855 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001856 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001857
1858 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001859 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001860 cmd->i_state == ISTATE_SEND_REJECT ||
1861 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001862 isert_unmap_tx_desc(tx_desc, ib_dev);
1863
1864 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1865 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1866 return;
1867 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001868
1869 /**
1870 * If send_wr_num is 0 this means that we got
1871 * RDMA completion and we cleared it and we should
1872 * simply decrement the response post. else the
1873 * response is incorporated in send_wr_num, just
1874 * sub it.
1875 **/
1876 if (wr->send_wr_num)
1877 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1878 else
1879 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001880
1881 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001882 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001883}
1884
1885static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001886__isert_send_completion(struct iser_tx_desc *tx_desc,
1887 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001888{
1889 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1890 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1891 struct isert_rdma_wr *wr;
1892
1893 if (!isert_cmd) {
1894 atomic_dec(&isert_conn->post_send_buf_count);
1895 isert_unmap_tx_desc(tx_desc, ib_dev);
1896 return;
1897 }
1898 wr = &isert_cmd->rdma_wr;
1899
1900 switch (wr->iser_ib_op) {
1901 case ISER_IB_RECV:
1902 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1903 dump_stack();
1904 break;
1905 case ISER_IB_SEND:
1906 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1907 isert_response_completion(tx_desc, isert_cmd,
1908 isert_conn, ib_dev);
1909 break;
1910 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001911 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001912 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001913 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001914 break;
1915 case ISER_IB_RDMA_READ:
1916 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1917
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001918 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001919 isert_completion_rdma_read(tx_desc, isert_cmd);
1920 break;
1921 default:
1922 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1923 dump_stack();
1924 break;
1925 }
1926}
1927
1928static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001929isert_send_completion(struct iser_tx_desc *tx_desc,
1930 struct isert_conn *isert_conn)
1931{
1932 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1933 struct iser_tx_desc *t;
1934 /*
1935 * Drain coalesced completion llist starting from comp_llnode_batch
1936 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1937 */
1938 while (llnode) {
1939 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1940 llnode = llist_next(llnode);
1941 __isert_send_completion(t, isert_conn);
1942 }
1943 __isert_send_completion(tx_desc, isert_conn);
1944}
1945
1946static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001947isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1948{
1949 struct llist_node *llnode;
1950 struct isert_rdma_wr *wr;
1951 struct iser_tx_desc *t;
1952
1953 mutex_lock(&isert_conn->conn_mutex);
1954 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1955 isert_conn->conn_comp_batch = 0;
1956 mutex_unlock(&isert_conn->conn_mutex);
1957
1958 while (llnode) {
1959 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1960 llnode = llist_next(llnode);
1961 wr = &t->isert_cmd->rdma_wr;
1962
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001963 /**
1964 * If send_wr_num is 0 this means that we got
1965 * RDMA completion and we cleared it and we should
1966 * simply decrement the response post. else the
1967 * response is incorporated in send_wr_num, just
1968 * sub it.
1969 **/
1970 if (wr->send_wr_num)
1971 atomic_sub(wr->send_wr_num,
1972 &isert_conn->post_send_buf_count);
1973 else
1974 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001975
1976 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001977 }
1978}
1979
1980static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001981isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982{
1983 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001984 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001985 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1986 struct isert_rdma_wr *wr;
1987 struct iser_tx_desc *t;
1988
1989 while (llnode) {
1990 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1991 llnode = llist_next(llnode);
1992 wr = &t->isert_cmd->rdma_wr;
1993
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001994 /**
1995 * If send_wr_num is 0 this means that we got
1996 * RDMA completion and we cleared it and we should
1997 * simply decrement the response post. else the
1998 * response is incorporated in send_wr_num, just
1999 * sub it.
2000 **/
2001 if (wr->send_wr_num)
2002 atomic_sub(wr->send_wr_num,
2003 &isert_conn->post_send_buf_count);
2004 else
2005 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002006
2007 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002008 }
2009 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002010
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002011 if (!isert_cmd)
2012 isert_unmap_tx_desc(tx_desc, ib_dev);
2013 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002014 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002015}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002016
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002017static void
2018isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2019{
2020 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2021 struct iscsi_conn *conn = isert_conn->conn;
2022
2023 if (isert_conn->post_recv_buf_count)
2024 return;
2025
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002026 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2027
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002028 if (conn->sess) {
2029 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2030 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002031 }
2032
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002033 while (atomic_read(&isert_conn->post_send_buf_count))
2034 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002035
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002036 mutex_lock(&isert_conn->conn_mutex);
2037 isert_conn->state = ISER_CONN_DOWN;
2038 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002039
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002040 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2041
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002042 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043}
2044
2045static void
2046isert_cq_tx_work(struct work_struct *work)
2047{
2048 struct isert_cq_desc *cq_desc = container_of(work,
2049 struct isert_cq_desc, cq_tx_work);
2050 struct isert_device *device = cq_desc->device;
2051 int cq_index = cq_desc->cq_index;
2052 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2053 struct isert_conn *isert_conn;
2054 struct iser_tx_desc *tx_desc;
2055 struct ib_wc wc;
2056
2057 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2058 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2059 isert_conn = wc.qp->qp_context;
2060
2061 if (wc.status == IB_WC_SUCCESS) {
2062 isert_send_completion(tx_desc, isert_conn);
2063 } else {
2064 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2065 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002066 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002067
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002068 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002069 if (tx_desc->llnode_active)
2070 continue;
2071
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002072 atomic_dec(&isert_conn->post_send_buf_count);
2073 isert_cq_tx_comp_err(tx_desc, isert_conn);
2074 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002075 }
2076 }
2077
2078 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2079}
2080
2081static void
2082isert_cq_tx_callback(struct ib_cq *cq, void *context)
2083{
2084 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2085
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002086 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2087}
2088
2089static void
2090isert_cq_rx_work(struct work_struct *work)
2091{
2092 struct isert_cq_desc *cq_desc = container_of(work,
2093 struct isert_cq_desc, cq_rx_work);
2094 struct isert_device *device = cq_desc->device;
2095 int cq_index = cq_desc->cq_index;
2096 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2097 struct isert_conn *isert_conn;
2098 struct iser_rx_desc *rx_desc;
2099 struct ib_wc wc;
2100 unsigned long xfer_len;
2101
2102 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2103 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2104 isert_conn = wc.qp->qp_context;
2105
2106 if (wc.status == IB_WC_SUCCESS) {
2107 xfer_len = (unsigned long)wc.byte_len;
2108 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2109 } else {
2110 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002111 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002112 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002113 pr_debug("RX wc.vendor_err: 0x%08x\n",
2114 wc.vendor_err);
2115 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002116 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002117 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118 }
2119 }
2120
2121 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2122}
2123
2124static void
2125isert_cq_rx_callback(struct ib_cq *cq, void *context)
2126{
2127 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2128
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002129 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2130}
2131
2132static int
2133isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2134{
2135 struct ib_send_wr *wr_failed;
2136 int ret;
2137
2138 atomic_inc(&isert_conn->post_send_buf_count);
2139
2140 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2141 &wr_failed);
2142 if (ret) {
2143 pr_err("ib_post_send failed with %d\n", ret);
2144 atomic_dec(&isert_conn->post_send_buf_count);
2145 return ret;
2146 }
2147 return ret;
2148}
2149
2150static int
2151isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2152{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002153 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002154 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2155 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2156 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2157 &isert_cmd->tx_desc.iscsi_header;
2158
2159 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2160 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2161 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2162 /*
2163 * Attach SENSE DATA payload to iSCSI Response PDU
2164 */
2165 if (cmd->se_cmd.sense_buffer &&
2166 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2167 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2168 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2169 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002170 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002171
2172 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2173 cmd->sense_buffer);
2174 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2175
2176 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2177 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002178 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002180 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2181 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002182 DMA_TO_DEVICE);
2183
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002184 isert_cmd->pdu_buf_len = pdu_len;
2185 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2186 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2188 isert_cmd->tx_desc.num_sge = 2;
2189 }
2190
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002191 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002192
2193 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2194
2195 return isert_post_response(isert_conn, isert_cmd);
2196}
2197
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002198static void
2199isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2200{
2201 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2202 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2203 struct isert_device *device = isert_conn->conn_device;
2204
2205 spin_lock_bh(&conn->cmd_lock);
2206 if (!list_empty(&cmd->i_conn_node))
2207 list_del_init(&cmd->i_conn_node);
2208 spin_unlock_bh(&conn->cmd_lock);
2209
2210 if (cmd->data_direction == DMA_TO_DEVICE)
2211 iscsit_stop_dataout_timer(cmd);
2212
2213 device->unreg_rdma_mem(isert_cmd, isert_conn);
2214}
2215
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002216static enum target_prot_op
2217isert_get_sup_prot_ops(struct iscsi_conn *conn)
2218{
2219 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2220 struct isert_device *device = isert_conn->conn_device;
2221
2222 if (device->pi_capable)
2223 return TARGET_PROT_ALL;
2224
2225 return TARGET_PROT_NORMAL;
2226}
2227
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002228static int
2229isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2230 bool nopout_response)
2231{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002232 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002233 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2234 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2235
2236 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2237 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2238 &isert_cmd->tx_desc.iscsi_header,
2239 nopout_response);
2240 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002241 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002242
Masanari Iida8b513d02013-05-21 23:13:12 +09002243 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002244
2245 return isert_post_response(isert_conn, isert_cmd);
2246}
2247
2248static int
2249isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2250{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002251 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002252 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2253 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2254
2255 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2256 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2257 &isert_cmd->tx_desc.iscsi_header);
2258 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002259 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002260
2261 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2262
2263 return isert_post_response(isert_conn, isert_cmd);
2264}
2265
2266static int
2267isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2268{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002269 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002270 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2271 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2272
2273 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2274 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2275 &isert_cmd->tx_desc.iscsi_header);
2276 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002277 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278
2279 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2280
2281 return isert_post_response(isert_conn, isert_cmd);
2282}
2283
2284static int
2285isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2286{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002287 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002288 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2289 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002290 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2291 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2292 struct iscsi_reject *hdr =
2293 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002294
2295 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002296 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002297 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002298
2299 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002300 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002301 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2302 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002303 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2304 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002305 tx_dsg->length = ISCSI_HDR_LEN;
2306 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2307 isert_cmd->tx_desc.num_sge = 2;
2308
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002309 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002310
2311 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2312
2313 return isert_post_response(isert_conn, isert_cmd);
2314}
2315
2316static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002317isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2318{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002319 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002320 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2321 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2322 struct iscsi_text_rsp *hdr =
2323 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2324 u32 txt_rsp_len;
2325 int rc;
2326
2327 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002328 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002329 if (rc < 0)
2330 return rc;
2331
2332 txt_rsp_len = rc;
2333 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2334
2335 if (txt_rsp_len) {
2336 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2337 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2338 void *txt_rsp_buf = cmd->buf_ptr;
2339
2340 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2341 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2342
2343 isert_cmd->pdu_buf_len = txt_rsp_len;
2344 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2345 tx_dsg->length = txt_rsp_len;
2346 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2347 isert_cmd->tx_desc.num_sge = 2;
2348 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002349 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002350
2351 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2352
2353 return isert_post_response(isert_conn, isert_cmd);
2354}
2355
2356static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2358 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2359 u32 data_left, u32 offset)
2360{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002361 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002362 struct scatterlist *sg_start, *tmp_sg;
2363 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2364 u32 sg_off, page_off;
2365 int i = 0, sg_nents;
2366
2367 sg_off = offset / PAGE_SIZE;
2368 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2369 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2370 page_off = offset % PAGE_SIZE;
2371
2372 send_wr->sg_list = ib_sge;
2373 send_wr->num_sge = sg_nents;
2374 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2375 /*
2376 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2377 */
2378 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2379 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2380 (unsigned long long)tmp_sg->dma_address,
2381 tmp_sg->length, page_off);
2382
2383 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2384 ib_sge->length = min_t(u32, data_left,
2385 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2386 ib_sge->lkey = isert_conn->conn_mr->lkey;
2387
Vu Pham90ecc6e2013-08-28 23:23:33 +03002388 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2389 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002390 page_off = 0;
2391 data_left -= ib_sge->length;
2392 ib_sge++;
2393 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2394 }
2395
2396 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2397 send_wr->sg_list, send_wr->num_sge);
2398
2399 return sg_nents;
2400}
2401
2402static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002403isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2404 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002405{
2406 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002407 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002408 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002409 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002410 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002412 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2413 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002414
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002415 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002417 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2418 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2419 se_cmd->t_data_nents, se_cmd->data_length,
2420 offset, wr->iser_ib_op, &wr->data);
2421 if (ret)
2422 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002423
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002424 data_left = data->len;
2425 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002427 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002428 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002429 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002431 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002432 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002433 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002434
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002435 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002436 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2437 GFP_KERNEL);
2438 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002439 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002440 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002441 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002442 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002443
2444 wr->isert_cmd = isert_cmd;
2445 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002446
2447 for (i = 0; i < wr->send_wr_num; i++) {
2448 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2449 data_len = min(data_left, rdma_write_max);
2450
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002451 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002452 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2453 send_wr->opcode = IB_WR_RDMA_WRITE;
2454 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2455 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2456 if (i + 1 == wr->send_wr_num)
2457 send_wr->next = &isert_cmd->tx_desc.send_wr;
2458 else
2459 send_wr->next = &wr->send_wr[i + 1];
2460 } else {
2461 send_wr->opcode = IB_WR_RDMA_READ;
2462 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2463 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2464 if (i + 1 == wr->send_wr_num)
2465 send_wr->send_flags = IB_SEND_SIGNALED;
2466 else
2467 send_wr->next = &wr->send_wr[i + 1];
2468 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002469
2470 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2471 send_wr, data_len, offset);
2472 ib_sge += ib_sge_cnt;
2473
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002474 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002475 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002476 data_left -= data_len;
2477 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002478
2479 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002480unmap_cmd:
2481 isert_unmap_data_buf(isert_conn, data);
2482
Vu Pham90ecc6e2013-08-28 23:23:33 +03002483 return ret;
2484}
2485
2486static int
Vu Pham59464ef2013-08-28 23:23:35 +03002487isert_map_fr_pagelist(struct ib_device *ib_dev,
2488 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2489{
2490 u64 start_addr, end_addr, page, chunk_start = 0;
2491 struct scatterlist *tmp_sg;
2492 int i = 0, new_chunk, last_ent, n_pages;
2493
2494 n_pages = 0;
2495 new_chunk = 1;
2496 last_ent = sg_nents - 1;
2497 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2498 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2499 if (new_chunk)
2500 chunk_start = start_addr;
2501 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2502
2503 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2504 i, (unsigned long long)tmp_sg->dma_address,
2505 tmp_sg->length);
2506
2507 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2508 new_chunk = 0;
2509 continue;
2510 }
2511 new_chunk = 1;
2512
2513 page = chunk_start & PAGE_MASK;
2514 do {
2515 fr_pl[n_pages++] = page;
2516 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2517 n_pages - 1, page);
2518 page += PAGE_SIZE;
2519 } while (page < end_addr);
2520 }
2521
2522 return n_pages;
2523}
2524
2525static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002526isert_fast_reg_mr(struct isert_conn *isert_conn,
2527 struct fast_reg_descriptor *fr_desc,
2528 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002529 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002530 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002531{
Vu Pham59464ef2013-08-28 23:23:35 +03002532 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002533 struct ib_mr *mr;
2534 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002535 struct ib_send_wr fr_wr, inv_wr;
2536 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002537 int ret, pagelist_len;
2538 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002539 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002540
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002541 if (mem->dma_nents == 1) {
2542 sge->lkey = isert_conn->conn_mr->lkey;
2543 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2544 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002545 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2546 __func__, __LINE__, sge->addr, sge->length,
2547 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002548 return 0;
2549 }
2550
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002551 if (ind == ISERT_DATA_KEY_VALID) {
2552 /* Registering data buffer */
2553 mr = fr_desc->data_mr;
2554 frpl = fr_desc->data_frpl;
2555 } else {
2556 /* Registering protection buffer */
2557 mr = fr_desc->pi_ctx->prot_mr;
2558 frpl = fr_desc->pi_ctx->prot_frpl;
2559 }
2560
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002561 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002562
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002563 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002564 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002565
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002566 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002567 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002568
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002569 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002570 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002571 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002572 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002573 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002574 wr = &inv_wr;
2575 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002576 key = (u8)(mr->rkey & 0x000000FF);
2577 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002578 }
2579
2580 /* Prepare FASTREG WR */
2581 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002582 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002583 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002584 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2585 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002586 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2587 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002588 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002589 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002590 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2591
2592 if (!wr)
2593 wr = &fr_wr;
2594 else
2595 wr->next = &fr_wr;
2596
2597 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2598 if (ret) {
2599 pr_err("fast registration failed, ret:%d\n", ret);
2600 return ret;
2601 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002602 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002603
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002604 sge->lkey = mr->lkey;
2605 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002606 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002607
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002608 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2609 __func__, __LINE__, sge->addr, sge->length,
2610 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002611
2612 return ret;
2613}
2614
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002615static inline void
2616isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
2617 struct ib_sig_domain *domain)
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002618{
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002619 domain->sig_type = IB_SIG_TYPE_T10_DIF;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002620 domain->sig.dif.bg_type = IB_T10DIF_CRC;
2621 domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
2622 domain->sig.dif.ref_tag = se_cmd->reftag_seed;
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002623 /*
2624 * At the moment we hard code those, but if in the future
2625 * the target core would like to use it, we will take it
2626 * from se_cmd.
2627 */
2628 domain->sig.dif.apptag_check_mask = 0xffff;
2629 domain->sig.dif.app_escape = true;
2630 domain->sig.dif.ref_escape = true;
2631 if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
2632 se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
2633 domain->sig.dif.ref_remap = true;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002634};
2635
2636static int
2637isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2638{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002639 switch (se_cmd->prot_op) {
2640 case TARGET_PROT_DIN_INSERT:
2641 case TARGET_PROT_DOUT_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002642 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002643 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002644 break;
2645 case TARGET_PROT_DOUT_INSERT:
2646 case TARGET_PROT_DIN_STRIP:
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03002647 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002648 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002649 break;
2650 case TARGET_PROT_DIN_PASS:
2651 case TARGET_PROT_DOUT_PASS:
Sagi Grimberg3d73cf12014-08-13 19:54:34 +03002652 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
2653 isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002654 break;
2655 default:
2656 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2657 return -EINVAL;
2658 }
2659
2660 return 0;
2661}
2662
2663static inline u8
2664isert_set_prot_checks(u8 prot_checks)
2665{
2666 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2667 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2668 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2669}
2670
2671static int
2672isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2673 struct fast_reg_descriptor *fr_desc,
2674 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2675 struct ib_sge *sig_sge)
2676{
2677 struct ib_send_wr sig_wr, inv_wr;
2678 struct ib_send_wr *bad_wr, *wr = NULL;
2679 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2680 struct ib_sig_attrs sig_attrs;
2681 int ret;
2682 u32 key;
2683
2684 memset(&sig_attrs, 0, sizeof(sig_attrs));
2685 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2686 if (ret)
2687 goto err;
2688
2689 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2690
2691 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2692 memset(&inv_wr, 0, sizeof(inv_wr));
2693 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002694 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002695 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2696 wr = &inv_wr;
2697 /* Bump the key */
2698 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2699 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2700 }
2701
2702 memset(&sig_wr, 0, sizeof(sig_wr));
2703 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002704 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002705 sig_wr.sg_list = data_sge;
2706 sig_wr.num_sge = 1;
2707 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2708 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2709 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2710 if (se_cmd->t_prot_sg)
2711 sig_wr.wr.sig_handover.prot = prot_sge;
2712
2713 if (!wr)
2714 wr = &sig_wr;
2715 else
2716 wr->next = &sig_wr;
2717
2718 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2719 if (ret) {
2720 pr_err("fast registration failed, ret:%d\n", ret);
2721 goto err;
2722 }
2723 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2724
2725 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2726 sig_sge->addr = 0;
2727 sig_sge->length = se_cmd->data_length;
2728 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2729 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2730 /*
2731 * We have protection guards on the wire
2732 * so we need to set a larget transfer
2733 */
2734 sig_sge->length += se_cmd->prot_length;
2735
2736 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2737 sig_sge->addr, sig_sge->length,
2738 sig_sge->lkey);
2739err:
2740 return ret;
2741}
2742
Vu Pham59464ef2013-08-28 23:23:35 +03002743static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002744isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2745 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002746{
2747 struct se_cmd *se_cmd = &cmd->se_cmd;
2748 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002749 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002750 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002751 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002752 struct fast_reg_descriptor *fr_desc = NULL;
2753 u32 offset;
2754 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002755 unsigned long flags;
2756
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002757 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2758
2759 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2760 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2761 se_cmd->t_data_nents, se_cmd->data_length,
2762 offset, wr->iser_ib_op, &wr->data);
2763 if (ret)
2764 return ret;
2765
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002766 if (wr->data.dma_nents != 1 ||
2767 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002768 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2769 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2770 struct fast_reg_descriptor, list);
2771 list_del(&fr_desc->list);
2772 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2773 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002774 }
2775
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002776 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2777 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002778 if (ret)
2779 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002780
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002781 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2782 struct ib_sge prot_sge, sig_sge;
2783
2784 if (se_cmd->t_prot_sg) {
2785 ret = isert_map_data_buf(isert_conn, isert_cmd,
2786 se_cmd->t_prot_sg,
2787 se_cmd->t_prot_nents,
2788 se_cmd->prot_length,
2789 0, wr->iser_ib_op, &wr->prot);
2790 if (ret)
2791 goto unmap_cmd;
2792
2793 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2794 ISERT_PROT_KEY_VALID, &prot_sge);
2795 if (ret)
2796 goto unmap_prot_cmd;
2797 }
2798
2799 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2800 &data_sge, &prot_sge, &sig_sge);
2801 if (ret)
2802 goto unmap_prot_cmd;
2803
2804 fr_desc->ind |= ISERT_PROTECTED;
2805 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2806 } else
2807 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2808
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002809 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002810 wr->send_wr_num = 1;
2811 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2812 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002813 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002814
2815 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002816 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002817 send_wr->num_sge = 1;
2818 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2819 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2820 send_wr->opcode = IB_WR_RDMA_WRITE;
2821 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2822 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002823 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2824 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002825 } else {
2826 send_wr->opcode = IB_WR_RDMA_READ;
2827 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2828 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2829 send_wr->send_flags = IB_SEND_SIGNALED;
2830 }
2831
Vu Pham59464ef2013-08-28 23:23:35 +03002832 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002833unmap_prot_cmd:
2834 if (se_cmd->t_prot_sg)
2835 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002836unmap_cmd:
2837 if (fr_desc) {
2838 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2839 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2840 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2841 }
2842 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002843
Vu Pham59464ef2013-08-28 23:23:35 +03002844 return ret;
2845}
2846
2847static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002848isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2849{
2850 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002851 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002852 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2853 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002854 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002855 struct ib_send_wr *wr_failed;
2856 int rc;
2857
2858 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2859 isert_cmd, se_cmd->data_length);
2860 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002861 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002862 if (rc) {
2863 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2864 return rc;
2865 }
2866
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002867 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2868 /*
2869 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2870 */
2871 isert_create_send_desc(isert_conn, isert_cmd,
2872 &isert_cmd->tx_desc);
2873 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2874 &isert_cmd->tx_desc.iscsi_header);
2875 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2876 isert_init_send_wr(isert_conn, isert_cmd,
Nicholas Bellinger0d0f6602014-10-05 02:13:03 -07002877 &isert_cmd->tx_desc.send_wr, false);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002878 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002879 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002880 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002881
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002882 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002883
2884 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2885 if (rc) {
2886 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002887 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002888 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002889
2890 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2891 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2892 "READ\n", isert_cmd);
2893 else
2894 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2895 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896
Vu Pham90ecc6e2013-08-28 23:23:33 +03002897 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002898}
2899
2900static int
2901isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2902{
2903 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002904 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002905 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2906 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002907 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002908 struct ib_send_wr *wr_failed;
2909 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002910
Vu Pham90ecc6e2013-08-28 23:23:33 +03002911 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2912 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002914 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002915 if (rc) {
2916 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2917 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918 }
2919
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002920 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002921
2922 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2923 if (rc) {
2924 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002925 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002927 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2928 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002929
Vu Pham90ecc6e2013-08-28 23:23:33 +03002930 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002931}
2932
2933static int
2934isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2935{
2936 int ret;
2937
2938 switch (state) {
2939 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2940 ret = isert_put_nopin(cmd, conn, false);
2941 break;
2942 default:
2943 pr_err("Unknown immediate state: 0x%02x\n", state);
2944 ret = -EINVAL;
2945 break;
2946 }
2947
2948 return ret;
2949}
2950
2951static int
2952isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2953{
2954 int ret;
2955
2956 switch (state) {
2957 case ISTATE_SEND_LOGOUTRSP:
2958 ret = isert_put_logout_rsp(cmd, conn);
2959 if (!ret) {
2960 pr_debug("Returning iSER Logout -EAGAIN\n");
2961 ret = -EAGAIN;
2962 }
2963 break;
2964 case ISTATE_SEND_NOPIN:
2965 ret = isert_put_nopin(cmd, conn, true);
2966 break;
2967 case ISTATE_SEND_TASKMGTRSP:
2968 ret = isert_put_tm_rsp(cmd, conn);
2969 break;
2970 case ISTATE_SEND_REJECT:
2971 ret = isert_put_reject(cmd, conn);
2972 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002973 case ISTATE_SEND_TEXTRSP:
2974 ret = isert_put_text_rsp(cmd, conn);
2975 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002976 case ISTATE_SEND_STATUS:
2977 /*
2978 * Special case for sending non GOOD SCSI status from TX thread
2979 * context during pre se_cmd excecution failure.
2980 */
2981 ret = isert_put_response(conn, cmd);
2982 break;
2983 default:
2984 pr_err("Unknown response state: 0x%02x\n", state);
2985 ret = -EINVAL;
2986 break;
2987 }
2988
2989 return ret;
2990}
2991
2992static int
2993isert_setup_np(struct iscsi_np *np,
2994 struct __kernel_sockaddr_storage *ksockaddr)
2995{
2996 struct isert_np *isert_np;
2997 struct rdma_cm_id *isert_lid;
2998 struct sockaddr *sa;
2999 int ret;
3000
3001 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3002 if (!isert_np) {
3003 pr_err("Unable to allocate struct isert_np\n");
3004 return -ENOMEM;
3005 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003006 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003007 mutex_init(&isert_np->np_accept_mutex);
3008 INIT_LIST_HEAD(&isert_np->np_accept_list);
3009 init_completion(&isert_np->np_login_comp);
3010
3011 sa = (struct sockaddr *)ksockaddr;
3012 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3013 /*
3014 * Setup the np->np_sockaddr from the passed sockaddr setup
3015 * in iscsi_target_configfs.c code..
3016 */
3017 memcpy(&np->np_sockaddr, ksockaddr,
3018 sizeof(struct __kernel_sockaddr_storage));
3019
3020 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3021 IB_QPT_RC);
3022 if (IS_ERR(isert_lid)) {
3023 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3024 PTR_ERR(isert_lid));
3025 ret = PTR_ERR(isert_lid);
3026 goto out;
3027 }
3028
3029 ret = rdma_bind_addr(isert_lid, sa);
3030 if (ret) {
3031 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3032 goto out_lid;
3033 }
3034
3035 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3036 if (ret) {
3037 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3038 goto out_lid;
3039 }
3040
3041 isert_np->np_cm_id = isert_lid;
3042 np->np_context = isert_np;
3043 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3044
3045 return 0;
3046
3047out_lid:
3048 rdma_destroy_id(isert_lid);
3049out:
3050 kfree(isert_np);
3051 return ret;
3052}
3053
3054static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003055isert_rdma_accept(struct isert_conn *isert_conn)
3056{
3057 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3058 struct rdma_conn_param cp;
3059 int ret;
3060
3061 memset(&cp, 0, sizeof(struct rdma_conn_param));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003062 cp.initiator_depth = isert_conn->initiator_depth;
3063 cp.retry_count = 7;
3064 cp.rnr_retry_count = 7;
3065
3066 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3067
3068 ret = rdma_accept(cm_id, &cp);
3069 if (ret) {
3070 pr_err("rdma_accept() failed with: %d\n", ret);
3071 return ret;
3072 }
3073
3074 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3075
3076 return 0;
3077}
3078
3079static int
3080isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3081{
3082 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3083 int ret;
3084
3085 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003086 /*
3087 * For login requests after the first PDU, isert_rx_login_req() will
3088 * kick schedule_delayed_work(&conn->login_work) as the packet is
3089 * received, which turns this callback from iscsi_target_do_login_rx()
3090 * into a NOP.
3091 */
3092 if (!login->first_request)
3093 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003094
3095 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3096 if (ret)
3097 return ret;
3098
3099 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3100 return 0;
3101}
3102
3103static void
3104isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3105 struct isert_conn *isert_conn)
3106{
3107 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3108 struct rdma_route *cm_route = &cm_id->route;
3109 struct sockaddr_in *sock_in;
3110 struct sockaddr_in6 *sock_in6;
3111
3112 conn->login_family = np->np_sockaddr.ss_family;
3113
3114 if (np->np_sockaddr.ss_family == AF_INET6) {
3115 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3116 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3117 &sock_in6->sin6_addr.in6_u);
3118 conn->login_port = ntohs(sock_in6->sin6_port);
3119
3120 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3121 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3122 &sock_in6->sin6_addr.in6_u);
3123 conn->local_port = ntohs(sock_in6->sin6_port);
3124 } else {
3125 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3126 sprintf(conn->login_ip, "%pI4",
3127 &sock_in->sin_addr.s_addr);
3128 conn->login_port = ntohs(sock_in->sin_port);
3129
3130 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3131 sprintf(conn->local_ip, "%pI4",
3132 &sock_in->sin_addr.s_addr);
3133 conn->local_port = ntohs(sock_in->sin_port);
3134 }
3135}
3136
3137static int
3138isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3139{
3140 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3141 struct isert_conn *isert_conn;
3142 int max_accept = 0, ret;
3143
3144accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003145 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003146 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003147 return -ENODEV;
3148
3149 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003150 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003151 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003152 pr_debug("np_thread_state %d for isert_accept_np\n",
3153 np->np_thread_state);
3154 /**
3155 * No point in stalling here when np_thread
3156 * is in state RESET/SHUTDOWN/EXIT - bail
3157 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003158 return -ENODEV;
3159 }
3160 spin_unlock_bh(&np->np_thread_lock);
3161
3162 mutex_lock(&isert_np->np_accept_mutex);
3163 if (list_empty(&isert_np->np_accept_list)) {
3164 mutex_unlock(&isert_np->np_accept_mutex);
3165 max_accept++;
3166 goto accept_wait;
3167 }
3168 isert_conn = list_first_entry(&isert_np->np_accept_list,
3169 struct isert_conn, conn_accept_node);
3170 list_del_init(&isert_conn->conn_accept_node);
3171 mutex_unlock(&isert_np->np_accept_mutex);
3172
3173 conn->context = isert_conn;
3174 isert_conn->conn = conn;
3175 max_accept = 0;
3176
3177 ret = isert_rdma_post_recvl(isert_conn);
3178 if (ret)
3179 return ret;
3180
3181 ret = isert_rdma_accept(isert_conn);
3182 if (ret)
3183 return ret;
3184
3185 isert_set_conn_info(np, conn, isert_conn);
3186
3187 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3188 return 0;
3189}
3190
3191static void
3192isert_free_np(struct iscsi_np *np)
3193{
3194 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3195
3196 rdma_destroy_id(isert_np->np_cm_id);
3197
3198 np->np_context = NULL;
3199 kfree(isert_np);
3200}
3201
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003202static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003203{
3204 struct isert_conn *isert_conn = conn->context;
3205
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003206 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003207
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003208 mutex_lock(&isert_conn->conn_mutex);
Sagi Grimberg38a83162014-07-02 16:19:26 +03003209 if (isert_conn->conn_cm_id && !isert_conn->disconnect) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003210 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003211 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003212 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003213 /*
3214 * Only wait for conn_wait_comp_err if the isert_conn made it
3215 * into full feature phase..
3216 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003217 if (isert_conn->state == ISER_CONN_INIT) {
3218 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003219 return;
3220 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003221 if (isert_conn->state == ISER_CONN_UP)
3222 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003223 mutex_unlock(&isert_conn->conn_mutex);
3224
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003225 wait_for_completion(&isert_conn->conn_wait_comp_err);
3226
3227 wait_for_completion(&isert_conn->conn_wait);
Sagi Grimberg0fc4ea72014-07-02 16:19:25 +03003228 isert_put_conn(isert_conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003229}
3230
3231static void isert_free_conn(struct iscsi_conn *conn)
3232{
3233 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003234
3235 isert_put_conn(isert_conn);
3236}
3237
3238static struct iscsit_transport iser_target_transport = {
3239 .name = "IB/iSER",
3240 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003241 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003242 .owner = THIS_MODULE,
3243 .iscsit_setup_np = isert_setup_np,
3244 .iscsit_accept_np = isert_accept_np,
3245 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003246 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003247 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003248 .iscsit_get_login_rx = isert_get_login_rx,
3249 .iscsit_put_login_tx = isert_put_login_tx,
3250 .iscsit_immediate_queue = isert_immediate_queue,
3251 .iscsit_response_queue = isert_response_queue,
3252 .iscsit_get_dataout = isert_get_dataout,
3253 .iscsit_queue_data_in = isert_put_datain,
3254 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003255 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003256 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003257};
3258
3259static int __init isert_init(void)
3260{
3261 int ret;
3262
3263 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3264 if (!isert_rx_wq) {
3265 pr_err("Unable to allocate isert_rx_wq\n");
3266 return -ENOMEM;
3267 }
3268
3269 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3270 if (!isert_comp_wq) {
3271 pr_err("Unable to allocate isert_comp_wq\n");
3272 ret = -ENOMEM;
3273 goto destroy_rx_wq;
3274 }
3275
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003276 iscsit_register_transport(&iser_target_transport);
3277 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3278 return 0;
3279
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003280destroy_rx_wq:
3281 destroy_workqueue(isert_rx_wq);
3282 return ret;
3283}
3284
3285static void __exit isert_exit(void)
3286{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003287 flush_scheduled_work();
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003288 destroy_workqueue(isert_comp_wq);
3289 destroy_workqueue(isert_rx_wq);
3290 iscsit_unregister_transport(&iser_target_transport);
3291 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3292}
3293
3294MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3295MODULE_VERSION("0.1");
3296MODULE_AUTHOR("nab@Linux-iSCSI.org");
3297MODULE_LICENSE("GPL");
3298
3299module_init(isert_init);
3300module_exit(isert_exit);