blob: 40969b683fdd60637b98e711ff79c1f544b3dbf2 [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
118 * work-around for RDMA_READ..
119 */
Vu Pham59464ef2013-08-28 23:23:35 +0300120 attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800121 isert_conn->max_sge = attr.cap.max_send_sge;
122
123 attr.cap.max_recv_sge = 1;
124 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
125 attr.qp_type = IB_QPT_RC;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200126 if (protection)
127 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800128
129 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
130 cma_id->device);
131 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
132 isert_conn->conn_pd->device);
133
134 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
135 if (ret) {
136 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
137 return ret;
138 }
139 isert_conn->conn_qp = cma_id->qp;
140 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
141
142 return 0;
143}
144
145static void
146isert_cq_event_callback(struct ib_event *e, void *context)
147{
148 pr_debug("isert_cq_event_callback event: %d\n", e->event);
149}
150
151static int
152isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
153{
154 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
155 struct iser_rx_desc *rx_desc;
156 struct ib_sge *rx_sg;
157 u64 dma_addr;
158 int i, j;
159
160 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
161 sizeof(struct iser_rx_desc), GFP_KERNEL);
162 if (!isert_conn->conn_rx_descs)
163 goto fail;
164
165 rx_desc = isert_conn->conn_rx_descs;
166
167 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
168 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
169 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
170 if (ib_dma_mapping_error(ib_dev, dma_addr))
171 goto dma_map_fail;
172
173 rx_desc->dma_addr = dma_addr;
174
175 rx_sg = &rx_desc->rx_sg;
176 rx_sg->addr = rx_desc->dma_addr;
177 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
178 rx_sg->lkey = isert_conn->conn_mr->lkey;
179 }
180
181 isert_conn->conn_rx_desc_head = 0;
182 return 0;
183
184dma_map_fail:
185 rx_desc = isert_conn->conn_rx_descs;
186 for (j = 0; j < i; j++, rx_desc++) {
187 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
188 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
189 }
190 kfree(isert_conn->conn_rx_descs);
191 isert_conn->conn_rx_descs = NULL;
192fail:
193 return -ENOMEM;
194}
195
196static void
197isert_free_rx_descriptors(struct isert_conn *isert_conn)
198{
199 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
200 struct iser_rx_desc *rx_desc;
201 int i;
202
203 if (!isert_conn->conn_rx_descs)
204 return;
205
206 rx_desc = isert_conn->conn_rx_descs;
207 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
208 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
209 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
210 }
211
212 kfree(isert_conn->conn_rx_descs);
213 isert_conn->conn_rx_descs = NULL;
214}
215
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800216static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800217static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800218static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800219static void isert_cq_rx_callback(struct ib_cq *, void *);
220
221static int
222isert_create_device_ib_res(struct isert_device *device)
223{
224 struct ib_device *ib_dev = device->ib_device;
225 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300226 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800227 int ret = 0, i, j;
228
Vu Pham59464ef2013-08-28 23:23:35 +0300229 dev_attr = &device->dev_attr;
230 ret = isert_query_device(ib_dev, dev_attr);
231 if (ret)
232 return ret;
233
Vu Phamd40945d2013-08-28 23:23:34 +0300234 /* asign function handlers */
Sagi Grimbergf2252252014-03-27 19:22:25 +0200235 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
236 dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200237 device->use_fastreg = 1;
238 device->reg_rdma_mem = isert_reg_rdma;
239 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300240 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200241 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300242 device->reg_rdma_mem = isert_map_rdma;
243 device->unreg_rdma_mem = isert_unmap_cmd;
244 }
Vu Phamd40945d2013-08-28 23:23:34 +0300245
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200246 /* Check signature cap */
247 device->pi_capable = dev_attr->device_cap_flags &
248 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
249
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800250 device->cqs_used = min_t(int, num_online_cpus(),
251 device->ib_device->num_comp_vectors);
252 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200253 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200254 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800255 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200256 device->ib_device->num_comp_vectors, device->use_fastreg,
257 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800258 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
259 device->cqs_used, GFP_KERNEL);
260 if (!device->cq_desc) {
261 pr_err("Unable to allocate device->cq_desc\n");
262 return -ENOMEM;
263 }
264 cq_desc = device->cq_desc;
265
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800266 for (i = 0; i < device->cqs_used; i++) {
267 cq_desc[i].device = device;
268 cq_desc[i].cq_index = i;
269
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800270 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800271 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
272 isert_cq_rx_callback,
273 isert_cq_event_callback,
274 (void *)&cq_desc[i],
275 ISER_MAX_RX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800276 if (IS_ERR(device->dev_rx_cq[i])) {
277 ret = PTR_ERR(device->dev_rx_cq[i]);
278 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800279 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800280 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800281
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800282 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800283 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
284 isert_cq_tx_callback,
285 isert_cq_event_callback,
286 (void *)&cq_desc[i],
287 ISER_MAX_TX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800288 if (IS_ERR(device->dev_tx_cq[i])) {
289 ret = PTR_ERR(device->dev_tx_cq[i]);
290 device->dev_tx_cq[i] = NULL;
291 goto out_cq;
292 }
293
294 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
295 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800296 goto out_cq;
297
Wei Yongjun94a71112013-10-29 09:56:34 +0800298 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
299 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800300 goto out_cq;
301 }
302
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800303 return 0;
304
305out_cq:
306 for (j = 0; j < i; j++) {
307 cq_desc = &device->cq_desc[j];
308
309 if (device->dev_rx_cq[j]) {
310 cancel_work_sync(&cq_desc->cq_rx_work);
311 ib_destroy_cq(device->dev_rx_cq[j]);
312 }
313 if (device->dev_tx_cq[j]) {
314 cancel_work_sync(&cq_desc->cq_tx_work);
315 ib_destroy_cq(device->dev_tx_cq[j]);
316 }
317 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800318 kfree(device->cq_desc);
319
320 return ret;
321}
322
323static void
324isert_free_device_ib_res(struct isert_device *device)
325{
326 struct isert_cq_desc *cq_desc;
327 int i;
328
329 for (i = 0; i < device->cqs_used; i++) {
330 cq_desc = &device->cq_desc[i];
331
332 cancel_work_sync(&cq_desc->cq_rx_work);
333 cancel_work_sync(&cq_desc->cq_tx_work);
334 ib_destroy_cq(device->dev_rx_cq[i]);
335 ib_destroy_cq(device->dev_tx_cq[i]);
336 device->dev_rx_cq[i] = NULL;
337 device->dev_tx_cq[i] = NULL;
338 }
339
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800340 kfree(device->cq_desc);
341}
342
343static void
344isert_device_try_release(struct isert_device *device)
345{
346 mutex_lock(&device_list_mutex);
347 device->refcount--;
348 if (!device->refcount) {
349 isert_free_device_ib_res(device);
350 list_del(&device->dev_node);
351 kfree(device);
352 }
353 mutex_unlock(&device_list_mutex);
354}
355
356static struct isert_device *
357isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
358{
359 struct isert_device *device;
360 int ret;
361
362 mutex_lock(&device_list_mutex);
363 list_for_each_entry(device, &device_list, dev_node) {
364 if (device->ib_device->node_guid == cma_id->device->node_guid) {
365 device->refcount++;
366 mutex_unlock(&device_list_mutex);
367 return device;
368 }
369 }
370
371 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
372 if (!device) {
373 mutex_unlock(&device_list_mutex);
374 return ERR_PTR(-ENOMEM);
375 }
376
377 INIT_LIST_HEAD(&device->dev_node);
378
379 device->ib_device = cma_id->device;
380 ret = isert_create_device_ib_res(device);
381 if (ret) {
382 kfree(device);
383 mutex_unlock(&device_list_mutex);
384 return ERR_PTR(ret);
385 }
386
387 device->refcount++;
388 list_add_tail(&device->dev_node, &device_list);
389 mutex_unlock(&device_list_mutex);
390
391 return device;
392}
393
Vu Pham59464ef2013-08-28 23:23:35 +0300394static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200395isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300396{
397 struct fast_reg_descriptor *fr_desc, *tmp;
398 int i = 0;
399
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200400 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300401 return;
402
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200403 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300404
405 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200406 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300407 list_del(&fr_desc->list);
408 ib_free_fast_reg_page_list(fr_desc->data_frpl);
409 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200410 if (fr_desc->pi_ctx) {
411 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
412 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
413 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
414 kfree(fr_desc->pi_ctx);
415 }
Vu Pham59464ef2013-08-28 23:23:35 +0300416 kfree(fr_desc);
417 ++i;
418 }
419
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200420 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300421 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200422 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300423}
424
425static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200426isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200427 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200428{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200429 int ret;
430
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200431 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
432 ISCSI_ISER_SG_TABLESIZE);
433 if (IS_ERR(fr_desc->data_frpl)) {
434 pr_err("Failed to allocate data frpl err=%ld\n",
435 PTR_ERR(fr_desc->data_frpl));
436 return PTR_ERR(fr_desc->data_frpl);
437 }
438
439 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
440 if (IS_ERR(fr_desc->data_mr)) {
441 pr_err("Failed to allocate data frmr err=%ld\n",
442 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200443 ret = PTR_ERR(fr_desc->data_mr);
444 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200445 }
446 pr_debug("Create fr_desc %p page_list %p\n",
447 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200448 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200449
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200450 if (protection) {
451 struct ib_mr_init_attr mr_init_attr = {0};
452 struct pi_context *pi_ctx;
453
454 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
455 if (!fr_desc->pi_ctx) {
456 pr_err("Failed to allocate pi context\n");
457 ret = -ENOMEM;
458 goto err_data_mr;
459 }
460 pi_ctx = fr_desc->pi_ctx;
461
462 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
463 ISCSI_ISER_SG_TABLESIZE);
464 if (IS_ERR(pi_ctx->prot_frpl)) {
465 pr_err("Failed to allocate prot frpl err=%ld\n",
466 PTR_ERR(pi_ctx->prot_frpl));
467 ret = PTR_ERR(pi_ctx->prot_frpl);
468 goto err_pi_ctx;
469 }
470
471 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
472 if (IS_ERR(pi_ctx->prot_mr)) {
473 pr_err("Failed to allocate prot frmr err=%ld\n",
474 PTR_ERR(pi_ctx->prot_mr));
475 ret = PTR_ERR(pi_ctx->prot_mr);
476 goto err_prot_frpl;
477 }
478 fr_desc->ind |= ISERT_PROT_KEY_VALID;
479
480 mr_init_attr.max_reg_descriptors = 2;
481 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
482 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
483 if (IS_ERR(pi_ctx->sig_mr)) {
484 pr_err("Failed to allocate signature enabled mr err=%ld\n",
485 PTR_ERR(pi_ctx->sig_mr));
486 ret = PTR_ERR(pi_ctx->sig_mr);
487 goto err_prot_mr;
488 }
489 fr_desc->ind |= ISERT_SIG_KEY_VALID;
490 }
491 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200492
493 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200494err_prot_mr:
495 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
496err_prot_frpl:
497 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
498err_pi_ctx:
499 kfree(fr_desc->pi_ctx);
500err_data_mr:
501 ib_dereg_mr(fr_desc->data_mr);
502err_data_frpl:
503 ib_free_fast_reg_page_list(fr_desc->data_frpl);
504
505 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200506}
507
508static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200509isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
Vu Pham59464ef2013-08-28 23:23:35 +0300510{
511 struct fast_reg_descriptor *fr_desc;
512 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700513 struct se_session *se_sess = isert_conn->conn->sess->se_sess;
514 struct se_node_acl *se_nacl = se_sess->se_node_acl;
515 int i, ret, tag_num;
516 /*
517 * Setup the number of FRMRs based upon the number of tags
518 * available to session in iscsi_target_locate_portal().
519 */
520 tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
521 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
Vu Pham59464ef2013-08-28 23:23:35 +0300522
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200523 isert_conn->conn_fr_pool_size = 0;
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700524 for (i = 0; i < tag_num; i++) {
Vu Pham59464ef2013-08-28 23:23:35 +0300525 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
526 if (!fr_desc) {
527 pr_err("Failed to allocate fast_reg descriptor\n");
528 ret = -ENOMEM;
529 goto err;
530 }
531
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200532 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200533 isert_conn->conn_pd, fr_desc,
534 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200535 if (ret) {
536 pr_err("Failed to create fastreg descriptor err=%d\n",
537 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800538 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300539 goto err;
540 }
541
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200542 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
543 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300544 }
545
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200546 pr_debug("Creating conn %p fastreg pool size=%d",
547 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300548
549 return 0;
550
551err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200552 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300553 return ret;
554}
555
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800556static int
557isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
558{
559 struct iscsi_np *np = cma_id->context;
560 struct isert_np *isert_np = np->np_context;
561 struct isert_conn *isert_conn;
562 struct isert_device *device;
563 struct ib_device *ib_dev = cma_id->device;
564 int ret = 0;
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300565 u8 pi_support;
566
567 spin_lock_bh(&np->np_thread_lock);
568 if (!np->enabled) {
569 spin_unlock_bh(&np->np_thread_lock);
570 pr_debug("iscsi_np is not enabled, reject connect request\n");
571 return rdma_reject(cma_id, NULL, 0);
572 }
573 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800574
575 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
576 cma_id, cma_id->context);
577
578 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
579 if (!isert_conn) {
580 pr_err("Unable to allocate isert_conn\n");
581 return -ENOMEM;
582 }
583 isert_conn->state = ISER_CONN_INIT;
584 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
585 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800586 init_completion(&isert_conn->conn_wait);
587 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800588 kref_init(&isert_conn->conn_kref);
589 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700590 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300591 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700592 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800593
594 cma_id->context = isert_conn;
595 isert_conn->conn_cm_id = cma_id;
596 isert_conn->responder_resources = event->param.conn.responder_resources;
597 isert_conn->initiator_depth = event->param.conn.initiator_depth;
598 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
599 isert_conn->responder_resources, isert_conn->initiator_depth);
600
601 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
602 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
603 if (!isert_conn->login_buf) {
604 pr_err("Unable to allocate isert_conn->login_buf\n");
605 ret = -ENOMEM;
606 goto out;
607 }
608
609 isert_conn->login_req_buf = isert_conn->login_buf;
610 isert_conn->login_rsp_buf = isert_conn->login_buf +
611 ISCSI_DEF_MAX_RECV_SEG_LEN;
612 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
613 isert_conn->login_buf, isert_conn->login_req_buf,
614 isert_conn->login_rsp_buf);
615
616 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
617 (void *)isert_conn->login_req_buf,
618 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
619
620 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
621 if (ret) {
622 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
623 ret);
624 isert_conn->login_req_dma = 0;
625 goto out_login_buf;
626 }
627
628 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
629 (void *)isert_conn->login_rsp_buf,
630 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
631
632 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
633 if (ret) {
634 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
635 ret);
636 isert_conn->login_rsp_dma = 0;
637 goto out_req_dma_map;
638 }
639
640 device = isert_device_find_by_ib_dev(cma_id);
641 if (IS_ERR(device)) {
642 ret = PTR_ERR(device);
643 goto out_rsp_dma_map;
644 }
645
646 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200647 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
648 if (IS_ERR(isert_conn->conn_pd)) {
649 ret = PTR_ERR(isert_conn->conn_pd);
650 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
651 isert_conn, ret);
652 goto out_pd;
653 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800654
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200655 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
656 IB_ACCESS_LOCAL_WRITE);
657 if (IS_ERR(isert_conn->conn_mr)) {
658 ret = PTR_ERR(isert_conn->conn_mr);
659 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
660 isert_conn, ret);
661 goto out_mr;
662 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800663
Sagi Grimberg14f4b542014-04-29 13:13:47 +0300664 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200665 if (pi_support && !device->pi_capable) {
Sagi Grimberg5f80ff82014-05-20 13:28:11 +0300666 pr_err("Protection information requested but not supported, "
667 "rejecting connect request\n");
668 ret = rdma_reject(cma_id, NULL, 0);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200669 goto out_mr;
670 }
671
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200672 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800673 if (ret)
674 goto out_conn_dev;
675
676 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300677 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800678 mutex_unlock(&isert_np->np_accept_mutex);
679
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300680 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
681 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800682 return 0;
683
684out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200685 ib_dereg_mr(isert_conn->conn_mr);
686out_mr:
687 ib_dealloc_pd(isert_conn->conn_pd);
688out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800689 isert_device_try_release(device);
690out_rsp_dma_map:
691 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
692 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
693out_req_dma_map:
694 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
695 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
696out_login_buf:
697 kfree(isert_conn->login_buf);
698out:
699 kfree(isert_conn);
700 return ret;
701}
702
703static void
704isert_connect_release(struct isert_conn *isert_conn)
705{
706 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
707 struct isert_device *device = isert_conn->conn_device;
708 int cq_index;
709
710 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
711
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200712 if (device && device->use_fastreg)
713 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300714
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800715 if (isert_conn->conn_qp) {
716 cq_index = ((struct isert_cq_desc *)
717 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
718 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
719 isert_conn->conn_device->cq_active_qps[cq_index]--;
720
721 rdma_destroy_qp(isert_conn->conn_cm_id);
722 }
723
724 isert_free_rx_descriptors(isert_conn);
725 rdma_destroy_id(isert_conn->conn_cm_id);
726
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200727 ib_dereg_mr(isert_conn->conn_mr);
728 ib_dealloc_pd(isert_conn->conn_pd);
729
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800730 if (isert_conn->login_buf) {
731 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
732 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
733 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
734 ISCSI_DEF_MAX_RECV_SEG_LEN,
735 DMA_FROM_DEVICE);
736 kfree(isert_conn->login_buf);
737 }
738 kfree(isert_conn);
739
740 if (device)
741 isert_device_try_release(device);
742
743 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
744}
745
746static void
747isert_connected_handler(struct rdma_cm_id *cma_id)
748{
749 return;
750}
751
752static void
753isert_release_conn_kref(struct kref *kref)
754{
755 struct isert_conn *isert_conn = container_of(kref,
756 struct isert_conn, conn_kref);
757
758 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
759 current->comm, current->pid);
760
761 isert_connect_release(isert_conn);
762}
763
764static void
765isert_put_conn(struct isert_conn *isert_conn)
766{
767 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
768}
769
770static void
771isert_disconnect_work(struct work_struct *work)
772{
773 struct isert_conn *isert_conn = container_of(work,
774 struct isert_conn, conn_logout_work);
775
776 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700777 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800778 if (isert_conn->state == ISER_CONN_UP)
779 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800780
781 if (isert_conn->post_recv_buf_count == 0 &&
782 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700783 mutex_unlock(&isert_conn->conn_mutex);
784 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800785 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700786 if (!isert_conn->conn_cm_id) {
787 mutex_unlock(&isert_conn->conn_mutex);
788 isert_put_conn(isert_conn);
789 return;
790 }
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +0300791
Sagi Grimberg88c40152014-05-19 17:44:24 +0300792 if (isert_conn->disconnect) {
793 /* Send DREQ/DREP towards our initiator */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700794 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700795 }
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +0300796
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700797 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800798
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700799wake_up:
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800800 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800801 isert_put_conn(isert_conn);
802}
803
804static void
Sagi Grimberg88c40152014-05-19 17:44:24 +0300805isert_disconnected_handler(struct rdma_cm_id *cma_id, bool disconnect)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800806{
807 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
808
Sagi Grimberg88c40152014-05-19 17:44:24 +0300809 isert_conn->disconnect = disconnect;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800810 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
811 schedule_work(&isert_conn->conn_logout_work);
812}
813
814static int
815isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
816{
817 int ret = 0;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300818 bool disconnect = false;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800819
820 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
821 event->event, event->status, cma_id->context, cma_id);
822
823 switch (event->event) {
824 case RDMA_CM_EVENT_CONNECT_REQUEST:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800825 ret = isert_connect_request(cma_id, event);
826 break;
827 case RDMA_CM_EVENT_ESTABLISHED:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800828 isert_connected_handler(cma_id);
829 break;
Sagi Grimberg88c40152014-05-19 17:44:24 +0300830 case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
831 case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
832 case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
833 disconnect = true;
834 case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
835 isert_disconnected_handler(cma_id, disconnect);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800836 break;
837 case RDMA_CM_EVENT_CONNECT_ERROR:
838 default:
Sagi Grimberg88c40152014-05-19 17:44:24 +0300839 pr_err("Unhandled RDMA CMA event: %d\n", event->event);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800840 break;
841 }
842
843 if (ret != 0) {
844 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
845 event->event, ret);
846 dump_stack();
847 }
848
849 return ret;
850}
851
852static int
853isert_post_recv(struct isert_conn *isert_conn, u32 count)
854{
855 struct ib_recv_wr *rx_wr, *rx_wr_failed;
856 int i, ret;
857 unsigned int rx_head = isert_conn->conn_rx_desc_head;
858 struct iser_rx_desc *rx_desc;
859
860 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
861 rx_desc = &isert_conn->conn_rx_descs[rx_head];
862 rx_wr->wr_id = (unsigned long)rx_desc;
863 rx_wr->sg_list = &rx_desc->rx_sg;
864 rx_wr->num_sge = 1;
865 rx_wr->next = rx_wr + 1;
866 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
867 }
868
869 rx_wr--;
870 rx_wr->next = NULL; /* mark end of work requests list */
871
872 isert_conn->post_recv_buf_count += count;
873 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
874 &rx_wr_failed);
875 if (ret) {
876 pr_err("ib_post_recv() failed with ret: %d\n", ret);
877 isert_conn->post_recv_buf_count -= count;
878 } else {
879 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
880 isert_conn->conn_rx_desc_head = rx_head;
881 }
882 return ret;
883}
884
885static int
886isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
887{
888 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
889 struct ib_send_wr send_wr, *send_wr_failed;
890 int ret;
891
892 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
893 ISER_HEADERS_LEN, DMA_TO_DEVICE);
894
895 send_wr.next = NULL;
896 send_wr.wr_id = (unsigned long)tx_desc;
897 send_wr.sg_list = tx_desc->tx_sg;
898 send_wr.num_sge = tx_desc->num_sge;
899 send_wr.opcode = IB_WR_SEND;
900 send_wr.send_flags = IB_SEND_SIGNALED;
901
902 atomic_inc(&isert_conn->post_send_buf_count);
903
904 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
905 if (ret) {
906 pr_err("ib_post_send() failed, ret: %d\n", ret);
907 atomic_dec(&isert_conn->post_send_buf_count);
908 }
909
910 return ret;
911}
912
913static void
914isert_create_send_desc(struct isert_conn *isert_conn,
915 struct isert_cmd *isert_cmd,
916 struct iser_tx_desc *tx_desc)
917{
918 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
919
920 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
921 ISER_HEADERS_LEN, DMA_TO_DEVICE);
922
923 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
924 tx_desc->iser_header.flags = ISER_VER;
925
926 tx_desc->num_sge = 1;
927 tx_desc->isert_cmd = isert_cmd;
928
929 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
930 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
931 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
932 }
933}
934
935static int
936isert_init_tx_hdrs(struct isert_conn *isert_conn,
937 struct iser_tx_desc *tx_desc)
938{
939 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
940 u64 dma_addr;
941
942 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
943 ISER_HEADERS_LEN, DMA_TO_DEVICE);
944 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
945 pr_err("ib_dma_mapping_error() failed\n");
946 return -ENOMEM;
947 }
948
949 tx_desc->dma_addr = dma_addr;
950 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
951 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
952 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
953
954 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
955 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
956 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
957
958 return 0;
959}
960
961static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800962isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
963 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800964{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800965 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
966
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800967 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
968 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
969 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800970 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800971 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800972 /*
973 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
974 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
975 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800976 mutex_lock(&isert_conn->conn_mutex);
977 if (coalesce && isert_conn->state == ISER_CONN_UP &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800978 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800979 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800980 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800981 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800982 return;
983 }
984 isert_conn->conn_comp_batch = 0;
985 tx_desc->comp_llnode_batch = llist_del_all(&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
988 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800989}
990
991static int
992isert_rdma_post_recvl(struct isert_conn *isert_conn)
993{
994 struct ib_recv_wr rx_wr, *rx_wr_fail;
995 struct ib_sge sge;
996 int ret;
997
998 memset(&sge, 0, sizeof(struct ib_sge));
999 sge.addr = isert_conn->login_req_dma;
1000 sge.length = ISER_RX_LOGIN_SIZE;
1001 sge.lkey = isert_conn->conn_mr->lkey;
1002
1003 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
1004 sge.addr, sge.length, sge.lkey);
1005
1006 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
1007 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1008 rx_wr.sg_list = &sge;
1009 rx_wr.num_sge = 1;
1010
1011 isert_conn->post_recv_buf_count++;
1012 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1013 if (ret) {
1014 pr_err("ib_post_recv() failed: %d\n", ret);
1015 isert_conn->post_recv_buf_count--;
1016 }
1017
1018 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1019 return ret;
1020}
1021
1022static int
1023isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1024 u32 length)
1025{
1026 struct isert_conn *isert_conn = conn->context;
1027 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1028 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1029 int ret;
1030
1031 isert_create_send_desc(isert_conn, NULL, tx_desc);
1032
1033 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1034 sizeof(struct iscsi_hdr));
1035
1036 isert_init_tx_hdrs(isert_conn, tx_desc);
1037
1038 if (length > 0) {
1039 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1040
1041 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1042 length, DMA_TO_DEVICE);
1043
1044 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1045
1046 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1047 length, DMA_TO_DEVICE);
1048
1049 tx_dsg->addr = isert_conn->login_rsp_dma;
1050 tx_dsg->length = length;
1051 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1052 tx_desc->num_sge = 2;
1053 }
1054 if (!login->login_failed) {
1055 if (login->login_complete) {
Sagi Grimberge0546fc2014-06-10 13:41:41 +03001056 if (!conn->sess->sess_ops->SessionType &&
1057 isert_conn->conn_device->use_fastreg) {
1058 /* Normal Session and fastreg is used */
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001059 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1060
1061 ret = isert_conn_create_fastreg_pool(isert_conn,
1062 pi_support);
1063 if (ret) {
1064 pr_err("Conn: %p failed to create"
1065 " fastreg pool\n", isert_conn);
1066 return ret;
1067 }
1068 }
1069
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001070 ret = isert_alloc_rx_descriptors(isert_conn);
1071 if (ret)
1072 return ret;
1073
1074 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1075 if (ret)
1076 return ret;
1077
1078 isert_conn->state = ISER_CONN_UP;
1079 goto post_send;
1080 }
1081
1082 ret = isert_rdma_post_recvl(isert_conn);
1083 if (ret)
1084 return ret;
1085 }
1086post_send:
1087 ret = isert_post_send(isert_conn, tx_desc);
1088 if (ret)
1089 return ret;
1090
1091 return 0;
1092}
1093
1094static void
1095isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1096 struct isert_conn *isert_conn)
1097{
1098 struct iscsi_conn *conn = isert_conn->conn;
1099 struct iscsi_login *login = conn->conn_login;
1100 int size;
1101
1102 if (!login) {
1103 pr_err("conn->conn_login is NULL\n");
1104 dump_stack();
1105 return;
1106 }
1107
1108 if (login->first_request) {
1109 struct iscsi_login_req *login_req =
1110 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1111 /*
1112 * Setup the initial iscsi_login values from the leading
1113 * login request PDU.
1114 */
1115 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1116 login->current_stage =
1117 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1118 >> 2;
1119 login->version_min = login_req->min_version;
1120 login->version_max = login_req->max_version;
1121 memcpy(login->isid, login_req->isid, 6);
1122 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1123 login->init_task_tag = login_req->itt;
1124 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1125 login->cid = be16_to_cpu(login_req->cid);
1126 login->tsih = be16_to_cpu(login_req->tsih);
1127 }
1128
1129 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1130
1131 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1132 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1133 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1134 memcpy(login->req_buf, &rx_desc->data[0], size);
1135
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001136 if (login->first_request) {
1137 complete(&isert_conn->conn_login_comp);
1138 return;
1139 }
1140 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001141}
1142
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001143static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001144*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001145{
1146 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1147 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001148 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001149
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001150 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001151 if (!cmd) {
1152 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001153 return NULL;
1154 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001155 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001156 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001157 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001158
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001159 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001160}
1161
1162static int
1163isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001164 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1165 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001166{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001167 struct iscsi_conn *conn = isert_conn->conn;
1168 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1169 struct scatterlist *sg;
1170 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1171 bool dump_payload = false;
1172
1173 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1174 if (rc < 0)
1175 return rc;
1176
1177 imm_data = cmd->immediate_data;
1178 imm_data_len = cmd->first_burst_len;
1179 unsol_data = cmd->unsolicited_data;
1180
1181 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1182 if (rc < 0) {
1183 return 0;
1184 } else if (rc > 0) {
1185 dump_payload = true;
1186 goto sequence_cmd;
1187 }
1188
1189 if (!imm_data)
1190 return 0;
1191
1192 sg = &cmd->se_cmd.t_data_sg[0];
1193 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1194
1195 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1196 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1197
1198 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1199
1200 cmd->write_data_done += imm_data_len;
1201
1202 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1203 spin_lock_bh(&cmd->istate_lock);
1204 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1205 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1206 spin_unlock_bh(&cmd->istate_lock);
1207 }
1208
1209sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001210 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001211
1212 if (!rc && dump_payload == false && unsol_data)
1213 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger6cc44a62014-05-23 00:48:35 -07001214 else if (dump_payload && imm_data)
1215 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001216
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001217 return 0;
1218}
1219
1220static int
1221isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1222 struct iser_rx_desc *rx_desc, unsigned char *buf)
1223{
1224 struct scatterlist *sg_start;
1225 struct iscsi_conn *conn = isert_conn->conn;
1226 struct iscsi_cmd *cmd = NULL;
1227 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1228 u32 unsol_data_len = ntoh24(hdr->dlength);
1229 int rc, sg_nents, sg_off, page_off;
1230
1231 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1232 if (rc < 0)
1233 return rc;
1234 else if (!cmd)
1235 return 0;
1236 /*
1237 * FIXME: Unexpected unsolicited_data out
1238 */
1239 if (!cmd->unsolicited_data) {
1240 pr_err("Received unexpected solicited data payload\n");
1241 dump_stack();
1242 return -1;
1243 }
1244
1245 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1246 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1247
1248 sg_off = cmd->write_data_done / PAGE_SIZE;
1249 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1250 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1251 page_off = cmd->write_data_done % PAGE_SIZE;
1252 /*
1253 * FIXME: Non page-aligned unsolicited_data out
1254 */
1255 if (page_off) {
1256 pr_err("Received unexpected non-page aligned data payload\n");
1257 dump_stack();
1258 return -1;
1259 }
1260 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1261 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1262
1263 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1264 unsol_data_len);
1265
1266 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1267 if (rc < 0)
1268 return rc;
1269
1270 return 0;
1271}
1272
1273static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001274isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001275 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1276 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001277{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001278 struct iscsi_conn *conn = isert_conn->conn;
1279 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1280 int rc;
1281
1282 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1283 if (rc < 0)
1284 return rc;
1285 /*
1286 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1287 */
1288
1289 return iscsit_process_nop_out(conn, cmd, hdr);
1290}
1291
1292static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001293isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001294 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1295 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001296{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001297 struct iscsi_conn *conn = isert_conn->conn;
1298 u32 payload_length = ntoh24(hdr->dlength);
1299 int rc;
1300 unsigned char *text_in;
1301
1302 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1303 if (rc < 0)
1304 return rc;
1305
1306 text_in = kzalloc(payload_length, GFP_KERNEL);
1307 if (!text_in) {
1308 pr_err("Unable to allocate text_in of payload_length: %u\n",
1309 payload_length);
1310 return -ENOMEM;
1311 }
1312 cmd->text_in_ptr = text_in;
1313
1314 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1315
1316 return iscsit_process_text_cmd(conn, cmd, hdr);
1317}
1318
1319static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001320isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1321 uint32_t read_stag, uint64_t read_va,
1322 uint32_t write_stag, uint64_t write_va)
1323{
1324 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1325 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001326 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001327 struct iscsi_cmd *cmd;
1328 struct isert_cmd *isert_cmd;
1329 int ret = -EINVAL;
1330 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1331
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001332 if (sess->sess_ops->SessionType &&
1333 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1334 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1335 " ignoring\n", opcode);
1336 return 0;
1337 }
1338
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001339 switch (opcode) {
1340 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001341 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001342 if (!cmd)
1343 break;
1344
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001345 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001346 isert_cmd->read_stag = read_stag;
1347 isert_cmd->read_va = read_va;
1348 isert_cmd->write_stag = write_stag;
1349 isert_cmd->write_va = write_va;
1350
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001351 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001352 rx_desc, (unsigned char *)hdr);
1353 break;
1354 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001355 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001356 if (!cmd)
1357 break;
1358
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001359 isert_cmd = iscsit_priv_cmd(cmd);
1360 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001361 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001362 break;
1363 case ISCSI_OP_SCSI_DATA_OUT:
1364 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1365 (unsigned char *)hdr);
1366 break;
1367 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001368 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001369 if (!cmd)
1370 break;
1371
1372 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1373 (unsigned char *)hdr);
1374 break;
1375 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001376 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001377 if (!cmd)
1378 break;
1379
1380 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1381 if (ret > 0)
1382 wait_for_completion_timeout(&conn->conn_logout_comp,
1383 SECONDS_FOR_LOGOUT_COMP *
1384 HZ);
1385 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001386 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001387 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001388 if (!cmd)
1389 break;
1390
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001391 isert_cmd = iscsit_priv_cmd(cmd);
1392 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001393 rx_desc, (struct iscsi_text *)hdr);
1394 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001395 default:
1396 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1397 dump_stack();
1398 break;
1399 }
1400
1401 return ret;
1402}
1403
1404static void
1405isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1406{
1407 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1408 uint64_t read_va = 0, write_va = 0;
1409 uint32_t read_stag = 0, write_stag = 0;
1410 int rc;
1411
1412 switch (iser_hdr->flags & 0xF0) {
1413 case ISCSI_CTRL:
1414 if (iser_hdr->flags & ISER_RSV) {
1415 read_stag = be32_to_cpu(iser_hdr->read_stag);
1416 read_va = be64_to_cpu(iser_hdr->read_va);
1417 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1418 read_stag, (unsigned long long)read_va);
1419 }
1420 if (iser_hdr->flags & ISER_WSV) {
1421 write_stag = be32_to_cpu(iser_hdr->write_stag);
1422 write_va = be64_to_cpu(iser_hdr->write_va);
1423 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1424 write_stag, (unsigned long long)write_va);
1425 }
1426
1427 pr_debug("ISER ISCSI_CTRL PDU\n");
1428 break;
1429 case ISER_HELLO:
1430 pr_err("iSER Hello message\n");
1431 break;
1432 default:
1433 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1434 break;
1435 }
1436
1437 rc = isert_rx_opcode(isert_conn, rx_desc,
1438 read_stag, read_va, write_stag, write_va);
1439}
1440
1441static void
1442isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1443 unsigned long xfer_len)
1444{
1445 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1446 struct iscsi_hdr *hdr;
1447 u64 rx_dma;
1448 int rx_buflen, outstanding;
1449
1450 if ((char *)desc == isert_conn->login_req_buf) {
1451 rx_dma = isert_conn->login_req_dma;
1452 rx_buflen = ISER_RX_LOGIN_SIZE;
1453 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1454 rx_dma, rx_buflen);
1455 } else {
1456 rx_dma = desc->dma_addr;
1457 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1458 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1459 rx_dma, rx_buflen);
1460 }
1461
1462 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1463
1464 hdr = &desc->iscsi_header;
1465 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1466 hdr->opcode, hdr->itt, hdr->flags,
1467 (int)(xfer_len - ISER_HEADERS_LEN));
1468
1469 if ((char *)desc == isert_conn->login_req_buf)
1470 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1471 isert_conn);
1472 else
1473 isert_rx_do_work(desc, isert_conn);
1474
1475 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1476 DMA_FROM_DEVICE);
1477
1478 isert_conn->post_recv_buf_count--;
1479 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1480 isert_conn->post_recv_buf_count);
1481
1482 if ((char *)desc == isert_conn->login_req_buf)
1483 return;
1484
1485 outstanding = isert_conn->post_recv_buf_count;
1486 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1487 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1488 ISERT_MIN_POSTED_RX);
1489 err = isert_post_recv(isert_conn, count);
1490 if (err) {
1491 pr_err("isert_post_recv() count: %d failed, %d\n",
1492 count, err);
1493 }
1494 }
1495}
1496
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001497static int
1498isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1499 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1500 enum iser_ib_op_code op, struct isert_data_buf *data)
1501{
1502 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1503
1504 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1505 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1506
1507 data->len = length - offset;
1508 data->offset = offset;
1509 data->sg_off = data->offset / PAGE_SIZE;
1510
1511 data->sg = &sg[data->sg_off];
1512 data->nents = min_t(unsigned int, nents - data->sg_off,
1513 ISCSI_ISER_SG_TABLESIZE);
1514 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1515 PAGE_SIZE);
1516
1517 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1518 data->dma_dir);
1519 if (unlikely(!data->dma_nents)) {
1520 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1521 return -EINVAL;
1522 }
1523
1524 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1525 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1526
1527 return 0;
1528}
1529
1530static void
1531isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1532{
1533 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1534
1535 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1536 memset(data, 0, sizeof(*data));
1537}
1538
1539
1540
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001541static void
1542isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1543{
1544 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001545
Vu Pham90ecc6e2013-08-28 23:23:33 +03001546 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001547
1548 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001549 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001550 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001551 }
1552
Vu Pham90ecc6e2013-08-28 23:23:33 +03001553 if (wr->send_wr) {
1554 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1555 kfree(wr->send_wr);
1556 wr->send_wr = NULL;
1557 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001558
Vu Pham90ecc6e2013-08-28 23:23:33 +03001559 if (wr->ib_sge) {
1560 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1561 kfree(wr->ib_sge);
1562 wr->ib_sge = NULL;
1563 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001564}
1565
1566static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001567isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001568{
1569 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001570 LIST_HEAD(unmap_list);
1571
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001572 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001573
1574 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001575 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001576 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001577 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1578 isert_unmap_data_buf(isert_conn, &wr->prot);
1579 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1580 }
Vu Pham59464ef2013-08-28 23:23:35 +03001581 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001582 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001583 spin_unlock_bh(&isert_conn->conn_lock);
1584 wr->fr_desc = NULL;
1585 }
1586
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001587 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001588 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001589 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001590 }
1591
1592 wr->ib_sge = NULL;
1593 wr->send_wr = NULL;
1594}
1595
1596static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001597isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001598{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001599 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001600 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001601 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001602 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001603
1604 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1605
1606 switch (cmd->iscsi_opcode) {
1607 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001608 spin_lock_bh(&conn->cmd_lock);
1609 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001610 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611 spin_unlock_bh(&conn->cmd_lock);
1612
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001613 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001614 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001615 /*
1616 * Check for special case during comp_err where
1617 * WRITE_PENDING has been handed off from core,
1618 * but requires an extra target_put_sess_cmd()
1619 * before transport_generic_free_cmd() below.
1620 */
1621 if (comp_err &&
1622 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1623 struct se_cmd *se_cmd = &cmd->se_cmd;
1624
1625 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1626 }
1627 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001628
Vu Phamd40945d2013-08-28 23:23:34 +03001629 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001630 transport_generic_free_cmd(&cmd->se_cmd, 0);
1631 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001632 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001633 spin_lock_bh(&conn->cmd_lock);
1634 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001635 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001636 spin_unlock_bh(&conn->cmd_lock);
1637
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001638 transport_generic_free_cmd(&cmd->se_cmd, 0);
1639 break;
1640 case ISCSI_OP_REJECT:
1641 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001642 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001643 spin_lock_bh(&conn->cmd_lock);
1644 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001645 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001646 spin_unlock_bh(&conn->cmd_lock);
1647
1648 /*
1649 * Handle special case for REJECT when iscsi_add_reject*() has
1650 * overwritten the original iscsi_opcode assignment, and the
1651 * associated cmd->se_cmd needs to be released.
1652 */
1653 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001654 pr_debug("Calling transport_generic_free_cmd from"
1655 " isert_put_cmd for 0x%02x\n",
1656 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001657 transport_generic_free_cmd(&cmd->se_cmd, 0);
1658 break;
1659 }
1660 /*
1661 * Fall-through
1662 */
1663 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001664 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001665 break;
1666 }
1667}
1668
1669static void
1670isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1671{
1672 if (tx_desc->dma_addr != 0) {
1673 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1674 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1675 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1676 tx_desc->dma_addr = 0;
1677 }
1678}
1679
1680static void
1681isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001682 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001683{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001684 if (isert_cmd->pdu_buf_dma != 0) {
1685 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1686 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1687 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1688 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001689 }
1690
1691 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001692 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001693}
1694
Sagi Grimberg96b79732014-03-17 12:52:18 +02001695static int
1696isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1697{
1698 struct ib_mr_status mr_status;
1699 int ret;
1700
1701 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1702 if (ret) {
1703 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1704 goto fail_mr_status;
1705 }
1706
1707 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1708 u64 sec_offset_err;
1709 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1710
1711 switch (mr_status.sig_err.err_type) {
1712 case IB_SIG_BAD_GUARD:
1713 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1714 break;
1715 case IB_SIG_BAD_REFTAG:
1716 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1717 break;
1718 case IB_SIG_BAD_APPTAG:
1719 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1720 break;
1721 }
1722 sec_offset_err = mr_status.sig_err.sig_err_offset;
1723 do_div(sec_offset_err, block_size);
1724 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1725
1726 pr_err("isert: PI error found type %d at sector 0x%llx "
1727 "expected 0x%x vs actual 0x%x\n",
1728 mr_status.sig_err.err_type,
1729 (unsigned long long)se_cmd->bad_sector,
1730 mr_status.sig_err.expected,
1731 mr_status.sig_err.actual);
1732 ret = 1;
1733 }
1734
1735fail_mr_status:
1736 return ret;
1737}
1738
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001739static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001740isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1741 struct isert_cmd *isert_cmd)
1742{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001743 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001744 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001745 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001746 struct isert_conn *isert_conn = isert_cmd->conn;
1747 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001748 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001749
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001750 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001751 ret = isert_check_pi_status(se_cmd,
1752 wr->fr_desc->pi_ctx->sig_mr);
1753 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001754 }
1755
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001756 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001757 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001758 if (ret)
1759 transport_send_check_condition_and_sense(se_cmd,
1760 se_cmd->pi_err, 0);
1761 else
1762 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001763}
1764
1765static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001766isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1767 struct isert_cmd *isert_cmd)
1768{
1769 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001770 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001771 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001772 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001773 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001774 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001775
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001776 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001777 ret = isert_check_pi_status(se_cmd,
1778 wr->fr_desc->pi_ctx->sig_mr);
1779 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001780 }
1781
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001782 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001783 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001784 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001785 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786
Vu Pham90ecc6e2013-08-28 23:23:33 +03001787 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001788 spin_lock_bh(&cmd->istate_lock);
1789 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1790 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1791 spin_unlock_bh(&cmd->istate_lock);
1792
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001793 if (ret)
1794 transport_send_check_condition_and_sense(se_cmd,
1795 se_cmd->pi_err, 0);
1796 else
1797 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001798}
1799
1800static void
1801isert_do_control_comp(struct work_struct *work)
1802{
1803 struct isert_cmd *isert_cmd = container_of(work,
1804 struct isert_cmd, comp_work);
1805 struct isert_conn *isert_conn = isert_cmd->conn;
1806 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001807 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001808
1809 switch (cmd->i_state) {
1810 case ISTATE_SEND_TASKMGTRSP:
1811 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1812
1813 atomic_dec(&isert_conn->post_send_buf_count);
1814 iscsit_tmr_post_handler(cmd, cmd->conn);
1815
1816 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001817 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001818 break;
1819 case ISTATE_SEND_REJECT:
1820 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1821 atomic_dec(&isert_conn->post_send_buf_count);
1822
1823 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001824 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001825 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001826 case ISTATE_SEND_LOGOUTRSP:
1827 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03001828
1829 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001830 iscsit_logout_post_handler(cmd, cmd->conn);
1831 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001832 case ISTATE_SEND_TEXTRSP:
1833 atomic_dec(&isert_conn->post_send_buf_count);
1834 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001835 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001836 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001837 default:
1838 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1839 dump_stack();
1840 break;
1841 }
1842}
1843
1844static void
1845isert_response_completion(struct iser_tx_desc *tx_desc,
1846 struct isert_cmd *isert_cmd,
1847 struct isert_conn *isert_conn,
1848 struct ib_device *ib_dev)
1849{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001850 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001851 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001852
1853 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001854 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001855 cmd->i_state == ISTATE_SEND_REJECT ||
1856 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001857 isert_unmap_tx_desc(tx_desc, ib_dev);
1858
1859 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1860 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1861 return;
1862 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001863
1864 /**
1865 * If send_wr_num is 0 this means that we got
1866 * RDMA completion and we cleared it and we should
1867 * simply decrement the response post. else the
1868 * response is incorporated in send_wr_num, just
1869 * sub it.
1870 **/
1871 if (wr->send_wr_num)
1872 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1873 else
1874 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001875
1876 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001877 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001878}
1879
1880static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001881__isert_send_completion(struct iser_tx_desc *tx_desc,
1882 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001883{
1884 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1885 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1886 struct isert_rdma_wr *wr;
1887
1888 if (!isert_cmd) {
1889 atomic_dec(&isert_conn->post_send_buf_count);
1890 isert_unmap_tx_desc(tx_desc, ib_dev);
1891 return;
1892 }
1893 wr = &isert_cmd->rdma_wr;
1894
1895 switch (wr->iser_ib_op) {
1896 case ISER_IB_RECV:
1897 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1898 dump_stack();
1899 break;
1900 case ISER_IB_SEND:
1901 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1902 isert_response_completion(tx_desc, isert_cmd,
1903 isert_conn, ib_dev);
1904 break;
1905 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001906 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001907 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001908 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001909 break;
1910 case ISER_IB_RDMA_READ:
1911 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1912
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001913 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001914 isert_completion_rdma_read(tx_desc, isert_cmd);
1915 break;
1916 default:
1917 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1918 dump_stack();
1919 break;
1920 }
1921}
1922
1923static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001924isert_send_completion(struct iser_tx_desc *tx_desc,
1925 struct isert_conn *isert_conn)
1926{
1927 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1928 struct iser_tx_desc *t;
1929 /*
1930 * Drain coalesced completion llist starting from comp_llnode_batch
1931 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1932 */
1933 while (llnode) {
1934 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1935 llnode = llist_next(llnode);
1936 __isert_send_completion(t, isert_conn);
1937 }
1938 __isert_send_completion(tx_desc, isert_conn);
1939}
1940
1941static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001942isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1943{
1944 struct llist_node *llnode;
1945 struct isert_rdma_wr *wr;
1946 struct iser_tx_desc *t;
1947
1948 mutex_lock(&isert_conn->conn_mutex);
1949 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1950 isert_conn->conn_comp_batch = 0;
1951 mutex_unlock(&isert_conn->conn_mutex);
1952
1953 while (llnode) {
1954 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1955 llnode = llist_next(llnode);
1956 wr = &t->isert_cmd->rdma_wr;
1957
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001958 /**
1959 * If send_wr_num is 0 this means that we got
1960 * RDMA completion and we cleared it and we should
1961 * simply decrement the response post. else the
1962 * response is incorporated in send_wr_num, just
1963 * sub it.
1964 **/
1965 if (wr->send_wr_num)
1966 atomic_sub(wr->send_wr_num,
1967 &isert_conn->post_send_buf_count);
1968 else
1969 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001970
1971 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001972 }
1973}
1974
1975static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001976isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977{
1978 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001979 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001980 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1981 struct isert_rdma_wr *wr;
1982 struct iser_tx_desc *t;
1983
1984 while (llnode) {
1985 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1986 llnode = llist_next(llnode);
1987 wr = &t->isert_cmd->rdma_wr;
1988
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001989 /**
1990 * If send_wr_num is 0 this means that we got
1991 * RDMA completion and we cleared it and we should
1992 * simply decrement the response post. else the
1993 * response is incorporated in send_wr_num, just
1994 * sub it.
1995 **/
1996 if (wr->send_wr_num)
1997 atomic_sub(wr->send_wr_num,
1998 &isert_conn->post_send_buf_count);
1999 else
2000 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002001
2002 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002003 }
2004 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002005
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002006 if (!isert_cmd)
2007 isert_unmap_tx_desc(tx_desc, ib_dev);
2008 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002009 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002010}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002011
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002012static void
2013isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2014{
2015 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2016 struct iscsi_conn *conn = isert_conn->conn;
2017
2018 if (isert_conn->post_recv_buf_count)
2019 return;
2020
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002021 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2022
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002023 if (conn->sess) {
2024 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2025 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026 }
2027
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002028 while (atomic_read(&isert_conn->post_send_buf_count))
2029 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002031 mutex_lock(&isert_conn->conn_mutex);
2032 isert_conn->state = ISER_CONN_DOWN;
2033 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002034
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03002035 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
2036
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002037 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002038}
2039
2040static void
2041isert_cq_tx_work(struct work_struct *work)
2042{
2043 struct isert_cq_desc *cq_desc = container_of(work,
2044 struct isert_cq_desc, cq_tx_work);
2045 struct isert_device *device = cq_desc->device;
2046 int cq_index = cq_desc->cq_index;
2047 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2048 struct isert_conn *isert_conn;
2049 struct iser_tx_desc *tx_desc;
2050 struct ib_wc wc;
2051
2052 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2053 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2054 isert_conn = wc.qp->qp_context;
2055
2056 if (wc.status == IB_WC_SUCCESS) {
2057 isert_send_completion(tx_desc, isert_conn);
2058 } else {
2059 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2060 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002061 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002062
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002063 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002064 if (tx_desc->llnode_active)
2065 continue;
2066
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002067 atomic_dec(&isert_conn->post_send_buf_count);
2068 isert_cq_tx_comp_err(tx_desc, isert_conn);
2069 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070 }
2071 }
2072
2073 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2074}
2075
2076static void
2077isert_cq_tx_callback(struct ib_cq *cq, void *context)
2078{
2079 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2080
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2082}
2083
2084static void
2085isert_cq_rx_work(struct work_struct *work)
2086{
2087 struct isert_cq_desc *cq_desc = container_of(work,
2088 struct isert_cq_desc, cq_rx_work);
2089 struct isert_device *device = cq_desc->device;
2090 int cq_index = cq_desc->cq_index;
2091 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2092 struct isert_conn *isert_conn;
2093 struct iser_rx_desc *rx_desc;
2094 struct ib_wc wc;
2095 unsigned long xfer_len;
2096
2097 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2098 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2099 isert_conn = wc.qp->qp_context;
2100
2101 if (wc.status == IB_WC_SUCCESS) {
2102 xfer_len = (unsigned long)wc.byte_len;
2103 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2104 } else {
2105 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002106 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002107 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002108 pr_debug("RX wc.vendor_err: 0x%08x\n",
2109 wc.vendor_err);
2110 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002111 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002112 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002113 }
2114 }
2115
2116 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2117}
2118
2119static void
2120isert_cq_rx_callback(struct ib_cq *cq, void *context)
2121{
2122 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2123
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002124 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2125}
2126
2127static int
2128isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2129{
2130 struct ib_send_wr *wr_failed;
2131 int ret;
2132
2133 atomic_inc(&isert_conn->post_send_buf_count);
2134
2135 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2136 &wr_failed);
2137 if (ret) {
2138 pr_err("ib_post_send failed with %d\n", ret);
2139 atomic_dec(&isert_conn->post_send_buf_count);
2140 return ret;
2141 }
2142 return ret;
2143}
2144
2145static int
2146isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2147{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002148 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002149 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2150 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2151 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2152 &isert_cmd->tx_desc.iscsi_header;
2153
2154 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2155 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2156 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2157 /*
2158 * Attach SENSE DATA payload to iSCSI Response PDU
2159 */
2160 if (cmd->se_cmd.sense_buffer &&
2161 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2162 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2163 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2164 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002165 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002166
2167 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2168 cmd->sense_buffer);
2169 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2170
2171 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2172 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002173 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002174
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002175 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2176 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002177 DMA_TO_DEVICE);
2178
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002179 isert_cmd->pdu_buf_len = pdu_len;
2180 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2181 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002182 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2183 isert_cmd->tx_desc.num_sge = 2;
2184 }
2185
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002186 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002187
2188 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2189
2190 return isert_post_response(isert_conn, isert_cmd);
2191}
2192
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002193static void
2194isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2195{
2196 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2197 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2198 struct isert_device *device = isert_conn->conn_device;
2199
2200 spin_lock_bh(&conn->cmd_lock);
2201 if (!list_empty(&cmd->i_conn_node))
2202 list_del_init(&cmd->i_conn_node);
2203 spin_unlock_bh(&conn->cmd_lock);
2204
2205 if (cmd->data_direction == DMA_TO_DEVICE)
2206 iscsit_stop_dataout_timer(cmd);
2207
2208 device->unreg_rdma_mem(isert_cmd, isert_conn);
2209}
2210
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002211static enum target_prot_op
2212isert_get_sup_prot_ops(struct iscsi_conn *conn)
2213{
2214 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2215 struct isert_device *device = isert_conn->conn_device;
2216
2217 if (device->pi_capable)
2218 return TARGET_PROT_ALL;
2219
2220 return TARGET_PROT_NORMAL;
2221}
2222
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002223static int
2224isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2225 bool nopout_response)
2226{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002227 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002228 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2229 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2230
2231 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2232 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2233 &isert_cmd->tx_desc.iscsi_header,
2234 nopout_response);
2235 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002236 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002237
Masanari Iida8b513d02013-05-21 23:13:12 +09002238 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002239
2240 return isert_post_response(isert_conn, isert_cmd);
2241}
2242
2243static int
2244isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2245{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002246 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002247 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2248 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2249
2250 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2251 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2252 &isert_cmd->tx_desc.iscsi_header);
2253 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002254 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002255
2256 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2257
2258 return isert_post_response(isert_conn, isert_cmd);
2259}
2260
2261static int
2262isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2263{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002264 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002265 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2266 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2267
2268 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2269 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2270 &isert_cmd->tx_desc.iscsi_header);
2271 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002272 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002273
2274 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2275
2276 return isert_post_response(isert_conn, isert_cmd);
2277}
2278
2279static int
2280isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2281{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002282 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002283 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2284 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002285 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2286 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2287 struct iscsi_reject *hdr =
2288 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289
2290 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002291 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002292 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002293
2294 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002295 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002296 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2297 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002298 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2299 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002300 tx_dsg->length = ISCSI_HDR_LEN;
2301 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2302 isert_cmd->tx_desc.num_sge = 2;
2303
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002304 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305
2306 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2307
2308 return isert_post_response(isert_conn, isert_cmd);
2309}
2310
2311static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002312isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2313{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002314 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002315 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2316 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2317 struct iscsi_text_rsp *hdr =
2318 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2319 u32 txt_rsp_len;
2320 int rc;
2321
2322 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03002323 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002324 if (rc < 0)
2325 return rc;
2326
2327 txt_rsp_len = rc;
2328 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2329
2330 if (txt_rsp_len) {
2331 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2332 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2333 void *txt_rsp_buf = cmd->buf_ptr;
2334
2335 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2336 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2337
2338 isert_cmd->pdu_buf_len = txt_rsp_len;
2339 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2340 tx_dsg->length = txt_rsp_len;
2341 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2342 isert_cmd->tx_desc.num_sge = 2;
2343 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002344 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002345
2346 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2347
2348 return isert_post_response(isert_conn, isert_cmd);
2349}
2350
2351static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002352isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2353 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2354 u32 data_left, u32 offset)
2355{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002356 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002357 struct scatterlist *sg_start, *tmp_sg;
2358 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2359 u32 sg_off, page_off;
2360 int i = 0, sg_nents;
2361
2362 sg_off = offset / PAGE_SIZE;
2363 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2364 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2365 page_off = offset % PAGE_SIZE;
2366
2367 send_wr->sg_list = ib_sge;
2368 send_wr->num_sge = sg_nents;
2369 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2370 /*
2371 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2372 */
2373 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2374 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2375 (unsigned long long)tmp_sg->dma_address,
2376 tmp_sg->length, page_off);
2377
2378 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2379 ib_sge->length = min_t(u32, data_left,
2380 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2381 ib_sge->lkey = isert_conn->conn_mr->lkey;
2382
Vu Pham90ecc6e2013-08-28 23:23:33 +03002383 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2384 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002385 page_off = 0;
2386 data_left -= ib_sge->length;
2387 ib_sge++;
2388 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2389 }
2390
2391 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2392 send_wr->sg_list, send_wr->num_sge);
2393
2394 return sg_nents;
2395}
2396
2397static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002398isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2399 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400{
2401 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002402 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002404 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002405 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002406 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002407 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2408 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002409
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002410 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002411
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002412 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2413 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2414 se_cmd->t_data_nents, se_cmd->data_length,
2415 offset, wr->iser_ib_op, &wr->data);
2416 if (ret)
2417 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002419 data_left = data->len;
2420 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002421
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002422 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002423 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002424 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002425 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002426 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002427 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002428 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002429
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002430 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002431 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2432 GFP_KERNEL);
2433 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002434 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002436 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002437 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002438
2439 wr->isert_cmd = isert_cmd;
2440 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002441
2442 for (i = 0; i < wr->send_wr_num; i++) {
2443 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2444 data_len = min(data_left, rdma_write_max);
2445
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002446 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002447 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2448 send_wr->opcode = IB_WR_RDMA_WRITE;
2449 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2450 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2451 if (i + 1 == wr->send_wr_num)
2452 send_wr->next = &isert_cmd->tx_desc.send_wr;
2453 else
2454 send_wr->next = &wr->send_wr[i + 1];
2455 } else {
2456 send_wr->opcode = IB_WR_RDMA_READ;
2457 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2458 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2459 if (i + 1 == wr->send_wr_num)
2460 send_wr->send_flags = IB_SEND_SIGNALED;
2461 else
2462 send_wr->next = &wr->send_wr[i + 1];
2463 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002464
2465 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2466 send_wr, data_len, offset);
2467 ib_sge += ib_sge_cnt;
2468
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002469 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002470 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002471 data_left -= data_len;
2472 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002473
2474 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002475unmap_cmd:
2476 isert_unmap_data_buf(isert_conn, data);
2477
Vu Pham90ecc6e2013-08-28 23:23:33 +03002478 return ret;
2479}
2480
2481static int
Vu Pham59464ef2013-08-28 23:23:35 +03002482isert_map_fr_pagelist(struct ib_device *ib_dev,
2483 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2484{
2485 u64 start_addr, end_addr, page, chunk_start = 0;
2486 struct scatterlist *tmp_sg;
2487 int i = 0, new_chunk, last_ent, n_pages;
2488
2489 n_pages = 0;
2490 new_chunk = 1;
2491 last_ent = sg_nents - 1;
2492 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2493 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2494 if (new_chunk)
2495 chunk_start = start_addr;
2496 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2497
2498 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2499 i, (unsigned long long)tmp_sg->dma_address,
2500 tmp_sg->length);
2501
2502 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2503 new_chunk = 0;
2504 continue;
2505 }
2506 new_chunk = 1;
2507
2508 page = chunk_start & PAGE_MASK;
2509 do {
2510 fr_pl[n_pages++] = page;
2511 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2512 n_pages - 1, page);
2513 page += PAGE_SIZE;
2514 } while (page < end_addr);
2515 }
2516
2517 return n_pages;
2518}
2519
2520static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002521isert_fast_reg_mr(struct isert_conn *isert_conn,
2522 struct fast_reg_descriptor *fr_desc,
2523 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002524 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002525 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002526{
Vu Pham59464ef2013-08-28 23:23:35 +03002527 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002528 struct ib_mr *mr;
2529 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002530 struct ib_send_wr fr_wr, inv_wr;
2531 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002532 int ret, pagelist_len;
2533 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002534 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002535
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002536 if (mem->dma_nents == 1) {
2537 sge->lkey = isert_conn->conn_mr->lkey;
2538 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2539 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002540 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2541 __func__, __LINE__, sge->addr, sge->length,
2542 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002543 return 0;
2544 }
2545
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002546 if (ind == ISERT_DATA_KEY_VALID) {
2547 /* Registering data buffer */
2548 mr = fr_desc->data_mr;
2549 frpl = fr_desc->data_frpl;
2550 } else {
2551 /* Registering protection buffer */
2552 mr = fr_desc->pi_ctx->prot_mr;
2553 frpl = fr_desc->pi_ctx->prot_frpl;
2554 }
2555
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002556 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002557
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002558 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002559 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002560
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002561 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002562 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002563
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002564 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002565 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002566 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002567 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002568 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002569 wr = &inv_wr;
2570 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002571 key = (u8)(mr->rkey & 0x000000FF);
2572 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002573 }
2574
2575 /* Prepare FASTREG WR */
2576 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002577 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002578 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002579 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2580 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002581 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2582 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002583 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002584 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002585 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2586
2587 if (!wr)
2588 wr = &fr_wr;
2589 else
2590 wr->next = &fr_wr;
2591
2592 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2593 if (ret) {
2594 pr_err("fast registration failed, ret:%d\n", ret);
2595 return ret;
2596 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002597 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002598
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002599 sge->lkey = mr->lkey;
2600 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002601 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002602
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002603 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2604 __func__, __LINE__, sge->addr, sge->length,
2605 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002606
2607 return ret;
2608}
2609
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002610static inline enum ib_t10_dif_type
2611se2ib_prot_type(enum target_prot_type prot_type)
2612{
2613 switch (prot_type) {
2614 case TARGET_DIF_TYPE0_PROT:
2615 return IB_T10DIF_NONE;
2616 case TARGET_DIF_TYPE1_PROT:
2617 return IB_T10DIF_TYPE1;
2618 case TARGET_DIF_TYPE2_PROT:
2619 return IB_T10DIF_TYPE2;
2620 case TARGET_DIF_TYPE3_PROT:
2621 return IB_T10DIF_TYPE3;
2622 default:
2623 return IB_T10DIF_NONE;
2624 }
2625}
2626
2627static int
2628isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2629{
2630 enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
2631
2632 sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
2633 sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
2634 sig_attrs->mem.sig.dif.pi_interval =
2635 se_cmd->se_dev->dev_attrib.block_size;
2636 sig_attrs->wire.sig.dif.pi_interval =
2637 se_cmd->se_dev->dev_attrib.block_size;
2638
2639 switch (se_cmd->prot_op) {
2640 case TARGET_PROT_DIN_INSERT:
2641 case TARGET_PROT_DOUT_STRIP:
2642 sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
2643 sig_attrs->wire.sig.dif.type = ib_prot_type;
2644 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2645 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2646 break;
2647 case TARGET_PROT_DOUT_INSERT:
2648 case TARGET_PROT_DIN_STRIP:
2649 sig_attrs->mem.sig.dif.type = ib_prot_type;
2650 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2651 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2652 sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
2653 break;
2654 case TARGET_PROT_DIN_PASS:
2655 case TARGET_PROT_DOUT_PASS:
2656 sig_attrs->mem.sig.dif.type = ib_prot_type;
2657 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2658 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2659 sig_attrs->wire.sig.dif.type = ib_prot_type;
2660 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2661 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2662 break;
2663 default:
2664 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2665 return -EINVAL;
2666 }
2667
2668 return 0;
2669}
2670
2671static inline u8
2672isert_set_prot_checks(u8 prot_checks)
2673{
2674 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2675 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2676 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2677}
2678
2679static int
2680isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2681 struct fast_reg_descriptor *fr_desc,
2682 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2683 struct ib_sge *sig_sge)
2684{
2685 struct ib_send_wr sig_wr, inv_wr;
2686 struct ib_send_wr *bad_wr, *wr = NULL;
2687 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2688 struct ib_sig_attrs sig_attrs;
2689 int ret;
2690 u32 key;
2691
2692 memset(&sig_attrs, 0, sizeof(sig_attrs));
2693 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2694 if (ret)
2695 goto err;
2696
2697 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2698
2699 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2700 memset(&inv_wr, 0, sizeof(inv_wr));
2701 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002702 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002703 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2704 wr = &inv_wr;
2705 /* Bump the key */
2706 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2707 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2708 }
2709
2710 memset(&sig_wr, 0, sizeof(sig_wr));
2711 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002712 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002713 sig_wr.sg_list = data_sge;
2714 sig_wr.num_sge = 1;
2715 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2716 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2717 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2718 if (se_cmd->t_prot_sg)
2719 sig_wr.wr.sig_handover.prot = prot_sge;
2720
2721 if (!wr)
2722 wr = &sig_wr;
2723 else
2724 wr->next = &sig_wr;
2725
2726 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2727 if (ret) {
2728 pr_err("fast registration failed, ret:%d\n", ret);
2729 goto err;
2730 }
2731 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2732
2733 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2734 sig_sge->addr = 0;
2735 sig_sge->length = se_cmd->data_length;
2736 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2737 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2738 /*
2739 * We have protection guards on the wire
2740 * so we need to set a larget transfer
2741 */
2742 sig_sge->length += se_cmd->prot_length;
2743
2744 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2745 sig_sge->addr, sig_sge->length,
2746 sig_sge->lkey);
2747err:
2748 return ret;
2749}
2750
Vu Pham59464ef2013-08-28 23:23:35 +03002751static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002752isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2753 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002754{
2755 struct se_cmd *se_cmd = &cmd->se_cmd;
2756 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002757 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002758 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002759 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002760 struct fast_reg_descriptor *fr_desc = NULL;
2761 u32 offset;
2762 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002763 unsigned long flags;
2764
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002765 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2766
2767 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2768 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2769 se_cmd->t_data_nents, se_cmd->data_length,
2770 offset, wr->iser_ib_op, &wr->data);
2771 if (ret)
2772 return ret;
2773
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002774 if (wr->data.dma_nents != 1 ||
2775 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002776 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2777 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2778 struct fast_reg_descriptor, list);
2779 list_del(&fr_desc->list);
2780 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2781 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002782 }
2783
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002784 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2785 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002786 if (ret)
2787 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002788
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002789 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2790 struct ib_sge prot_sge, sig_sge;
2791
2792 if (se_cmd->t_prot_sg) {
2793 ret = isert_map_data_buf(isert_conn, isert_cmd,
2794 se_cmd->t_prot_sg,
2795 se_cmd->t_prot_nents,
2796 se_cmd->prot_length,
2797 0, wr->iser_ib_op, &wr->prot);
2798 if (ret)
2799 goto unmap_cmd;
2800
2801 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2802 ISERT_PROT_KEY_VALID, &prot_sge);
2803 if (ret)
2804 goto unmap_prot_cmd;
2805 }
2806
2807 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2808 &data_sge, &prot_sge, &sig_sge);
2809 if (ret)
2810 goto unmap_prot_cmd;
2811
2812 fr_desc->ind |= ISERT_PROTECTED;
2813 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2814 } else
2815 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2816
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002817 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002818 wr->send_wr_num = 1;
2819 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2820 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002821 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002822
2823 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002824 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002825 send_wr->num_sge = 1;
2826 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2827 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2828 send_wr->opcode = IB_WR_RDMA_WRITE;
2829 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2830 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002831 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2832 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002833 } else {
2834 send_wr->opcode = IB_WR_RDMA_READ;
2835 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2836 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2837 send_wr->send_flags = IB_SEND_SIGNALED;
2838 }
2839
Vu Pham59464ef2013-08-28 23:23:35 +03002840 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002841unmap_prot_cmd:
2842 if (se_cmd->t_prot_sg)
2843 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002844unmap_cmd:
2845 if (fr_desc) {
2846 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2847 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2848 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2849 }
2850 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002851
Vu Pham59464ef2013-08-28 23:23:35 +03002852 return ret;
2853}
2854
2855static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002856isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2857{
2858 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002859 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002860 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2861 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002862 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002863 struct ib_send_wr *wr_failed;
2864 int rc;
2865
2866 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2867 isert_cmd, se_cmd->data_length);
2868 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002869 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002870 if (rc) {
2871 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2872 return rc;
2873 }
2874
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002875 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2876 /*
2877 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2878 */
2879 isert_create_send_desc(isert_conn, isert_cmd,
2880 &isert_cmd->tx_desc);
2881 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2882 &isert_cmd->tx_desc.iscsi_header);
2883 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2884 isert_init_send_wr(isert_conn, isert_cmd,
2885 &isert_cmd->tx_desc.send_wr, true);
2886 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002887 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002888 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002889
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002890 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002891
2892 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2893 if (rc) {
2894 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002895 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002896 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002897
2898 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2899 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2900 "READ\n", isert_cmd);
2901 else
2902 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2903 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002904
Vu Pham90ecc6e2013-08-28 23:23:33 +03002905 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002906}
2907
2908static int
2909isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2910{
2911 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002912 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2914 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002915 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002916 struct ib_send_wr *wr_failed;
2917 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918
Vu Pham90ecc6e2013-08-28 23:23:33 +03002919 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2920 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002921 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002922 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002923 if (rc) {
2924 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2925 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926 }
2927
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002928 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002929
2930 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2931 if (rc) {
2932 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002933 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002934 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002935 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2936 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002937
Vu Pham90ecc6e2013-08-28 23:23:33 +03002938 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002939}
2940
2941static int
2942isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2943{
2944 int ret;
2945
2946 switch (state) {
2947 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2948 ret = isert_put_nopin(cmd, conn, false);
2949 break;
2950 default:
2951 pr_err("Unknown immediate state: 0x%02x\n", state);
2952 ret = -EINVAL;
2953 break;
2954 }
2955
2956 return ret;
2957}
2958
2959static int
2960isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2961{
2962 int ret;
2963
2964 switch (state) {
2965 case ISTATE_SEND_LOGOUTRSP:
2966 ret = isert_put_logout_rsp(cmd, conn);
2967 if (!ret) {
2968 pr_debug("Returning iSER Logout -EAGAIN\n");
2969 ret = -EAGAIN;
2970 }
2971 break;
2972 case ISTATE_SEND_NOPIN:
2973 ret = isert_put_nopin(cmd, conn, true);
2974 break;
2975 case ISTATE_SEND_TASKMGTRSP:
2976 ret = isert_put_tm_rsp(cmd, conn);
2977 break;
2978 case ISTATE_SEND_REJECT:
2979 ret = isert_put_reject(cmd, conn);
2980 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002981 case ISTATE_SEND_TEXTRSP:
2982 ret = isert_put_text_rsp(cmd, conn);
2983 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002984 case ISTATE_SEND_STATUS:
2985 /*
2986 * Special case for sending non GOOD SCSI status from TX thread
2987 * context during pre se_cmd excecution failure.
2988 */
2989 ret = isert_put_response(conn, cmd);
2990 break;
2991 default:
2992 pr_err("Unknown response state: 0x%02x\n", state);
2993 ret = -EINVAL;
2994 break;
2995 }
2996
2997 return ret;
2998}
2999
3000static int
3001isert_setup_np(struct iscsi_np *np,
3002 struct __kernel_sockaddr_storage *ksockaddr)
3003{
3004 struct isert_np *isert_np;
3005 struct rdma_cm_id *isert_lid;
3006 struct sockaddr *sa;
3007 int ret;
3008
3009 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
3010 if (!isert_np) {
3011 pr_err("Unable to allocate struct isert_np\n");
3012 return -ENOMEM;
3013 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003014 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003015 mutex_init(&isert_np->np_accept_mutex);
3016 INIT_LIST_HEAD(&isert_np->np_accept_list);
3017 init_completion(&isert_np->np_login_comp);
3018
3019 sa = (struct sockaddr *)ksockaddr;
3020 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3021 /*
3022 * Setup the np->np_sockaddr from the passed sockaddr setup
3023 * in iscsi_target_configfs.c code..
3024 */
3025 memcpy(&np->np_sockaddr, ksockaddr,
3026 sizeof(struct __kernel_sockaddr_storage));
3027
3028 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3029 IB_QPT_RC);
3030 if (IS_ERR(isert_lid)) {
3031 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3032 PTR_ERR(isert_lid));
3033 ret = PTR_ERR(isert_lid);
3034 goto out;
3035 }
3036
3037 ret = rdma_bind_addr(isert_lid, sa);
3038 if (ret) {
3039 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3040 goto out_lid;
3041 }
3042
3043 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3044 if (ret) {
3045 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3046 goto out_lid;
3047 }
3048
3049 isert_np->np_cm_id = isert_lid;
3050 np->np_context = isert_np;
3051 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3052
3053 return 0;
3054
3055out_lid:
3056 rdma_destroy_id(isert_lid);
3057out:
3058 kfree(isert_np);
3059 return ret;
3060}
3061
3062static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003063isert_rdma_accept(struct isert_conn *isert_conn)
3064{
3065 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3066 struct rdma_conn_param cp;
3067 int ret;
3068
3069 memset(&cp, 0, sizeof(struct rdma_conn_param));
3070 cp.responder_resources = isert_conn->responder_resources;
3071 cp.initiator_depth = isert_conn->initiator_depth;
3072 cp.retry_count = 7;
3073 cp.rnr_retry_count = 7;
3074
3075 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3076
3077 ret = rdma_accept(cm_id, &cp);
3078 if (ret) {
3079 pr_err("rdma_accept() failed with: %d\n", ret);
3080 return ret;
3081 }
3082
3083 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3084
3085 return 0;
3086}
3087
3088static int
3089isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3090{
3091 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3092 int ret;
3093
3094 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003095 /*
3096 * For login requests after the first PDU, isert_rx_login_req() will
3097 * kick schedule_delayed_work(&conn->login_work) as the packet is
3098 * received, which turns this callback from iscsi_target_do_login_rx()
3099 * into a NOP.
3100 */
3101 if (!login->first_request)
3102 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003103
3104 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3105 if (ret)
3106 return ret;
3107
3108 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3109 return 0;
3110}
3111
3112static void
3113isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3114 struct isert_conn *isert_conn)
3115{
3116 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3117 struct rdma_route *cm_route = &cm_id->route;
3118 struct sockaddr_in *sock_in;
3119 struct sockaddr_in6 *sock_in6;
3120
3121 conn->login_family = np->np_sockaddr.ss_family;
3122
3123 if (np->np_sockaddr.ss_family == AF_INET6) {
3124 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3125 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3126 &sock_in6->sin6_addr.in6_u);
3127 conn->login_port = ntohs(sock_in6->sin6_port);
3128
3129 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3130 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3131 &sock_in6->sin6_addr.in6_u);
3132 conn->local_port = ntohs(sock_in6->sin6_port);
3133 } else {
3134 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3135 sprintf(conn->login_ip, "%pI4",
3136 &sock_in->sin_addr.s_addr);
3137 conn->login_port = ntohs(sock_in->sin_port);
3138
3139 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3140 sprintf(conn->local_ip, "%pI4",
3141 &sock_in->sin_addr.s_addr);
3142 conn->local_port = ntohs(sock_in->sin_port);
3143 }
3144}
3145
3146static int
3147isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3148{
3149 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3150 struct isert_conn *isert_conn;
3151 int max_accept = 0, ret;
3152
3153accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003154 ret = down_interruptible(&isert_np->np_sem);
Sagi Grimberg1acff632014-10-02 21:40:34 -07003155 if (ret || max_accept > 5)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003156 return -ENODEV;
3157
3158 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003159 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003160 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberge346ab32014-05-19 17:44:22 +03003161 pr_debug("np_thread_state %d for isert_accept_np\n",
3162 np->np_thread_state);
3163 /**
3164 * No point in stalling here when np_thread
3165 * is in state RESET/SHUTDOWN/EXIT - bail
3166 **/
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003167 return -ENODEV;
3168 }
3169 spin_unlock_bh(&np->np_thread_lock);
3170
3171 mutex_lock(&isert_np->np_accept_mutex);
3172 if (list_empty(&isert_np->np_accept_list)) {
3173 mutex_unlock(&isert_np->np_accept_mutex);
3174 max_accept++;
3175 goto accept_wait;
3176 }
3177 isert_conn = list_first_entry(&isert_np->np_accept_list,
3178 struct isert_conn, conn_accept_node);
3179 list_del_init(&isert_conn->conn_accept_node);
3180 mutex_unlock(&isert_np->np_accept_mutex);
3181
3182 conn->context = isert_conn;
3183 isert_conn->conn = conn;
3184 max_accept = 0;
3185
3186 ret = isert_rdma_post_recvl(isert_conn);
3187 if (ret)
3188 return ret;
3189
3190 ret = isert_rdma_accept(isert_conn);
3191 if (ret)
3192 return ret;
3193
3194 isert_set_conn_info(np, conn, isert_conn);
3195
3196 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3197 return 0;
3198}
3199
3200static void
3201isert_free_np(struct iscsi_np *np)
3202{
3203 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3204
3205 rdma_destroy_id(isert_np->np_cm_id);
3206
3207 np->np_context = NULL;
3208 kfree(isert_np);
3209}
3210
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003211static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003212{
3213 struct isert_conn *isert_conn = conn->context;
3214
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003215 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003216
Sagi Grimberg9d49f5e2014-05-19 17:44:23 +03003217 mutex_lock(&isert_conn->conn_mutex);
3218 if (isert_conn->conn_cm_id) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003219 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003220 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003221 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003222 /*
3223 * Only wait for conn_wait_comp_err if the isert_conn made it
3224 * into full feature phase..
3225 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003226 if (isert_conn->state == ISER_CONN_INIT) {
3227 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003228 return;
3229 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003230 if (isert_conn->state == ISER_CONN_UP)
3231 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003232 mutex_unlock(&isert_conn->conn_mutex);
3233
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003234 wait_for_completion(&isert_conn->conn_wait_comp_err);
3235
3236 wait_for_completion(&isert_conn->conn_wait);
3237}
3238
3239static void isert_free_conn(struct iscsi_conn *conn)
3240{
3241 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003242
3243 isert_put_conn(isert_conn);
3244}
3245
3246static struct iscsit_transport iser_target_transport = {
3247 .name = "IB/iSER",
3248 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003249 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003250 .owner = THIS_MODULE,
3251 .iscsit_setup_np = isert_setup_np,
3252 .iscsit_accept_np = isert_accept_np,
3253 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003254 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003255 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003256 .iscsit_get_login_rx = isert_get_login_rx,
3257 .iscsit_put_login_tx = isert_put_login_tx,
3258 .iscsit_immediate_queue = isert_immediate_queue,
3259 .iscsit_response_queue = isert_response_queue,
3260 .iscsit_get_dataout = isert_get_dataout,
3261 .iscsit_queue_data_in = isert_put_datain,
3262 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003263 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003264 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003265};
3266
3267static int __init isert_init(void)
3268{
3269 int ret;
3270
3271 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3272 if (!isert_rx_wq) {
3273 pr_err("Unable to allocate isert_rx_wq\n");
3274 return -ENOMEM;
3275 }
3276
3277 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3278 if (!isert_comp_wq) {
3279 pr_err("Unable to allocate isert_comp_wq\n");
3280 ret = -ENOMEM;
3281 goto destroy_rx_wq;
3282 }
3283
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003284 iscsit_register_transport(&iser_target_transport);
3285 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3286 return 0;
3287
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003288destroy_rx_wq:
3289 destroy_workqueue(isert_rx_wq);
3290 return ret;
3291}
3292
3293static void __exit isert_exit(void)
3294{
Sagi Grimbergf5ebec92014-05-19 17:44:25 +03003295 flush_scheduled_work();
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003296 destroy_workqueue(isert_comp_wq);
3297 destroy_workqueue(isert_rx_wq);
3298 iscsit_unregister_transport(&iser_target_transport);
3299 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3300}
3301
3302MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3303MODULE_VERSION("0.1");
3304MODULE_AUTHOR("nab@Linux-iSCSI.org");
3305MODULE_LICENSE("GPL");
3306
3307module_init(isert_init);
3308module_exit(isert_exit);