blob: 862d7b4b0411ad837b3f87fe36bee6e6c4e4e24d [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
90isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
91{
92 struct isert_device *device = isert_conn->conn_device;
93 struct ib_qp_init_attr attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080094 int ret, index, min_index = 0;
95
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080096 mutex_lock(&device_list_mutex);
97 for (index = 0; index < device->cqs_used; index++)
98 if (device->cq_active_qps[index] <
99 device->cq_active_qps[min_index])
100 min_index = index;
101 device->cq_active_qps[min_index]++;
102 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
103 mutex_unlock(&device_list_mutex);
104
105 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
106 attr.event_handler = isert_qp_event_callback;
107 attr.qp_context = isert_conn;
108 attr.send_cq = device->dev_tx_cq[min_index];
109 attr.recv_cq = device->dev_rx_cq[min_index];
110 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
111 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
112 /*
113 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
114 * work-around for RDMA_READ..
115 */
Vu Pham59464ef2013-08-28 23:23:35 +0300116 attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800117 isert_conn->max_sge = attr.cap.max_send_sge;
118
119 attr.cap.max_recv_sge = 1;
120 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
121 attr.qp_type = IB_QPT_RC;
122
123 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
124 cma_id->device);
125 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
126 isert_conn->conn_pd->device);
127
128 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
129 if (ret) {
130 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
131 return ret;
132 }
133 isert_conn->conn_qp = cma_id->qp;
134 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
135
136 return 0;
137}
138
139static void
140isert_cq_event_callback(struct ib_event *e, void *context)
141{
142 pr_debug("isert_cq_event_callback event: %d\n", e->event);
143}
144
145static int
146isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
147{
148 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
149 struct iser_rx_desc *rx_desc;
150 struct ib_sge *rx_sg;
151 u64 dma_addr;
152 int i, j;
153
154 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
155 sizeof(struct iser_rx_desc), GFP_KERNEL);
156 if (!isert_conn->conn_rx_descs)
157 goto fail;
158
159 rx_desc = isert_conn->conn_rx_descs;
160
161 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
162 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
163 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
164 if (ib_dma_mapping_error(ib_dev, dma_addr))
165 goto dma_map_fail;
166
167 rx_desc->dma_addr = dma_addr;
168
169 rx_sg = &rx_desc->rx_sg;
170 rx_sg->addr = rx_desc->dma_addr;
171 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
172 rx_sg->lkey = isert_conn->conn_mr->lkey;
173 }
174
175 isert_conn->conn_rx_desc_head = 0;
176 return 0;
177
178dma_map_fail:
179 rx_desc = isert_conn->conn_rx_descs;
180 for (j = 0; j < i; j++, rx_desc++) {
181 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
182 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
183 }
184 kfree(isert_conn->conn_rx_descs);
185 isert_conn->conn_rx_descs = NULL;
186fail:
187 return -ENOMEM;
188}
189
190static void
191isert_free_rx_descriptors(struct isert_conn *isert_conn)
192{
193 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
194 struct iser_rx_desc *rx_desc;
195 int i;
196
197 if (!isert_conn->conn_rx_descs)
198 return;
199
200 rx_desc = isert_conn->conn_rx_descs;
201 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
202 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
203 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
204 }
205
206 kfree(isert_conn->conn_rx_descs);
207 isert_conn->conn_rx_descs = NULL;
208}
209
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800210static void isert_cq_tx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800211static void isert_cq_tx_callback(struct ib_cq *, void *);
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800212static void isert_cq_rx_work(struct work_struct *);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800213static void isert_cq_rx_callback(struct ib_cq *, void *);
214
215static int
216isert_create_device_ib_res(struct isert_device *device)
217{
218 struct ib_device *ib_dev = device->ib_device;
219 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300220 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800221 int ret = 0, i, j;
222
Vu Pham59464ef2013-08-28 23:23:35 +0300223 dev_attr = &device->dev_attr;
224 ret = isert_query_device(ib_dev, dev_attr);
225 if (ret)
226 return ret;
227
Vu Phamd40945d2013-08-28 23:23:34 +0300228 /* asign function handlers */
Vu Pham59464ef2013-08-28 23:23:35 +0300229 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200230 device->use_fastreg = 1;
231 device->reg_rdma_mem = isert_reg_rdma;
232 device->unreg_rdma_mem = isert_unreg_rdma;
Vu Pham59464ef2013-08-28 23:23:35 +0300233 } else {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200234 device->use_fastreg = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300235 device->reg_rdma_mem = isert_map_rdma;
236 device->unreg_rdma_mem = isert_unmap_cmd;
237 }
Vu Phamd40945d2013-08-28 23:23:34 +0300238
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800239 device->cqs_used = min_t(int, num_online_cpus(),
240 device->ib_device->num_comp_vectors);
241 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200242 pr_debug("Using %d CQs, device %s supports %d vectors support "
243 "Fast registration %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800244 device->cqs_used, device->ib_device->name,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200245 device->ib_device->num_comp_vectors, device->use_fastreg);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800246 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
247 device->cqs_used, GFP_KERNEL);
248 if (!device->cq_desc) {
249 pr_err("Unable to allocate device->cq_desc\n");
250 return -ENOMEM;
251 }
252 cq_desc = device->cq_desc;
253
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800254 for (i = 0; i < device->cqs_used; i++) {
255 cq_desc[i].device = device;
256 cq_desc[i].cq_index = i;
257
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800258 INIT_WORK(&cq_desc[i].cq_rx_work, isert_cq_rx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800259 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
260 isert_cq_rx_callback,
261 isert_cq_event_callback,
262 (void *)&cq_desc[i],
263 ISER_MAX_RX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800264 if (IS_ERR(device->dev_rx_cq[i])) {
265 ret = PTR_ERR(device->dev_rx_cq[i]);
266 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800267 goto out_cq;
Wei Yongjun94a71112013-10-29 09:56:34 +0800268 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800269
Nicholas Bellinger2853c2b2013-12-11 16:20:13 -0800270 INIT_WORK(&cq_desc[i].cq_tx_work, isert_cq_tx_work);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800271 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
272 isert_cq_tx_callback,
273 isert_cq_event_callback,
274 (void *)&cq_desc[i],
275 ISER_MAX_TX_CQ_LEN, i);
Wei Yongjun94a71112013-10-29 09:56:34 +0800276 if (IS_ERR(device->dev_tx_cq[i])) {
277 ret = PTR_ERR(device->dev_tx_cq[i]);
278 device->dev_tx_cq[i] = NULL;
279 goto out_cq;
280 }
281
282 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
283 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800284 goto out_cq;
285
Wei Yongjun94a71112013-10-29 09:56:34 +0800286 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
287 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800288 goto out_cq;
289 }
290
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800291 return 0;
292
293out_cq:
294 for (j = 0; j < i; j++) {
295 cq_desc = &device->cq_desc[j];
296
297 if (device->dev_rx_cq[j]) {
298 cancel_work_sync(&cq_desc->cq_rx_work);
299 ib_destroy_cq(device->dev_rx_cq[j]);
300 }
301 if (device->dev_tx_cq[j]) {
302 cancel_work_sync(&cq_desc->cq_tx_work);
303 ib_destroy_cq(device->dev_tx_cq[j]);
304 }
305 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800306 kfree(device->cq_desc);
307
308 return ret;
309}
310
311static void
312isert_free_device_ib_res(struct isert_device *device)
313{
314 struct isert_cq_desc *cq_desc;
315 int i;
316
317 for (i = 0; i < device->cqs_used; i++) {
318 cq_desc = &device->cq_desc[i];
319
320 cancel_work_sync(&cq_desc->cq_rx_work);
321 cancel_work_sync(&cq_desc->cq_tx_work);
322 ib_destroy_cq(device->dev_rx_cq[i]);
323 ib_destroy_cq(device->dev_tx_cq[i]);
324 device->dev_rx_cq[i] = NULL;
325 device->dev_tx_cq[i] = NULL;
326 }
327
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800328 kfree(device->cq_desc);
329}
330
331static void
332isert_device_try_release(struct isert_device *device)
333{
334 mutex_lock(&device_list_mutex);
335 device->refcount--;
336 if (!device->refcount) {
337 isert_free_device_ib_res(device);
338 list_del(&device->dev_node);
339 kfree(device);
340 }
341 mutex_unlock(&device_list_mutex);
342}
343
344static struct isert_device *
345isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
346{
347 struct isert_device *device;
348 int ret;
349
350 mutex_lock(&device_list_mutex);
351 list_for_each_entry(device, &device_list, dev_node) {
352 if (device->ib_device->node_guid == cma_id->device->node_guid) {
353 device->refcount++;
354 mutex_unlock(&device_list_mutex);
355 return device;
356 }
357 }
358
359 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
360 if (!device) {
361 mutex_unlock(&device_list_mutex);
362 return ERR_PTR(-ENOMEM);
363 }
364
365 INIT_LIST_HEAD(&device->dev_node);
366
367 device->ib_device = cma_id->device;
368 ret = isert_create_device_ib_res(device);
369 if (ret) {
370 kfree(device);
371 mutex_unlock(&device_list_mutex);
372 return ERR_PTR(ret);
373 }
374
375 device->refcount++;
376 list_add_tail(&device->dev_node, &device_list);
377 mutex_unlock(&device_list_mutex);
378
379 return device;
380}
381
Vu Pham59464ef2013-08-28 23:23:35 +0300382static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200383isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300384{
385 struct fast_reg_descriptor *fr_desc, *tmp;
386 int i = 0;
387
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200388 if (list_empty(&isert_conn->conn_fr_pool))
Vu Pham59464ef2013-08-28 23:23:35 +0300389 return;
390
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200391 pr_debug("Freeing conn %p fastreg pool", isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300392
393 list_for_each_entry_safe(fr_desc, tmp,
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200394 &isert_conn->conn_fr_pool, list) {
Vu Pham59464ef2013-08-28 23:23:35 +0300395 list_del(&fr_desc->list);
396 ib_free_fast_reg_page_list(fr_desc->data_frpl);
397 ib_dereg_mr(fr_desc->data_mr);
398 kfree(fr_desc);
399 ++i;
400 }
401
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200402 if (i < isert_conn->conn_fr_pool_size)
Vu Pham59464ef2013-08-28 23:23:35 +0300403 pr_warn("Pool still has %d regions registered\n",
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200404 isert_conn->conn_fr_pool_size - i);
Vu Pham59464ef2013-08-28 23:23:35 +0300405}
406
407static int
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200408isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
409 struct fast_reg_descriptor *fr_desc)
410{
411 fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
412 ISCSI_ISER_SG_TABLESIZE);
413 if (IS_ERR(fr_desc->data_frpl)) {
414 pr_err("Failed to allocate data frpl err=%ld\n",
415 PTR_ERR(fr_desc->data_frpl));
416 return PTR_ERR(fr_desc->data_frpl);
417 }
418
419 fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
420 if (IS_ERR(fr_desc->data_mr)) {
421 pr_err("Failed to allocate data frmr err=%ld\n",
422 PTR_ERR(fr_desc->data_mr));
423 ib_free_fast_reg_page_list(fr_desc->data_frpl);
424 return PTR_ERR(fr_desc->data_mr);
425 }
426 pr_debug("Create fr_desc %p page_list %p\n",
427 fr_desc, fr_desc->data_frpl->page_list);
428
429 fr_desc->valid = true;
430
431 return 0;
432}
433
434static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200435isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +0300436{
437 struct fast_reg_descriptor *fr_desc;
438 struct isert_device *device = isert_conn->conn_device;
439 int i, ret;
440
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200441 INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
442 isert_conn->conn_fr_pool_size = 0;
Vu Pham59464ef2013-08-28 23:23:35 +0300443 for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
444 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
445 if (!fr_desc) {
446 pr_err("Failed to allocate fast_reg descriptor\n");
447 ret = -ENOMEM;
448 goto err;
449 }
450
Sagi Grimbergdc87a902014-01-09 18:40:51 +0200451 ret = isert_create_fr_desc(device->ib_device,
452 isert_conn->conn_pd, fr_desc);
453 if (ret) {
454 pr_err("Failed to create fastreg descriptor err=%d\n",
455 ret);
Nicholas Bellingera80e21b2014-02-03 12:59:56 -0800456 kfree(fr_desc);
Vu Pham59464ef2013-08-28 23:23:35 +0300457 goto err;
458 }
459
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200460 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
461 isert_conn->conn_fr_pool_size++;
Vu Pham59464ef2013-08-28 23:23:35 +0300462 }
463
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200464 pr_debug("Creating conn %p fastreg pool size=%d",
465 isert_conn, isert_conn->conn_fr_pool_size);
Vu Pham59464ef2013-08-28 23:23:35 +0300466
467 return 0;
468
469err:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200470 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300471 return ret;
472}
473
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800474static int
475isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
476{
477 struct iscsi_np *np = cma_id->context;
478 struct isert_np *isert_np = np->np_context;
479 struct isert_conn *isert_conn;
480 struct isert_device *device;
481 struct ib_device *ib_dev = cma_id->device;
482 int ret = 0;
483
484 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
485 cma_id, cma_id->context);
486
487 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
488 if (!isert_conn) {
489 pr_err("Unable to allocate isert_conn\n");
490 return -ENOMEM;
491 }
492 isert_conn->state = ISER_CONN_INIT;
493 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
494 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800495 init_completion(&isert_conn->conn_wait);
496 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800497 kref_init(&isert_conn->conn_kref);
498 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700499 mutex_init(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800500 mutex_init(&isert_conn->conn_comp_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300501 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800502
503 cma_id->context = isert_conn;
504 isert_conn->conn_cm_id = cma_id;
505 isert_conn->responder_resources = event->param.conn.responder_resources;
506 isert_conn->initiator_depth = event->param.conn.initiator_depth;
507 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
508 isert_conn->responder_resources, isert_conn->initiator_depth);
509
510 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
511 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
512 if (!isert_conn->login_buf) {
513 pr_err("Unable to allocate isert_conn->login_buf\n");
514 ret = -ENOMEM;
515 goto out;
516 }
517
518 isert_conn->login_req_buf = isert_conn->login_buf;
519 isert_conn->login_rsp_buf = isert_conn->login_buf +
520 ISCSI_DEF_MAX_RECV_SEG_LEN;
521 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
522 isert_conn->login_buf, isert_conn->login_req_buf,
523 isert_conn->login_rsp_buf);
524
525 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
526 (void *)isert_conn->login_req_buf,
527 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
528
529 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
530 if (ret) {
531 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
532 ret);
533 isert_conn->login_req_dma = 0;
534 goto out_login_buf;
535 }
536
537 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
538 (void *)isert_conn->login_rsp_buf,
539 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
540
541 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
542 if (ret) {
543 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
544 ret);
545 isert_conn->login_rsp_dma = 0;
546 goto out_req_dma_map;
547 }
548
549 device = isert_device_find_by_ib_dev(cma_id);
550 if (IS_ERR(device)) {
551 ret = PTR_ERR(device);
552 goto out_rsp_dma_map;
553 }
554
555 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200556 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
557 if (IS_ERR(isert_conn->conn_pd)) {
558 ret = PTR_ERR(isert_conn->conn_pd);
559 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
560 isert_conn, ret);
561 goto out_pd;
562 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800563
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200564 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
565 IB_ACCESS_LOCAL_WRITE);
566 if (IS_ERR(isert_conn->conn_mr)) {
567 ret = PTR_ERR(isert_conn->conn_mr);
568 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
569 isert_conn, ret);
570 goto out_mr;
571 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800572
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200573 if (device->use_fastreg) {
574 ret = isert_conn_create_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300575 if (ret) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200576 pr_err("Conn: %p failed to create fastreg pool\n",
577 isert_conn);
578 goto out_fastreg;
Vu Pham59464ef2013-08-28 23:23:35 +0300579 }
580 }
581
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800582 ret = isert_conn_setup_qp(isert_conn, cma_id);
583 if (ret)
584 goto out_conn_dev;
585
586 mutex_lock(&isert_np->np_accept_mutex);
587 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
588 mutex_unlock(&isert_np->np_accept_mutex);
589
590 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
591 wake_up(&isert_np->np_accept_wq);
592 return 0;
593
594out_conn_dev:
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200595 if (device->use_fastreg)
596 isert_conn_free_fastreg_pool(isert_conn);
597out_fastreg:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200598 ib_dereg_mr(isert_conn->conn_mr);
599out_mr:
600 ib_dealloc_pd(isert_conn->conn_pd);
601out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800602 isert_device_try_release(device);
603out_rsp_dma_map:
604 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
605 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
606out_req_dma_map:
607 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
608 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
609out_login_buf:
610 kfree(isert_conn->login_buf);
611out:
612 kfree(isert_conn);
613 return ret;
614}
615
616static void
617isert_connect_release(struct isert_conn *isert_conn)
618{
619 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
620 struct isert_device *device = isert_conn->conn_device;
621 int cq_index;
622
623 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
624
Sagi Grimberga3a5a822014-01-09 18:40:50 +0200625 if (device && device->use_fastreg)
626 isert_conn_free_fastreg_pool(isert_conn);
Vu Pham59464ef2013-08-28 23:23:35 +0300627
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800628 if (isert_conn->conn_qp) {
629 cq_index = ((struct isert_cq_desc *)
630 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
631 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
632 isert_conn->conn_device->cq_active_qps[cq_index]--;
633
634 rdma_destroy_qp(isert_conn->conn_cm_id);
635 }
636
637 isert_free_rx_descriptors(isert_conn);
638 rdma_destroy_id(isert_conn->conn_cm_id);
639
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200640 ib_dereg_mr(isert_conn->conn_mr);
641 ib_dealloc_pd(isert_conn->conn_pd);
642
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800643 if (isert_conn->login_buf) {
644 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
645 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
646 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
647 ISCSI_DEF_MAX_RECV_SEG_LEN,
648 DMA_FROM_DEVICE);
649 kfree(isert_conn->login_buf);
650 }
651 kfree(isert_conn);
652
653 if (device)
654 isert_device_try_release(device);
655
656 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
657}
658
659static void
660isert_connected_handler(struct rdma_cm_id *cma_id)
661{
662 return;
663}
664
665static void
666isert_release_conn_kref(struct kref *kref)
667{
668 struct isert_conn *isert_conn = container_of(kref,
669 struct isert_conn, conn_kref);
670
671 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
672 current->comm, current->pid);
673
674 isert_connect_release(isert_conn);
675}
676
677static void
678isert_put_conn(struct isert_conn *isert_conn)
679{
680 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
681}
682
683static void
684isert_disconnect_work(struct work_struct *work)
685{
686 struct isert_conn *isert_conn = container_of(work,
687 struct isert_conn, conn_logout_work);
688
689 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700690 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800691 if (isert_conn->state == ISER_CONN_UP)
692 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800693
694 if (isert_conn->post_recv_buf_count == 0 &&
695 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700696 mutex_unlock(&isert_conn->conn_mutex);
697 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800698 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700699 if (!isert_conn->conn_cm_id) {
700 mutex_unlock(&isert_conn->conn_mutex);
701 isert_put_conn(isert_conn);
702 return;
703 }
704 if (!isert_conn->logout_posted) {
705 pr_debug("Calling rdma_disconnect for !logout_posted from"
706 " isert_disconnect_work\n");
707 rdma_disconnect(isert_conn->conn_cm_id);
708 mutex_unlock(&isert_conn->conn_mutex);
709 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
710 goto wake_up;
711 }
712 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800713
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700714wake_up:
Nicholas Bellingerdefd8842014-02-03 12:54:39 -0800715 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800716 isert_put_conn(isert_conn);
717}
718
719static void
720isert_disconnected_handler(struct rdma_cm_id *cma_id)
721{
722 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
723
724 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
725 schedule_work(&isert_conn->conn_logout_work);
726}
727
728static int
729isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
730{
731 int ret = 0;
732
733 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
734 event->event, event->status, cma_id->context, cma_id);
735
736 switch (event->event) {
737 case RDMA_CM_EVENT_CONNECT_REQUEST:
738 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
739 ret = isert_connect_request(cma_id, event);
740 break;
741 case RDMA_CM_EVENT_ESTABLISHED:
742 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
743 isert_connected_handler(cma_id);
744 break;
745 case RDMA_CM_EVENT_DISCONNECTED:
746 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
747 isert_disconnected_handler(cma_id);
748 break;
749 case RDMA_CM_EVENT_DEVICE_REMOVAL:
750 case RDMA_CM_EVENT_ADDR_CHANGE:
751 break;
752 case RDMA_CM_EVENT_CONNECT_ERROR:
753 default:
754 pr_err("Unknown RDMA CMA event: %d\n", event->event);
755 break;
756 }
757
758 if (ret != 0) {
759 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
760 event->event, ret);
761 dump_stack();
762 }
763
764 return ret;
765}
766
767static int
768isert_post_recv(struct isert_conn *isert_conn, u32 count)
769{
770 struct ib_recv_wr *rx_wr, *rx_wr_failed;
771 int i, ret;
772 unsigned int rx_head = isert_conn->conn_rx_desc_head;
773 struct iser_rx_desc *rx_desc;
774
775 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
776 rx_desc = &isert_conn->conn_rx_descs[rx_head];
777 rx_wr->wr_id = (unsigned long)rx_desc;
778 rx_wr->sg_list = &rx_desc->rx_sg;
779 rx_wr->num_sge = 1;
780 rx_wr->next = rx_wr + 1;
781 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
782 }
783
784 rx_wr--;
785 rx_wr->next = NULL; /* mark end of work requests list */
786
787 isert_conn->post_recv_buf_count += count;
788 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
789 &rx_wr_failed);
790 if (ret) {
791 pr_err("ib_post_recv() failed with ret: %d\n", ret);
792 isert_conn->post_recv_buf_count -= count;
793 } else {
794 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
795 isert_conn->conn_rx_desc_head = rx_head;
796 }
797 return ret;
798}
799
800static int
801isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
802{
803 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
804 struct ib_send_wr send_wr, *send_wr_failed;
805 int ret;
806
807 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
808 ISER_HEADERS_LEN, DMA_TO_DEVICE);
809
810 send_wr.next = NULL;
811 send_wr.wr_id = (unsigned long)tx_desc;
812 send_wr.sg_list = tx_desc->tx_sg;
813 send_wr.num_sge = tx_desc->num_sge;
814 send_wr.opcode = IB_WR_SEND;
815 send_wr.send_flags = IB_SEND_SIGNALED;
816
817 atomic_inc(&isert_conn->post_send_buf_count);
818
819 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
820 if (ret) {
821 pr_err("ib_post_send() failed, ret: %d\n", ret);
822 atomic_dec(&isert_conn->post_send_buf_count);
823 }
824
825 return ret;
826}
827
828static void
829isert_create_send_desc(struct isert_conn *isert_conn,
830 struct isert_cmd *isert_cmd,
831 struct iser_tx_desc *tx_desc)
832{
833 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
834
835 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
836 ISER_HEADERS_LEN, DMA_TO_DEVICE);
837
838 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
839 tx_desc->iser_header.flags = ISER_VER;
840
841 tx_desc->num_sge = 1;
842 tx_desc->isert_cmd = isert_cmd;
843
844 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
845 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
846 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
847 }
848}
849
850static int
851isert_init_tx_hdrs(struct isert_conn *isert_conn,
852 struct iser_tx_desc *tx_desc)
853{
854 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
855 u64 dma_addr;
856
857 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
858 ISER_HEADERS_LEN, DMA_TO_DEVICE);
859 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
860 pr_err("ib_dma_mapping_error() failed\n");
861 return -ENOMEM;
862 }
863
864 tx_desc->dma_addr = dma_addr;
865 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
866 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
867 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
868
869 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
870 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
871 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
872
873 return 0;
874}
875
876static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800877isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
878 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800879{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800880 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
881
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800882 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
883 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
884 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800885 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800886 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800887 /*
888 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
889 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
890 */
891 mutex_lock(&isert_conn->conn_comp_mutex);
892 if (coalesce &&
893 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
894 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
895 mutex_unlock(&isert_conn->conn_comp_mutex);
896 return;
897 }
898 isert_conn->conn_comp_batch = 0;
899 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
900 mutex_unlock(&isert_conn->conn_comp_mutex);
901
902 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800903}
904
905static int
906isert_rdma_post_recvl(struct isert_conn *isert_conn)
907{
908 struct ib_recv_wr rx_wr, *rx_wr_fail;
909 struct ib_sge sge;
910 int ret;
911
912 memset(&sge, 0, sizeof(struct ib_sge));
913 sge.addr = isert_conn->login_req_dma;
914 sge.length = ISER_RX_LOGIN_SIZE;
915 sge.lkey = isert_conn->conn_mr->lkey;
916
917 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
918 sge.addr, sge.length, sge.lkey);
919
920 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
921 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
922 rx_wr.sg_list = &sge;
923 rx_wr.num_sge = 1;
924
925 isert_conn->post_recv_buf_count++;
926 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
927 if (ret) {
928 pr_err("ib_post_recv() failed: %d\n", ret);
929 isert_conn->post_recv_buf_count--;
930 }
931
932 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
933 return ret;
934}
935
936static int
937isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
938 u32 length)
939{
940 struct isert_conn *isert_conn = conn->context;
941 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
942 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
943 int ret;
944
945 isert_create_send_desc(isert_conn, NULL, tx_desc);
946
947 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
948 sizeof(struct iscsi_hdr));
949
950 isert_init_tx_hdrs(isert_conn, tx_desc);
951
952 if (length > 0) {
953 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
954
955 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
956 length, DMA_TO_DEVICE);
957
958 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
959
960 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
961 length, DMA_TO_DEVICE);
962
963 tx_dsg->addr = isert_conn->login_rsp_dma;
964 tx_dsg->length = length;
965 tx_dsg->lkey = isert_conn->conn_mr->lkey;
966 tx_desc->num_sge = 2;
967 }
968 if (!login->login_failed) {
969 if (login->login_complete) {
970 ret = isert_alloc_rx_descriptors(isert_conn);
971 if (ret)
972 return ret;
973
974 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
975 if (ret)
976 return ret;
977
978 isert_conn->state = ISER_CONN_UP;
979 goto post_send;
980 }
981
982 ret = isert_rdma_post_recvl(isert_conn);
983 if (ret)
984 return ret;
985 }
986post_send:
987 ret = isert_post_send(isert_conn, tx_desc);
988 if (ret)
989 return ret;
990
991 return 0;
992}
993
994static void
995isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
996 struct isert_conn *isert_conn)
997{
998 struct iscsi_conn *conn = isert_conn->conn;
999 struct iscsi_login *login = conn->conn_login;
1000 int size;
1001
1002 if (!login) {
1003 pr_err("conn->conn_login is NULL\n");
1004 dump_stack();
1005 return;
1006 }
1007
1008 if (login->first_request) {
1009 struct iscsi_login_req *login_req =
1010 (struct iscsi_login_req *)&rx_desc->iscsi_header;
1011 /*
1012 * Setup the initial iscsi_login values from the leading
1013 * login request PDU.
1014 */
1015 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1016 login->current_stage =
1017 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
1018 >> 2;
1019 login->version_min = login_req->min_version;
1020 login->version_max = login_req->max_version;
1021 memcpy(login->isid, login_req->isid, 6);
1022 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1023 login->init_task_tag = login_req->itt;
1024 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1025 login->cid = be16_to_cpu(login_req->cid);
1026 login->tsih = be16_to_cpu(login_req->tsih);
1027 }
1028
1029 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1030
1031 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1032 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1033 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1034 memcpy(login->req_buf, &rx_desc->data[0], size);
1035
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001036 if (login->first_request) {
1037 complete(&isert_conn->conn_login_comp);
1038 return;
1039 }
1040 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001041}
1042
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001043static struct iscsi_cmd
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001044*isert_allocate_cmd(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001045{
1046 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1047 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001048 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001049
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001050 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001051 if (!cmd) {
1052 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001053 return NULL;
1054 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001055 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001056 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001057 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001058
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001059 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001060}
1061
1062static int
1063isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001064 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1065 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001066{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001067 struct iscsi_conn *conn = isert_conn->conn;
1068 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1069 struct scatterlist *sg;
1070 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1071 bool dump_payload = false;
1072
1073 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1074 if (rc < 0)
1075 return rc;
1076
1077 imm_data = cmd->immediate_data;
1078 imm_data_len = cmd->first_burst_len;
1079 unsol_data = cmd->unsolicited_data;
1080
1081 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1082 if (rc < 0) {
1083 return 0;
1084 } else if (rc > 0) {
1085 dump_payload = true;
1086 goto sequence_cmd;
1087 }
1088
1089 if (!imm_data)
1090 return 0;
1091
1092 sg = &cmd->se_cmd.t_data_sg[0];
1093 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1094
1095 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1096 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1097
1098 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1099
1100 cmd->write_data_done += imm_data_len;
1101
1102 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1103 spin_lock_bh(&cmd->istate_lock);
1104 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1105 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1106 spin_unlock_bh(&cmd->istate_lock);
1107 }
1108
1109sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001110 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001111
1112 if (!rc && dump_payload == false && unsol_data)
1113 iscsit_set_unsoliticed_dataout(cmd);
1114
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001115 return 0;
1116}
1117
1118static int
1119isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1120 struct iser_rx_desc *rx_desc, unsigned char *buf)
1121{
1122 struct scatterlist *sg_start;
1123 struct iscsi_conn *conn = isert_conn->conn;
1124 struct iscsi_cmd *cmd = NULL;
1125 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1126 u32 unsol_data_len = ntoh24(hdr->dlength);
1127 int rc, sg_nents, sg_off, page_off;
1128
1129 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1130 if (rc < 0)
1131 return rc;
1132 else if (!cmd)
1133 return 0;
1134 /*
1135 * FIXME: Unexpected unsolicited_data out
1136 */
1137 if (!cmd->unsolicited_data) {
1138 pr_err("Received unexpected solicited data payload\n");
1139 dump_stack();
1140 return -1;
1141 }
1142
1143 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1144 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1145
1146 sg_off = cmd->write_data_done / PAGE_SIZE;
1147 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1148 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1149 page_off = cmd->write_data_done % PAGE_SIZE;
1150 /*
1151 * FIXME: Non page-aligned unsolicited_data out
1152 */
1153 if (page_off) {
1154 pr_err("Received unexpected non-page aligned data payload\n");
1155 dump_stack();
1156 return -1;
1157 }
1158 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1159 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1160
1161 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1162 unsol_data_len);
1163
1164 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1165 if (rc < 0)
1166 return rc;
1167
1168 return 0;
1169}
1170
1171static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001172isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001173 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1174 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001175{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001176 struct iscsi_conn *conn = isert_conn->conn;
1177 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1178 int rc;
1179
1180 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1181 if (rc < 0)
1182 return rc;
1183 /*
1184 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1185 */
1186
1187 return iscsit_process_nop_out(conn, cmd, hdr);
1188}
1189
1190static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001191isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001192 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1193 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001194{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001195 struct iscsi_conn *conn = isert_conn->conn;
1196 u32 payload_length = ntoh24(hdr->dlength);
1197 int rc;
1198 unsigned char *text_in;
1199
1200 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1201 if (rc < 0)
1202 return rc;
1203
1204 text_in = kzalloc(payload_length, GFP_KERNEL);
1205 if (!text_in) {
1206 pr_err("Unable to allocate text_in of payload_length: %u\n",
1207 payload_length);
1208 return -ENOMEM;
1209 }
1210 cmd->text_in_ptr = text_in;
1211
1212 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1213
1214 return iscsit_process_text_cmd(conn, cmd, hdr);
1215}
1216
1217static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001218isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1219 uint32_t read_stag, uint64_t read_va,
1220 uint32_t write_stag, uint64_t write_va)
1221{
1222 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1223 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001224 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001225 struct iscsi_cmd *cmd;
1226 struct isert_cmd *isert_cmd;
1227 int ret = -EINVAL;
1228 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1229
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001230 if (sess->sess_ops->SessionType &&
1231 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1232 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1233 " ignoring\n", opcode);
1234 return 0;
1235 }
1236
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001237 switch (opcode) {
1238 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001239 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001240 if (!cmd)
1241 break;
1242
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001243 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001244 isert_cmd->read_stag = read_stag;
1245 isert_cmd->read_va = read_va;
1246 isert_cmd->write_stag = write_stag;
1247 isert_cmd->write_va = write_va;
1248
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001249 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001250 rx_desc, (unsigned char *)hdr);
1251 break;
1252 case ISCSI_OP_NOOP_OUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001253 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001254 if (!cmd)
1255 break;
1256
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001257 isert_cmd = iscsit_priv_cmd(cmd);
1258 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001259 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001260 break;
1261 case ISCSI_OP_SCSI_DATA_OUT:
1262 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1263 (unsigned char *)hdr);
1264 break;
1265 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001266 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001267 if (!cmd)
1268 break;
1269
1270 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1271 (unsigned char *)hdr);
1272 break;
1273 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001274 cmd = isert_allocate_cmd(conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001275 if (!cmd)
1276 break;
1277
1278 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1279 if (ret > 0)
1280 wait_for_completion_timeout(&conn->conn_logout_comp,
1281 SECONDS_FOR_LOGOUT_COMP *
1282 HZ);
1283 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001284 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00001285 cmd = isert_allocate_cmd(conn);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001286 if (!cmd)
1287 break;
1288
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001289 isert_cmd = iscsit_priv_cmd(cmd);
1290 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001291 rx_desc, (struct iscsi_text *)hdr);
1292 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001293 default:
1294 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1295 dump_stack();
1296 break;
1297 }
1298
1299 return ret;
1300}
1301
1302static void
1303isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1304{
1305 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1306 uint64_t read_va = 0, write_va = 0;
1307 uint32_t read_stag = 0, write_stag = 0;
1308 int rc;
1309
1310 switch (iser_hdr->flags & 0xF0) {
1311 case ISCSI_CTRL:
1312 if (iser_hdr->flags & ISER_RSV) {
1313 read_stag = be32_to_cpu(iser_hdr->read_stag);
1314 read_va = be64_to_cpu(iser_hdr->read_va);
1315 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1316 read_stag, (unsigned long long)read_va);
1317 }
1318 if (iser_hdr->flags & ISER_WSV) {
1319 write_stag = be32_to_cpu(iser_hdr->write_stag);
1320 write_va = be64_to_cpu(iser_hdr->write_va);
1321 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1322 write_stag, (unsigned long long)write_va);
1323 }
1324
1325 pr_debug("ISER ISCSI_CTRL PDU\n");
1326 break;
1327 case ISER_HELLO:
1328 pr_err("iSER Hello message\n");
1329 break;
1330 default:
1331 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1332 break;
1333 }
1334
1335 rc = isert_rx_opcode(isert_conn, rx_desc,
1336 read_stag, read_va, write_stag, write_va);
1337}
1338
1339static void
1340isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1341 unsigned long xfer_len)
1342{
1343 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1344 struct iscsi_hdr *hdr;
1345 u64 rx_dma;
1346 int rx_buflen, outstanding;
1347
1348 if ((char *)desc == isert_conn->login_req_buf) {
1349 rx_dma = isert_conn->login_req_dma;
1350 rx_buflen = ISER_RX_LOGIN_SIZE;
1351 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1352 rx_dma, rx_buflen);
1353 } else {
1354 rx_dma = desc->dma_addr;
1355 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1356 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1357 rx_dma, rx_buflen);
1358 }
1359
1360 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1361
1362 hdr = &desc->iscsi_header;
1363 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1364 hdr->opcode, hdr->itt, hdr->flags,
1365 (int)(xfer_len - ISER_HEADERS_LEN));
1366
1367 if ((char *)desc == isert_conn->login_req_buf)
1368 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1369 isert_conn);
1370 else
1371 isert_rx_do_work(desc, isert_conn);
1372
1373 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1374 DMA_FROM_DEVICE);
1375
1376 isert_conn->post_recv_buf_count--;
1377 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1378 isert_conn->post_recv_buf_count);
1379
1380 if ((char *)desc == isert_conn->login_req_buf)
1381 return;
1382
1383 outstanding = isert_conn->post_recv_buf_count;
1384 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1385 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1386 ISERT_MIN_POSTED_RX);
1387 err = isert_post_recv(isert_conn, count);
1388 if (err) {
1389 pr_err("isert_post_recv() count: %d failed, %d\n",
1390 count, err);
1391 }
1392 }
1393}
1394
1395static void
1396isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1397{
1398 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1399 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1400
Vu Pham90ecc6e2013-08-28 23:23:33 +03001401 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001402 if (wr->sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001403 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1404 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1405 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1406 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001407 wr->sge = NULL;
1408 }
1409
Vu Pham90ecc6e2013-08-28 23:23:33 +03001410 if (wr->send_wr) {
1411 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1412 kfree(wr->send_wr);
1413 wr->send_wr = NULL;
1414 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001415
Vu Pham90ecc6e2013-08-28 23:23:33 +03001416 if (wr->ib_sge) {
1417 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1418 kfree(wr->ib_sge);
1419 wr->ib_sge = NULL;
1420 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001421}
1422
1423static void
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001424isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
Vu Pham59464ef2013-08-28 23:23:35 +03001425{
1426 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1427 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1428 LIST_HEAD(unmap_list);
1429
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001430 pr_debug("unreg_fastreg_cmd: %p\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001431
1432 if (wr->fr_desc) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001433 pr_debug("unreg_fastreg_cmd: %p free fr_desc %p\n",
Vu Pham59464ef2013-08-28 23:23:35 +03001434 isert_cmd, wr->fr_desc);
1435 spin_lock_bh(&isert_conn->conn_lock);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001436 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
Vu Pham59464ef2013-08-28 23:23:35 +03001437 spin_unlock_bh(&isert_conn->conn_lock);
1438 wr->fr_desc = NULL;
1439 }
1440
1441 if (wr->sge) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02001442 pr_debug("unreg_fastreg_cmd: %p unmap_sg op\n", isert_cmd);
Vu Pham59464ef2013-08-28 23:23:35 +03001443 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1444 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1445 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1446 wr->sge = NULL;
1447 }
1448
1449 wr->ib_sge = NULL;
1450 wr->send_wr = NULL;
1451}
1452
1453static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001454isert_put_cmd(struct isert_cmd *isert_cmd)
1455{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001456 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001457 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001458 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001459 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001460
1461 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1462
1463 switch (cmd->iscsi_opcode) {
1464 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001465 spin_lock_bh(&conn->cmd_lock);
1466 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001467 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001468 spin_unlock_bh(&conn->cmd_lock);
1469
1470 if (cmd->data_direction == DMA_TO_DEVICE)
1471 iscsit_stop_dataout_timer(cmd);
1472
Vu Phamd40945d2013-08-28 23:23:34 +03001473 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001474 transport_generic_free_cmd(&cmd->se_cmd, 0);
1475 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001476 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001477 spin_lock_bh(&conn->cmd_lock);
1478 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001479 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001480 spin_unlock_bh(&conn->cmd_lock);
1481
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001482 transport_generic_free_cmd(&cmd->se_cmd, 0);
1483 break;
1484 case ISCSI_OP_REJECT:
1485 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001486 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001487 spin_lock_bh(&conn->cmd_lock);
1488 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellinger5159d762014-02-03 12:53:51 -08001489 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001490 spin_unlock_bh(&conn->cmd_lock);
1491
1492 /*
1493 * Handle special case for REJECT when iscsi_add_reject*() has
1494 * overwritten the original iscsi_opcode assignment, and the
1495 * associated cmd->se_cmd needs to be released.
1496 */
1497 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001498 pr_debug("Calling transport_generic_free_cmd from"
1499 " isert_put_cmd for 0x%02x\n",
1500 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001501 transport_generic_free_cmd(&cmd->se_cmd, 0);
1502 break;
1503 }
1504 /*
1505 * Fall-through
1506 */
1507 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001508 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001509 break;
1510 }
1511}
1512
1513static void
1514isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1515{
1516 if (tx_desc->dma_addr != 0) {
1517 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1518 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1519 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1520 tx_desc->dma_addr = 0;
1521 }
1522}
1523
1524static void
1525isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1526 struct ib_device *ib_dev)
1527{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001528 if (isert_cmd->pdu_buf_dma != 0) {
1529 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1530 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1531 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1532 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001533 }
1534
1535 isert_unmap_tx_desc(tx_desc, ib_dev);
1536 isert_put_cmd(isert_cmd);
1537}
1538
1539static void
1540isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1541 struct isert_cmd *isert_cmd)
1542{
1543 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001544 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001545 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001546 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001547 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001548
1549 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001550 device->unreg_rdma_mem(isert_cmd, isert_conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001551 cmd->write_data_done = wr->cur_rdma_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001552
Vu Pham90ecc6e2013-08-28 23:23:33 +03001553 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001554 spin_lock_bh(&cmd->istate_lock);
1555 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1556 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1557 spin_unlock_bh(&cmd->istate_lock);
1558
1559 target_execute_cmd(se_cmd);
1560}
1561
1562static void
1563isert_do_control_comp(struct work_struct *work)
1564{
1565 struct isert_cmd *isert_cmd = container_of(work,
1566 struct isert_cmd, comp_work);
1567 struct isert_conn *isert_conn = isert_cmd->conn;
1568 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001569 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001570
1571 switch (cmd->i_state) {
1572 case ISTATE_SEND_TASKMGTRSP:
1573 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1574
1575 atomic_dec(&isert_conn->post_send_buf_count);
1576 iscsit_tmr_post_handler(cmd, cmd->conn);
1577
1578 cmd->i_state = ISTATE_SENT_STATUS;
1579 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1580 break;
1581 case ISTATE_SEND_REJECT:
1582 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1583 atomic_dec(&isert_conn->post_send_buf_count);
1584
1585 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001586 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001587 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001588 case ISTATE_SEND_LOGOUTRSP:
1589 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1590 /*
1591 * Call atomic_dec(&isert_conn->post_send_buf_count)
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001592 * from isert_wait_conn()
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001593 */
1594 isert_conn->logout_posted = true;
1595 iscsit_logout_post_handler(cmd, cmd->conn);
1596 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001597 case ISTATE_SEND_TEXTRSP:
1598 atomic_dec(&isert_conn->post_send_buf_count);
1599 cmd->i_state = ISTATE_SENT_STATUS;
1600 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1601 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001602 default:
1603 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1604 dump_stack();
1605 break;
1606 }
1607}
1608
1609static void
1610isert_response_completion(struct iser_tx_desc *tx_desc,
1611 struct isert_cmd *isert_cmd,
1612 struct isert_conn *isert_conn,
1613 struct ib_device *ib_dev)
1614{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001615 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001616
1617 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001618 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001619 cmd->i_state == ISTATE_SEND_REJECT ||
1620 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001621 isert_unmap_tx_desc(tx_desc, ib_dev);
1622
1623 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1624 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1625 return;
1626 }
1627 atomic_dec(&isert_conn->post_send_buf_count);
1628
1629 cmd->i_state = ISTATE_SENT_STATUS;
1630 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1631}
1632
1633static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001634__isert_send_completion(struct iser_tx_desc *tx_desc,
1635 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001636{
1637 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1638 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1639 struct isert_rdma_wr *wr;
1640
1641 if (!isert_cmd) {
1642 atomic_dec(&isert_conn->post_send_buf_count);
1643 isert_unmap_tx_desc(tx_desc, ib_dev);
1644 return;
1645 }
1646 wr = &isert_cmd->rdma_wr;
1647
1648 switch (wr->iser_ib_op) {
1649 case ISER_IB_RECV:
1650 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1651 dump_stack();
1652 break;
1653 case ISER_IB_SEND:
1654 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1655 isert_response_completion(tx_desc, isert_cmd,
1656 isert_conn, ib_dev);
1657 break;
1658 case ISER_IB_RDMA_WRITE:
1659 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1660 dump_stack();
1661 break;
1662 case ISER_IB_RDMA_READ:
1663 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1664
1665 atomic_dec(&isert_conn->post_send_buf_count);
1666 isert_completion_rdma_read(tx_desc, isert_cmd);
1667 break;
1668 default:
1669 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1670 dump_stack();
1671 break;
1672 }
1673}
1674
1675static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001676isert_send_completion(struct iser_tx_desc *tx_desc,
1677 struct isert_conn *isert_conn)
1678{
1679 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1680 struct iser_tx_desc *t;
1681 /*
1682 * Drain coalesced completion llist starting from comp_llnode_batch
1683 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1684 */
1685 while (llnode) {
1686 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1687 llnode = llist_next(llnode);
1688 __isert_send_completion(t, isert_conn);
1689 }
1690 __isert_send_completion(tx_desc, isert_conn);
1691}
1692
1693static void
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001694isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001695{
1696 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001697 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001698
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001699 if (!isert_cmd)
1700 isert_unmap_tx_desc(tx_desc, ib_dev);
1701 else
1702 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1703}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001705static void
1706isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1707{
1708 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1709 struct iscsi_conn *conn = isert_conn->conn;
1710
1711 if (isert_conn->post_recv_buf_count)
1712 return;
1713
1714 if (conn->sess) {
1715 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1716 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001717 }
1718
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001719 while (atomic_read(&isert_conn->post_send_buf_count))
1720 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001721
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001722 mutex_lock(&isert_conn->conn_mutex);
1723 isert_conn->state = ISER_CONN_DOWN;
1724 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07001725
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001726 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001727}
1728
1729static void
1730isert_cq_tx_work(struct work_struct *work)
1731{
1732 struct isert_cq_desc *cq_desc = container_of(work,
1733 struct isert_cq_desc, cq_tx_work);
1734 struct isert_device *device = cq_desc->device;
1735 int cq_index = cq_desc->cq_index;
1736 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1737 struct isert_conn *isert_conn;
1738 struct iser_tx_desc *tx_desc;
1739 struct ib_wc wc;
1740
1741 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1742 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1743 isert_conn = wc.qp->qp_context;
1744
1745 if (wc.status == IB_WC_SUCCESS) {
1746 isert_send_completion(tx_desc, isert_conn);
1747 } else {
1748 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1749 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001750 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001751
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001752 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001753 isert_cq_tx_comp_err(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001754 }
1755 }
1756
1757 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1758}
1759
1760static void
1761isert_cq_tx_callback(struct ib_cq *cq, void *context)
1762{
1763 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1764
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001765 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1766}
1767
1768static void
1769isert_cq_rx_work(struct work_struct *work)
1770{
1771 struct isert_cq_desc *cq_desc = container_of(work,
1772 struct isert_cq_desc, cq_rx_work);
1773 struct isert_device *device = cq_desc->device;
1774 int cq_index = cq_desc->cq_index;
1775 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1776 struct isert_conn *isert_conn;
1777 struct iser_rx_desc *rx_desc;
1778 struct ib_wc wc;
1779 unsigned long xfer_len;
1780
1781 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1782 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1783 isert_conn = wc.qp->qp_context;
1784
1785 if (wc.status == IB_WC_SUCCESS) {
1786 xfer_len = (unsigned long)wc.byte_len;
1787 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1788 } else {
1789 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001790 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001791 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001792 pr_debug("RX wc.vendor_err: 0x%08x\n",
1793 wc.vendor_err);
1794 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001795 isert_conn->post_recv_buf_count--;
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08001796 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001797 }
1798 }
1799
1800 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1801}
1802
1803static void
1804isert_cq_rx_callback(struct ib_cq *cq, void *context)
1805{
1806 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1807
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001808 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1809}
1810
1811static int
1812isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1813{
1814 struct ib_send_wr *wr_failed;
1815 int ret;
1816
1817 atomic_inc(&isert_conn->post_send_buf_count);
1818
1819 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1820 &wr_failed);
1821 if (ret) {
1822 pr_err("ib_post_send failed with %d\n", ret);
1823 atomic_dec(&isert_conn->post_send_buf_count);
1824 return ret;
1825 }
1826 return ret;
1827}
1828
1829static int
1830isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1831{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001832 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001833 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1834 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1835 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1836 &isert_cmd->tx_desc.iscsi_header;
1837
1838 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1839 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1840 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1841 /*
1842 * Attach SENSE DATA payload to iSCSI Response PDU
1843 */
1844 if (cmd->se_cmd.sense_buffer &&
1845 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1846 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1847 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1848 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001849 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001850
1851 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1852 cmd->sense_buffer);
1853 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1854
1855 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1856 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001857 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001858
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001859 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1860 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861 DMA_TO_DEVICE);
1862
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001863 isert_cmd->pdu_buf_len = pdu_len;
1864 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1865 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001866 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1867 isert_cmd->tx_desc.num_sge = 2;
1868 }
1869
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001870 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001871
1872 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1873
1874 return isert_post_response(isert_conn, isert_cmd);
1875}
1876
1877static int
1878isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1879 bool nopout_response)
1880{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001881 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001882 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1883 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1884
1885 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1886 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1887 &isert_cmd->tx_desc.iscsi_header,
1888 nopout_response);
1889 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001890 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001891
Masanari Iida8b513d02013-05-21 23:13:12 +09001892 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001893
1894 return isert_post_response(isert_conn, isert_cmd);
1895}
1896
1897static int
1898isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1899{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001900 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001901 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1902 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1903
1904 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1905 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1906 &isert_cmd->tx_desc.iscsi_header);
1907 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001908 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001909
1910 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1911
1912 return isert_post_response(isert_conn, isert_cmd);
1913}
1914
1915static int
1916isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1917{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001918 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001919 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1920 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1921
1922 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1923 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1924 &isert_cmd->tx_desc.iscsi_header);
1925 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001926 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001927
1928 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1929
1930 return isert_post_response(isert_conn, isert_cmd);
1931}
1932
1933static int
1934isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1935{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001936 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001937 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1938 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001939 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1940 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1941 struct iscsi_reject *hdr =
1942 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001943
1944 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001945 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001946 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001947
1948 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001949 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001950 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1951 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001952 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1953 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001954 tx_dsg->length = ISCSI_HDR_LEN;
1955 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1956 isert_cmd->tx_desc.num_sge = 2;
1957
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001958 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001959
1960 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1961
1962 return isert_post_response(isert_conn, isert_cmd);
1963}
1964
1965static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001966isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1967{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001968 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001969 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1970 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1971 struct iscsi_text_rsp *hdr =
1972 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1973 u32 txt_rsp_len;
1974 int rc;
1975
1976 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1977 rc = iscsit_build_text_rsp(cmd, conn, hdr);
1978 if (rc < 0)
1979 return rc;
1980
1981 txt_rsp_len = rc;
1982 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1983
1984 if (txt_rsp_len) {
1985 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1986 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1987 void *txt_rsp_buf = cmd->buf_ptr;
1988
1989 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1990 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1991
1992 isert_cmd->pdu_buf_len = txt_rsp_len;
1993 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1994 tx_dsg->length = txt_rsp_len;
1995 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1996 isert_cmd->tx_desc.num_sge = 2;
1997 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001998 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001999
2000 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
2001
2002 return isert_post_response(isert_conn, isert_cmd);
2003}
2004
2005static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002006isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
2007 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
2008 u32 data_left, u32 offset)
2009{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002010 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002011 struct scatterlist *sg_start, *tmp_sg;
2012 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2013 u32 sg_off, page_off;
2014 int i = 0, sg_nents;
2015
2016 sg_off = offset / PAGE_SIZE;
2017 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2018 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
2019 page_off = offset % PAGE_SIZE;
2020
2021 send_wr->sg_list = ib_sge;
2022 send_wr->num_sge = sg_nents;
2023 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2024 /*
2025 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
2026 */
2027 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2028 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
2029 (unsigned long long)tmp_sg->dma_address,
2030 tmp_sg->length, page_off);
2031
2032 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
2033 ib_sge->length = min_t(u32, data_left,
2034 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2035 ib_sge->lkey = isert_conn->conn_mr->lkey;
2036
Vu Pham90ecc6e2013-08-28 23:23:33 +03002037 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2038 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002039 page_off = 0;
2040 data_left -= ib_sge->length;
2041 ib_sge++;
2042 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2043 }
2044
2045 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2046 send_wr->sg_list, send_wr->num_sge);
2047
2048 return sg_nents;
2049}
2050
2051static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002052isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2053 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002054{
2055 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002056 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002057 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002058 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002059 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002060 struct ib_sge *ib_sge;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002061 struct scatterlist *sg_start;
2062 u32 sg_off = 0, sg_nents;
2063 u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
2064 int ret = 0, count, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002065
Vu Pham90ecc6e2013-08-28 23:23:33 +03002066 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2067 data_left = se_cmd->data_length;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002068 } else {
2069 sg_off = cmd->write_data_done / PAGE_SIZE;
2070 data_left = se_cmd->data_length - cmd->write_data_done;
2071 offset = cmd->write_data_done;
2072 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2073 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002074
Vu Pham90ecc6e2013-08-28 23:23:33 +03002075 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2076 sg_nents = se_cmd->t_data_nents - sg_off;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002077
Vu Pham90ecc6e2013-08-28 23:23:33 +03002078 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2079 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2080 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081 if (unlikely(!count)) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002082 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002083 return -EINVAL;
2084 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002085 wr->sge = sg_start;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002086 wr->num_sge = sg_nents;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002087 wr->cur_rdma_length = data_left;
2088 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2089 isert_cmd, count, sg_start, sg_nents, data_left);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002090
2091 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2092 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002093 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002094 ret = -ENOMEM;
2095 goto unmap_sg;
2096 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002097 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002098
2099 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2100 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2101 GFP_KERNEL);
2102 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002103 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104 ret = -ENOMEM;
2105 goto unmap_sg;
2106 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002107
2108 wr->isert_cmd = isert_cmd;
2109 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002110
2111 for (i = 0; i < wr->send_wr_num; i++) {
2112 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2113 data_len = min(data_left, rdma_write_max);
2114
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002115 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002116 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2117 send_wr->opcode = IB_WR_RDMA_WRITE;
2118 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2119 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2120 if (i + 1 == wr->send_wr_num)
2121 send_wr->next = &isert_cmd->tx_desc.send_wr;
2122 else
2123 send_wr->next = &wr->send_wr[i + 1];
2124 } else {
2125 send_wr->opcode = IB_WR_RDMA_READ;
2126 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2127 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2128 if (i + 1 == wr->send_wr_num)
2129 send_wr->send_flags = IB_SEND_SIGNALED;
2130 else
2131 send_wr->next = &wr->send_wr[i + 1];
2132 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002133
2134 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2135 send_wr, data_len, offset);
2136 ib_sge += ib_sge_cnt;
2137
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002138 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002139 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002140 data_left -= data_len;
2141 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002142
2143 return 0;
2144unmap_sg:
2145 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2146 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2147 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2148 return ret;
2149}
2150
2151static int
Vu Pham59464ef2013-08-28 23:23:35 +03002152isert_map_fr_pagelist(struct ib_device *ib_dev,
2153 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2154{
2155 u64 start_addr, end_addr, page, chunk_start = 0;
2156 struct scatterlist *tmp_sg;
2157 int i = 0, new_chunk, last_ent, n_pages;
2158
2159 n_pages = 0;
2160 new_chunk = 1;
2161 last_ent = sg_nents - 1;
2162 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2163 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2164 if (new_chunk)
2165 chunk_start = start_addr;
2166 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2167
2168 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2169 i, (unsigned long long)tmp_sg->dma_address,
2170 tmp_sg->length);
2171
2172 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2173 new_chunk = 0;
2174 continue;
2175 }
2176 new_chunk = 1;
2177
2178 page = chunk_start & PAGE_MASK;
2179 do {
2180 fr_pl[n_pages++] = page;
2181 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2182 n_pages - 1, page);
2183 page += PAGE_SIZE;
2184 } while (page < end_addr);
2185 }
2186
2187 return n_pages;
2188}
2189
2190static int
2191isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002192 struct isert_conn *isert_conn, struct scatterlist *sg_start,
2193 struct ib_sge *ib_sge, u32 sg_nents, u32 offset,
2194 unsigned int data_len)
Vu Pham59464ef2013-08-28 23:23:35 +03002195{
Vu Pham59464ef2013-08-28 23:23:35 +03002196 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Vu Pham59464ef2013-08-28 23:23:35 +03002197 struct ib_send_wr fr_wr, inv_wr;
2198 struct ib_send_wr *bad_wr, *wr = NULL;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002199 int ret, pagelist_len;
2200 u32 page_off;
Vu Pham59464ef2013-08-28 23:23:35 +03002201 u8 key;
Vu Pham59464ef2013-08-28 23:23:35 +03002202
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002203 sg_nents = min_t(unsigned int, sg_nents, ISCSI_ISER_SG_TABLESIZE);
Vu Pham59464ef2013-08-28 23:23:35 +03002204 page_off = offset % PAGE_SIZE;
2205
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002206 pr_debug("Use fr_desc %p sg_nents %d offset %u\n",
2207 fr_desc, sg_nents, offset);
Vu Pham59464ef2013-08-28 23:23:35 +03002208
2209 pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
2210 &fr_desc->data_frpl->page_list[0]);
2211
2212 if (!fr_desc->valid) {
2213 memset(&inv_wr, 0, sizeof(inv_wr));
2214 inv_wr.opcode = IB_WR_LOCAL_INV;
2215 inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
2216 wr = &inv_wr;
2217 /* Bump the key */
2218 key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
2219 ib_update_fast_reg_key(fr_desc->data_mr, ++key);
2220 }
2221
2222 /* Prepare FASTREG WR */
2223 memset(&fr_wr, 0, sizeof(fr_wr));
2224 fr_wr.opcode = IB_WR_FAST_REG_MR;
2225 fr_wr.wr.fast_reg.iova_start =
2226 fr_desc->data_frpl->page_list[0] + page_off;
2227 fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
2228 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2229 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2230 fr_wr.wr.fast_reg.length = data_len;
2231 fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
2232 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2233
2234 if (!wr)
2235 wr = &fr_wr;
2236 else
2237 wr->next = &fr_wr;
2238
2239 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2240 if (ret) {
2241 pr_err("fast registration failed, ret:%d\n", ret);
2242 return ret;
2243 }
2244 fr_desc->valid = false;
2245
2246 ib_sge->lkey = fr_desc->data_mr->lkey;
2247 ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2248 ib_sge->length = data_len;
2249
2250 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2251 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2252
2253 return ret;
2254}
2255
2256static int
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002257isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2258 struct isert_rdma_wr *wr)
Vu Pham59464ef2013-08-28 23:23:35 +03002259{
2260 struct se_cmd *se_cmd = &cmd->se_cmd;
2261 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2262 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2263 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2264 struct ib_send_wr *send_wr;
2265 struct ib_sge *ib_sge;
2266 struct scatterlist *sg_start;
2267 struct fast_reg_descriptor *fr_desc;
2268 u32 sg_off = 0, sg_nents;
2269 u32 offset = 0, data_len, data_left, rdma_write_max;
2270 int ret = 0, count;
2271 unsigned long flags;
2272
2273 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2274 data_left = se_cmd->data_length;
Vu Pham59464ef2013-08-28 23:23:35 +03002275 } else {
Vu Pham59464ef2013-08-28 23:23:35 +03002276 offset = cmd->write_data_done;
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002277 sg_off = offset / PAGE_SIZE;
2278 data_left = se_cmd->data_length - cmd->write_data_done;
Vu Pham59464ef2013-08-28 23:23:35 +03002279 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2280 }
2281
2282 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2283 sg_nents = se_cmd->t_data_nents - sg_off;
2284
2285 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2286 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2287 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2288 if (unlikely(!count)) {
2289 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2290 return -EINVAL;
2291 }
2292 wr->sge = sg_start;
2293 wr->num_sge = sg_nents;
2294 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2295 isert_cmd, count, sg_start, sg_nents, data_left);
2296
2297 memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
2298 ib_sge = &wr->s_ib_sge;
2299 wr->ib_sge = ib_sge;
2300
2301 wr->send_wr_num = 1;
2302 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2303 wr->send_wr = &wr->s_send_wr;
2304
2305 wr->isert_cmd = isert_cmd;
2306 rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
2307
2308 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2309 send_wr->sg_list = ib_sge;
2310 send_wr->num_sge = 1;
2311 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2312 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2313 send_wr->opcode = IB_WR_RDMA_WRITE;
2314 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2315 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2316 send_wr->send_flags = 0;
2317 send_wr->next = &isert_cmd->tx_desc.send_wr;
2318 } else {
2319 send_wr->opcode = IB_WR_RDMA_READ;
2320 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2321 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2322 send_wr->send_flags = IB_SEND_SIGNALED;
2323 }
2324
2325 data_len = min(data_left, rdma_write_max);
2326 wr->cur_rdma_length = data_len;
2327
Vu Phamf01b9f72013-11-11 19:04:29 +02002328 /* if there is a single dma entry, dma mr is sufficient */
2329 if (count == 1) {
2330 ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
2331 ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
2332 ib_sge->lkey = isert_conn->conn_mr->lkey;
2333 wr->fr_desc = NULL;
2334 } else {
2335 spin_lock_irqsave(&isert_conn->conn_lock, flags);
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002336 fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
Vu Phamf01b9f72013-11-11 19:04:29 +02002337 struct fast_reg_descriptor, list);
2338 list_del(&fr_desc->list);
2339 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2340 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002341
Sagi Grimberg9bd626e2014-01-09 18:40:54 +02002342 ret = isert_fast_reg_mr(fr_desc, isert_conn, sg_start,
2343 ib_sge, sg_nents, offset, data_len);
Vu Phamf01b9f72013-11-11 19:04:29 +02002344 if (ret) {
Sagi Grimberga3a5a822014-01-09 18:40:50 +02002345 list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
Vu Phamf01b9f72013-11-11 19:04:29 +02002346 goto unmap_sg;
2347 }
Vu Pham59464ef2013-08-28 23:23:35 +03002348 }
2349
2350 return 0;
2351
2352unmap_sg:
2353 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2354 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2355 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2356 return ret;
2357}
2358
2359static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002360isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2361{
2362 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002363 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002364 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2365 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002366 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002367 struct ib_send_wr *wr_failed;
2368 int rc;
2369
2370 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2371 isert_cmd, se_cmd->data_length);
2372 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002373 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002374 if (rc) {
2375 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2376 return rc;
2377 }
2378
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002379 /*
2380 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2381 */
2382 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger04d9cd12013-11-12 18:05:07 -08002383 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002384 &isert_cmd->tx_desc.iscsi_header);
2385 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002386 isert_init_send_wr(isert_conn, isert_cmd,
2387 &isert_cmd->tx_desc.send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002388
2389 atomic_inc(&isert_conn->post_send_buf_count);
2390
2391 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2392 if (rc) {
2393 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2394 atomic_dec(&isert_conn->post_send_buf_count);
2395 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002396 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
2397 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002398
Vu Pham90ecc6e2013-08-28 23:23:33 +03002399 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002400}
2401
2402static int
2403isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2404{
2405 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002406 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002407 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2408 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002409 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002410 struct ib_send_wr *wr_failed;
2411 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002412
Vu Pham90ecc6e2013-08-28 23:23:33 +03002413 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2414 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002415 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002416 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002417 if (rc) {
2418 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2419 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002420 }
2421
2422 atomic_inc(&isert_conn->post_send_buf_count);
2423
2424 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2425 if (rc) {
2426 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2427 atomic_dec(&isert_conn->post_send_buf_count);
2428 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002429 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2430 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002431
Vu Pham90ecc6e2013-08-28 23:23:33 +03002432 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002433}
2434
2435static int
2436isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2437{
2438 int ret;
2439
2440 switch (state) {
2441 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2442 ret = isert_put_nopin(cmd, conn, false);
2443 break;
2444 default:
2445 pr_err("Unknown immediate state: 0x%02x\n", state);
2446 ret = -EINVAL;
2447 break;
2448 }
2449
2450 return ret;
2451}
2452
2453static int
2454isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2455{
2456 int ret;
2457
2458 switch (state) {
2459 case ISTATE_SEND_LOGOUTRSP:
2460 ret = isert_put_logout_rsp(cmd, conn);
2461 if (!ret) {
2462 pr_debug("Returning iSER Logout -EAGAIN\n");
2463 ret = -EAGAIN;
2464 }
2465 break;
2466 case ISTATE_SEND_NOPIN:
2467 ret = isert_put_nopin(cmd, conn, true);
2468 break;
2469 case ISTATE_SEND_TASKMGTRSP:
2470 ret = isert_put_tm_rsp(cmd, conn);
2471 break;
2472 case ISTATE_SEND_REJECT:
2473 ret = isert_put_reject(cmd, conn);
2474 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002475 case ISTATE_SEND_TEXTRSP:
2476 ret = isert_put_text_rsp(cmd, conn);
2477 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002478 case ISTATE_SEND_STATUS:
2479 /*
2480 * Special case for sending non GOOD SCSI status from TX thread
2481 * context during pre se_cmd excecution failure.
2482 */
2483 ret = isert_put_response(conn, cmd);
2484 break;
2485 default:
2486 pr_err("Unknown response state: 0x%02x\n", state);
2487 ret = -EINVAL;
2488 break;
2489 }
2490
2491 return ret;
2492}
2493
2494static int
2495isert_setup_np(struct iscsi_np *np,
2496 struct __kernel_sockaddr_storage *ksockaddr)
2497{
2498 struct isert_np *isert_np;
2499 struct rdma_cm_id *isert_lid;
2500 struct sockaddr *sa;
2501 int ret;
2502
2503 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2504 if (!isert_np) {
2505 pr_err("Unable to allocate struct isert_np\n");
2506 return -ENOMEM;
2507 }
2508 init_waitqueue_head(&isert_np->np_accept_wq);
2509 mutex_init(&isert_np->np_accept_mutex);
2510 INIT_LIST_HEAD(&isert_np->np_accept_list);
2511 init_completion(&isert_np->np_login_comp);
2512
2513 sa = (struct sockaddr *)ksockaddr;
2514 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2515 /*
2516 * Setup the np->np_sockaddr from the passed sockaddr setup
2517 * in iscsi_target_configfs.c code..
2518 */
2519 memcpy(&np->np_sockaddr, ksockaddr,
2520 sizeof(struct __kernel_sockaddr_storage));
2521
2522 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2523 IB_QPT_RC);
2524 if (IS_ERR(isert_lid)) {
2525 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2526 PTR_ERR(isert_lid));
2527 ret = PTR_ERR(isert_lid);
2528 goto out;
2529 }
2530
2531 ret = rdma_bind_addr(isert_lid, sa);
2532 if (ret) {
2533 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2534 goto out_lid;
2535 }
2536
2537 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2538 if (ret) {
2539 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2540 goto out_lid;
2541 }
2542
2543 isert_np->np_cm_id = isert_lid;
2544 np->np_context = isert_np;
2545 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2546
2547 return 0;
2548
2549out_lid:
2550 rdma_destroy_id(isert_lid);
2551out:
2552 kfree(isert_np);
2553 return ret;
2554}
2555
2556static int
2557isert_check_accept_queue(struct isert_np *isert_np)
2558{
2559 int empty;
2560
2561 mutex_lock(&isert_np->np_accept_mutex);
2562 empty = list_empty(&isert_np->np_accept_list);
2563 mutex_unlock(&isert_np->np_accept_mutex);
2564
2565 return empty;
2566}
2567
2568static int
2569isert_rdma_accept(struct isert_conn *isert_conn)
2570{
2571 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2572 struct rdma_conn_param cp;
2573 int ret;
2574
2575 memset(&cp, 0, sizeof(struct rdma_conn_param));
2576 cp.responder_resources = isert_conn->responder_resources;
2577 cp.initiator_depth = isert_conn->initiator_depth;
2578 cp.retry_count = 7;
2579 cp.rnr_retry_count = 7;
2580
2581 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2582
2583 ret = rdma_accept(cm_id, &cp);
2584 if (ret) {
2585 pr_err("rdma_accept() failed with: %d\n", ret);
2586 return ret;
2587 }
2588
2589 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2590
2591 return 0;
2592}
2593
2594static int
2595isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2596{
2597 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2598 int ret;
2599
2600 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002601 /*
2602 * For login requests after the first PDU, isert_rx_login_req() will
2603 * kick schedule_delayed_work(&conn->login_work) as the packet is
2604 * received, which turns this callback from iscsi_target_do_login_rx()
2605 * into a NOP.
2606 */
2607 if (!login->first_request)
2608 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002609
2610 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2611 if (ret)
2612 return ret;
2613
2614 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2615 return 0;
2616}
2617
2618static void
2619isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2620 struct isert_conn *isert_conn)
2621{
2622 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2623 struct rdma_route *cm_route = &cm_id->route;
2624 struct sockaddr_in *sock_in;
2625 struct sockaddr_in6 *sock_in6;
2626
2627 conn->login_family = np->np_sockaddr.ss_family;
2628
2629 if (np->np_sockaddr.ss_family == AF_INET6) {
2630 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2631 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2632 &sock_in6->sin6_addr.in6_u);
2633 conn->login_port = ntohs(sock_in6->sin6_port);
2634
2635 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2636 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2637 &sock_in6->sin6_addr.in6_u);
2638 conn->local_port = ntohs(sock_in6->sin6_port);
2639 } else {
2640 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2641 sprintf(conn->login_ip, "%pI4",
2642 &sock_in->sin_addr.s_addr);
2643 conn->login_port = ntohs(sock_in->sin_port);
2644
2645 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2646 sprintf(conn->local_ip, "%pI4",
2647 &sock_in->sin_addr.s_addr);
2648 conn->local_port = ntohs(sock_in->sin_port);
2649 }
2650}
2651
2652static int
2653isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2654{
2655 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2656 struct isert_conn *isert_conn;
2657 int max_accept = 0, ret;
2658
2659accept_wait:
2660 ret = wait_event_interruptible(isert_np->np_accept_wq,
2661 !isert_check_accept_queue(isert_np) ||
2662 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2663 if (max_accept > 5)
2664 return -ENODEV;
2665
2666 spin_lock_bh(&np->np_thread_lock);
2667 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2668 spin_unlock_bh(&np->np_thread_lock);
2669 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2670 return -ENODEV;
2671 }
2672 spin_unlock_bh(&np->np_thread_lock);
2673
2674 mutex_lock(&isert_np->np_accept_mutex);
2675 if (list_empty(&isert_np->np_accept_list)) {
2676 mutex_unlock(&isert_np->np_accept_mutex);
2677 max_accept++;
2678 goto accept_wait;
2679 }
2680 isert_conn = list_first_entry(&isert_np->np_accept_list,
2681 struct isert_conn, conn_accept_node);
2682 list_del_init(&isert_conn->conn_accept_node);
2683 mutex_unlock(&isert_np->np_accept_mutex);
2684
2685 conn->context = isert_conn;
2686 isert_conn->conn = conn;
2687 max_accept = 0;
2688
2689 ret = isert_rdma_post_recvl(isert_conn);
2690 if (ret)
2691 return ret;
2692
2693 ret = isert_rdma_accept(isert_conn);
2694 if (ret)
2695 return ret;
2696
2697 isert_set_conn_info(np, conn, isert_conn);
2698
2699 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2700 return 0;
2701}
2702
2703static void
2704isert_free_np(struct iscsi_np *np)
2705{
2706 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2707
2708 rdma_destroy_id(isert_np->np_cm_id);
2709
2710 np->np_context = NULL;
2711 kfree(isert_np);
2712}
2713
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002714static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002715{
2716 struct isert_conn *isert_conn = conn->context;
2717
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002718 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002719 /*
2720 * Decrement post_send_buf_count for special case when called
2721 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2722 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002723 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002724 if (isert_conn->logout_posted)
2725 atomic_dec(&isert_conn->post_send_buf_count);
2726
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002727 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002728 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002729 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002730 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002731 /*
2732 * Only wait for conn_wait_comp_err if the isert_conn made it
2733 * into full feature phase..
2734 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002735 if (isert_conn->state == ISER_CONN_INIT) {
2736 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002737 return;
2738 }
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002739 if (isert_conn->state == ISER_CONN_UP)
2740 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002741 mutex_unlock(&isert_conn->conn_mutex);
2742
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002743 wait_for_completion(&isert_conn->conn_wait_comp_err);
2744
2745 wait_for_completion(&isert_conn->conn_wait);
2746}
2747
2748static void isert_free_conn(struct iscsi_conn *conn)
2749{
2750 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002751
2752 isert_put_conn(isert_conn);
2753}
2754
2755static struct iscsit_transport iser_target_transport = {
2756 .name = "IB/iSER",
2757 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002758 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002759 .owner = THIS_MODULE,
2760 .iscsit_setup_np = isert_setup_np,
2761 .iscsit_accept_np = isert_accept_np,
2762 .iscsit_free_np = isert_free_np,
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08002763 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002764 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002765 .iscsit_get_login_rx = isert_get_login_rx,
2766 .iscsit_put_login_tx = isert_put_login_tx,
2767 .iscsit_immediate_queue = isert_immediate_queue,
2768 .iscsit_response_queue = isert_response_queue,
2769 .iscsit_get_dataout = isert_get_dataout,
2770 .iscsit_queue_data_in = isert_put_datain,
2771 .iscsit_queue_status = isert_put_response,
2772};
2773
2774static int __init isert_init(void)
2775{
2776 int ret;
2777
2778 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2779 if (!isert_rx_wq) {
2780 pr_err("Unable to allocate isert_rx_wq\n");
2781 return -ENOMEM;
2782 }
2783
2784 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2785 if (!isert_comp_wq) {
2786 pr_err("Unable to allocate isert_comp_wq\n");
2787 ret = -ENOMEM;
2788 goto destroy_rx_wq;
2789 }
2790
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002791 iscsit_register_transport(&iser_target_transport);
2792 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2793 return 0;
2794
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002795destroy_rx_wq:
2796 destroy_workqueue(isert_rx_wq);
2797 return ret;
2798}
2799
2800static void __exit isert_exit(void)
2801{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002802 destroy_workqueue(isert_comp_wq);
2803 destroy_workqueue(isert_rx_wq);
2804 iscsit_unregister_transport(&iser_target_transport);
2805 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2806}
2807
2808MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2809MODULE_VERSION("0.1");
2810MODULE_AUTHOR("nab@Linux-iSCSI.org");
2811MODULE_LICENSE("GPL");
2812
2813module_init(isert_init);
2814module_exit(isert_exit);