blob: 72063147933cdbc7e8e89d785bf24fb235219aa8 [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>
31
32#include "isert_proto.h"
33#include "ib_isert.h"
34
35#define ISERT_MAX_CONN 8
36#define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
37#define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
38
39static DEFINE_MUTEX(device_list_mutex);
40static LIST_HEAD(device_list);
41static struct workqueue_struct *isert_rx_wq;
42static struct workqueue_struct *isert_comp_wq;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080043
44static void
Vu Phamd40945d2013-08-28 23:23:34 +030045isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
46static int
47isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
48 struct isert_rdma_wr *wr);
Vu Pham59464ef2013-08-28 23:23:35 +030049static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +020050isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +030051static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +020052isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
53 struct isert_rdma_wr *wr);
Vu Phamd40945d2013-08-28 23:23:34 +030054
55static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080056isert_qp_event_callback(struct ib_event *e, void *context)
57{
58 struct isert_conn *isert_conn = (struct isert_conn *)context;
59
60 pr_err("isert_qp_event_callback event: %d\n", e->event);
61 switch (e->event) {
62 case IB_EVENT_COMM_EST:
63 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
64 break;
65 case IB_EVENT_QP_LAST_WQE_REACHED:
66 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
67 break;
68 default:
69 break;
70 }
71}
72
73static int
74isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
75{
76 int ret;
77
78 ret = ib_query_device(ib_dev, devattr);
79 if (ret) {
80 pr_err("ib_query_device() failed: %d\n", ret);
81 return ret;
82 }
83 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
84 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
85
86 return 0;
87}
88
89static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +020090isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id,
91 u8 protection)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080092{
93 struct isert_device *device = isert_conn->conn_device;
94 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080095 int ret, index, min_index = 0;
96
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080097 mutex_lock(&device_list_mutex);
98 for (index = 0; index < device->cqs_used; index++)
99 if (device->cq_active_qps[index] <
100 device->cq_active_qps[min_index])
101 min_index = index;
102 device->cq_active_qps[min_index]++;
103 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
104 mutex_unlock(&device_list_mutex);
105
106 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
107 attr.event_handler = isert_qp_event_callback;
108 attr.qp_context = isert_conn;
109 attr.send_cq = device->dev_tx_cq[min_index];
110 attr.recv_cq = device->dev_rx_cq[min_index];
111 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
112 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
113 /*
114 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
115 * work-around for RDMA_READ..
116 */
Vu Pham59464ef2013-08-28 23:23:35 +0300117 attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800118 isert_conn->max_sge = attr.cap.max_send_sge;
119
120 attr.cap.max_recv_sge = 1;
121 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
122 attr.qp_type = IB_QPT_RC;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200123 if (protection)
124 attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800125
126 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
127 cma_id->device);
128 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
129 isert_conn->conn_pd->device);
130
131 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
132 if (ret) {
133 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
134 return ret;
135 }
136 isert_conn->conn_qp = cma_id->qp;
137 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
138
139 return 0;
140}
141
142static void
143isert_cq_event_callback(struct ib_event *e, void *context)
144{
145 pr_debug("isert_cq_event_callback event: %d\n", e->event);
146}
147
148static int
149isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
150{
151 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
152 struct iser_rx_desc *rx_desc;
153 struct ib_sge *rx_sg;
154 u64 dma_addr;
155 int i, j;
156
157 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
158 sizeof(struct iser_rx_desc), GFP_KERNEL);
159 if (!isert_conn->conn_rx_descs)
160 goto fail;
161
162 rx_desc = isert_conn->conn_rx_descs;
163
164 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
165 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
166 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
167 if (ib_dma_mapping_error(ib_dev, dma_addr))
168 goto dma_map_fail;
169
170 rx_desc->dma_addr = dma_addr;
171
172 rx_sg = &rx_desc->rx_sg;
173 rx_sg->addr = rx_desc->dma_addr;
174 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
175 rx_sg->lkey = isert_conn->conn_mr->lkey;
176 }
177
178 isert_conn->conn_rx_desc_head = 0;
179 return 0;
180
181dma_map_fail:
182 rx_desc = isert_conn->conn_rx_descs;
183 for (j = 0; j < i; j++, rx_desc++) {
184 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
185 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
186 }
187 kfree(isert_conn->conn_rx_descs);
188 isert_conn->conn_rx_descs = NULL;
189fail:
190 return -ENOMEM;
191}
192
193static void
194isert_free_rx_descriptors(struct isert_conn *isert_conn)
195{
196 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
197 struct iser_rx_desc *rx_desc;
198 int i;
199
200 if (!isert_conn->conn_rx_descs)
201 return;
202
203 rx_desc = isert_conn->conn_rx_descs;
204 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
205 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
206 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
207 }
208
209 kfree(isert_conn->conn_rx_descs);
210 isert_conn->conn_rx_descs = NULL;
211}
212
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800213static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800214static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800215static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800216static void isert_cq_rx_callback(struct ib_cq *, void *);
217
218static int
219isert_create_device_ib_res(struct isert_device *device)
220{
221 struct ib_device *ib_dev = device->ib_device;
222 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300223 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800224 int ret = 0, i, j;
225
Vu Pham59464ef2013-08-28 23:23:35 +0300226 dev_attr = &device->dev_attr;
227 ret = isert_query_device(ib_dev, dev_attr);
228 if (ret)
229 return ret;
230
Vu Phamd40945d2013-08-28 23:23:34 +0300231 /* asign function handlers */
Vu Pham59464ef2013-08-28 23:23:35 +0300232 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200233 device->use_fastreg = 1;
234 device->reg_rdma_mem = isert_reg_rdma;
235 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300236 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200237 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300238 device->reg_rdma_mem = isert_map_rdma;
239 device->unreg_rdma_mem = isert_unmap_cmd;
240 }
Vu Phamd40945d2013-08-28 23:23:34 +0300241
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200242 /* Check signature cap */
243 device->pi_capable = dev_attr->device_cap_flags &
244 IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
245
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800246 device->cqs_used = min_t(int, num_online_cpus(),
247 device->ib_device->num_comp_vectors);
248 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200249 pr_debug("Using %d CQs, device %s supports %d vectors support "
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200250 "Fast registration %d pi_capable %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251 device->cqs_used, device->ib_device->name,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200252 device->ib_device->num_comp_vectors, device->use_fastreg,
253 device->pi_capable);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800254 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
255 device->cqs_used, GFP_KERNEL);
256 if (!device->cq_desc) {
257 pr_err("Unable to allocate device->cq_desc\n");
258 return -ENOMEM;
259 }
260 cq_desc = device->cq_desc;
261
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800262 for (i = 0; i < device->cqs_used; i++) {
263 cq_desc[i].device = device;
264 cq_desc[i].cq_index = i;
265
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800266 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800267 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
268 isert_cq_rx_callback,
269 isert_cq_event_callback,
270 (void *)&cq_desc[i],
271 ISER_MAX_RX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800272 if (IS_ERR(device->dev_rx_cq[i])) {
273 ret = PTR_ERR(device->dev_rx_cq[i]);
274 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800275 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800276 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800277
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800278 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800279 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
280 isert_cq_tx_callback,
281 isert_cq_event_callback,
282 (void *)&cq_desc[i],
283 ISER_MAX_TX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800284 if (IS_ERR(device->dev_tx_cq[i])) {
285 ret = PTR_ERR(device->dev_tx_cq[i]);
286 device->dev_tx_cq[i] = NULL;
287 goto out_cq;
288 }
289
290 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
291 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800292 goto out_cq;
293
Wei Yongjun94a71112013-10-29 09:56:34 +0800294 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
295 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800296 goto out_cq;
297 }
298
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800299 return 0;
300
301out_cq:
302 for (j = 0; j < i; j++) {
303 cq_desc = &device->cq_desc[j];
304
305 if (device->dev_rx_cq[j]) {
306 cancel_work_sync(&cq_desc->cq_rx_work);
307 ib_destroy_cq(device->dev_rx_cq[j]);
308 }
309 if (device->dev_tx_cq[j]) {
310 cancel_work_sync(&cq_desc->cq_tx_work);
311 ib_destroy_cq(device->dev_tx_cq[j]);
312 }
313 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800314 kfree(device->cq_desc);
315
316 return ret;
317}
318
319static void
320isert_free_device_ib_res(struct isert_device *device)
321{
322 struct isert_cq_desc *cq_desc;
323 int i;
324
325 for (i = 0; i < device->cqs_used; i++) {
326 cq_desc = &device->cq_desc[i];
327
328 cancel_work_sync(&cq_desc->cq_rx_work);
329 cancel_work_sync(&cq_desc->cq_tx_work);
330 ib_destroy_cq(device->dev_rx_cq[i]);
331 ib_destroy_cq(device->dev_tx_cq[i]);
332 device->dev_rx_cq[i] = NULL;
333 device->dev_tx_cq[i] = NULL;
334 }
335
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800336 kfree(device->cq_desc);
337}
338
339static void
340isert_device_try_release(struct isert_device *device)
341{
342 mutex_lock(&device_list_mutex);
343 device->refcount--;
344 if (!device->refcount) {
345 isert_free_device_ib_res(device);
346 list_del(&device->dev_node);
347 kfree(device);
348 }
349 mutex_unlock(&device_list_mutex);
350}
351
352static struct isert_device *
353isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
354{
355 struct isert_device *device;
356 int ret;
357
358 mutex_lock(&device_list_mutex);
359 list_for_each_entry(device, &device_list, dev_node) {
360 if (device->ib_device->node_guid == cma_id->device->node_guid) {
361 device->refcount++;
362 mutex_unlock(&device_list_mutex);
363 return device;
364 }
365 }
366
367 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
368 if (!device) {
369 mutex_unlock(&device_list_mutex);
370 return ERR_PTR(-ENOMEM);
371 }
372
373 INIT_LIST_HEAD(&device->dev_node);
374
375 device->ib_device = cma_id->device;
376 ret = isert_create_device_ib_res(device);
377 if (ret) {
378 kfree(device);
379 mutex_unlock(&device_list_mutex);
380 return ERR_PTR(ret);
381 }
382
383 device->refcount++;
384 list_add_tail(&device->dev_node, &device_list);
385 mutex_unlock(&device_list_mutex);
386
387 return device;
388}
389
Vu Pham59464ef2013-08-28 23:23:35 +0300390static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200391isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300392{
393 struct fast_reg_descriptor *fr_desc, *tmp;
394 int i = 0;
395
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200396 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300397 return;
398
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200399 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300400
401 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200402 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300403 list_del(&fr_desc->list);
404 ib_free_fast_reg_page_list(fr_desc->data_frpl);
405 ib_dereg_mr(fr_desc->data_mr);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200406 if (fr_desc->pi_ctx) {
407 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
408 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
409 ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
410 kfree(fr_desc->pi_ctx);
411 }
Vu Pham59464ef2013-08-28 23:23:35 +0300412 kfree(fr_desc);
413 ++i;
414 }
415
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200416 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300417 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200418 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300419}
420
421static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200422isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200423 struct fast_reg_descriptor *fr_desc, u8 protection)
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200424{
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200425 int ret;
426
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200427 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
428 ISCSI_ISER_SG_TABLESIZE);
429 if (IS_ERR(fr_desc->data_frpl)) {
430 pr_err("Failed to allocate data frpl err=%ld\n",
431 PTR_ERR(fr_desc->data_frpl));
432 return PTR_ERR(fr_desc->data_frpl);
433 }
434
435 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
436 if (IS_ERR(fr_desc->data_mr)) {
437 pr_err("Failed to allocate data frmr err=%ld\n",
438 PTR_ERR(fr_desc->data_mr));
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200439 ret = PTR_ERR(fr_desc->data_mr);
440 goto err_data_frpl;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200441 }
442 pr_debug("Create fr_desc %p page_list %p\n",
443 fr_desc, fr_desc->data_frpl->page_list);
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200444 fr_desc->ind |= ISERT_DATA_KEY_VALID;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200445
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200446 if (protection) {
447 struct ib_mr_init_attr mr_init_attr = {0};
448 struct pi_context *pi_ctx;
449
450 fr_desc->pi_ctx = kzalloc(sizeof(*fr_desc->pi_ctx), GFP_KERNEL);
451 if (!fr_desc->pi_ctx) {
452 pr_err("Failed to allocate pi context\n");
453 ret = -ENOMEM;
454 goto err_data_mr;
455 }
456 pi_ctx = fr_desc->pi_ctx;
457
458 pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
459 ISCSI_ISER_SG_TABLESIZE);
460 if (IS_ERR(pi_ctx->prot_frpl)) {
461 pr_err("Failed to allocate prot frpl err=%ld\n",
462 PTR_ERR(pi_ctx->prot_frpl));
463 ret = PTR_ERR(pi_ctx->prot_frpl);
464 goto err_pi_ctx;
465 }
466
467 pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
468 if (IS_ERR(pi_ctx->prot_mr)) {
469 pr_err("Failed to allocate prot frmr err=%ld\n",
470 PTR_ERR(pi_ctx->prot_mr));
471 ret = PTR_ERR(pi_ctx->prot_mr);
472 goto err_prot_frpl;
473 }
474 fr_desc->ind |= ISERT_PROT_KEY_VALID;
475
476 mr_init_attr.max_reg_descriptors = 2;
477 mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
478 pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
479 if (IS_ERR(pi_ctx->sig_mr)) {
480 pr_err("Failed to allocate signature enabled mr err=%ld\n",
481 PTR_ERR(pi_ctx->sig_mr));
482 ret = PTR_ERR(pi_ctx->sig_mr);
483 goto err_prot_mr;
484 }
485 fr_desc->ind |= ISERT_SIG_KEY_VALID;
486 }
487 fr_desc->ind &= ~ISERT_PROTECTED;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200488
489 return 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200490err_prot_mr:
491 ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
492err_prot_frpl:
493 ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
494err_pi_ctx:
495 kfree(fr_desc->pi_ctx);
496err_data_mr:
497 ib_dereg_mr(fr_desc->data_mr);
498err_data_frpl:
499 ib_free_fast_reg_page_list(fr_desc->data_frpl);
500
501 return ret;
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200502}
503
504static int
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200505isert_conn_create_fastreg_pool(struct isert_conn *isert_conn, u8 pi_support)
Vu Pham59464ef2013-08-28 23:23:35 +0300506{
507 struct fast_reg_descriptor *fr_desc;
508 struct isert_device *device = isert_conn->conn_device;
509 int i, ret;
510
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200511 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
512 isert_conn->conn_fr_pool_size = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300513 for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
514 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
515 if (!fr_desc) {
516 pr_err("Failed to allocate fast_reg descriptor\n");
517 ret = -ENOMEM;
518 goto err;
519 }
520
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200521 ret = isert_create_fr_desc(device->ib_device,
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200522 isert_conn->conn_pd, fr_desc,
523 pi_support);
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200524 if (ret) {
525 pr_err("Failed to create fastreg descriptor err=%d\n",
526 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800527 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300528 goto err;
529 }
530
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200531 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
532 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300533 }
534
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200535 pr_debug("Creating conn %p fastreg pool size=%d",
536 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300537
538 return 0;
539
540err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200541 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300542 return ret;
543}
544
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800545static int
546isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
547{
548 struct iscsi_np *np = cma_id->context;
549 struct isert_np *isert_np = np->np_context;
550 struct isert_conn *isert_conn;
551 struct isert_device *device;
552 struct ib_device *ib_dev = cma_id->device;
553 int ret = 0;
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200554 u8 pi_support = np->tpg_np->tpg->tpg_attrib.t10_pi;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800555
556 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
557 cma_id, cma_id->context);
558
559 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
560 if (!isert_conn) {
561 pr_err("Unable to allocate isert_conn\n");
562 return -ENOMEM;
563 }
564 isert_conn->state = ISER_CONN_INIT;
565 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
566 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800567 init_completion(&isert_conn->conn_wait);
568 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800569 kref_init(&isert_conn->conn_kref);
570 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700571 mutex_init(&isert_conn->conn_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300572 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800573
574 cma_id->context = isert_conn;
575 isert_conn->conn_cm_id = cma_id;
576 isert_conn->responder_resources = event->param.conn.responder_resources;
577 isert_conn->initiator_depth = event->param.conn.initiator_depth;
578 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
579 isert_conn->responder_resources, isert_conn->initiator_depth);
580
581 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
582 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
583 if (!isert_conn->login_buf) {
584 pr_err("Unable to allocate isert_conn->login_buf\n");
585 ret = -ENOMEM;
586 goto out;
587 }
588
589 isert_conn->login_req_buf = isert_conn->login_buf;
590 isert_conn->login_rsp_buf = isert_conn->login_buf +
591 ISCSI_DEF_MAX_RECV_SEG_LEN;
592 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
593 isert_conn->login_buf, isert_conn->login_req_buf,
594 isert_conn->login_rsp_buf);
595
596 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
597 (void *)isert_conn->login_req_buf,
598 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
599
600 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
601 if (ret) {
602 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
603 ret);
604 isert_conn->login_req_dma = 0;
605 goto out_login_buf;
606 }
607
608 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
609 (void *)isert_conn->login_rsp_buf,
610 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
611
612 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
613 if (ret) {
614 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
615 ret);
616 isert_conn->login_rsp_dma = 0;
617 goto out_req_dma_map;
618 }
619
620 device = isert_device_find_by_ib_dev(cma_id);
621 if (IS_ERR(device)) {
622 ret = PTR_ERR(device);
623 goto out_rsp_dma_map;
624 }
625
626 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200627 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
628 if (IS_ERR(isert_conn->conn_pd)) {
629 ret = PTR_ERR(isert_conn->conn_pd);
630 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
631 isert_conn, ret);
632 goto out_pd;
633 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800634
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200635 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
636 IB_ACCESS_LOCAL_WRITE);
637 if (IS_ERR(isert_conn->conn_mr)) {
638 ret = PTR_ERR(isert_conn->conn_mr);
639 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
640 isert_conn, ret);
641 goto out_mr;
642 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800643
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200644 if (pi_support && !device->pi_capable) {
645 pr_err("Protection information requested but not supported\n");
646 ret = -EINVAL;
647 goto out_mr;
648 }
649
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200650 if (device->use_fastreg) {
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200651 ret = isert_conn_create_fastreg_pool(isert_conn, pi_support);
Vu Pham59464ef2013-08-28 23:23:35 +0300652 if (ret) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200653 pr_err("Conn: %p failed to create fastreg pool\n",
654 isert_conn);
655 goto out_fastreg;
Vu Pham59464ef2013-08-28 23:23:35 +0300656 }
657 }
658
Sagi Grimbergd3e125d2014-02-19 17:50:23 +0200659 ret = isert_conn_setup_qp(isert_conn, cma_id, pi_support);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800660 if (ret)
661 goto out_conn_dev;
662
663 mutex_lock(&isert_np->np_accept_mutex);
664 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
665 mutex_unlock(&isert_np->np_accept_mutex);
666
667 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
668 wake_up(&isert_np->np_accept_wq);
669 return 0;
670
671out_conn_dev:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200672 if (device->use_fastreg)
673 isert_conn_free_fastreg_pool(isert_conn);
674out_fastreg:
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) {
1048 ret = isert_alloc_rx_descriptors(isert_conn);
1049 if (ret)
1050 return ret;
1051
1052 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
1053 if (ret)
1054 return ret;
1055
1056 isert_conn->state = ISER_CONN_UP;
1057 goto post_send;
1058 }
1059
1060 ret = isert_rdma_post_recvl(isert_conn);
1061 if (ret)
1062 return ret;
1063 }
1064post_send:
1065 ret = isert_post_send(isert_conn, tx_desc);
1066 if (ret)
1067 return ret;
1068
1069 return 0;
1070}
1071
1072static void
1073isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
1074 struct isert_conn *isert_conn)
1075{
1076 struct iscsi_conn *conn = isert_conn->conn;
1077 struct iscsi_login *login = conn->conn_login;
1078 int size;
1079
1080 if (!login) {
1081 pr_err("conn->conn_login is NULL\n");
1082 dump_stack();
1083 return;
1084 }
1085
1086 if (login->first_request) {
1087 struct iscsi_login_req *login_req =
1088 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1089 /*
1090 * Setup the initial iscsi_login values from the leading
1091 * login request PDU.
1092 */
1093 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1094 login->current_stage =
1095 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1096 >> 2;
1097 login->version_min = login_req->min_version;
1098 login->version_max = login_req->max_version;
1099 memcpy(login->isid, login_req->isid, 6);
1100 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1101 login->init_task_tag = login_req->itt;
1102 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1103 login->cid = be16_to_cpu(login_req->cid);
1104 login->tsih = be16_to_cpu(login_req->tsih);
1105 }
1106
1107 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1108
1109 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1110 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1111 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1112 memcpy(login->req_buf, &rx_desc->data[0], size);
1113
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001114 if (login->first_request) {
1115 complete(&isert_conn->conn_login_comp);
1116 return;
1117 }
1118 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001119}
1120
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001121static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001122*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001123{
1124 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1125 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001126 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001127
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001128 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001129 if (!cmd) {
1130 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001131 return NULL;
1132 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001133 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001134 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001135 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001136
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001137 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001138}
1139
1140static int
1141isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001142 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1143 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001144{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001145 struct iscsi_conn *conn = isert_conn->conn;
1146 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1147 struct scatterlist *sg;
1148 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1149 bool dump_payload = false;
1150
1151 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1152 if (rc < 0)
1153 return rc;
1154
1155 imm_data = cmd->immediate_data;
1156 imm_data_len = cmd->first_burst_len;
1157 unsol_data = cmd->unsolicited_data;
1158
1159 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1160 if (rc < 0) {
1161 return 0;
1162 } else if (rc > 0) {
1163 dump_payload = true;
1164 goto sequence_cmd;
1165 }
1166
1167 if (!imm_data)
1168 return 0;
1169
1170 sg = &cmd->se_cmd.t_data_sg[0];
1171 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1172
1173 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1174 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1175
1176 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1177
1178 cmd->write_data_done += imm_data_len;
1179
1180 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1181 spin_lock_bh(&cmd->istate_lock);
1182 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1183 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1184 spin_unlock_bh(&cmd->istate_lock);
1185 }
1186
1187sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001188 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001189
1190 if (!rc && dump_payload == false && unsol_data)
1191 iscsit_set_unsoliticed_dataout(cmd);
1192
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001193 return 0;
1194}
1195
1196static int
1197isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1198 struct iser_rx_desc *rx_desc, unsigned char *buf)
1199{
1200 struct scatterlist *sg_start;
1201 struct iscsi_conn *conn = isert_conn->conn;
1202 struct iscsi_cmd *cmd = NULL;
1203 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1204 u32 unsol_data_len = ntoh24(hdr->dlength);
1205 int rc, sg_nents, sg_off, page_off;
1206
1207 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1208 if (rc < 0)
1209 return rc;
1210 else if (!cmd)
1211 return 0;
1212 /*
1213 * FIXME: Unexpected unsolicited_data out
1214 */
1215 if (!cmd->unsolicited_data) {
1216 pr_err("Received unexpected solicited data payload\n");
1217 dump_stack();
1218 return -1;
1219 }
1220
1221 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1222 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1223
1224 sg_off = cmd->write_data_done / PAGE_SIZE;
1225 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1226 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1227 page_off = cmd->write_data_done % PAGE_SIZE;
1228 /*
1229 * FIXME: Non page-aligned unsolicited_data out
1230 */
1231 if (page_off) {
1232 pr_err("Received unexpected non-page aligned data payload\n");
1233 dump_stack();
1234 return -1;
1235 }
1236 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1237 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1238
1239 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1240 unsol_data_len);
1241
1242 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1243 if (rc < 0)
1244 return rc;
1245
1246 return 0;
1247}
1248
1249static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001250isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001251 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1252 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001253{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001254 struct iscsi_conn *conn = isert_conn->conn;
1255 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1256 int rc;
1257
1258 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1259 if (rc < 0)
1260 return rc;
1261 /*
1262 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1263 */
1264
1265 return iscsit_process_nop_out(conn, cmd, hdr);
1266}
1267
1268static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001269isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001270 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1271 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001272{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001273 struct iscsi_conn *conn = isert_conn->conn;
1274 u32 payload_length = ntoh24(hdr->dlength);
1275 int rc;
1276 unsigned char *text_in;
1277
1278 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1279 if (rc < 0)
1280 return rc;
1281
1282 text_in = kzalloc(payload_length, GFP_KERNEL);
1283 if (!text_in) {
1284 pr_err("Unable to allocate text_in of payload_length: %u\n",
1285 payload_length);
1286 return -ENOMEM;
1287 }
1288 cmd->text_in_ptr = text_in;
1289
1290 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1291
1292 return iscsit_process_text_cmd(conn, cmd, hdr);
1293}
1294
1295static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001296isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1297 uint32_t read_stag, uint64_t read_va,
1298 uint32_t write_stag, uint64_t write_va)
1299{
1300 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1301 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001302 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001303 struct iscsi_cmd *cmd;
1304 struct isert_cmd *isert_cmd;
1305 int ret = -EINVAL;
1306 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1307
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001308 if (sess->sess_ops->SessionType &&
1309 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1310 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1311 " ignoring\n", opcode);
1312 return 0;
1313 }
1314
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001315 switch (opcode) {
1316 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001317 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001318 if (!cmd)
1319 break;
1320
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001321 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001322 isert_cmd->read_stag = read_stag;
1323 isert_cmd->read_va = read_va;
1324 isert_cmd->write_stag = write_stag;
1325 isert_cmd->write_va = write_va;
1326
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001327 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001328 rx_desc, (unsigned char *)hdr);
1329 break;
1330 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001331 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001332 if (!cmd)
1333 break;
1334
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001335 isert_cmd = iscsit_priv_cmd(cmd);
1336 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001337 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001338 break;
1339 case ISCSI_OP_SCSI_DATA_OUT:
1340 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1341 (unsigned char *)hdr);
1342 break;
1343 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001344 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001345 if (!cmd)
1346 break;
1347
1348 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1349 (unsigned char *)hdr);
1350 break;
1351 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001352 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001353 if (!cmd)
1354 break;
1355
1356 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1357 if (ret > 0)
1358 wait_for_completion_timeout(&conn->conn_logout_comp,
1359 SECONDS_FOR_LOGOUT_COMP *
1360 HZ);
1361 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001362 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001363 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001364 if (!cmd)
1365 break;
1366
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001367 isert_cmd = iscsit_priv_cmd(cmd);
1368 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001369 rx_desc, (struct iscsi_text *)hdr);
1370 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001371 default:
1372 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1373 dump_stack();
1374 break;
1375 }
1376
1377 return ret;
1378}
1379
1380static void
1381isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1382{
1383 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1384 uint64_t read_va = 0, write_va = 0;
1385 uint32_t read_stag = 0, write_stag = 0;
1386 int rc;
1387
1388 switch (iser_hdr->flags & 0xF0) {
1389 case ISCSI_CTRL:
1390 if (iser_hdr->flags & ISER_RSV) {
1391 read_stag = be32_to_cpu(iser_hdr->read_stag);
1392 read_va = be64_to_cpu(iser_hdr->read_va);
1393 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1394 read_stag, (unsigned long long)read_va);
1395 }
1396 if (iser_hdr->flags & ISER_WSV) {
1397 write_stag = be32_to_cpu(iser_hdr->write_stag);
1398 write_va = be64_to_cpu(iser_hdr->write_va);
1399 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1400 write_stag, (unsigned long long)write_va);
1401 }
1402
1403 pr_debug("ISER ISCSI_CTRL PDU\n");
1404 break;
1405 case ISER_HELLO:
1406 pr_err("iSER Hello message\n");
1407 break;
1408 default:
1409 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1410 break;
1411 }
1412
1413 rc = isert_rx_opcode(isert_conn, rx_desc,
1414 read_stag, read_va, write_stag, write_va);
1415}
1416
1417static void
1418isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1419 unsigned long xfer_len)
1420{
1421 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1422 struct iscsi_hdr *hdr;
1423 u64 rx_dma;
1424 int rx_buflen, outstanding;
1425
1426 if ((char *)desc == isert_conn->login_req_buf) {
1427 rx_dma = isert_conn->login_req_dma;
1428 rx_buflen = ISER_RX_LOGIN_SIZE;
1429 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1430 rx_dma, rx_buflen);
1431 } else {
1432 rx_dma = desc->dma_addr;
1433 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1434 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1435 rx_dma, rx_buflen);
1436 }
1437
1438 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1439
1440 hdr = &desc->iscsi_header;
1441 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1442 hdr->opcode, hdr->itt, hdr->flags,
1443 (int)(xfer_len - ISER_HEADERS_LEN));
1444
1445 if ((char *)desc == isert_conn->login_req_buf)
1446 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1447 isert_conn);
1448 else
1449 isert_rx_do_work(desc, isert_conn);
1450
1451 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1452 DMA_FROM_DEVICE);
1453
1454 isert_conn->post_recv_buf_count--;
1455 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1456 isert_conn->post_recv_buf_count);
1457
1458 if ((char *)desc == isert_conn->login_req_buf)
1459 return;
1460
1461 outstanding = isert_conn->post_recv_buf_count;
1462 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1463 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1464 ISERT_MIN_POSTED_RX);
1465 err = isert_post_recv(isert_conn, count);
1466 if (err) {
1467 pr_err("isert_post_recv() count: %d failed, %d\n",
1468 count, err);
1469 }
1470 }
1471}
1472
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001473static int
1474isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1475 struct scatterlist *sg, u32 nents, u32 length, u32 offset,
1476 enum iser_ib_op_code op, struct isert_data_buf *data)
1477{
1478 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1479
1480 data->dma_dir = op == ISER_IB_RDMA_WRITE ?
1481 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1482
1483 data->len = length - offset;
1484 data->offset = offset;
1485 data->sg_off = data->offset / PAGE_SIZE;
1486
1487 data->sg = &sg[data->sg_off];
1488 data->nents = min_t(unsigned int, nents - data->sg_off,
1489 ISCSI_ISER_SG_TABLESIZE);
1490 data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
1491 PAGE_SIZE);
1492
1493 data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
1494 data->dma_dir);
1495 if (unlikely(!data->dma_nents)) {
1496 pr_err("Cmd: unable to dma map SGs %p\n", sg);
1497 return -EINVAL;
1498 }
1499
1500 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1501 isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
1502
1503 return 0;
1504}
1505
1506static void
1507isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
1508{
1509 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1510
1511 ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
1512 memset(data, 0, sizeof(*data));
1513}
1514
1515
1516
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001517static void
1518isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1519{
1520 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001521
Vu Pham90ecc6e2013-08-28 23:23:33 +03001522 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001523
1524 if (wr->data.sg) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001525 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001526 isert_unmap_data_buf(isert_conn, &wr->data);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 }
1528
Vu Pham90ecc6e2013-08-28 23:23:33 +03001529 if (wr->send_wr) {
1530 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1531 kfree(wr->send_wr);
1532 wr->send_wr = NULL;
1533 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001534
Vu Pham90ecc6e2013-08-28 23:23:33 +03001535 if (wr->ib_sge) {
1536 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1537 kfree(wr->ib_sge);
1538 wr->ib_sge = NULL;
1539 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001540}
1541
1542static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001543isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001544{
1545 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03001546 LIST_HEAD(unmap_list);
1547
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001548 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001549
1550 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001551 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001552 isert_cmd, wr->fr_desc);
1553 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001554 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001555 spin_unlock_bh(&isert_conn->conn_lock);
1556 wr->fr_desc = NULL;
1557 }
1558
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001559 if (wr->data.sg) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001560 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001561 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03001562 }
1563
1564 wr->ib_sge = NULL;
1565 wr->send_wr = NULL;
1566}
1567
1568static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001569isert_put_cmd(struct isert_cmd *isert_cmd)
1570{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001571 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001572 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001573 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001574 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001575
1576 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1577
1578 switch (cmd->iscsi_opcode) {
1579 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001580 spin_lock_bh(&conn->cmd_lock);
1581 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001582 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001583 spin_unlock_bh(&conn->cmd_lock);
1584
1585 if (cmd->data_direction == DMA_TO_DEVICE)
1586 iscsit_stop_dataout_timer(cmd);
1587
Vu Phamd40945d2013-08-28 23:23:34 +03001588 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001589 transport_generic_free_cmd(&cmd->se_cmd, 0);
1590 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001591 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001592 spin_lock_bh(&conn->cmd_lock);
1593 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001594 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001595 spin_unlock_bh(&conn->cmd_lock);
1596
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001597 transport_generic_free_cmd(&cmd->se_cmd, 0);
1598 break;
1599 case ISCSI_OP_REJECT:
1600 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001601 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001602 spin_lock_bh(&conn->cmd_lock);
1603 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001604 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001605 spin_unlock_bh(&conn->cmd_lock);
1606
1607 /*
1608 * Handle special case for REJECT when iscsi_add_reject*() has
1609 * overwritten the original iscsi_opcode assignment, and the
1610 * associated cmd->se_cmd needs to be released.
1611 */
1612 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001613 pr_debug("Calling transport_generic_free_cmd from"
1614 " isert_put_cmd for 0x%02x\n",
1615 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616 transport_generic_free_cmd(&cmd->se_cmd, 0);
1617 break;
1618 }
1619 /*
1620 * Fall-through
1621 */
1622 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001623 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001624 break;
1625 }
1626}
1627
1628static void
1629isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1630{
1631 if (tx_desc->dma_addr != 0) {
1632 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1633 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1634 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1635 tx_desc->dma_addr = 0;
1636 }
1637}
1638
1639static void
1640isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1641 struct ib_device *ib_dev)
1642{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001643 if (isert_cmd->pdu_buf_dma != 0) {
1644 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1645 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1646 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1647 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001648 }
1649
1650 isert_unmap_tx_desc(tx_desc, ib_dev);
1651 isert_put_cmd(isert_cmd);
1652}
1653
1654static void
1655isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1656 struct isert_cmd *isert_cmd)
1657{
1658 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001659 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001660 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001661 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001662 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001663
1664 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001665 device->unreg_rdma_mem(isert_cmd, isert_conn);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02001666 cmd->write_data_done = wr->data.len;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001667 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001668
Vu Pham90ecc6e2013-08-28 23:23:33 +03001669 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001670 spin_lock_bh(&cmd->istate_lock);
1671 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1672 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1673 spin_unlock_bh(&cmd->istate_lock);
1674
1675 target_execute_cmd(se_cmd);
1676}
1677
1678static void
1679isert_do_control_comp(struct work_struct *work)
1680{
1681 struct isert_cmd *isert_cmd = container_of(work,
1682 struct isert_cmd, comp_work);
1683 struct isert_conn *isert_conn = isert_cmd->conn;
1684 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001685 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001686
1687 switch (cmd->i_state) {
1688 case ISTATE_SEND_TASKMGTRSP:
1689 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1690
1691 atomic_dec(&isert_conn->post_send_buf_count);
1692 iscsit_tmr_post_handler(cmd, cmd->conn);
1693
1694 cmd->i_state = ISTATE_SENT_STATUS;
1695 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1696 break;
1697 case ISTATE_SEND_REJECT:
1698 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1699 atomic_dec(&isert_conn->post_send_buf_count);
1700
1701 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001702 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001703 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704 case ISTATE_SEND_LOGOUTRSP:
1705 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1706 /*
1707 * Call atomic_dec(&isert_conn->post_send_buf_count)
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001708 * from isert_wait_conn()
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001709 */
1710 isert_conn->logout_posted = true;
1711 iscsit_logout_post_handler(cmd, cmd->conn);
1712 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001713 case ISTATE_SEND_TEXTRSP:
1714 atomic_dec(&isert_conn->post_send_buf_count);
1715 cmd->i_state = ISTATE_SENT_STATUS;
1716 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1717 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001718 default:
1719 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1720 dump_stack();
1721 break;
1722 }
1723}
1724
1725static void
1726isert_response_completion(struct iser_tx_desc *tx_desc,
1727 struct isert_cmd *isert_cmd,
1728 struct isert_conn *isert_conn,
1729 struct ib_device *ib_dev)
1730{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001731 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001732 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001733
1734 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001735 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001736 cmd->i_state == ISTATE_SEND_REJECT ||
1737 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001738 isert_unmap_tx_desc(tx_desc, ib_dev);
1739
1740 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1741 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1742 return;
1743 }
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001744 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001745
1746 cmd->i_state = ISTATE_SENT_STATUS;
1747 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1748}
1749
1750static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001751__isert_send_completion(struct iser_tx_desc *tx_desc,
1752 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001753{
1754 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1755 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1756 struct isert_rdma_wr *wr;
1757
1758 if (!isert_cmd) {
1759 atomic_dec(&isert_conn->post_send_buf_count);
1760 isert_unmap_tx_desc(tx_desc, ib_dev);
1761 return;
1762 }
1763 wr = &isert_cmd->rdma_wr;
1764
1765 switch (wr->iser_ib_op) {
1766 case ISER_IB_RECV:
1767 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1768 dump_stack();
1769 break;
1770 case ISER_IB_SEND:
1771 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1772 isert_response_completion(tx_desc, isert_cmd,
1773 isert_conn, ib_dev);
1774 break;
1775 case ISER_IB_RDMA_WRITE:
1776 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1777 dump_stack();
1778 break;
1779 case ISER_IB_RDMA_READ:
1780 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1781
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08001782 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001783 isert_completion_rdma_read(tx_desc, isert_cmd);
1784 break;
1785 default:
1786 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1787 dump_stack();
1788 break;
1789 }
1790}
1791
1792static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001793isert_send_completion(struct iser_tx_desc *tx_desc,
1794 struct isert_conn *isert_conn)
1795{
1796 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1797 struct iser_tx_desc *t;
1798 /*
1799 * Drain coalesced completion llist starting from comp_llnode_batch
1800 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1801 */
1802 while (llnode) {
1803 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1804 llnode = llist_next(llnode);
1805 __isert_send_completion(t, isert_conn);
1806 }
1807 __isert_send_completion(tx_desc, isert_conn);
1808}
1809
1810static void
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001811isert_cq_drain_comp_llist(struct isert_conn *isert_conn, struct ib_device *ib_dev)
1812{
1813 struct llist_node *llnode;
1814 struct isert_rdma_wr *wr;
1815 struct iser_tx_desc *t;
1816
1817 mutex_lock(&isert_conn->conn_mutex);
1818 llnode = llist_del_all(&isert_conn->conn_comp_llist);
1819 isert_conn->conn_comp_batch = 0;
1820 mutex_unlock(&isert_conn->conn_mutex);
1821
1822 while (llnode) {
1823 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1824 llnode = llist_next(llnode);
1825 wr = &t->isert_cmd->rdma_wr;
1826
1827 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1828 isert_completion_put(t, t->isert_cmd, ib_dev);
1829 }
1830}
1831
1832static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001833isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001834{
1835 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001836 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001837 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1838 struct isert_rdma_wr *wr;
1839 struct iser_tx_desc *t;
1840
1841 while (llnode) {
1842 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1843 llnode = llist_next(llnode);
1844 wr = &t->isert_cmd->rdma_wr;
1845
1846 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
1847 isert_completion_put(t, t->isert_cmd, ib_dev);
1848 }
1849 tx_desc->comp_llnode_batch = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001850
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001851 if (!isert_cmd)
1852 isert_unmap_tx_desc(tx_desc, ib_dev);
1853 else
1854 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1855}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001856
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001857static void
1858isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1859{
1860 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1861 struct iscsi_conn *conn = isert_conn->conn;
1862
1863 if (isert_conn->post_recv_buf_count)
1864 return;
1865
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001866 isert_cq_drain_comp_llist(isert_conn, ib_dev);
1867
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001868 if (conn->sess) {
1869 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1870 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001871 }
1872
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001873 while (atomic_read(&isert_conn->post_send_buf_count))
1874 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001875
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001876 mutex_lock(&isert_conn->conn_mutex);
1877 isert_conn->state = ISER_CONN_DOWN;
1878 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07001879
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001880 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001881}
1882
1883static void
1884isert_cq_tx_work(struct work_struct *work)
1885{
1886 struct isert_cq_desc *cq_desc = container_of(work,
1887 struct isert_cq_desc, cq_tx_work);
1888 struct isert_device *device = cq_desc->device;
1889 int cq_index = cq_desc->cq_index;
1890 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1891 struct isert_conn *isert_conn;
1892 struct iser_tx_desc *tx_desc;
1893 struct ib_wc wc;
1894
1895 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1896 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1897 isert_conn = wc.qp->qp_context;
1898
1899 if (wc.status == IB_WC_SUCCESS) {
1900 isert_send_completion(tx_desc, isert_conn);
1901 } else {
1902 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1903 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001904 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001905
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08001906 if (wc.wr_id != ISER_FASTREG_LI_WRID) {
Nicholas Bellingerebbe4422014-03-02 14:51:12 -08001907 if (tx_desc->llnode_active)
1908 continue;
1909
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08001910 atomic_dec(&isert_conn->post_send_buf_count);
1911 isert_cq_tx_comp_err(tx_desc, isert_conn);
1912 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913 }
1914 }
1915
1916 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1917}
1918
1919static void
1920isert_cq_tx_callback(struct ib_cq *cq, void *context)
1921{
1922 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1923
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001924 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1925}
1926
1927static void
1928isert_cq_rx_work(struct work_struct *work)
1929{
1930 struct isert_cq_desc *cq_desc = container_of(work,
1931 struct isert_cq_desc, cq_rx_work);
1932 struct isert_device *device = cq_desc->device;
1933 int cq_index = cq_desc->cq_index;
1934 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1935 struct isert_conn *isert_conn;
1936 struct iser_rx_desc *rx_desc;
1937 struct ib_wc wc;
1938 unsigned long xfer_len;
1939
1940 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1941 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1942 isert_conn = wc.qp->qp_context;
1943
1944 if (wc.status == IB_WC_SUCCESS) {
1945 xfer_len = (unsigned long)wc.byte_len;
1946 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1947 } else {
1948 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001949 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001950 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001951 pr_debug("RX wc.vendor_err: 0x%08x\n",
1952 wc.vendor_err);
1953 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001954 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001955 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001956 }
1957 }
1958
1959 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1960}
1961
1962static void
1963isert_cq_rx_callback(struct ib_cq *cq, void *context)
1964{
1965 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1966
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1968}
1969
1970static int
1971isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1972{
1973 struct ib_send_wr *wr_failed;
1974 int ret;
1975
1976 atomic_inc(&isert_conn->post_send_buf_count);
1977
1978 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1979 &wr_failed);
1980 if (ret) {
1981 pr_err("ib_post_send failed with %d\n", ret);
1982 atomic_dec(&isert_conn->post_send_buf_count);
1983 return ret;
1984 }
1985 return ret;
1986}
1987
1988static int
1989isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1990{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001991 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001992 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1993 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1994 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1995 &isert_cmd->tx_desc.iscsi_header;
1996
1997 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1998 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1999 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2000 /*
2001 * Attach SENSE DATA payload to iSCSI Response PDU
2002 */
2003 if (cmd->se_cmd.sense_buffer &&
2004 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
2005 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
2006 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2007 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002008 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002009
2010 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
2011 cmd->sense_buffer);
2012 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
2013
2014 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
2015 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002016 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002017
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002018 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2019 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002020 DMA_TO_DEVICE);
2021
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002022 isert_cmd->pdu_buf_len = pdu_len;
2023 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2024 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002025 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2026 isert_cmd->tx_desc.num_sge = 2;
2027 }
2028
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002029 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002030
2031 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2032
2033 return isert_post_response(isert_conn, isert_cmd);
2034}
2035
2036static int
2037isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2038 bool nopout_response)
2039{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002040 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002041 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2042 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2043
2044 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2045 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
2046 &isert_cmd->tx_desc.iscsi_header,
2047 nopout_response);
2048 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002049 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002050
Masanari Iida8b513d02013-05-21 23:13:12 +09002051 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052
2053 return isert_post_response(isert_conn, isert_cmd);
2054}
2055
2056static int
2057isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2058{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002059 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002060 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2061 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2062
2063 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2064 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
2065 &isert_cmd->tx_desc.iscsi_header);
2066 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002067 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002068
2069 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2070
2071 return isert_post_response(isert_conn, isert_cmd);
2072}
2073
2074static int
2075isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2076{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002077 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002078 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2079 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2080
2081 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2082 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
2083 &isert_cmd->tx_desc.iscsi_header);
2084 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002085 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002086
2087 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2088
2089 return isert_post_response(isert_conn, isert_cmd);
2090}
2091
2092static int
2093isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2094{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002095 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002096 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2097 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002098 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2099 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2100 struct iscsi_reject *hdr =
2101 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002102
2103 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002104 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002105 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002106
2107 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002108 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002109 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
2110 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07002111 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
2112 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07002113 tx_dsg->length = ISCSI_HDR_LEN;
2114 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2115 isert_cmd->tx_desc.num_sge = 2;
2116
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002117 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002118
2119 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2120
2121 return isert_post_response(isert_conn, isert_cmd);
2122}
2123
2124static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002125isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2126{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002127 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002128 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2129 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
2130 struct iscsi_text_rsp *hdr =
2131 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
2132 u32 txt_rsp_len;
2133 int rc;
2134
2135 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
2136 rc = iscsit_build_text_rsp(cmd, conn, hdr);
2137 if (rc < 0)
2138 return rc;
2139
2140 txt_rsp_len = rc;
2141 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
2142
2143 if (txt_rsp_len) {
2144 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2145 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
2146 void *txt_rsp_buf = cmd->buf_ptr;
2147
2148 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
2149 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
2150
2151 isert_cmd->pdu_buf_len = txt_rsp_len;
2152 tx_dsg->addr = isert_cmd->pdu_buf_dma;
2153 tx_dsg->length = txt_rsp_len;
2154 tx_dsg->lkey = isert_conn->conn_mr->lkey;
2155 isert_cmd->tx_desc.num_sge = 2;
2156 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002157 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002158
2159 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2160
2161 return isert_post_response(isert_conn, isert_cmd);
2162}
2163
2164static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002165isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2166 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2167 u32 data_left, u32 offset)
2168{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002169 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002170 struct scatterlist *sg_start, *tmp_sg;
2171 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2172 u32 sg_off, page_off;
2173 int i = 0, sg_nents;
2174
2175 sg_off = offset / PAGE_SIZE;
2176 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2177 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2178 page_off = offset % PAGE_SIZE;
2179
2180 send_wr->sg_list = ib_sge;
2181 send_wr->num_sge = sg_nents;
2182 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2183 /*
2184 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2185 */
2186 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2187 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2188 (unsigned long long)tmp_sg->dma_address,
2189 tmp_sg->length, page_off);
2190
2191 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2192 ib_sge->length = min_t(u32, data_left,
2193 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2194 ib_sge->lkey = isert_conn->conn_mr->lkey;
2195
Vu Pham90ecc6e2013-08-28 23:23:33 +03002196 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2197 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002198 page_off = 0;
2199 data_left -= ib_sge->length;
2200 ib_sge++;
2201 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2202 }
2203
2204 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2205 send_wr->sg_list, send_wr->num_sge);
2206
2207 return sg_nents;
2208}
2209
2210static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002211isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2212 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002213{
2214 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002215 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002216 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002217 struct isert_data_buf *data = &wr->data;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002218 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002219 struct ib_sge *ib_sge;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002220 u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
2221 int ret = 0, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002222
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002223 isert_cmd->tx_desc.isert_cmd = isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002224
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002225 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2226 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2227 se_cmd->t_data_nents, se_cmd->data_length,
2228 offset, wr->iser_ib_op, &wr->data);
2229 if (ret)
2230 return ret;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002232 data_left = data->len;
2233 offset = data->offset;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002234
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002235 ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002236 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002237 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002238 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002239 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002240 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002241 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002242
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002243 wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002244 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2245 GFP_KERNEL);
2246 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002247 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002248 ret = -ENOMEM;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002249 goto unmap_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002250 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002251
2252 wr->isert_cmd = isert_cmd;
2253 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002254
2255 for (i = 0; i < wr->send_wr_num; i++) {
2256 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2257 data_len = min(data_left, rdma_write_max);
2258
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002259 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002260 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2261 send_wr->opcode = IB_WR_RDMA_WRITE;
2262 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2263 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2264 if (i + 1 == wr->send_wr_num)
2265 send_wr->next = &isert_cmd->tx_desc.send_wr;
2266 else
2267 send_wr->next = &wr->send_wr[i + 1];
2268 } else {
2269 send_wr->opcode = IB_WR_RDMA_READ;
2270 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2271 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2272 if (i + 1 == wr->send_wr_num)
2273 send_wr->send_flags = IB_SEND_SIGNALED;
2274 else
2275 send_wr->next = &wr->send_wr[i + 1];
2276 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002277
2278 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2279 send_wr, data_len, offset);
2280 ib_sge += ib_sge_cnt;
2281
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002282 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002283 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002284 data_left -= data_len;
2285 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002286
2287 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002288unmap_cmd:
2289 isert_unmap_data_buf(isert_conn, data);
2290
Vu Pham90ecc6e2013-08-28 23:23:33 +03002291 return ret;
2292}
2293
2294static int
Vu Pham59464ef2013-08-28 23:23:35 +03002295isert_map_fr_pagelist(struct ib_device *ib_dev,
2296 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2297{
2298 u64 start_addr, end_addr, page, chunk_start = 0;
2299 struct scatterlist *tmp_sg;
2300 int i = 0, new_chunk, last_ent, n_pages;
2301
2302 n_pages = 0;
2303 new_chunk = 1;
2304 last_ent = sg_nents - 1;
2305 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2306 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2307 if (new_chunk)
2308 chunk_start = start_addr;
2309 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2310
2311 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2312 i, (unsigned long long)tmp_sg->dma_address,
2313 tmp_sg->length);
2314
2315 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2316 new_chunk = 0;
2317 continue;
2318 }
2319 new_chunk = 1;
2320
2321 page = chunk_start & PAGE_MASK;
2322 do {
2323 fr_pl[n_pages++] = page;
2324 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2325 n_pages - 1, page);
2326 page += PAGE_SIZE;
2327 } while (page < end_addr);
2328 }
2329
2330 return n_pages;
2331}
2332
2333static int
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002334isert_fast_reg_mr(struct isert_conn *isert_conn,
2335 struct fast_reg_descriptor *fr_desc,
2336 struct isert_data_buf *mem,
2337 struct ib_sge *sge)
Vu Pham59464ef2013-08-28 23:23:35 +03002338{
Vu Pham59464ef2013-08-28 23:23:35 +03002339 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Vu Pham59464ef2013-08-28 23:23:35 +03002340 struct ib_send_wr fr_wr, inv_wr;
2341 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002342 int ret, pagelist_len;
2343 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002344 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002345
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002346 if (mem->dma_nents == 1) {
2347 sge->lkey = isert_conn->conn_mr->lkey;
2348 sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
2349 sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
2350 return 0;
2351 }
2352
2353 page_off = mem->offset % PAGE_SIZE;
Vu Pham59464ef2013-08-28 23:23:35 +03002354
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002355 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002356 fr_desc, mem->nents, mem->offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002357
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002358 pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
Vu Pham59464ef2013-08-28 23:23:35 +03002359 &fr_desc->data_frpl->page_list[0]);
2360
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002361 if (!(fr_desc->ind & ISERT_DATA_KEY_VALID)) {
Vu Pham59464ef2013-08-28 23:23:35 +03002362 memset(&inv_wr, 0, sizeof(inv_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002363 inv_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002364 inv_wr.opcode = IB_WR_LOCAL_INV;
2365 inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
2366 wr = &inv_wr;
2367 /* Bump the key */
2368 key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
2369 ib_update_fast_reg_key(fr_desc->data_mr, ++key);
2370 }
2371
2372 /* Prepare FASTREG WR */
2373 memset(&fr_wr, 0, sizeof(fr_wr));
Nicholas Bellinger9bb4ca62014-02-27 07:02:48 -08002374 fr_wr.wr_id = ISER_FASTREG_LI_WRID;
Vu Pham59464ef2013-08-28 23:23:35 +03002375 fr_wr.opcode = IB_WR_FAST_REG_MR;
2376 fr_wr.wr.fast_reg.iova_start =
2377 fr_desc->data_frpl->page_list[0] + page_off;
2378 fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
2379 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2380 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002381 fr_wr.wr.fast_reg.length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002382 fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
2383 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2384
2385 if (!wr)
2386 wr = &fr_wr;
2387 else
2388 wr->next = &fr_wr;
2389
2390 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2391 if (ret) {
2392 pr_err("fast registration failed, ret:%d\n", ret);
2393 return ret;
2394 }
Sagi Grimbergd3e125d2014-02-19 17:50:23 +02002395 fr_desc->ind &= ~ISERT_DATA_KEY_VALID;
Vu Pham59464ef2013-08-28 23:23:35 +03002396
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002397 sge->lkey = fr_desc->data_mr->lkey;
2398 sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2399 sge->length = mem->len;
Vu Pham59464ef2013-08-28 23:23:35 +03002400
2401 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002402 sge->addr, sge->length, sge->lkey);
Vu Pham59464ef2013-08-28 23:23:35 +03002403
2404 return ret;
2405}
2406
2407static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002408isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2409 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002410{
2411 struct se_cmd *se_cmd = &cmd->se_cmd;
2412 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002413 struct isert_conn *isert_conn = conn->context;
Vu Pham59464ef2013-08-28 23:23:35 +03002414 struct ib_send_wr *send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002415 struct fast_reg_descriptor *fr_desc = NULL;
2416 u32 offset;
2417 int ret = 0;
Vu Pham59464ef2013-08-28 23:23:35 +03002418 unsigned long flags;
2419
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002420 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2421
2422 offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
2423 ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
2424 se_cmd->t_data_nents, se_cmd->data_length,
2425 offset, wr->iser_ib_op, &wr->data);
2426 if (ret)
2427 return ret;
2428
2429 if (wr->data.dma_nents != 1) {
2430 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2431 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
2432 struct fast_reg_descriptor, list);
2433 list_del(&fr_desc->list);
2434 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2435 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002436 }
2437
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002438 ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data, &wr->s_ib_sge);
2439 if (ret)
2440 goto unmap_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002441
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002442 wr->ib_sge = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002443 wr->send_wr_num = 1;
2444 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2445 wr->send_wr = &wr->s_send_wr;
Vu Pham59464ef2013-08-28 23:23:35 +03002446 wr->isert_cmd = isert_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002447
2448 send_wr = &isert_cmd->rdma_wr.s_send_wr;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002449 send_wr->sg_list = &wr->s_ib_sge;
Vu Pham59464ef2013-08-28 23:23:35 +03002450 send_wr->num_sge = 1;
2451 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2452 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2453 send_wr->opcode = IB_WR_RDMA_WRITE;
2454 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2455 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2456 send_wr->send_flags = 0;
2457 send_wr->next = &isert_cmd->tx_desc.send_wr;
2458 } else {
2459 send_wr->opcode = IB_WR_RDMA_READ;
2460 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2461 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2462 send_wr->send_flags = IB_SEND_SIGNALED;
2463 }
2464
Vu Pham59464ef2013-08-28 23:23:35 +03002465 return 0;
Sagi Grimberge3d7e4c2014-02-19 17:50:22 +02002466unmap_cmd:
2467 if (fr_desc) {
2468 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2469 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
2470 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2471 }
2472 isert_unmap_data_buf(isert_conn, &wr->data);
Vu Pham59464ef2013-08-28 23:23:35 +03002473
Vu Pham59464ef2013-08-28 23:23:35 +03002474 return ret;
2475}
2476
2477static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002478isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2479{
2480 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002481 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002482 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2483 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002484 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002485 struct ib_send_wr *wr_failed;
2486 int rc;
2487
2488 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2489 isert_cmd, se_cmd->data_length);
2490 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002491 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002492 if (rc) {
2493 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2494 return rc;
2495 }
2496
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002497 /*
2498 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2499 */
2500 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger04d9cd12013-11-12 18:05:07 -08002501 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002502 &isert_cmd->tx_desc.iscsi_header);
2503 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002504 isert_init_send_wr(isert_conn, isert_cmd,
2505 &isert_cmd->tx_desc.send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002506
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002507 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002508
2509 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2510 if (rc) {
2511 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002512 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002513 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002514 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
2515 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002516
Vu Pham90ecc6e2013-08-28 23:23:33 +03002517 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002518}
2519
2520static int
2521isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2522{
2523 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002524 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002525 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2526 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002527 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002528 struct ib_send_wr *wr_failed;
2529 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002530
Vu Pham90ecc6e2013-08-28 23:23:33 +03002531 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2532 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002533 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002534 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002535 if (rc) {
2536 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2537 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002538 }
2539
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002540 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002541
2542 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2543 if (rc) {
2544 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellingerb6b87a12014-02-27 09:05:03 -08002545 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002546 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002547 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2548 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002549
Vu Pham90ecc6e2013-08-28 23:23:33 +03002550 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002551}
2552
2553static int
2554isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2555{
2556 int ret;
2557
2558 switch (state) {
2559 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2560 ret = isert_put_nopin(cmd, conn, false);
2561 break;
2562 default:
2563 pr_err("Unknown immediate state: 0x%02x\n", state);
2564 ret = -EINVAL;
2565 break;
2566 }
2567
2568 return ret;
2569}
2570
2571static int
2572isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2573{
2574 int ret;
2575
2576 switch (state) {
2577 case ISTATE_SEND_LOGOUTRSP:
2578 ret = isert_put_logout_rsp(cmd, conn);
2579 if (!ret) {
2580 pr_debug("Returning iSER Logout -EAGAIN\n");
2581 ret = -EAGAIN;
2582 }
2583 break;
2584 case ISTATE_SEND_NOPIN:
2585 ret = isert_put_nopin(cmd, conn, true);
2586 break;
2587 case ISTATE_SEND_TASKMGTRSP:
2588 ret = isert_put_tm_rsp(cmd, conn);
2589 break;
2590 case ISTATE_SEND_REJECT:
2591 ret = isert_put_reject(cmd, conn);
2592 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002593 case ISTATE_SEND_TEXTRSP:
2594 ret = isert_put_text_rsp(cmd, conn);
2595 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002596 case ISTATE_SEND_STATUS:
2597 /*
2598 * Special case for sending non GOOD SCSI status from TX thread
2599 * context during pre se_cmd excecution failure.
2600 */
2601 ret = isert_put_response(conn, cmd);
2602 break;
2603 default:
2604 pr_err("Unknown response state: 0x%02x\n", state);
2605 ret = -EINVAL;
2606 break;
2607 }
2608
2609 return ret;
2610}
2611
2612static int
2613isert_setup_np(struct iscsi_np *np,
2614 struct __kernel_sockaddr_storage *ksockaddr)
2615{
2616 struct isert_np *isert_np;
2617 struct rdma_cm_id *isert_lid;
2618 struct sockaddr *sa;
2619 int ret;
2620
2621 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2622 if (!isert_np) {
2623 pr_err("Unable to allocate struct isert_np\n");
2624 return -ENOMEM;
2625 }
2626 init_waitqueue_head(&isert_np->np_accept_wq);
2627 mutex_init(&isert_np->np_accept_mutex);
2628 INIT_LIST_HEAD(&isert_np->np_accept_list);
2629 init_completion(&isert_np->np_login_comp);
2630
2631 sa = (struct sockaddr *)ksockaddr;
2632 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2633 /*
2634 * Setup the np->np_sockaddr from the passed sockaddr setup
2635 * in iscsi_target_configfs.c code..
2636 */
2637 memcpy(&np->np_sockaddr, ksockaddr,
2638 sizeof(struct __kernel_sockaddr_storage));
2639
2640 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2641 IB_QPT_RC);
2642 if (IS_ERR(isert_lid)) {
2643 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2644 PTR_ERR(isert_lid));
2645 ret = PTR_ERR(isert_lid);
2646 goto out;
2647 }
2648
2649 ret = rdma_bind_addr(isert_lid, sa);
2650 if (ret) {
2651 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2652 goto out_lid;
2653 }
2654
2655 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2656 if (ret) {
2657 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2658 goto out_lid;
2659 }
2660
2661 isert_np->np_cm_id = isert_lid;
2662 np->np_context = isert_np;
2663 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2664
2665 return 0;
2666
2667out_lid:
2668 rdma_destroy_id(isert_lid);
2669out:
2670 kfree(isert_np);
2671 return ret;
2672}
2673
2674static int
2675isert_check_accept_queue(struct isert_np *isert_np)
2676{
2677 int empty;
2678
2679 mutex_lock(&isert_np->np_accept_mutex);
2680 empty = list_empty(&isert_np->np_accept_list);
2681 mutex_unlock(&isert_np->np_accept_mutex);
2682
2683 return empty;
2684}
2685
2686static int
2687isert_rdma_accept(struct isert_conn *isert_conn)
2688{
2689 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2690 struct rdma_conn_param cp;
2691 int ret;
2692
2693 memset(&cp, 0, sizeof(struct rdma_conn_param));
2694 cp.responder_resources = isert_conn->responder_resources;
2695 cp.initiator_depth = isert_conn->initiator_depth;
2696 cp.retry_count = 7;
2697 cp.rnr_retry_count = 7;
2698
2699 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2700
2701 ret = rdma_accept(cm_id, &cp);
2702 if (ret) {
2703 pr_err("rdma_accept() failed with: %d\n", ret);
2704 return ret;
2705 }
2706
2707 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2708
2709 return 0;
2710}
2711
2712static int
2713isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2714{
2715 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2716 int ret;
2717
2718 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002719 /*
2720 * For login requests after the first PDU, isert_rx_login_req() will
2721 * kick schedule_delayed_work(&conn->login_work) as the packet is
2722 * received, which turns this callback from iscsi_target_do_login_rx()
2723 * into a NOP.
2724 */
2725 if (!login->first_request)
2726 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002727
2728 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2729 if (ret)
2730 return ret;
2731
2732 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2733 return 0;
2734}
2735
2736static void
2737isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2738 struct isert_conn *isert_conn)
2739{
2740 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2741 struct rdma_route *cm_route = &cm_id->route;
2742 struct sockaddr_in *sock_in;
2743 struct sockaddr_in6 *sock_in6;
2744
2745 conn->login_family = np->np_sockaddr.ss_family;
2746
2747 if (np->np_sockaddr.ss_family == AF_INET6) {
2748 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2749 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2750 &sock_in6->sin6_addr.in6_u);
2751 conn->login_port = ntohs(sock_in6->sin6_port);
2752
2753 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2754 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2755 &sock_in6->sin6_addr.in6_u);
2756 conn->local_port = ntohs(sock_in6->sin6_port);
2757 } else {
2758 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2759 sprintf(conn->login_ip, "%pI4",
2760 &sock_in->sin_addr.s_addr);
2761 conn->login_port = ntohs(sock_in->sin_port);
2762
2763 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2764 sprintf(conn->local_ip, "%pI4",
2765 &sock_in->sin_addr.s_addr);
2766 conn->local_port = ntohs(sock_in->sin_port);
2767 }
2768}
2769
2770static int
2771isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2772{
2773 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2774 struct isert_conn *isert_conn;
2775 int max_accept = 0, ret;
2776
2777accept_wait:
2778 ret = wait_event_interruptible(isert_np->np_accept_wq,
2779 !isert_check_accept_queue(isert_np) ||
2780 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2781 if (max_accept > 5)
2782 return -ENODEV;
2783
2784 spin_lock_bh(&np->np_thread_lock);
2785 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2786 spin_unlock_bh(&np->np_thread_lock);
2787 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2788 return -ENODEV;
2789 }
2790 spin_unlock_bh(&np->np_thread_lock);
2791
2792 mutex_lock(&isert_np->np_accept_mutex);
2793 if (list_empty(&isert_np->np_accept_list)) {
2794 mutex_unlock(&isert_np->np_accept_mutex);
2795 max_accept++;
2796 goto accept_wait;
2797 }
2798 isert_conn = list_first_entry(&isert_np->np_accept_list,
2799 struct isert_conn, conn_accept_node);
2800 list_del_init(&isert_conn->conn_accept_node);
2801 mutex_unlock(&isert_np->np_accept_mutex);
2802
2803 conn->context = isert_conn;
2804 isert_conn->conn = conn;
2805 max_accept = 0;
2806
2807 ret = isert_rdma_post_recvl(isert_conn);
2808 if (ret)
2809 return ret;
2810
2811 ret = isert_rdma_accept(isert_conn);
2812 if (ret)
2813 return ret;
2814
2815 isert_set_conn_info(np, conn, isert_conn);
2816
2817 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2818 return 0;
2819}
2820
2821static void
2822isert_free_np(struct iscsi_np *np)
2823{
2824 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2825
2826 rdma_destroy_id(isert_np->np_cm_id);
2827
2828 np->np_context = NULL;
2829 kfree(isert_np);
2830}
2831
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002832static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002833{
2834 struct isert_conn *isert_conn = conn->context;
2835
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002836 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002837 /*
2838 * Decrement post_send_buf_count for special case when called
2839 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2840 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002841 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002842 if (isert_conn->logout_posted)
2843 atomic_dec(&isert_conn->post_send_buf_count);
2844
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002845 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002846 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002847 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002848 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002849 /*
2850 * Only wait for conn_wait_comp_err if the isert_conn made it
2851 * into full feature phase..
2852 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002853 if (isert_conn->state == ISER_CONN_INIT) {
2854 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002855 return;
2856 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002857 if (isert_conn->state == ISER_CONN_UP)
2858 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002859 mutex_unlock(&isert_conn->conn_mutex);
2860
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002861 wait_for_completion(&isert_conn->conn_wait_comp_err);
2862
2863 wait_for_completion(&isert_conn->conn_wait);
2864}
2865
2866static void isert_free_conn(struct iscsi_conn *conn)
2867{
2868 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002869
2870 isert_put_conn(isert_conn);
2871}
2872
2873static struct iscsit_transport iser_target_transport = {
2874 .name = "IB/iSER",
2875 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002876 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002877 .owner = THIS_MODULE,
2878 .iscsit_setup_np = isert_setup_np,
2879 .iscsit_accept_np = isert_accept_np,
2880 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002881 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002882 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002883 .iscsit_get_login_rx = isert_get_login_rx,
2884 .iscsit_put_login_tx = isert_put_login_tx,
2885 .iscsit_immediate_queue = isert_immediate_queue,
2886 .iscsit_response_queue = isert_response_queue,
2887 .iscsit_get_dataout = isert_get_dataout,
2888 .iscsit_queue_data_in = isert_put_datain,
2889 .iscsit_queue_status = isert_put_response,
2890};
2891
2892static int __init isert_init(void)
2893{
2894 int ret;
2895
2896 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2897 if (!isert_rx_wq) {
2898 pr_err("Unable to allocate isert_rx_wq\n");
2899 return -ENOMEM;
2900 }
2901
2902 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2903 if (!isert_comp_wq) {
2904 pr_err("Unable to allocate isert_comp_wq\n");
2905 ret = -ENOMEM;
2906 goto destroy_rx_wq;
2907 }
2908
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002909 iscsit_register_transport(&iser_target_transport);
2910 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2911 return 0;
2912
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002913destroy_rx_wq:
2914 destroy_workqueue(isert_rx_wq);
2915 return ret;
2916}
2917
2918static void __exit isert_exit(void)
2919{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002920 destroy_workqueue(isert_comp_wq);
2921 destroy_workqueue(isert_rx_wq);
2922 iscsit_unregister_transport(&iser_target_transport);
2923 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2924}
2925
2926MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2927MODULE_VERSION("0.1");
2928MODULE_AUTHOR("nab@Linux-iSCSI.org");
2929MODULE_LICENSE("GPL");
2930
2931module_init(isert_init);
2932module_exit(isert_exit);