blob: 826eaf5741f64241727db72d73cfb47b38de2ecd [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 Grimbergd3e125d2014-02-19 17:50:23 +0200565 u8 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800566
567 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
568 cma_id, cma_id->context);
569
570 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
571 if (!isert_conn) {
572 pr_err("Unable to allocate isert_conn\n");
573 return -ENOMEM;
574 }
575 isert_conn->state = ISER_CONN_INIT;
576 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
577 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800578 init_completion(&isert_conn->conn_wait);
579 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800580 kref_init(&isert_conn->conn_kref);
581 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700582 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300583 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -0700584 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800585
586 cma_id->context = isert_conn;
587 isert_conn->conn_cm_id = cma_id;
588 isert_conn->responder_resources = event->param.conn.responder_resources;
589 isert_conn->initiator_depth = event->param.conn.initiator_depth;
590 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
591 isert_conn->responder_resources, isert_conn->initiator_depth);
592
593 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
594 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
595 if (!isert_conn->login_buf) {
596 pr_err("Unable to allocate isert_conn->login_buf\n");
597 ret = -ENOMEM;
598 goto out;
599 }
600
601 isert_conn->login_req_buf = isert_conn->login_buf;
602 isert_conn->login_rsp_buf = isert_conn->login_buf +
603 ISCSI_DEF_MAX_RECV_SEG_LEN;
604 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
605 isert_conn->login_buf, isert_conn->login_req_buf,
606 isert_conn->login_rsp_buf);
607
608 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
609 (void *)isert_conn->login_req_buf,
610 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
611
612 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
613 if (ret) {
614 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
615 ret);
616 isert_conn->login_req_dma = 0;
617 goto out_login_buf;
618 }
619
620 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
621 (void *)isert_conn->login_rsp_buf,
622 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
623
624 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
625 if (ret) {
626 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
627 ret);
628 isert_conn->login_rsp_dma = 0;
629 goto out_req_dma_map;
630 }
631
632 device = isert_device_find_by_ib_dev(cma_id);
633 if (IS_ERR(device)) {
634 ret = PTR_ERR(device);
635 goto out_rsp_dma_map;
636 }
637
638 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200639 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
640 if (IS_ERR(isert_conn->conn_pd)) {
641 ret = PTR_ERR(isert_conn->conn_pd);
642 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
643 isert_conn, ret);
644 goto out_pd;
645 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800646
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200647 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
648 IB_ACCESS_LOCAL_WRITE);
649 if (IS_ERR(isert_conn->conn_mr)) {
650 ret = PTR_ERR(isert_conn->conn_mr);
651 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
652 isert_conn, ret);
653 goto out_mr;
654 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800655
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200656 if (pi_support && !device->pi_capable) {
657 pr_err("Protection information requested but not supported\n");
658 ret = -EINVAL;
659 goto out_mr;
660 }
661
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200662 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800663 if (ret)
664 goto out_conn_dev;
665
666 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg9fe63c82014-04-29 13:13:44 +0300667 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800668 mutex_unlock(&isert_np->np_accept_mutex);
669
Sagi Grimberg531b7bf2014-04-29 13:13:45 +0300670 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
671 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800672 return 0;
673
674out_conn_dev:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200675 ib_dereg_mr(isert_conn->conn_mr);
676out_mr:
677 ib_dealloc_pd(isert_conn->conn_pd);
678out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800679 isert_device_try_release(device);
680out_rsp_dma_map:
681 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
682 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
683out_req_dma_map:
684 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
685 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
686out_login_buf:
687 kfree(isert_conn->login_buf);
688out:
689 kfree(isert_conn);
690 return ret;
691}
692
693static void
694isert_connect_release(struct isert_conn *isert_conn)
695{
696 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
697 struct isert_device *device = isert_conn->conn_device;
698 int cq_index;
699
700 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
701
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200702 if (device && device->use_fastreg)
703 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300704
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800705 if (isert_conn->conn_qp) {
706 cq_index = ((struct isert_cq_desc *)
707 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
708 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
709 isert_conn->conn_device->cq_active_qps[cq_index]--;
710
711 rdma_destroy_qp(isert_conn->conn_cm_id);
712 }
713
714 isert_free_rx_descriptors(isert_conn);
715 rdma_destroy_id(isert_conn->conn_cm_id);
716
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200717 ib_dereg_mr(isert_conn->conn_mr);
718 ib_dealloc_pd(isert_conn->conn_pd);
719
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800720 if (isert_conn->login_buf) {
721 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
722 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
723 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
724 ISCSI_DEF_MAX_RECV_SEG_LEN,
725 DMA_FROM_DEVICE);
726 kfree(isert_conn->login_buf);
727 }
728 kfree(isert_conn);
729
730 if (device)
731 isert_device_try_release(device);
732
733 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
734}
735
736static void
737isert_connected_handler(struct rdma_cm_id *cma_id)
738{
739 return;
740}
741
742static void
743isert_release_conn_kref(struct kref *kref)
744{
745 struct isert_conn *isert_conn = container_of(kref,
746 struct isert_conn, conn_kref);
747
748 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
749 current->comm, current->pid);
750
751 isert_connect_release(isert_conn);
752}
753
754static void
755isert_put_conn(struct isert_conn *isert_conn)
756{
757 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
758}
759
760static void
761isert_disconnect_work(struct work_struct *work)
762{
763 struct isert_conn *isert_conn = container_of(work,
764 struct isert_conn, conn_logout_work);
765
766 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700767 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800768 if (isert_conn->state == ISER_CONN_UP)
769 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800770
771 if (isert_conn->post_recv_buf_count == 0 &&
772 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700773 mutex_unlock(&isert_conn->conn_mutex);
774 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800775 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700776 if (!isert_conn->conn_cm_id) {
777 mutex_unlock(&isert_conn->conn_mutex);
778 isert_put_conn(isert_conn);
779 return;
780 }
781 if (!isert_conn->logout_posted) {
782 pr_debug("Calling rdma_disconnect for !logout_posted from"
783 " isert_disconnect_work\n");
784 rdma_disconnect(isert_conn->conn_cm_id);
785 mutex_unlock(&isert_conn->conn_mutex);
786 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
787 goto wake_up;
788 }
789 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800790
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700791wake_up:
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800792 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800793 isert_put_conn(isert_conn);
794}
795
796static void
797isert_disconnected_handler(struct rdma_cm_id *cma_id)
798{
799 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
800
801 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
802 schedule_work(&isert_conn->conn_logout_work);
803}
804
805static int
806isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
807{
808 int ret = 0;
809
810 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
811 event->event, event->status, cma_id->context, cma_id);
812
813 switch (event->event) {
814 case RDMA_CM_EVENT_CONNECT_REQUEST:
815 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
816 ret = isert_connect_request(cma_id, event);
817 break;
818 case RDMA_CM_EVENT_ESTABLISHED:
819 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
820 isert_connected_handler(cma_id);
821 break;
822 case RDMA_CM_EVENT_DISCONNECTED:
823 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
824 isert_disconnected_handler(cma_id);
825 break;
826 case RDMA_CM_EVENT_DEVICE_REMOVAL:
827 case RDMA_CM_EVENT_ADDR_CHANGE:
828 break;
829 case RDMA_CM_EVENT_CONNECT_ERROR:
830 default:
831 pr_err("Unknown RDMA CMA event: %d\n", event->event);
832 break;
833 }
834
835 if (ret != 0) {
836 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
837 event->event, ret);
838 dump_stack();
839 }
840
841 return ret;
842}
843
844static int
845isert_post_recv(struct isert_conn *isert_conn, u32 count)
846{
847 struct ib_recv_wr *rx_wr, *rx_wr_failed;
848 int i, ret;
849 unsigned int rx_head = isert_conn->conn_rx_desc_head;
850 struct iser_rx_desc *rx_desc;
851
852 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
853 rx_desc = &isert_conn->conn_rx_descs[rx_head];
854 rx_wr->wr_id = (unsigned long)rx_desc;
855 rx_wr->sg_list = &rx_desc->rx_sg;
856 rx_wr->num_sge = 1;
857 rx_wr->next = rx_wr + 1;
858 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
859 }
860
861 rx_wr--;
862 rx_wr->next = NULL; /* mark end of work requests list */
863
864 isert_conn->post_recv_buf_count += count;
865 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
866 &rx_wr_failed);
867 if (ret) {
868 pr_err("ib_post_recv() failed with ret: %d\n", ret);
869 isert_conn->post_recv_buf_count -= count;
870 } else {
871 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
872 isert_conn->conn_rx_desc_head = rx_head;
873 }
874 return ret;
875}
876
877static int
878isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
879{
880 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
881 struct ib_send_wr send_wr, *send_wr_failed;
882 int ret;
883
884 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
885 ISER_HEADERS_LEN, DMA_TO_DEVICE);
886
887 send_wr.next = NULL;
888 send_wr.wr_id = (unsigned long)tx_desc;
889 send_wr.sg_list = tx_desc->tx_sg;
890 send_wr.num_sge = tx_desc->num_sge;
891 send_wr.opcode = IB_WR_SEND;
892 send_wr.send_flags = IB_SEND_SIGNALED;
893
894 atomic_inc(&isert_conn->post_send_buf_count);
895
896 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
897 if (ret) {
898 pr_err("ib_post_send() failed, ret: %d\n", ret);
899 atomic_dec(&isert_conn->post_send_buf_count);
900 }
901
902 return ret;
903}
904
905static void
906isert_create_send_desc(struct isert_conn *isert_conn,
907 struct isert_cmd *isert_cmd,
908 struct iser_tx_desc *tx_desc)
909{
910 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
911
912 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
913 ISER_HEADERS_LEN, DMA_TO_DEVICE);
914
915 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
916 tx_desc->iser_header.flags = ISER_VER;
917
918 tx_desc->num_sge = 1;
919 tx_desc->isert_cmd = isert_cmd;
920
921 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
922 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
923 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
924 }
925}
926
927static int
928isert_init_tx_hdrs(struct isert_conn *isert_conn,
929 struct iser_tx_desc *tx_desc)
930{
931 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
932 u64 dma_addr;
933
934 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
935 ISER_HEADERS_LEN, DMA_TO_DEVICE);
936 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
937 pr_err("ib_dma_mapping_error() failed\n");
938 return -ENOMEM;
939 }
940
941 tx_desc->dma_addr = dma_addr;
942 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
943 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
944 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
945
946 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
947 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
948 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
949
950 return 0;
951}
952
953static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800954isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
955 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800956{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800957 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
958
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800959 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
960 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
961 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800962 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800963 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800964 /*
965 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
966 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
967 */
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800968 mutex_lock(&isert_conn->conn_mutex);
969 if (coalesce && isert_conn->state == ISER_CONN_UP &&
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800970 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800971 tx_desc->llnode_active = true;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800972 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800973 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800974 return;
975 }
976 isert_conn->conn_comp_batch = 0;
977 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -0800978 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800979
980 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800981}
982
983static int
984isert_rdma_post_recvl(struct isert_conn *isert_conn)
985{
986 struct ib_recv_wr rx_wr, *rx_wr_fail;
987 struct ib_sge sge;
988 int ret;
989
990 memset(&sge, 0, sizeof(struct ib_sge));
991 sge.addr = isert_conn->login_req_dma;
992 sge.length = ISER_RX_LOGIN_SIZE;
993 sge.lkey = isert_conn->conn_mr->lkey;
994
995 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
996 sge.addr, sge.length, sge.lkey);
997
998 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
999 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
1000 rx_wr.sg_list = &sge;
1001 rx_wr.num_sge = 1;
1002
1003 isert_conn->post_recv_buf_count++;
1004 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
1005 if (ret) {
1006 pr_err("ib_post_recv() failed: %d\n", ret);
1007 isert_conn->post_recv_buf_count--;
1008 }
1009
1010 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
1011 return ret;
1012}
1013
1014static int
1015isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1016 u32 length)
1017{
1018 struct isert_conn *isert_conn = conn->context;
1019 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1020 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
1021 int ret;
1022
1023 isert_create_send_desc(isert_conn, NULL, tx_desc);
1024
1025 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
1026 sizeof(struct iscsi_hdr));
1027
1028 isert_init_tx_hdrs(isert_conn, tx_desc);
1029
1030 if (length > 0) {
1031 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
1032
1033 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
1034 length, DMA_TO_DEVICE);
1035
1036 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
1037
1038 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
1039 length, DMA_TO_DEVICE);
1040
1041 tx_dsg->addr = isert_conn->login_rsp_dma;
1042 tx_dsg->length = length;
1043 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1044 tx_desc->num_sge = 2;
1045 }
1046 if (!login->login_failed) {
1047 if (login->login_complete) {
Nicholas Bellingerf46d6a82014-03-21 18:10:25 -07001048 if (isert_conn->conn_device->use_fastreg) {
1049 u8 pi_support = login->np->tpg_np->tpg->tpg_attrib.t10_pi;
1050
1051 ret = isert_conn_create_fastreg_pool(isert_conn,
1052 pi_support);
1053 if (ret) {
1054 pr_err("Conn: %p failed to create"
1055 " fastreg pool\n", isert_conn);
1056 return ret;
1057 }
1058 }
1059
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001060 ret = isert_alloc_rx_descriptors(isert_conn);
1061 if (ret)
1062 return ret;
1063
1064 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1065 if (ret)
1066 return ret;
1067
1068 isert_conn->state = ISER_CONN_UP;
1069 goto post_send;
1070 }
1071
1072 ret = isert_rdma_post_recvl(isert_conn);
1073 if (ret)
1074 return ret;
1075 }
1076post_send:
1077 ret = isert_post_send(isert_conn, tx_desc);
1078 if (ret)
1079 return ret;
1080
1081 return 0;
1082}
1083
1084static void
1085isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1086 struct isert_conn *isert_conn)
1087{
1088 struct iscsi_conn *conn = isert_conn->conn;
1089 struct iscsi_login *login = conn->conn_login;
1090 int size;
1091
1092 if (!login) {
1093 pr_err("conn->conn_login is NULL\n");
1094 dump_stack();
1095 return;
1096 }
1097
1098 if (login->first_request) {
1099 struct iscsi_login_req *login_req =
1100 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1101 /*
1102 * Setup the initial iscsi_login values from the leading
1103 * login request PDU.
1104 */
1105 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1106 login->current_stage =
1107 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1108 >> 2;
1109 login->version_min = login_req->min_version;
1110 login->version_max = login_req->max_version;
1111 memcpy(login->isid, login_req->isid, 6);
1112 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1113 login->init_task_tag = login_req->itt;
1114 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1115 login->cid = be16_to_cpu(login_req->cid);
1116 login->tsih = be16_to_cpu(login_req->tsih);
1117 }
1118
1119 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1120
1121 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1122 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1123 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1124 memcpy(login->req_buf, &rx_desc->data[0], size);
1125
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001126 if (login->first_request) {
1127 complete(&isert_conn->conn_login_comp);
1128 return;
1129 }
1130 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001131}
1132
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001133static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001134*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001135{
1136 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1137 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001138 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001139
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001140 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001141 if (!cmd) {
1142 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001143 return NULL;
1144 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001145 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001146 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001147 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001148
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001149 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001150}
1151
1152static int
1153isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001154 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1155 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001156{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001157 struct iscsi_conn *conn = isert_conn->conn;
1158 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1159 struct scatterlist *sg;
1160 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1161 bool dump_payload = false;
1162
1163 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1164 if (rc < 0)
1165 return rc;
1166
1167 imm_data = cmd->immediate_data;
1168 imm_data_len = cmd->first_burst_len;
1169 unsol_data = cmd->unsolicited_data;
1170
1171 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1172 if (rc < 0) {
1173 return 0;
1174 } else if (rc > 0) {
1175 dump_payload = true;
1176 goto sequence_cmd;
1177 }
1178
1179 if (!imm_data)
1180 return 0;
1181
1182 sg = &cmd->se_cmd.t_data_sg[0];
1183 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1184
1185 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1186 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1187
1188 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1189
1190 cmd->write_data_done += imm_data_len;
1191
1192 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1193 spin_lock_bh(&cmd->istate_lock);
1194 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1195 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1196 spin_unlock_bh(&cmd->istate_lock);
1197 }
1198
1199sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001200 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001201
1202 if (!rc && dump_payload == false && unsol_data)
1203 iscsit_set_unsoliticed_dataout(cmd);
1204
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001205 return 0;
1206}
1207
1208static int
1209isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1210 struct iser_rx_desc *rx_desc, unsigned char *buf)
1211{
1212 struct scatterlist *sg_start;
1213 struct iscsi_conn *conn = isert_conn->conn;
1214 struct iscsi_cmd *cmd = NULL;
1215 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1216 u32 unsol_data_len = ntoh24(hdr->dlength);
1217 int rc, sg_nents, sg_off, page_off;
1218
1219 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1220 if (rc < 0)
1221 return rc;
1222 else if (!cmd)
1223 return 0;
1224 /*
1225 * FIXME: Unexpected unsolicited_data out
1226 */
1227 if (!cmd->unsolicited_data) {
1228 pr_err("Received unexpected solicited data payload\n");
1229 dump_stack();
1230 return -1;
1231 }
1232
1233 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1234 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1235
1236 sg_off = cmd->write_data_done / PAGE_SIZE;
1237 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1238 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1239 page_off = cmd->write_data_done % PAGE_SIZE;
1240 /*
1241 * FIXME: Non page-aligned unsolicited_data out
1242 */
1243 if (page_off) {
1244 pr_err("Received unexpected non-page aligned data payload\n");
1245 dump_stack();
1246 return -1;
1247 }
1248 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1249 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1250
1251 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1252 unsol_data_len);
1253
1254 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1255 if (rc < 0)
1256 return rc;
1257
1258 return 0;
1259}
1260
1261static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001262isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001263 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1264 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001265{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001266 struct iscsi_conn *conn = isert_conn->conn;
1267 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1268 int rc;
1269
1270 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1271 if (rc < 0)
1272 return rc;
1273 /*
1274 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1275 */
1276
1277 return iscsit_process_nop_out(conn, cmd, hdr);
1278}
1279
1280static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001281isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001282 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1283 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001284{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001285 struct iscsi_conn *conn = isert_conn->conn;
1286 u32 payload_length = ntoh24(hdr->dlength);
1287 int rc;
1288 unsigned char *text_in;
1289
1290 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1291 if (rc < 0)
1292 return rc;
1293
1294 text_in = kzalloc(payload_length, GFP_KERNEL);
1295 if (!text_in) {
1296 pr_err("Unable to allocate text_in of payload_length: %u\n",
1297 payload_length);
1298 return -ENOMEM;
1299 }
1300 cmd->text_in_ptr = text_in;
1301
1302 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1303
1304 return iscsit_process_text_cmd(conn, cmd, hdr);
1305}
1306
1307static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001308isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1309 uint32_t read_stag, uint64_t read_va,
1310 uint32_t write_stag, uint64_t write_va)
1311{
1312 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1313 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001314 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001315 struct iscsi_cmd *cmd;
1316 struct isert_cmd *isert_cmd;
1317 int ret = -EINVAL;
1318 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1319
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001320 if (sess->sess_ops->SessionType &&
1321 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1322 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1323 " ignoring\n", opcode);
1324 return 0;
1325 }
1326
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001327 switch (opcode) {
1328 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001329 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001330 if (!cmd)
1331 break;
1332
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001333 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001334 isert_cmd->read_stag = read_stag;
1335 isert_cmd->read_va = read_va;
1336 isert_cmd->write_stag = write_stag;
1337 isert_cmd->write_va = write_va;
1338
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001339 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001340 rx_desc, (unsigned char *)hdr);
1341 break;
1342 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001343 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001344 if (!cmd)
1345 break;
1346
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001347 isert_cmd = iscsit_priv_cmd(cmd);
1348 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001349 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001350 break;
1351 case ISCSI_OP_SCSI_DATA_OUT:
1352 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1353 (unsigned char *)hdr);
1354 break;
1355 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001356 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001357 if (!cmd)
1358 break;
1359
1360 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1361 (unsigned char *)hdr);
1362 break;
1363 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001364 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001365 if (!cmd)
1366 break;
1367
1368 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1369 if (ret > 0)
1370 wait_for_completion_timeout(&conn->conn_logout_comp,
1371 SECONDS_FOR_LOGOUT_COMP *
1372 HZ);
1373 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001374 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001375 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001376 if (!cmd)
1377 break;
1378
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001379 isert_cmd = iscsit_priv_cmd(cmd);
1380 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001381 rx_desc, (struct iscsi_text *)hdr);
1382 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001383 default:
1384 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1385 dump_stack();
1386 break;
1387 }
1388
1389 return ret;
1390}
1391
1392static void
1393isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1394{
1395 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1396 uint64_t read_va = 0, write_va = 0;
1397 uint32_t read_stag = 0, write_stag = 0;
1398 int rc;
1399
1400 switch (iser_hdr->flags & 0xF0) {
1401 case ISCSI_CTRL:
1402 if (iser_hdr->flags & ISER_RSV) {
1403 read_stag = be32_to_cpu(iser_hdr->read_stag);
1404 read_va = be64_to_cpu(iser_hdr->read_va);
1405 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1406 read_stag, (unsigned long long)read_va);
1407 }
1408 if (iser_hdr->flags & ISER_WSV) {
1409 write_stag = be32_to_cpu(iser_hdr->write_stag);
1410 write_va = be64_to_cpu(iser_hdr->write_va);
1411 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1412 write_stag, (unsigned long long)write_va);
1413 }
1414
1415 pr_debug("ISER ISCSI_CTRL PDU\n");
1416 break;
1417 case ISER_HELLO:
1418 pr_err("iSER Hello message\n");
1419 break;
1420 default:
1421 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1422 break;
1423 }
1424
1425 rc = isert_rx_opcode(isert_conn, rx_desc,
1426 read_stag, read_va, write_stag, write_va);
1427}
1428
1429static void
1430isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1431 unsigned long xfer_len)
1432{
1433 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1434 struct iscsi_hdr *hdr;
1435 u64 rx_dma;
1436 int rx_buflen, outstanding;
1437
1438 if ((char *)desc == isert_conn->login_req_buf) {
1439 rx_dma = isert_conn->login_req_dma;
1440 rx_buflen = ISER_RX_LOGIN_SIZE;
1441 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1442 rx_dma, rx_buflen);
1443 } else {
1444 rx_dma = desc->dma_addr;
1445 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1446 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1447 rx_dma, rx_buflen);
1448 }
1449
1450 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1451
1452 hdr = &desc->iscsi_header;
1453 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1454 hdr->opcode, hdr->itt, hdr->flags,
1455 (int)(xfer_len - ISER_HEADERS_LEN));
1456
1457 if ((char *)desc == isert_conn->login_req_buf)
1458 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1459 isert_conn);
1460 else
1461 isert_rx_do_work(desc, isert_conn);
1462
1463 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1464 DMA_FROM_DEVICE);
1465
1466 isert_conn->post_recv_buf_count--;
1467 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1468 isert_conn->post_recv_buf_count);
1469
1470 if ((char *)desc == isert_conn->login_req_buf)
1471 return;
1472
1473 outstanding = isert_conn->post_recv_buf_count;
1474 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1475 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1476 ISERT_MIN_POSTED_RX);
1477 err = isert_post_recv(isert_conn, count);
1478 if (err) {
1479 pr_err("isert_post_recv() count: %d failed, %d\n",
1480 count, err);
1481 }
1482 }
1483}
1484
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001485static int
1486isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1487 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1488 enum iser_ib_op_code op, struct isert_data_buf *data)
1489{
1490 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1491
1492 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1493 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1494
1495 data->len = length - offset;
1496 data->offset = offset;
1497 data->sg_off = data->offset / PAGE_SIZE;
1498
1499 data->sg = &sg[data->sg_off];
1500 data->nents = min_t(unsigned int, nents - data->sg_off,
1501 ISCSI_ISER_SG_TABLESIZE);
1502 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1503 PAGE_SIZE);
1504
1505 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1506 data->dma_dir);
1507 if (unlikely(!data->dma_nents)) {
1508 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1509 return -EINVAL;
1510 }
1511
1512 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1513 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1514
1515 return 0;
1516}
1517
1518static void
1519isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1520{
1521 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1522
1523 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1524 memset(data, 0, sizeof(*data));
1525}
1526
1527
1528
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001529static void
1530isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1531{
1532 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001533
Vu Pham90ecc6e2013-08-28 23:23:33 +03001534 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001535
1536 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001537 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001538 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001539 }
1540
Vu Pham90ecc6e2013-08-28 23:23:33 +03001541 if (wr->send_wr) {
1542 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1543 kfree(wr->send_wr);
1544 wr->send_wr = NULL;
1545 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001546
Vu Pham90ecc6e2013-08-28 23:23:33 +03001547 if (wr->ib_sge) {
1548 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1549 kfree(wr->ib_sge);
1550 wr->ib_sge = NULL;
1551 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001552}
1553
1554static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001555isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001556{
1557 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001558 LIST_HEAD(unmap_list);
1559
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001560 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001561
1562 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001563 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001564 isert_cmd, wr->fr_desc);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001565 if (wr->fr_desc->ind & ISERT_PROTECTED) {
1566 isert_unmap_data_buf(isert_conn, &wr->prot);
1567 wr->fr_desc->ind &= ~ISERT_PROTECTED;
1568 }
Vu Pham59464ef2013-08-28 23:23:35 +03001569 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001570 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001571 spin_unlock_bh(&isert_conn->conn_lock);
1572 wr->fr_desc = NULL;
1573 }
1574
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001575 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001576 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001577 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001578 }
1579
1580 wr->ib_sge = NULL;
1581 wr->send_wr = NULL;
1582}
1583
1584static void
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001585isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001586{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001587 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001588 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001589 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001590 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001591
1592 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1593
1594 switch (cmd->iscsi_opcode) {
1595 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001596 spin_lock_bh(&conn->cmd_lock);
1597 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001598 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001599 spin_unlock_bh(&conn->cmd_lock);
1600
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001601 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001602 iscsit_stop_dataout_timer(cmd);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001603 /*
1604 * Check for special case during comp_err where
1605 * WRITE_PENDING has been handed off from core,
1606 * but requires an extra target_put_sess_cmd()
1607 * before transport_generic_free_cmd() below.
1608 */
1609 if (comp_err &&
1610 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1611 struct se_cmd *se_cmd = &cmd->se_cmd;
1612
1613 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1614 }
1615 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616
Vu Phamd40945d2013-08-28 23:23:34 +03001617 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001618 transport_generic_free_cmd(&cmd->se_cmd, 0);
1619 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001620 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001621 spin_lock_bh(&conn->cmd_lock);
1622 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001623 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001624 spin_unlock_bh(&conn->cmd_lock);
1625
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001626 transport_generic_free_cmd(&cmd->se_cmd, 0);
1627 break;
1628 case ISCSI_OP_REJECT:
1629 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001630 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001631 spin_lock_bh(&conn->cmd_lock);
1632 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001633 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001634 spin_unlock_bh(&conn->cmd_lock);
1635
1636 /*
1637 * Handle special case for REJECT when iscsi_add_reject*() has
1638 * overwritten the original iscsi_opcode assignment, and the
1639 * associated cmd->se_cmd needs to be released.
1640 */
1641 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001642 pr_debug("Calling transport_generic_free_cmd from"
1643 " isert_put_cmd for 0x%02x\n",
1644 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001645 transport_generic_free_cmd(&cmd->se_cmd, 0);
1646 break;
1647 }
1648 /*
1649 * Fall-through
1650 */
1651 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001652 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001653 break;
1654 }
1655}
1656
1657static void
1658isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1659{
1660 if (tx_desc->dma_addr != 0) {
1661 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1662 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1663 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1664 tx_desc->dma_addr = 0;
1665 }
1666}
1667
1668static void
1669isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001670 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001671{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001672 if (isert_cmd->pdu_buf_dma != 0) {
1673 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1674 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1675 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1676 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001677 }
1678
1679 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001680 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001681}
1682
Sagi Grimberg96b79732014-03-17 12:52:18 +02001683static int
1684isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
1685{
1686 struct ib_mr_status mr_status;
1687 int ret;
1688
1689 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
1690 if (ret) {
1691 pr_err("ib_check_mr_status failed, ret %d\n", ret);
1692 goto fail_mr_status;
1693 }
1694
1695 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
1696 u64 sec_offset_err;
1697 u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
1698
1699 switch (mr_status.sig_err.err_type) {
1700 case IB_SIG_BAD_GUARD:
1701 se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1702 break;
1703 case IB_SIG_BAD_REFTAG:
1704 se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1705 break;
1706 case IB_SIG_BAD_APPTAG:
1707 se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
1708 break;
1709 }
1710 sec_offset_err = mr_status.sig_err.sig_err_offset;
1711 do_div(sec_offset_err, block_size);
1712 se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
1713
1714 pr_err("isert: PI error found type %d at sector 0x%llx "
1715 "expected 0x%x vs actual 0x%x\n",
1716 mr_status.sig_err.err_type,
1717 (unsigned long long)se_cmd->bad_sector,
1718 mr_status.sig_err.expected,
1719 mr_status.sig_err.actual);
1720 ret = 1;
1721 }
1722
1723fail_mr_status:
1724 return ret;
1725}
1726
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001727static void
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001728isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
1729 struct isert_cmd *isert_cmd)
1730{
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001731 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001732 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001733 struct se_cmd *se_cmd = &cmd->se_cmd;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001734 struct isert_conn *isert_conn = isert_cmd->conn;
1735 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001736 int ret = 0;
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001737
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001738 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001739 ret = isert_check_pi_status(se_cmd,
1740 wr->fr_desc->pi_ctx->sig_mr);
1741 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001742 }
1743
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001744 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001745 wr->send_wr_num = 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001746 if (ret)
1747 transport_send_check_condition_and_sense(se_cmd,
1748 se_cmd->pi_err, 0);
1749 else
1750 isert_put_response(isert_conn->conn, cmd);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001751}
1752
1753static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001754isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1755 struct isert_cmd *isert_cmd)
1756{
1757 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001758 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001759 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001760 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001761 struct isert_device *device = isert_conn->conn_device;
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001762 int ret = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001763
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001764 if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
Sagi Grimberg96b79732014-03-17 12:52:18 +02001765 ret = isert_check_pi_status(se_cmd,
1766 wr->fr_desc->pi_ctx->sig_mr);
1767 wr->fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02001768 }
1769
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001770 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001771 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001772 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001773 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001774
Vu Pham90ecc6e2013-08-28 23:23:33 +03001775 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001776 spin_lock_bh(&cmd->istate_lock);
1777 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1778 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1779 spin_unlock_bh(&cmd->istate_lock);
1780
Sagi Grimberg5bac4b12014-03-18 14:58:27 +02001781 if (ret)
1782 transport_send_check_condition_and_sense(se_cmd,
1783 se_cmd->pi_err, 0);
1784 else
1785 target_execute_cmd(se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001786}
1787
1788static void
1789isert_do_control_comp(struct work_struct *work)
1790{
1791 struct isert_cmd *isert_cmd = container_of(work,
1792 struct isert_cmd, comp_work);
1793 struct isert_conn *isert_conn = isert_cmd->conn;
1794 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001795 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001796
1797 switch (cmd->i_state) {
1798 case ISTATE_SEND_TASKMGTRSP:
1799 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1800
1801 atomic_dec(&isert_conn->post_send_buf_count);
1802 iscsit_tmr_post_handler(cmd, cmd->conn);
1803
1804 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001805 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001806 break;
1807 case ISTATE_SEND_REJECT:
1808 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1809 atomic_dec(&isert_conn->post_send_buf_count);
1810
1811 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001812 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001813 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001814 case ISTATE_SEND_LOGOUTRSP:
1815 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1816 /*
1817 * Call atomic_dec(&isert_conn->post_send_buf_count)
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001818 * from isert_wait_conn()
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001819 */
1820 isert_conn->logout_posted = true;
1821 iscsit_logout_post_handler(cmd, cmd->conn);
1822 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001823 case ISTATE_SEND_TEXTRSP:
1824 atomic_dec(&isert_conn->post_send_buf_count);
1825 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001826 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001827 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001828 default:
1829 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1830 dump_stack();
1831 break;
1832 }
1833}
1834
1835static void
1836isert_response_completion(struct iser_tx_desc *tx_desc,
1837 struct isert_cmd *isert_cmd,
1838 struct isert_conn *isert_conn,
1839 struct ib_device *ib_dev)
1840{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001841 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001842 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001843
1844 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001845 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001846 cmd->i_state == ISTATE_SEND_REJECT ||
1847 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001848 isert_unmap_tx_desc(tx_desc, ib_dev);
1849
1850 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1851 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1852 return;
1853 }
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001854
1855 /**
1856 * If send_wr_num is 0 this means that we got
1857 * RDMA completion and we cleared it and we should
1858 * simply decrement the response post. else the
1859 * response is incorporated in send_wr_num, just
1860 * sub it.
1861 **/
1862 if (wr->send_wr_num)
1863 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
1864 else
1865 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001866
1867 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001868 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001869}
1870
1871static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001872__isert_send_completion(struct iser_tx_desc *tx_desc,
1873 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001874{
1875 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1876 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1877 struct isert_rdma_wr *wr;
1878
1879 if (!isert_cmd) {
1880 atomic_dec(&isert_conn->post_send_buf_count);
1881 isert_unmap_tx_desc(tx_desc, ib_dev);
1882 return;
1883 }
1884 wr = &isert_cmd->rdma_wr;
1885
1886 switch (wr->iser_ib_op) {
1887 case ISER_IB_RECV:
1888 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1889 dump_stack();
1890 break;
1891 case ISER_IB_SEND:
1892 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1893 isert_response_completion(tx_desc, isert_cmd,
1894 isert_conn, ib_dev);
1895 break;
1896 case ISER_IB_RDMA_WRITE:
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001897 pr_debug("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001898 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Sagi Grimbergf93f3a72014-02-19 17:50:24 +02001899 isert_completion_rdma_write(tx_desc, isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001900 break;
1901 case ISER_IB_RDMA_READ:
1902 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1903
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001904 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001905 isert_completion_rdma_read(tx_desc, isert_cmd);
1906 break;
1907 default:
1908 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1909 dump_stack();
1910 break;
1911 }
1912}
1913
1914static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001915isert_send_completion(struct iser_tx_desc *tx_desc,
1916 struct isert_conn *isert_conn)
1917{
1918 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1919 struct iser_tx_desc *t;
1920 /*
1921 * Drain coalesced completion llist starting from comp_llnode_batch
1922 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1923 */
1924 while (llnode) {
1925 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1926 llnode = llist_next(llnode);
1927 __isert_send_completion(t, isert_conn);
1928 }
1929 __isert_send_completion(tx_desc, isert_conn);
1930}
1931
1932static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001933isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1934{
1935 struct llist_node *llnode;
1936 struct isert_rdma_wr *wr;
1937 struct iser_tx_desc *t;
1938
1939 mutex_lock(&isert_conn->conn_mutex);
1940 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1941 isert_conn->conn_comp_batch = 0;
1942 mutex_unlock(&isert_conn->conn_mutex);
1943
1944 while (llnode) {
1945 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1946 llnode = llist_next(llnode);
1947 wr = &t->isert_cmd->rdma_wr;
1948
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001949 /**
1950 * If send_wr_num is 0 this means that we got
1951 * RDMA completion and we cleared it and we should
1952 * simply decrement the response post. else the
1953 * response is incorporated in send_wr_num, just
1954 * sub it.
1955 **/
1956 if (wr->send_wr_num)
1957 atomic_sub(wr->send_wr_num,
1958 &isert_conn->post_send_buf_count);
1959 else
1960 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001961
1962 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001963 }
1964}
1965
1966static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001967isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001968{
1969 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001970 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001971 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1972 struct isert_rdma_wr *wr;
1973 struct iser_tx_desc *t;
1974
1975 while (llnode) {
1976 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1977 llnode = llist_next(llnode);
1978 wr = &t->isert_cmd->rdma_wr;
1979
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02001980 /**
1981 * If send_wr_num is 0 this means that we got
1982 * RDMA completion and we cleared it and we should
1983 * simply decrement the response post. else the
1984 * response is incorporated in send_wr_num, just
1985 * sub it.
1986 **/
1987 if (wr->send_wr_num)
1988 atomic_sub(wr->send_wr_num,
1989 &isert_conn->post_send_buf_count);
1990 else
1991 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellinger03e78482014-03-30 15:50:03 -07001992
1993 isert_completion_put(t, t->isert_cmd, ib_dev, true);
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001994 }
1995 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001996
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001997 if (!isert_cmd)
1998 isert_unmap_tx_desc(tx_desc, ib_dev);
1999 else
Nicholas Bellinger03e78482014-03-30 15:50:03 -07002000 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002001}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002002
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002003static void
2004isert_cq_rx_comp_err(struct isert_conn *isert_conn)
2005{
2006 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2007 struct iscsi_conn *conn = isert_conn->conn;
2008
2009 if (isert_conn->post_recv_buf_count)
2010 return;
2011
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002012 isert_cq_drain_comp_llist(isert_conn, ib_dev);
2013
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002014 if (conn->sess) {
2015 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
2016 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002017 }
2018
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002019 while (atomic_read(&isert_conn->post_send_buf_count))
2020 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002021
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002022 mutex_lock(&isert_conn->conn_mutex);
2023 isert_conn->state = ISER_CONN_DOWN;
2024 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002025
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002026 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002027}
2028
2029static void
2030isert_cq_tx_work(struct work_struct *work)
2031{
2032 struct isert_cq_desc *cq_desc = container_of(work,
2033 struct isert_cq_desc, cq_tx_work);
2034 struct isert_device *device = cq_desc->device;
2035 int cq_index = cq_desc->cq_index;
2036 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
2037 struct isert_conn *isert_conn;
2038 struct iser_tx_desc *tx_desc;
2039 struct ib_wc wc;
2040
2041 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
2042 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
2043 isert_conn = wc.qp->qp_context;
2044
2045 if (wc.status == IB_WC_SUCCESS) {
2046 isert_send_completion(tx_desc, isert_conn);
2047 } else {
2048 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
2049 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002050 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002051
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002052 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08002053 if (tx_desc->llnode_active)
2054 continue;
2055
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002056 atomic_dec(&isert_conn->post_send_buf_count);
2057 isert_cq_tx_comp_err(tx_desc, isert_conn);
2058 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002059 }
2060 }
2061
2062 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
2063}
2064
2065static void
2066isert_cq_tx_callback(struct ib_cq *cq, void *context)
2067{
2068 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2069
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
2071}
2072
2073static void
2074isert_cq_rx_work(struct work_struct *work)
2075{
2076 struct isert_cq_desc *cq_desc = container_of(work,
2077 struct isert_cq_desc, cq_rx_work);
2078 struct isert_device *device = cq_desc->device;
2079 int cq_index = cq_desc->cq_index;
2080 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
2081 struct isert_conn *isert_conn;
2082 struct iser_rx_desc *rx_desc;
2083 struct ib_wc wc;
2084 unsigned long xfer_len;
2085
2086 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
2087 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
2088 isert_conn = wc.qp->qp_context;
2089
2090 if (wc.status == IB_WC_SUCCESS) {
2091 xfer_len = (unsigned long)wc.byte_len;
2092 isert_rx_completion(rx_desc, isert_conn, xfer_len);
2093 } else {
2094 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002095 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002096 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07002097 pr_debug("RX wc.vendor_err: 0x%08x\n",
2098 wc.vendor_err);
2099 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002101 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002102 }
2103 }
2104
2105 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
2106}
2107
2108static void
2109isert_cq_rx_callback(struct ib_cq *cq, void *context)
2110{
2111 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
2112
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002113 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
2114}
2115
2116static int
2117isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
2118{
2119 struct ib_send_wr *wr_failed;
2120 int ret;
2121
2122 atomic_inc(&isert_conn->post_send_buf_count);
2123
2124 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
2125 &wr_failed);
2126 if (ret) {
2127 pr_err("ib_post_send failed with %d\n", ret);
2128 atomic_dec(&isert_conn->post_send_buf_count);
2129 return ret;
2130 }
2131 return ret;
2132}
2133
2134static int
2135isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2136{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002137 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002138 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2139 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2140 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
2141 &isert_cmd->tx_desc.iscsi_header;
2142
2143 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2144 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
2145 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2146 /*
2147 * Attach SENSE DATA payload to iSCSI Response PDU
2148 */
2149 if (cmd->se_cmd.sense_buffer &&
2150 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2151 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2152 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2153 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002154 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002155
2156 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2157 cmd->sense_buffer);
2158 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2159
2160 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2161 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002162 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002163
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002164 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2165 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002166 DMA_TO_DEVICE);
2167
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002168 isert_cmd->pdu_buf_len = pdu_len;
2169 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2170 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002171 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2172 isert_cmd->tx_desc.num_sge = 2;
2173 }
2174
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002175 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002176
2177 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2178
2179 return isert_post_response(isert_conn, isert_cmd);
2180}
2181
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002182static void
2183isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2184{
2185 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2186 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2187 struct isert_device *device = isert_conn->conn_device;
2188
2189 spin_lock_bh(&conn->cmd_lock);
2190 if (!list_empty(&cmd->i_conn_node))
2191 list_del_init(&cmd->i_conn_node);
2192 spin_unlock_bh(&conn->cmd_lock);
2193
2194 if (cmd->data_direction == DMA_TO_DEVICE)
2195 iscsit_stop_dataout_timer(cmd);
2196
2197 device->unreg_rdma_mem(isert_cmd, isert_conn);
2198}
2199
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002200static enum target_prot_op
2201isert_get_sup_prot_ops(struct iscsi_conn *conn)
2202{
2203 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2204 struct isert_device *device = isert_conn->conn_device;
2205
2206 if (device->pi_capable)
2207 return TARGET_PROT_ALL;
2208
2209 return TARGET_PROT_NORMAL;
2210}
2211
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002212static int
2213isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2214 bool nopout_response)
2215{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002216 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002217 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2218 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2219
2220 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2221 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2222 &isert_cmd->tx_desc.iscsi_header,
2223 nopout_response);
2224 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002225 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002226
Masanari Iida8b513d02013-05-21 23:13:12 +09002227 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002228
2229 return isert_post_response(isert_conn, isert_cmd);
2230}
2231
2232static int
2233isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2234{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002235 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002236 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2237 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2238
2239 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2240 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2241 &isert_cmd->tx_desc.iscsi_header);
2242 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002243 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002244
2245 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2246
2247 return isert_post_response(isert_conn, isert_cmd);
2248}
2249
2250static int
2251isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2252{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002253 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002254 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2255 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2256
2257 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2258 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2259 &isert_cmd->tx_desc.iscsi_header);
2260 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002261 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002262
2263 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2264
2265 return isert_post_response(isert_conn, isert_cmd);
2266}
2267
2268static int
2269isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2270{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002271 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002272 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2273 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002274 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2275 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2276 struct iscsi_reject *hdr =
2277 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002278
2279 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002280 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002281 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002282
2283 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002284 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002285 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2286 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002287 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2288 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002289 tx_dsg->length = ISCSI_HDR_LEN;
2290 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2291 isert_cmd->tx_desc.num_sge = 2;
2292
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002293 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002294
2295 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2296
2297 return isert_post_response(isert_conn, isert_cmd);
2298}
2299
2300static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002301isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2302{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002303 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002304 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2305 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2306 struct iscsi_text_rsp *hdr =
2307 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2308 u32 txt_rsp_len;
2309 int rc;
2310
2311 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2312 rc = iscsit_build_text_rsp(cmd, conn, hdr);
2313 if (rc < 0)
2314 return rc;
2315
2316 txt_rsp_len = rc;
2317 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2318
2319 if (txt_rsp_len) {
2320 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2321 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2322 void *txt_rsp_buf = cmd->buf_ptr;
2323
2324 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2325 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2326
2327 isert_cmd->pdu_buf_len = txt_rsp_len;
2328 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2329 tx_dsg->length = txt_rsp_len;
2330 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2331 isert_cmd->tx_desc.num_sge = 2;
2332 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002333 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002334
2335 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2336
2337 return isert_post_response(isert_conn, isert_cmd);
2338}
2339
2340static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002341isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2342 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2343 u32 data_left, u32 offset)
2344{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002345 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002346 struct scatterlist *sg_start, *tmp_sg;
2347 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2348 u32 sg_off, page_off;
2349 int i = 0, sg_nents;
2350
2351 sg_off = offset / PAGE_SIZE;
2352 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2353 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2354 page_off = offset % PAGE_SIZE;
2355
2356 send_wr->sg_list = ib_sge;
2357 send_wr->num_sge = sg_nents;
2358 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2359 /*
2360 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2361 */
2362 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2363 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2364 (unsigned long long)tmp_sg->dma_address,
2365 tmp_sg->length, page_off);
2366
2367 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2368 ib_sge->length = min_t(u32, data_left,
2369 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2370 ib_sge->lkey = isert_conn->conn_mr->lkey;
2371
Vu Pham90ecc6e2013-08-28 23:23:33 +03002372 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2373 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002374 page_off = 0;
2375 data_left -= ib_sge->length;
2376 ib_sge++;
2377 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2378 }
2379
2380 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2381 send_wr->sg_list, send_wr->num_sge);
2382
2383 return sg_nents;
2384}
2385
2386static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002387isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2388 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002389{
2390 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002391 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002392 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002393 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002394 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002395 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002396 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2397 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002399 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002401 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2402 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2403 se_cmd->t_data_nents, se_cmd->data_length,
2404 offset, wr->iser_ib_op, &wr->data);
2405 if (ret)
2406 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002407
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002408 data_left = data->len;
2409 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002410
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002411 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002413 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002414 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002415 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002416 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002417 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002418
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002419 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002420 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2421 GFP_KERNEL);
2422 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002423 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002424 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002425 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002426 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002427
2428 wr->isert_cmd = isert_cmd;
2429 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002430
2431 for (i = 0; i < wr->send_wr_num; i++) {
2432 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2433 data_len = min(data_left, rdma_write_max);
2434
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002435 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002436 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2437 send_wr->opcode = IB_WR_RDMA_WRITE;
2438 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2439 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2440 if (i + 1 == wr->send_wr_num)
2441 send_wr->next = &isert_cmd->tx_desc.send_wr;
2442 else
2443 send_wr->next = &wr->send_wr[i + 1];
2444 } else {
2445 send_wr->opcode = IB_WR_RDMA_READ;
2446 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2447 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2448 if (i + 1 == wr->send_wr_num)
2449 send_wr->send_flags = IB_SEND_SIGNALED;
2450 else
2451 send_wr->next = &wr->send_wr[i + 1];
2452 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002453
2454 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2455 send_wr, data_len, offset);
2456 ib_sge += ib_sge_cnt;
2457
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002458 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002459 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002460 data_left -= data_len;
2461 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002462
2463 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002464unmap_cmd:
2465 isert_unmap_data_buf(isert_conn, data);
2466
Vu Pham90ecc6e2013-08-28 23:23:33 +03002467 return ret;
2468}
2469
2470static int
Vu Pham59464ef2013-08-28 23:23:35 +03002471isert_map_fr_pagelist(struct ib_device *ib_dev,
2472 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2473{
2474 u64 start_addr, end_addr, page, chunk_start = 0;
2475 struct scatterlist *tmp_sg;
2476 int i = 0, new_chunk, last_ent, n_pages;
2477
2478 n_pages = 0;
2479 new_chunk = 1;
2480 last_ent = sg_nents - 1;
2481 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2482 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2483 if (new_chunk)
2484 chunk_start = start_addr;
2485 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2486
2487 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2488 i, (unsigned long long)tmp_sg->dma_address,
2489 tmp_sg->length);
2490
2491 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2492 new_chunk = 0;
2493 continue;
2494 }
2495 new_chunk = 1;
2496
2497 page = chunk_start & PAGE_MASK;
2498 do {
2499 fr_pl[n_pages++] = page;
2500 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2501 n_pages - 1, page);
2502 page += PAGE_SIZE;
2503 } while (page < end_addr);
2504 }
2505
2506 return n_pages;
2507}
2508
2509static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002510isert_fast_reg_mr(struct isert_conn *isert_conn,
2511 struct fast_reg_descriptor *fr_desc,
2512 struct isert_data_buf *mem,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002513 enum isert_indicator ind,
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002514 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002515{
Vu Pham59464ef2013-08-28 23:23:35 +03002516 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002517 struct ib_mr *mr;
2518 struct ib_fast_reg_page_list *frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002519 struct ib_send_wr fr_wr, inv_wr;
2520 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002521 int ret, pagelist_len;
2522 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002523 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002524
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002525 if (mem->dma_nents == 1) {
2526 sge->lkey = isert_conn->conn_mr->lkey;
2527 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2528 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002529 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2530 __func__, __LINE__, sge->addr, sge->length,
2531 sge->lkey);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002532 return 0;
2533 }
2534
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002535 if (ind == ISERT_DATA_KEY_VALID) {
2536 /* Registering data buffer */
2537 mr = fr_desc->data_mr;
2538 frpl = fr_desc->data_frpl;
2539 } else {
2540 /* Registering protection buffer */
2541 mr = fr_desc->pi_ctx->prot_mr;
2542 frpl = fr_desc->pi_ctx->prot_frpl;
2543 }
2544
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002545 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002546
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002547 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002548 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002549
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002550 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002551 &frpl->page_list[0]);
Vu Pham59464ef2013-08-28 23:23:35 +03002552
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002553 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002554 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002555 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002556 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002557 inv_wr.ex.invalidate_rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002558 wr = &inv_wr;
2559 /* Bump the key */
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002560 key = (u8)(mr->rkey & 0x000000FF);
2561 ib_update_fast_reg_key(mr, ++key);
Vu Pham59464ef2013-08-28 23:23:35 +03002562 }
2563
2564 /* Prepare FASTREG WR */
2565 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002566 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002567 fr_wr.opcode = IB_WR_FAST_REG_MR;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002568 fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
2569 fr_wr.wr.fast_reg.page_list = frpl;
Vu Pham59464ef2013-08-28 23:23:35 +03002570 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2571 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002572 fr_wr.wr.fast_reg.length = mem->len;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002573 fr_wr.wr.fast_reg.rkey = mr->rkey;
Vu Pham59464ef2013-08-28 23:23:35 +03002574 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2575
2576 if (!wr)
2577 wr = &fr_wr;
2578 else
2579 wr->next = &fr_wr;
2580
2581 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2582 if (ret) {
2583 pr_err("fast registration failed, ret:%d\n", ret);
2584 return ret;
2585 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002586 fr_desc->ind &= ~ind;
Vu Pham59464ef2013-08-28 23:23:35 +03002587
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002588 sge->lkey = mr->lkey;
2589 sge->addr = frpl->page_list[0] + page_off;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002590 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002591
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002592 pr_debug("%s:%d sge: addr: 0x%llx length: %u lkey: %x\n",
2593 __func__, __LINE__, sge->addr, sge->length,
2594 sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002595
2596 return ret;
2597}
2598
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002599static inline enum ib_t10_dif_type
2600se2ib_prot_type(enum target_prot_type prot_type)
2601{
2602 switch (prot_type) {
2603 case TARGET_DIF_TYPE0_PROT:
2604 return IB_T10DIF_NONE;
2605 case TARGET_DIF_TYPE1_PROT:
2606 return IB_T10DIF_TYPE1;
2607 case TARGET_DIF_TYPE2_PROT:
2608 return IB_T10DIF_TYPE2;
2609 case TARGET_DIF_TYPE3_PROT:
2610 return IB_T10DIF_TYPE3;
2611 default:
2612 return IB_T10DIF_NONE;
2613 }
2614}
2615
2616static int
2617isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
2618{
2619 enum ib_t10_dif_type ib_prot_type = se2ib_prot_type(se_cmd->prot_type);
2620
2621 sig_attrs->mem.sig_type = IB_SIG_TYPE_T10_DIF;
2622 sig_attrs->wire.sig_type = IB_SIG_TYPE_T10_DIF;
2623 sig_attrs->mem.sig.dif.pi_interval =
2624 se_cmd->se_dev->dev_attrib.block_size;
2625 sig_attrs->wire.sig.dif.pi_interval =
2626 se_cmd->se_dev->dev_attrib.block_size;
2627
2628 switch (se_cmd->prot_op) {
2629 case TARGET_PROT_DIN_INSERT:
2630 case TARGET_PROT_DOUT_STRIP:
2631 sig_attrs->mem.sig.dif.type = IB_T10DIF_NONE;
2632 sig_attrs->wire.sig.dif.type = ib_prot_type;
2633 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2634 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2635 break;
2636 case TARGET_PROT_DOUT_INSERT:
2637 case TARGET_PROT_DIN_STRIP:
2638 sig_attrs->mem.sig.dif.type = ib_prot_type;
2639 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2640 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2641 sig_attrs->wire.sig.dif.type = IB_T10DIF_NONE;
2642 break;
2643 case TARGET_PROT_DIN_PASS:
2644 case TARGET_PROT_DOUT_PASS:
2645 sig_attrs->mem.sig.dif.type = ib_prot_type;
2646 sig_attrs->mem.sig.dif.bg_type = IB_T10DIF_CRC;
2647 sig_attrs->mem.sig.dif.ref_tag = se_cmd->reftag_seed;
2648 sig_attrs->wire.sig.dif.type = ib_prot_type;
2649 sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
2650 sig_attrs->wire.sig.dif.ref_tag = se_cmd->reftag_seed;
2651 break;
2652 default:
2653 pr_err("Unsupported PI operation %d\n", se_cmd->prot_op);
2654 return -EINVAL;
2655 }
2656
2657 return 0;
2658}
2659
2660static inline u8
2661isert_set_prot_checks(u8 prot_checks)
2662{
2663 return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
2664 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
2665 (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
2666}
2667
2668static int
2669isert_reg_sig_mr(struct isert_conn *isert_conn, struct se_cmd *se_cmd,
2670 struct fast_reg_descriptor *fr_desc,
2671 struct ib_sge *data_sge, struct ib_sge *prot_sge,
2672 struct ib_sge *sig_sge)
2673{
2674 struct ib_send_wr sig_wr, inv_wr;
2675 struct ib_send_wr *bad_wr, *wr = NULL;
2676 struct pi_context *pi_ctx = fr_desc->pi_ctx;
2677 struct ib_sig_attrs sig_attrs;
2678 int ret;
2679 u32 key;
2680
2681 memset(&sig_attrs, 0, sizeof(sig_attrs));
2682 ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
2683 if (ret)
2684 goto err;
2685
2686 sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
2687
2688 if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
2689 memset(&inv_wr, 0, sizeof(inv_wr));
2690 inv_wr.opcode = IB_WR_LOCAL_INV;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002691 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002692 inv_wr.ex.invalidate_rkey = pi_ctx->sig_mr->rkey;
2693 wr = &inv_wr;
2694 /* Bump the key */
2695 key = (u8)(pi_ctx->sig_mr->rkey & 0x000000FF);
2696 ib_update_fast_reg_key(pi_ctx->sig_mr, ++key);
2697 }
2698
2699 memset(&sig_wr, 0, sizeof(sig_wr));
2700 sig_wr.opcode = IB_WR_REG_SIG_MR;
Sagi Grimbergc2caa202014-03-17 12:52:16 +02002701 sig_wr.wr_id = ISER_FASTREG_LI_WRID;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002702 sig_wr.sg_list = data_sge;
2703 sig_wr.num_sge = 1;
2704 sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
2705 sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
2706 sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
2707 if (se_cmd->t_prot_sg)
2708 sig_wr.wr.sig_handover.prot = prot_sge;
2709
2710 if (!wr)
2711 wr = &sig_wr;
2712 else
2713 wr->next = &sig_wr;
2714
2715 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2716 if (ret) {
2717 pr_err("fast registration failed, ret:%d\n", ret);
2718 goto err;
2719 }
2720 fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
2721
2722 sig_sge->lkey = pi_ctx->sig_mr->lkey;
2723 sig_sge->addr = 0;
2724 sig_sge->length = se_cmd->data_length;
2725 if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
2726 se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
2727 /*
2728 * We have protection guards on the wire
2729 * so we need to set a larget transfer
2730 */
2731 sig_sge->length += se_cmd->prot_length;
2732
2733 pr_debug("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
2734 sig_sge->addr, sig_sge->length,
2735 sig_sge->lkey);
2736err:
2737 return ret;
2738}
2739
Vu Pham59464ef2013-08-28 23:23:35 +03002740static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002741isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2742 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002743{
2744 struct se_cmd *se_cmd = &cmd->se_cmd;
2745 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002746 struct isert_conn *isert_conn = conn->context;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002747 struct ib_sge data_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002748 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002749 struct fast_reg_descriptor *fr_desc = NULL;
2750 u32 offset;
2751 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002752 unsigned long flags;
2753
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002754 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2755
2756 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2757 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2758 se_cmd->t_data_nents, se_cmd->data_length,
2759 offset, wr->iser_ib_op, &wr->data);
2760 if (ret)
2761 return ret;
2762
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002763 if (wr->data.dma_nents != 1 ||
2764 se_cmd->prot_op != TARGET_PROT_NORMAL) {
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002765 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2766 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2767 struct fast_reg_descriptor, list);
2768 list_del(&fr_desc->list);
2769 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2770 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002771 }
2772
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002773 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
2774 ISERT_DATA_KEY_VALID, &data_sge);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002775 if (ret)
2776 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002777
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002778 if (se_cmd->prot_op != TARGET_PROT_NORMAL) {
2779 struct ib_sge prot_sge, sig_sge;
2780
2781 if (se_cmd->t_prot_sg) {
2782 ret = isert_map_data_buf(isert_conn, isert_cmd,
2783 se_cmd->t_prot_sg,
2784 se_cmd->t_prot_nents,
2785 se_cmd->prot_length,
2786 0, wr->iser_ib_op, &wr->prot);
2787 if (ret)
2788 goto unmap_cmd;
2789
2790 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->prot,
2791 ISERT_PROT_KEY_VALID, &prot_sge);
2792 if (ret)
2793 goto unmap_prot_cmd;
2794 }
2795
2796 ret = isert_reg_sig_mr(isert_conn, se_cmd, fr_desc,
2797 &data_sge, &prot_sge, &sig_sge);
2798 if (ret)
2799 goto unmap_prot_cmd;
2800
2801 fr_desc->ind |= ISERT_PROTECTED;
2802 memcpy(&wr->s_ib_sge, &sig_sge, sizeof(sig_sge));
2803 } else
2804 memcpy(&wr->s_ib_sge, &data_sge, sizeof(data_sge));
2805
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002806 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002807 wr->send_wr_num = 1;
2808 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2809 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002810 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002811
2812 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002813 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002814 send_wr->num_sge = 1;
2815 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2816 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2817 send_wr->opcode = IB_WR_RDMA_WRITE;
2818 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2819 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002820 send_wr->send_flags = se_cmd->prot_op == TARGET_PROT_NORMAL ?
2821 0 : IB_SEND_SIGNALED;
Vu Pham59464ef2013-08-28 23:23:35 +03002822 } else {
2823 send_wr->opcode = IB_WR_RDMA_READ;
2824 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2825 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2826 send_wr->send_flags = IB_SEND_SIGNALED;
2827 }
2828
Vu Pham59464ef2013-08-28 23:23:35 +03002829 return 0;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002830unmap_prot_cmd:
2831 if (se_cmd->t_prot_sg)
2832 isert_unmap_data_buf(isert_conn, &wr->prot);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002833unmap_cmd:
2834 if (fr_desc) {
2835 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2836 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2837 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2838 }
2839 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002840
Vu Pham59464ef2013-08-28 23:23:35 +03002841 return ret;
2842}
2843
2844static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002845isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2846{
2847 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002848 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002849 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2850 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002851 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002852 struct ib_send_wr *wr_failed;
2853 int rc;
2854
2855 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2856 isert_cmd, se_cmd->data_length);
2857 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002858 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002859 if (rc) {
2860 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2861 return rc;
2862 }
2863
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002864 if (se_cmd->prot_op == TARGET_PROT_NORMAL) {
2865 /*
2866 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2867 */
2868 isert_create_send_desc(isert_conn, isert_cmd,
2869 &isert_cmd->tx_desc);
2870 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
2871 &isert_cmd->tx_desc.iscsi_header);
2872 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2873 isert_init_send_wr(isert_conn, isert_cmd,
2874 &isert_cmd->tx_desc.send_wr, true);
2875 isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002876 wr->send_wr_num += 1;
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002877 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002878
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002879 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002880
2881 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2882 if (rc) {
2883 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Sagi Grimberg897bb2c2014-03-17 12:52:17 +02002884 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002885 }
Sagi Grimberg9e961ae2014-02-19 17:50:25 +02002886
2887 if (se_cmd->prot_op == TARGET_PROT_NORMAL)
2888 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
2889 "READ\n", isert_cmd);
2890 else
2891 pr_debug("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
2892 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002893
Vu Pham90ecc6e2013-08-28 23:23:33 +03002894 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002895}
2896
2897static int
2898isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2899{
2900 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002901 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002902 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2903 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002904 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002905 struct ib_send_wr *wr_failed;
2906 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002907
Vu Pham90ecc6e2013-08-28 23:23:33 +03002908 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2909 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002910 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002911 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002912 if (rc) {
2913 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2914 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002915 }
2916
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002917 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002918
2919 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2920 if (rc) {
2921 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002922 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002923 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002924 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2925 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002926
Vu Pham90ecc6e2013-08-28 23:23:33 +03002927 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002928}
2929
2930static int
2931isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2932{
2933 int ret;
2934
2935 switch (state) {
2936 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2937 ret = isert_put_nopin(cmd, conn, false);
2938 break;
2939 default:
2940 pr_err("Unknown immediate state: 0x%02x\n", state);
2941 ret = -EINVAL;
2942 break;
2943 }
2944
2945 return ret;
2946}
2947
2948static int
2949isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2950{
2951 int ret;
2952
2953 switch (state) {
2954 case ISTATE_SEND_LOGOUTRSP:
2955 ret = isert_put_logout_rsp(cmd, conn);
2956 if (!ret) {
2957 pr_debug("Returning iSER Logout -EAGAIN\n");
2958 ret = -EAGAIN;
2959 }
2960 break;
2961 case ISTATE_SEND_NOPIN:
2962 ret = isert_put_nopin(cmd, conn, true);
2963 break;
2964 case ISTATE_SEND_TASKMGTRSP:
2965 ret = isert_put_tm_rsp(cmd, conn);
2966 break;
2967 case ISTATE_SEND_REJECT:
2968 ret = isert_put_reject(cmd, conn);
2969 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002970 case ISTATE_SEND_TEXTRSP:
2971 ret = isert_put_text_rsp(cmd, conn);
2972 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002973 case ISTATE_SEND_STATUS:
2974 /*
2975 * Special case for sending non GOOD SCSI status from TX thread
2976 * context during pre se_cmd excecution failure.
2977 */
2978 ret = isert_put_response(conn, cmd);
2979 break;
2980 default:
2981 pr_err("Unknown response state: 0x%02x\n", state);
2982 ret = -EINVAL;
2983 break;
2984 }
2985
2986 return ret;
2987}
2988
2989static int
2990isert_setup_np(struct iscsi_np *np,
2991 struct __kernel_sockaddr_storage *ksockaddr)
2992{
2993 struct isert_np *isert_np;
2994 struct rdma_cm_id *isert_lid;
2995 struct sockaddr *sa;
2996 int ret;
2997
2998 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2999 if (!isert_np) {
3000 pr_err("Unable to allocate struct isert_np\n");
3001 return -ENOMEM;
3002 }
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003003 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003004 mutex_init(&isert_np->np_accept_mutex);
3005 INIT_LIST_HEAD(&isert_np->np_accept_list);
3006 init_completion(&isert_np->np_login_comp);
3007
3008 sa = (struct sockaddr *)ksockaddr;
3009 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
3010 /*
3011 * Setup the np->np_sockaddr from the passed sockaddr setup
3012 * in iscsi_target_configfs.c code..
3013 */
3014 memcpy(&np->np_sockaddr, ksockaddr,
3015 sizeof(struct __kernel_sockaddr_storage));
3016
3017 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
3018 IB_QPT_RC);
3019 if (IS_ERR(isert_lid)) {
3020 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
3021 PTR_ERR(isert_lid));
3022 ret = PTR_ERR(isert_lid);
3023 goto out;
3024 }
3025
3026 ret = rdma_bind_addr(isert_lid, sa);
3027 if (ret) {
3028 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
3029 goto out_lid;
3030 }
3031
3032 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
3033 if (ret) {
3034 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
3035 goto out_lid;
3036 }
3037
3038 isert_np->np_cm_id = isert_lid;
3039 np->np_context = isert_np;
3040 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
3041
3042 return 0;
3043
3044out_lid:
3045 rdma_destroy_id(isert_lid);
3046out:
3047 kfree(isert_np);
3048 return ret;
3049}
3050
3051static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003052isert_rdma_accept(struct isert_conn *isert_conn)
3053{
3054 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3055 struct rdma_conn_param cp;
3056 int ret;
3057
3058 memset(&cp, 0, sizeof(struct rdma_conn_param));
3059 cp.responder_resources = isert_conn->responder_resources;
3060 cp.initiator_depth = isert_conn->initiator_depth;
3061 cp.retry_count = 7;
3062 cp.rnr_retry_count = 7;
3063
3064 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
3065
3066 ret = rdma_accept(cm_id, &cp);
3067 if (ret) {
3068 pr_err("rdma_accept() failed with: %d\n", ret);
3069 return ret;
3070 }
3071
3072 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
3073
3074 return 0;
3075}
3076
3077static int
3078isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
3079{
3080 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
3081 int ret;
3082
3083 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07003084 /*
3085 * For login requests after the first PDU, isert_rx_login_req() will
3086 * kick schedule_delayed_work(&conn->login_work) as the packet is
3087 * received, which turns this callback from iscsi_target_do_login_rx()
3088 * into a NOP.
3089 */
3090 if (!login->first_request)
3091 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003092
3093 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
3094 if (ret)
3095 return ret;
3096
3097 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
3098 return 0;
3099}
3100
3101static void
3102isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
3103 struct isert_conn *isert_conn)
3104{
3105 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
3106 struct rdma_route *cm_route = &cm_id->route;
3107 struct sockaddr_in *sock_in;
3108 struct sockaddr_in6 *sock_in6;
3109
3110 conn->login_family = np->np_sockaddr.ss_family;
3111
3112 if (np->np_sockaddr.ss_family == AF_INET6) {
3113 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
3114 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
3115 &sock_in6->sin6_addr.in6_u);
3116 conn->login_port = ntohs(sock_in6->sin6_port);
3117
3118 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
3119 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
3120 &sock_in6->sin6_addr.in6_u);
3121 conn->local_port = ntohs(sock_in6->sin6_port);
3122 } else {
3123 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
3124 sprintf(conn->login_ip, "%pI4",
3125 &sock_in->sin_addr.s_addr);
3126 conn->login_port = ntohs(sock_in->sin_port);
3127
3128 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
3129 sprintf(conn->local_ip, "%pI4",
3130 &sock_in->sin_addr.s_addr);
3131 conn->local_port = ntohs(sock_in->sin_port);
3132 }
3133}
3134
3135static int
3136isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
3137{
3138 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3139 struct isert_conn *isert_conn;
3140 int max_accept = 0, ret;
3141
3142accept_wait:
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003143 ret = down_interruptible(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003144 if (max_accept > 5)
3145 return -ENODEV;
3146
3147 spin_lock_bh(&np->np_thread_lock);
3148 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
3149 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg531b7bf2014-04-29 13:13:45 +03003150 pr_debug("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003151 return -ENODEV;
3152 }
3153 spin_unlock_bh(&np->np_thread_lock);
3154
3155 mutex_lock(&isert_np->np_accept_mutex);
3156 if (list_empty(&isert_np->np_accept_list)) {
3157 mutex_unlock(&isert_np->np_accept_mutex);
3158 max_accept++;
3159 goto accept_wait;
3160 }
3161 isert_conn = list_first_entry(&isert_np->np_accept_list,
3162 struct isert_conn, conn_accept_node);
3163 list_del_init(&isert_conn->conn_accept_node);
3164 mutex_unlock(&isert_np->np_accept_mutex);
3165
3166 conn->context = isert_conn;
3167 isert_conn->conn = conn;
3168 max_accept = 0;
3169
3170 ret = isert_rdma_post_recvl(isert_conn);
3171 if (ret)
3172 return ret;
3173
3174 ret = isert_rdma_accept(isert_conn);
3175 if (ret)
3176 return ret;
3177
3178 isert_set_conn_info(np, conn, isert_conn);
3179
3180 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
3181 return 0;
3182}
3183
3184static void
3185isert_free_np(struct iscsi_np *np)
3186{
3187 struct isert_np *isert_np = (struct isert_np *)np->np_context;
3188
3189 rdma_destroy_id(isert_np->np_cm_id);
3190
3191 np->np_context = NULL;
3192 kfree(isert_np);
3193}
3194
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003195static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003196{
3197 struct isert_conn *isert_conn = conn->context;
3198
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003199 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003200 /*
3201 * Decrement post_send_buf_count for special case when called
3202 * from isert_do_control_comp() -> iscsit_logout_post_handler()
3203 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003204 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003205 if (isert_conn->logout_posted)
3206 atomic_dec(&isert_conn->post_send_buf_count);
3207
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003208 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003209 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003210 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003211 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003212 /*
3213 * Only wait for conn_wait_comp_err if the isert_conn made it
3214 * into full feature phase..
3215 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003216 if (isert_conn->state == ISER_CONN_INIT) {
3217 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003218 return;
3219 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003220 if (isert_conn->state == ISER_CONN_UP)
3221 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07003222 mutex_unlock(&isert_conn->conn_mutex);
3223
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003224 wait_for_completion(&isert_conn->conn_wait_comp_err);
3225
3226 wait_for_completion(&isert_conn->conn_wait);
3227}
3228
3229static void isert_free_conn(struct iscsi_conn *conn)
3230{
3231 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003232
3233 isert_put_conn(isert_conn);
3234}
3235
3236static struct iscsit_transport iser_target_transport = {
3237 .name = "IB/iSER",
3238 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07003239 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003240 .owner = THIS_MODULE,
3241 .iscsit_setup_np = isert_setup_np,
3242 .iscsit_accept_np = isert_accept_np,
3243 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08003244 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003245 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003246 .iscsit_get_login_rx = isert_get_login_rx,
3247 .iscsit_put_login_tx = isert_put_login_tx,
3248 .iscsit_immediate_queue = isert_immediate_queue,
3249 .iscsit_response_queue = isert_response_queue,
3250 .iscsit_get_dataout = isert_get_dataout,
3251 .iscsit_queue_data_in = isert_put_datain,
3252 .iscsit_queue_status = isert_put_response,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003253 .iscsit_aborted_task = isert_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07003254 .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003255};
3256
3257static int __init isert_init(void)
3258{
3259 int ret;
3260
3261 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
3262 if (!isert_rx_wq) {
3263 pr_err("Unable to allocate isert_rx_wq\n");
3264 return -ENOMEM;
3265 }
3266
3267 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
3268 if (!isert_comp_wq) {
3269 pr_err("Unable to allocate isert_comp_wq\n");
3270 ret = -ENOMEM;
3271 goto destroy_rx_wq;
3272 }
3273
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003274 iscsit_register_transport(&iser_target_transport);
3275 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
3276 return 0;
3277
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003278destroy_rx_wq:
3279 destroy_workqueue(isert_rx_wq);
3280 return ret;
3281}
3282
3283static void __exit isert_exit(void)
3284{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08003285 destroy_workqueue(isert_comp_wq);
3286 destroy_workqueue(isert_rx_wq);
3287 iscsit_unregister_transport(&iser_target_transport);
3288 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
3289}
3290
3291MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
3292MODULE_VERSION("0.1");
3293MODULE_AUTHOR("nab@Linux-iSCSI.org");
3294MODULE_LICENSE("GPL");
3295
3296module_init(isert_init);
3297module_exit(isert_exit);