blob: 3dd24272b1cd58053392e66cda0c19f2085e6e34 [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
50isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
51static int
52isert_reg_rdma_frwr(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
210static void isert_cq_tx_callback(struct ib_cq *, void *);
211static void isert_cq_rx_callback(struct ib_cq *, void *);
212
213static int
214isert_create_device_ib_res(struct isert_device *device)
215{
216 struct ib_device *ib_dev = device->ib_device;
217 struct isert_cq_desc *cq_desc;
Vu Pham59464ef2013-08-28 23:23:35 +0300218 struct ib_device_attr *dev_attr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800219 int ret = 0, i, j;
220
Vu Pham59464ef2013-08-28 23:23:35 +0300221 dev_attr = &device->dev_attr;
222 ret = isert_query_device(ib_dev, dev_attr);
223 if (ret)
224 return ret;
225
Vu Phamd40945d2013-08-28 23:23:34 +0300226 /* asign function handlers */
Vu Pham59464ef2013-08-28 23:23:35 +0300227 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
228 device->use_frwr = 1;
229 device->reg_rdma_mem = isert_reg_rdma_frwr;
230 device->unreg_rdma_mem = isert_unreg_rdma_frwr;
231 } else {
232 device->use_frwr = 0;
233 device->reg_rdma_mem = isert_map_rdma;
234 device->unreg_rdma_mem = isert_unmap_cmd;
235 }
Vu Phamd40945d2013-08-28 23:23:34 +0300236
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800237 device->cqs_used = min_t(int, num_online_cpus(),
238 device->ib_device->num_comp_vectors);
239 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
Vu Pham59464ef2013-08-28 23:23:35 +0300240 pr_debug("Using %d CQs, device %s supports %d vectors support FRWR %d\n",
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800241 device->cqs_used, device->ib_device->name,
Vu Pham59464ef2013-08-28 23:23:35 +0300242 device->ib_device->num_comp_vectors, device->use_frwr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800243 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
244 device->cqs_used, GFP_KERNEL);
245 if (!device->cq_desc) {
246 pr_err("Unable to allocate device->cq_desc\n");
247 return -ENOMEM;
248 }
249 cq_desc = device->cq_desc;
250
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251 for (i = 0; i < device->cqs_used; i++) {
252 cq_desc[i].device = device;
253 cq_desc[i].cq_index = i;
254
255 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
256 isert_cq_rx_callback,
257 isert_cq_event_callback,
258 (void *)&cq_desc[i],
259 ISER_MAX_RX_CQ_LEN, i);
260 if (IS_ERR(device->dev_rx_cq[i]))
261 goto out_cq;
262
263 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
264 isert_cq_tx_callback,
265 isert_cq_event_callback,
266 (void *)&cq_desc[i],
267 ISER_MAX_TX_CQ_LEN, i);
268 if (IS_ERR(device->dev_tx_cq[i]))
269 goto out_cq;
270
271 if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
272 goto out_cq;
273
274 if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
275 goto out_cq;
276 }
277
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800278 return 0;
279
280out_cq:
281 for (j = 0; j < i; j++) {
282 cq_desc = &device->cq_desc[j];
283
284 if (device->dev_rx_cq[j]) {
285 cancel_work_sync(&cq_desc->cq_rx_work);
286 ib_destroy_cq(device->dev_rx_cq[j]);
287 }
288 if (device->dev_tx_cq[j]) {
289 cancel_work_sync(&cq_desc->cq_tx_work);
290 ib_destroy_cq(device->dev_tx_cq[j]);
291 }
292 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800293 kfree(device->cq_desc);
294
295 return ret;
296}
297
298static void
299isert_free_device_ib_res(struct isert_device *device)
300{
301 struct isert_cq_desc *cq_desc;
302 int i;
303
304 for (i = 0; i < device->cqs_used; i++) {
305 cq_desc = &device->cq_desc[i];
306
307 cancel_work_sync(&cq_desc->cq_rx_work);
308 cancel_work_sync(&cq_desc->cq_tx_work);
309 ib_destroy_cq(device->dev_rx_cq[i]);
310 ib_destroy_cq(device->dev_tx_cq[i]);
311 device->dev_rx_cq[i] = NULL;
312 device->dev_tx_cq[i] = NULL;
313 }
314
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800315 kfree(device->cq_desc);
316}
317
318static void
319isert_device_try_release(struct isert_device *device)
320{
321 mutex_lock(&device_list_mutex);
322 device->refcount--;
323 if (!device->refcount) {
324 isert_free_device_ib_res(device);
325 list_del(&device->dev_node);
326 kfree(device);
327 }
328 mutex_unlock(&device_list_mutex);
329}
330
331static struct isert_device *
332isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
333{
334 struct isert_device *device;
335 int ret;
336
337 mutex_lock(&device_list_mutex);
338 list_for_each_entry(device, &device_list, dev_node) {
339 if (device->ib_device->node_guid == cma_id->device->node_guid) {
340 device->refcount++;
341 mutex_unlock(&device_list_mutex);
342 return device;
343 }
344 }
345
346 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
347 if (!device) {
348 mutex_unlock(&device_list_mutex);
349 return ERR_PTR(-ENOMEM);
350 }
351
352 INIT_LIST_HEAD(&device->dev_node);
353
354 device->ib_device = cma_id->device;
355 ret = isert_create_device_ib_res(device);
356 if (ret) {
357 kfree(device);
358 mutex_unlock(&device_list_mutex);
359 return ERR_PTR(ret);
360 }
361
362 device->refcount++;
363 list_add_tail(&device->dev_node, &device_list);
364 mutex_unlock(&device_list_mutex);
365
366 return device;
367}
368
Vu Pham59464ef2013-08-28 23:23:35 +0300369static void
370isert_conn_free_frwr_pool(struct isert_conn *isert_conn)
371{
372 struct fast_reg_descriptor *fr_desc, *tmp;
373 int i = 0;
374
375 if (list_empty(&isert_conn->conn_frwr_pool))
376 return;
377
378 pr_debug("Freeing conn %p frwr pool", isert_conn);
379
380 list_for_each_entry_safe(fr_desc, tmp,
381 &isert_conn->conn_frwr_pool, list) {
382 list_del(&fr_desc->list);
383 ib_free_fast_reg_page_list(fr_desc->data_frpl);
384 ib_dereg_mr(fr_desc->data_mr);
385 kfree(fr_desc);
386 ++i;
387 }
388
389 if (i < isert_conn->conn_frwr_pool_size)
390 pr_warn("Pool still has %d regions registered\n",
391 isert_conn->conn_frwr_pool_size - i);
392}
393
394static int
395isert_conn_create_frwr_pool(struct isert_conn *isert_conn)
396{
397 struct fast_reg_descriptor *fr_desc;
398 struct isert_device *device = isert_conn->conn_device;
399 int i, ret;
400
401 INIT_LIST_HEAD(&isert_conn->conn_frwr_pool);
402 isert_conn->conn_frwr_pool_size = 0;
403 for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
404 fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
405 if (!fr_desc) {
406 pr_err("Failed to allocate fast_reg descriptor\n");
407 ret = -ENOMEM;
408 goto err;
409 }
410
411 fr_desc->data_frpl =
412 ib_alloc_fast_reg_page_list(device->ib_device,
413 ISCSI_ISER_SG_TABLESIZE);
414 if (IS_ERR(fr_desc->data_frpl)) {
415 pr_err("Failed to allocate fr_pg_list err=%ld\n",
416 PTR_ERR(fr_desc->data_frpl));
417 ret = PTR_ERR(fr_desc->data_frpl);
418 goto err;
419 }
420
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200421 fr_desc->data_mr = ib_alloc_fast_reg_mr(isert_conn->conn_pd,
Vu Pham59464ef2013-08-28 23:23:35 +0300422 ISCSI_ISER_SG_TABLESIZE);
423 if (IS_ERR(fr_desc->data_mr)) {
424 pr_err("Failed to allocate frmr err=%ld\n",
425 PTR_ERR(fr_desc->data_mr));
426 ret = PTR_ERR(fr_desc->data_mr);
427 ib_free_fast_reg_page_list(fr_desc->data_frpl);
428 goto err;
429 }
430 pr_debug("Create fr_desc %p page_list %p\n",
431 fr_desc, fr_desc->data_frpl->page_list);
432
433 fr_desc->valid = true;
434 list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
435 isert_conn->conn_frwr_pool_size++;
436 }
437
438 pr_debug("Creating conn %p frwr pool size=%d",
439 isert_conn, isert_conn->conn_frwr_pool_size);
440
441 return 0;
442
443err:
444 isert_conn_free_frwr_pool(isert_conn);
445 return ret;
446}
447
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800448static int
449isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
450{
451 struct iscsi_np *np = cma_id->context;
452 struct isert_np *isert_np = np->np_context;
453 struct isert_conn *isert_conn;
454 struct isert_device *device;
455 struct ib_device *ib_dev = cma_id->device;
456 int ret = 0;
457
458 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
459 cma_id, cma_id->context);
460
461 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
462 if (!isert_conn) {
463 pr_err("Unable to allocate isert_conn\n");
464 return -ENOMEM;
465 }
466 isert_conn->state = ISER_CONN_INIT;
467 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
468 init_completion(&isert_conn->conn_login_comp);
469 init_waitqueue_head(&isert_conn->conn_wait);
470 init_waitqueue_head(&isert_conn->conn_wait_comp_err);
471 kref_init(&isert_conn->conn_kref);
472 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700473 mutex_init(&isert_conn->conn_mutex);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800474 mutex_init(&isert_conn->conn_comp_mutex);
Vu Pham59464ef2013-08-28 23:23:35 +0300475 spin_lock_init(&isert_conn->conn_lock);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800476
477 cma_id->context = isert_conn;
478 isert_conn->conn_cm_id = cma_id;
479 isert_conn->responder_resources = event->param.conn.responder_resources;
480 isert_conn->initiator_depth = event->param.conn.initiator_depth;
481 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
482 isert_conn->responder_resources, isert_conn->initiator_depth);
483
484 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
485 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
486 if (!isert_conn->login_buf) {
487 pr_err("Unable to allocate isert_conn->login_buf\n");
488 ret = -ENOMEM;
489 goto out;
490 }
491
492 isert_conn->login_req_buf = isert_conn->login_buf;
493 isert_conn->login_rsp_buf = isert_conn->login_buf +
494 ISCSI_DEF_MAX_RECV_SEG_LEN;
495 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
496 isert_conn->login_buf, isert_conn->login_req_buf,
497 isert_conn->login_rsp_buf);
498
499 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
500 (void *)isert_conn->login_req_buf,
501 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
502
503 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
504 if (ret) {
505 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
506 ret);
507 isert_conn->login_req_dma = 0;
508 goto out_login_buf;
509 }
510
511 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
512 (void *)isert_conn->login_rsp_buf,
513 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
514
515 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
516 if (ret) {
517 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
518 ret);
519 isert_conn->login_rsp_dma = 0;
520 goto out_req_dma_map;
521 }
522
523 device = isert_device_find_by_ib_dev(cma_id);
524 if (IS_ERR(device)) {
525 ret = PTR_ERR(device);
526 goto out_rsp_dma_map;
527 }
528
529 isert_conn->conn_device = device;
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200530 isert_conn->conn_pd = ib_alloc_pd(isert_conn->conn_device->ib_device);
531 if (IS_ERR(isert_conn->conn_pd)) {
532 ret = PTR_ERR(isert_conn->conn_pd);
533 pr_err("ib_alloc_pd failed for conn %p: ret=%d\n",
534 isert_conn, ret);
535 goto out_pd;
536 }
537
538 isert_conn->conn_mr = ib_get_dma_mr(isert_conn->conn_pd,
539 IB_ACCESS_LOCAL_WRITE);
540 if (IS_ERR(isert_conn->conn_mr)) {
541 ret = PTR_ERR(isert_conn->conn_mr);
542 pr_err("ib_get_dma_mr failed for conn %p: ret=%d\n",
543 isert_conn, ret);
544 goto out_mr;
545 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800546
Vu Pham59464ef2013-08-28 23:23:35 +0300547 if (device->use_frwr) {
548 ret = isert_conn_create_frwr_pool(isert_conn);
549 if (ret) {
550 pr_err("Conn: %p failed to create frwr_pool\n", isert_conn);
551 goto out_frwr;
552 }
553 }
554
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800555 ret = isert_conn_setup_qp(isert_conn, cma_id);
556 if (ret)
557 goto out_conn_dev;
558
559 mutex_lock(&isert_np->np_accept_mutex);
560 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
561 mutex_unlock(&isert_np->np_accept_mutex);
562
563 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
564 wake_up(&isert_np->np_accept_wq);
565 return 0;
566
567out_conn_dev:
Vu Pham59464ef2013-08-28 23:23:35 +0300568 if (device->use_frwr)
569 isert_conn_free_frwr_pool(isert_conn);
570out_frwr:
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200571 ib_dereg_mr(isert_conn->conn_mr);
572out_mr:
573 ib_dealloc_pd(isert_conn->conn_pd);
574out_pd:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800575 isert_device_try_release(device);
576out_rsp_dma_map:
577 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
578 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
579out_req_dma_map:
580 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
581 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
582out_login_buf:
583 kfree(isert_conn->login_buf);
584out:
585 kfree(isert_conn);
586 return ret;
587}
588
589static void
590isert_connect_release(struct isert_conn *isert_conn)
591{
592 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
593 struct isert_device *device = isert_conn->conn_device;
594 int cq_index;
595
596 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
597
Vu Pham0a666142013-10-22 00:48:54 +0300598 if (device && device->use_frwr)
Vu Pham59464ef2013-08-28 23:23:35 +0300599 isert_conn_free_frwr_pool(isert_conn);
600
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800601 if (isert_conn->conn_qp) {
602 cq_index = ((struct isert_cq_desc *)
603 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
604 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
605 isert_conn->conn_device->cq_active_qps[cq_index]--;
606
607 rdma_destroy_qp(isert_conn->conn_cm_id);
608 }
609
610 isert_free_rx_descriptors(isert_conn);
611 rdma_destroy_id(isert_conn->conn_cm_id);
612
Sagi Grimbergeb6ab132014-01-09 18:40:49 +0200613 ib_dereg_mr(isert_conn->conn_mr);
614 ib_dealloc_pd(isert_conn->conn_pd);
615
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800616 if (isert_conn->login_buf) {
617 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
618 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
619 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
620 ISCSI_DEF_MAX_RECV_SEG_LEN,
621 DMA_FROM_DEVICE);
622 kfree(isert_conn->login_buf);
623 }
624 kfree(isert_conn);
625
626 if (device)
627 isert_device_try_release(device);
628
629 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
630}
631
632static void
633isert_connected_handler(struct rdma_cm_id *cma_id)
634{
635 return;
636}
637
638static void
639isert_release_conn_kref(struct kref *kref)
640{
641 struct isert_conn *isert_conn = container_of(kref,
642 struct isert_conn, conn_kref);
643
644 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
645 current->comm, current->pid);
646
647 isert_connect_release(isert_conn);
648}
649
650static void
651isert_put_conn(struct isert_conn *isert_conn)
652{
653 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
654}
655
656static void
657isert_disconnect_work(struct work_struct *work)
658{
659 struct isert_conn *isert_conn = container_of(work,
660 struct isert_conn, conn_logout_work);
661
662 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700663 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800664 isert_conn->state = ISER_CONN_DOWN;
665
666 if (isert_conn->post_recv_buf_count == 0 &&
667 atomic_read(&isert_conn->post_send_buf_count) == 0) {
668 pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700669 mutex_unlock(&isert_conn->conn_mutex);
670 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800671 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700672 if (!isert_conn->conn_cm_id) {
673 mutex_unlock(&isert_conn->conn_mutex);
674 isert_put_conn(isert_conn);
675 return;
676 }
677 if (!isert_conn->logout_posted) {
678 pr_debug("Calling rdma_disconnect for !logout_posted from"
679 " isert_disconnect_work\n");
680 rdma_disconnect(isert_conn->conn_cm_id);
681 mutex_unlock(&isert_conn->conn_mutex);
682 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
683 goto wake_up;
684 }
685 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800686
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700687wake_up:
688 wake_up(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800689 isert_put_conn(isert_conn);
690}
691
692static void
693isert_disconnected_handler(struct rdma_cm_id *cma_id)
694{
695 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
696
697 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
698 schedule_work(&isert_conn->conn_logout_work);
699}
700
701static int
702isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
703{
704 int ret = 0;
705
706 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
707 event->event, event->status, cma_id->context, cma_id);
708
709 switch (event->event) {
710 case RDMA_CM_EVENT_CONNECT_REQUEST:
711 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
712 ret = isert_connect_request(cma_id, event);
713 break;
714 case RDMA_CM_EVENT_ESTABLISHED:
715 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
716 isert_connected_handler(cma_id);
717 break;
718 case RDMA_CM_EVENT_DISCONNECTED:
719 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
720 isert_disconnected_handler(cma_id);
721 break;
722 case RDMA_CM_EVENT_DEVICE_REMOVAL:
723 case RDMA_CM_EVENT_ADDR_CHANGE:
724 break;
725 case RDMA_CM_EVENT_CONNECT_ERROR:
726 default:
727 pr_err("Unknown RDMA CMA event: %d\n", event->event);
728 break;
729 }
730
731 if (ret != 0) {
732 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
733 event->event, ret);
734 dump_stack();
735 }
736
737 return ret;
738}
739
740static int
741isert_post_recv(struct isert_conn *isert_conn, u32 count)
742{
743 struct ib_recv_wr *rx_wr, *rx_wr_failed;
744 int i, ret;
745 unsigned int rx_head = isert_conn->conn_rx_desc_head;
746 struct iser_rx_desc *rx_desc;
747
748 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
749 rx_desc = &isert_conn->conn_rx_descs[rx_head];
750 rx_wr->wr_id = (unsigned long)rx_desc;
751 rx_wr->sg_list = &rx_desc->rx_sg;
752 rx_wr->num_sge = 1;
753 rx_wr->next = rx_wr + 1;
754 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
755 }
756
757 rx_wr--;
758 rx_wr->next = NULL; /* mark end of work requests list */
759
760 isert_conn->post_recv_buf_count += count;
761 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
762 &rx_wr_failed);
763 if (ret) {
764 pr_err("ib_post_recv() failed with ret: %d\n", ret);
765 isert_conn->post_recv_buf_count -= count;
766 } else {
767 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
768 isert_conn->conn_rx_desc_head = rx_head;
769 }
770 return ret;
771}
772
773static int
774isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
775{
776 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
777 struct ib_send_wr send_wr, *send_wr_failed;
778 int ret;
779
780 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
781 ISER_HEADERS_LEN, DMA_TO_DEVICE);
782
783 send_wr.next = NULL;
784 send_wr.wr_id = (unsigned long)tx_desc;
785 send_wr.sg_list = tx_desc->tx_sg;
786 send_wr.num_sge = tx_desc->num_sge;
787 send_wr.opcode = IB_WR_SEND;
788 send_wr.send_flags = IB_SEND_SIGNALED;
789
790 atomic_inc(&isert_conn->post_send_buf_count);
791
792 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
793 if (ret) {
794 pr_err("ib_post_send() failed, ret: %d\n", ret);
795 atomic_dec(&isert_conn->post_send_buf_count);
796 }
797
798 return ret;
799}
800
801static void
802isert_create_send_desc(struct isert_conn *isert_conn,
803 struct isert_cmd *isert_cmd,
804 struct iser_tx_desc *tx_desc)
805{
806 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
807
808 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
809 ISER_HEADERS_LEN, DMA_TO_DEVICE);
810
811 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
812 tx_desc->iser_header.flags = ISER_VER;
813
814 tx_desc->num_sge = 1;
815 tx_desc->isert_cmd = isert_cmd;
816
817 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
818 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
819 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
820 }
821}
822
823static int
824isert_init_tx_hdrs(struct isert_conn *isert_conn,
825 struct iser_tx_desc *tx_desc)
826{
827 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
828 u64 dma_addr;
829
830 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
831 ISER_HEADERS_LEN, DMA_TO_DEVICE);
832 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
833 pr_err("ib_dma_mapping_error() failed\n");
834 return -ENOMEM;
835 }
836
837 tx_desc->dma_addr = dma_addr;
838 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
839 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
840 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
841
842 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
843 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
844 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
845
846 return 0;
847}
848
849static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800850isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
851 struct ib_send_wr *send_wr, bool coalesce)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800852{
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800853 struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
854
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800855 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
856 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
857 send_wr->opcode = IB_WR_SEND;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800858 send_wr->sg_list = &tx_desc->tx_sg[0];
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800859 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
Nicholas Bellinger95b60f02013-11-05 13:16:12 -0800860 /*
861 * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
862 * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
863 */
864 mutex_lock(&isert_conn->conn_comp_mutex);
865 if (coalesce &&
866 ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
867 llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
868 mutex_unlock(&isert_conn->conn_comp_mutex);
869 return;
870 }
871 isert_conn->conn_comp_batch = 0;
872 tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
873 mutex_unlock(&isert_conn->conn_comp_mutex);
874
875 send_wr->send_flags = IB_SEND_SIGNALED;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800876}
877
878static int
879isert_rdma_post_recvl(struct isert_conn *isert_conn)
880{
881 struct ib_recv_wr rx_wr, *rx_wr_fail;
882 struct ib_sge sge;
883 int ret;
884
885 memset(&sge, 0, sizeof(struct ib_sge));
886 sge.addr = isert_conn->login_req_dma;
887 sge.length = ISER_RX_LOGIN_SIZE;
888 sge.lkey = isert_conn->conn_mr->lkey;
889
890 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
891 sge.addr, sge.length, sge.lkey);
892
893 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
894 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
895 rx_wr.sg_list = &sge;
896 rx_wr.num_sge = 1;
897
898 isert_conn->post_recv_buf_count++;
899 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
900 if (ret) {
901 pr_err("ib_post_recv() failed: %d\n", ret);
902 isert_conn->post_recv_buf_count--;
903 }
904
905 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
906 return ret;
907}
908
909static int
910isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
911 u32 length)
912{
913 struct isert_conn *isert_conn = conn->context;
914 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
915 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
916 int ret;
917
918 isert_create_send_desc(isert_conn, NULL, tx_desc);
919
920 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
921 sizeof(struct iscsi_hdr));
922
923 isert_init_tx_hdrs(isert_conn, tx_desc);
924
925 if (length > 0) {
926 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
927
928 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
929 length, DMA_TO_DEVICE);
930
931 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
932
933 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
934 length, DMA_TO_DEVICE);
935
936 tx_dsg->addr = isert_conn->login_rsp_dma;
937 tx_dsg->length = length;
938 tx_dsg->lkey = isert_conn->conn_mr->lkey;
939 tx_desc->num_sge = 2;
940 }
941 if (!login->login_failed) {
942 if (login->login_complete) {
943 ret = isert_alloc_rx_descriptors(isert_conn);
944 if (ret)
945 return ret;
946
947 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
948 if (ret)
949 return ret;
950
951 isert_conn->state = ISER_CONN_UP;
952 goto post_send;
953 }
954
955 ret = isert_rdma_post_recvl(isert_conn);
956 if (ret)
957 return ret;
958 }
959post_send:
960 ret = isert_post_send(isert_conn, tx_desc);
961 if (ret)
962 return ret;
963
964 return 0;
965}
966
967static void
968isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
969 struct isert_conn *isert_conn)
970{
971 struct iscsi_conn *conn = isert_conn->conn;
972 struct iscsi_login *login = conn->conn_login;
973 int size;
974
975 if (!login) {
976 pr_err("conn->conn_login is NULL\n");
977 dump_stack();
978 return;
979 }
980
981 if (login->first_request) {
982 struct iscsi_login_req *login_req =
983 (struct iscsi_login_req *)&rx_desc->iscsi_header;
984 /*
985 * Setup the initial iscsi_login values from the leading
986 * login request PDU.
987 */
988 login->leading_connection = (!login_req->tsih) ? 1 : 0;
989 login->current_stage =
990 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
991 >> 2;
992 login->version_min = login_req->min_version;
993 login->version_max = login_req->max_version;
994 memcpy(login->isid, login_req->isid, 6);
995 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
996 login->init_task_tag = login_req->itt;
997 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
998 login->cid = be16_to_cpu(login_req->cid);
999 login->tsih = be16_to_cpu(login_req->tsih);
1000 }
1001
1002 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
1003
1004 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
1005 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
1006 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
1007 memcpy(login->req_buf, &rx_desc->data[0], size);
1008
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07001009 if (login->first_request) {
1010 complete(&isert_conn->conn_login_comp);
1011 return;
1012 }
1013 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001014}
1015
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001016static struct iscsi_cmd
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001017*isert_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001018{
1019 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1020 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001021 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001022
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001023 cmd = iscsit_allocate_cmd(conn, gfp);
1024 if (!cmd) {
1025 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001026 return NULL;
1027 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001028 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001029 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001030 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001031
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001032 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001033}
1034
1035static int
1036isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001037 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
1038 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001039{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001040 struct iscsi_conn *conn = isert_conn->conn;
1041 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1042 struct scatterlist *sg;
1043 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
1044 bool dump_payload = false;
1045
1046 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1047 if (rc < 0)
1048 return rc;
1049
1050 imm_data = cmd->immediate_data;
1051 imm_data_len = cmd->first_burst_len;
1052 unsol_data = cmd->unsolicited_data;
1053
1054 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1055 if (rc < 0) {
1056 return 0;
1057 } else if (rc > 0) {
1058 dump_payload = true;
1059 goto sequence_cmd;
1060 }
1061
1062 if (!imm_data)
1063 return 0;
1064
1065 sg = &cmd->se_cmd.t_data_sg[0];
1066 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
1067
1068 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
1069 sg, sg_nents, &rx_desc->data[0], imm_data_len);
1070
1071 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
1072
1073 cmd->write_data_done += imm_data_len;
1074
1075 if (cmd->write_data_done == cmd->se_cmd.data_length) {
1076 spin_lock_bh(&cmd->istate_lock);
1077 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1078 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1079 spin_unlock_bh(&cmd->istate_lock);
1080 }
1081
1082sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001083 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001084
1085 if (!rc && dump_payload == false && unsol_data)
1086 iscsit_set_unsoliticed_dataout(cmd);
1087
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001088 return 0;
1089}
1090
1091static int
1092isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
1093 struct iser_rx_desc *rx_desc, unsigned char *buf)
1094{
1095 struct scatterlist *sg_start;
1096 struct iscsi_conn *conn = isert_conn->conn;
1097 struct iscsi_cmd *cmd = NULL;
1098 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1099 u32 unsol_data_len = ntoh24(hdr->dlength);
1100 int rc, sg_nents, sg_off, page_off;
1101
1102 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1103 if (rc < 0)
1104 return rc;
1105 else if (!cmd)
1106 return 0;
1107 /*
1108 * FIXME: Unexpected unsolicited_data out
1109 */
1110 if (!cmd->unsolicited_data) {
1111 pr_err("Received unexpected solicited data payload\n");
1112 dump_stack();
1113 return -1;
1114 }
1115
1116 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1117 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1118
1119 sg_off = cmd->write_data_done / PAGE_SIZE;
1120 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1121 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1122 page_off = cmd->write_data_done % PAGE_SIZE;
1123 /*
1124 * FIXME: Non page-aligned unsolicited_data out
1125 */
1126 if (page_off) {
1127 pr_err("Received unexpected non-page aligned data payload\n");
1128 dump_stack();
1129 return -1;
1130 }
1131 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1132 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1133
1134 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1135 unsol_data_len);
1136
1137 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1138 if (rc < 0)
1139 return rc;
1140
1141 return 0;
1142}
1143
1144static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001145isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001146 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1147 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001148{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001149 struct iscsi_conn *conn = isert_conn->conn;
1150 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1151 int rc;
1152
1153 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1154 if (rc < 0)
1155 return rc;
1156 /*
1157 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1158 */
1159
1160 return iscsit_process_nop_out(conn, cmd, hdr);
1161}
1162
1163static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001164isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001165 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1166 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001167{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001168 struct iscsi_conn *conn = isert_conn->conn;
1169 u32 payload_length = ntoh24(hdr->dlength);
1170 int rc;
1171 unsigned char *text_in;
1172
1173 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1174 if (rc < 0)
1175 return rc;
1176
1177 text_in = kzalloc(payload_length, GFP_KERNEL);
1178 if (!text_in) {
1179 pr_err("Unable to allocate text_in of payload_length: %u\n",
1180 payload_length);
1181 return -ENOMEM;
1182 }
1183 cmd->text_in_ptr = text_in;
1184
1185 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1186
1187 return iscsit_process_text_cmd(conn, cmd, hdr);
1188}
1189
1190static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001191isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1192 uint32_t read_stag, uint64_t read_va,
1193 uint32_t write_stag, uint64_t write_va)
1194{
1195 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1196 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001197 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001198 struct iscsi_cmd *cmd;
1199 struct isert_cmd *isert_cmd;
1200 int ret = -EINVAL;
1201 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1202
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001203 if (sess->sess_ops->SessionType &&
1204 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1205 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1206 " ignoring\n", opcode);
1207 return 0;
1208 }
1209
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001210 switch (opcode) {
1211 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001212 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001213 if (!cmd)
1214 break;
1215
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001216 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001217 isert_cmd->read_stag = read_stag;
1218 isert_cmd->read_va = read_va;
1219 isert_cmd->write_stag = write_stag;
1220 isert_cmd->write_va = write_va;
1221
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001222 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001223 rx_desc, (unsigned char *)hdr);
1224 break;
1225 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001226 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227 if (!cmd)
1228 break;
1229
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001230 isert_cmd = iscsit_priv_cmd(cmd);
1231 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001232 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001233 break;
1234 case ISCSI_OP_SCSI_DATA_OUT:
1235 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1236 (unsigned char *)hdr);
1237 break;
1238 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001239 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001240 if (!cmd)
1241 break;
1242
1243 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1244 (unsigned char *)hdr);
1245 break;
1246 case ISCSI_OP_LOGOUT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001247 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001248 if (!cmd)
1249 break;
1250
1251 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1252 if (ret > 0)
1253 wait_for_completion_timeout(&conn->conn_logout_comp,
1254 SECONDS_FOR_LOGOUT_COMP *
1255 HZ);
1256 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001257 case ISCSI_OP_TEXT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001258 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001259 if (!cmd)
1260 break;
1261
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001262 isert_cmd = iscsit_priv_cmd(cmd);
1263 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001264 rx_desc, (struct iscsi_text *)hdr);
1265 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001266 default:
1267 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1268 dump_stack();
1269 break;
1270 }
1271
1272 return ret;
1273}
1274
1275static void
1276isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1277{
1278 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1279 uint64_t read_va = 0, write_va = 0;
1280 uint32_t read_stag = 0, write_stag = 0;
1281 int rc;
1282
1283 switch (iser_hdr->flags & 0xF0) {
1284 case ISCSI_CTRL:
1285 if (iser_hdr->flags & ISER_RSV) {
1286 read_stag = be32_to_cpu(iser_hdr->read_stag);
1287 read_va = be64_to_cpu(iser_hdr->read_va);
1288 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1289 read_stag, (unsigned long long)read_va);
1290 }
1291 if (iser_hdr->flags & ISER_WSV) {
1292 write_stag = be32_to_cpu(iser_hdr->write_stag);
1293 write_va = be64_to_cpu(iser_hdr->write_va);
1294 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1295 write_stag, (unsigned long long)write_va);
1296 }
1297
1298 pr_debug("ISER ISCSI_CTRL PDU\n");
1299 break;
1300 case ISER_HELLO:
1301 pr_err("iSER Hello message\n");
1302 break;
1303 default:
1304 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1305 break;
1306 }
1307
1308 rc = isert_rx_opcode(isert_conn, rx_desc,
1309 read_stag, read_va, write_stag, write_va);
1310}
1311
1312static void
1313isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1314 unsigned long xfer_len)
1315{
1316 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1317 struct iscsi_hdr *hdr;
1318 u64 rx_dma;
1319 int rx_buflen, outstanding;
1320
1321 if ((char *)desc == isert_conn->login_req_buf) {
1322 rx_dma = isert_conn->login_req_dma;
1323 rx_buflen = ISER_RX_LOGIN_SIZE;
1324 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1325 rx_dma, rx_buflen);
1326 } else {
1327 rx_dma = desc->dma_addr;
1328 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1329 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1330 rx_dma, rx_buflen);
1331 }
1332
1333 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1334
1335 hdr = &desc->iscsi_header;
1336 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1337 hdr->opcode, hdr->itt, hdr->flags,
1338 (int)(xfer_len - ISER_HEADERS_LEN));
1339
1340 if ((char *)desc == isert_conn->login_req_buf)
1341 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1342 isert_conn);
1343 else
1344 isert_rx_do_work(desc, isert_conn);
1345
1346 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1347 DMA_FROM_DEVICE);
1348
1349 isert_conn->post_recv_buf_count--;
1350 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1351 isert_conn->post_recv_buf_count);
1352
1353 if ((char *)desc == isert_conn->login_req_buf)
1354 return;
1355
1356 outstanding = isert_conn->post_recv_buf_count;
1357 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1358 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1359 ISERT_MIN_POSTED_RX);
1360 err = isert_post_recv(isert_conn, count);
1361 if (err) {
1362 pr_err("isert_post_recv() count: %d failed, %d\n",
1363 count, err);
1364 }
1365 }
1366}
1367
1368static void
1369isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1370{
1371 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1372 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1373
Vu Pham90ecc6e2013-08-28 23:23:33 +03001374 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001375 if (wr->sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001376 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1377 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1378 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1379 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001380 wr->sge = NULL;
1381 }
1382
Vu Pham90ecc6e2013-08-28 23:23:33 +03001383 if (wr->send_wr) {
1384 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1385 kfree(wr->send_wr);
1386 wr->send_wr = NULL;
1387 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001388
Vu Pham90ecc6e2013-08-28 23:23:33 +03001389 if (wr->ib_sge) {
1390 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1391 kfree(wr->ib_sge);
1392 wr->ib_sge = NULL;
1393 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001394}
1395
1396static void
Vu Pham59464ef2013-08-28 23:23:35 +03001397isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1398{
1399 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1400 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1401 LIST_HEAD(unmap_list);
1402
1403 pr_debug("unreg_frwr_cmd: %p\n", isert_cmd);
1404
1405 if (wr->fr_desc) {
1406 pr_debug("unreg_frwr_cmd: %p free fr_desc %p\n",
1407 isert_cmd, wr->fr_desc);
1408 spin_lock_bh(&isert_conn->conn_lock);
1409 list_add_tail(&wr->fr_desc->list, &isert_conn->conn_frwr_pool);
1410 spin_unlock_bh(&isert_conn->conn_lock);
1411 wr->fr_desc = NULL;
1412 }
1413
1414 if (wr->sge) {
1415 pr_debug("unreg_frwr_cmd: %p unmap_sg op\n", isert_cmd);
1416 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1417 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1418 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1419 wr->sge = NULL;
1420 }
1421
1422 wr->ib_sge = NULL;
1423 wr->send_wr = NULL;
1424}
1425
1426static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001427isert_put_cmd(struct isert_cmd *isert_cmd)
1428{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001429 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001430 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001431 struct iscsi_conn *conn = isert_conn->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001432 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001433
1434 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1435
1436 switch (cmd->iscsi_opcode) {
1437 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001438 spin_lock_bh(&conn->cmd_lock);
1439 if (!list_empty(&cmd->i_conn_node))
1440 list_del(&cmd->i_conn_node);
1441 spin_unlock_bh(&conn->cmd_lock);
1442
1443 if (cmd->data_direction == DMA_TO_DEVICE)
1444 iscsit_stop_dataout_timer(cmd);
1445
Vu Phamd40945d2013-08-28 23:23:34 +03001446 device->unreg_rdma_mem(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001447 transport_generic_free_cmd(&cmd->se_cmd, 0);
1448 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001449 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001450 spin_lock_bh(&conn->cmd_lock);
1451 if (!list_empty(&cmd->i_conn_node))
1452 list_del(&cmd->i_conn_node);
1453 spin_unlock_bh(&conn->cmd_lock);
1454
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001455 transport_generic_free_cmd(&cmd->se_cmd, 0);
1456 break;
1457 case ISCSI_OP_REJECT:
1458 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001459 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001460 spin_lock_bh(&conn->cmd_lock);
1461 if (!list_empty(&cmd->i_conn_node))
1462 list_del(&cmd->i_conn_node);
1463 spin_unlock_bh(&conn->cmd_lock);
1464
1465 /*
1466 * Handle special case for REJECT when iscsi_add_reject*() has
1467 * overwritten the original iscsi_opcode assignment, and the
1468 * associated cmd->se_cmd needs to be released.
1469 */
1470 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001471 pr_debug("Calling transport_generic_free_cmd from"
1472 " isert_put_cmd for 0x%02x\n",
1473 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474 transport_generic_free_cmd(&cmd->se_cmd, 0);
1475 break;
1476 }
1477 /*
1478 * Fall-through
1479 */
1480 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001481 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001482 break;
1483 }
1484}
1485
1486static void
1487isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1488{
1489 if (tx_desc->dma_addr != 0) {
1490 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1491 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1492 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1493 tx_desc->dma_addr = 0;
1494 }
1495}
1496
1497static void
1498isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1499 struct ib_device *ib_dev)
1500{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001501 if (isert_cmd->pdu_buf_dma != 0) {
1502 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1503 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1504 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1505 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001506 }
1507
1508 isert_unmap_tx_desc(tx_desc, ib_dev);
1509 isert_put_cmd(isert_cmd);
1510}
1511
1512static void
1513isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1514 struct isert_cmd *isert_cmd)
1515{
1516 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001517 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001518 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001519 struct isert_conn *isert_conn = isert_cmd->conn;
Vu Phamd40945d2013-08-28 23:23:34 +03001520 struct isert_device *device = isert_conn->conn_device;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001521
1522 iscsit_stop_dataout_timer(cmd);
Vu Phamd40945d2013-08-28 23:23:34 +03001523 device->unreg_rdma_mem(isert_cmd, isert_conn);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001524 cmd->write_data_done = wr->cur_rdma_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001525
Vu Pham90ecc6e2013-08-28 23:23:33 +03001526 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001527 spin_lock_bh(&cmd->istate_lock);
1528 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1529 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1530 spin_unlock_bh(&cmd->istate_lock);
1531
1532 target_execute_cmd(se_cmd);
1533}
1534
1535static void
1536isert_do_control_comp(struct work_struct *work)
1537{
1538 struct isert_cmd *isert_cmd = container_of(work,
1539 struct isert_cmd, comp_work);
1540 struct isert_conn *isert_conn = isert_cmd->conn;
1541 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001542 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001543
1544 switch (cmd->i_state) {
1545 case ISTATE_SEND_TASKMGTRSP:
1546 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1547
1548 atomic_dec(&isert_conn->post_send_buf_count);
1549 iscsit_tmr_post_handler(cmd, cmd->conn);
1550
1551 cmd->i_state = ISTATE_SENT_STATUS;
1552 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1553 break;
1554 case ISTATE_SEND_REJECT:
1555 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1556 atomic_dec(&isert_conn->post_send_buf_count);
1557
1558 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001559 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001560 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001561 case ISTATE_SEND_LOGOUTRSP:
1562 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1563 /*
1564 * Call atomic_dec(&isert_conn->post_send_buf_count)
1565 * from isert_free_conn()
1566 */
1567 isert_conn->logout_posted = true;
1568 iscsit_logout_post_handler(cmd, cmd->conn);
1569 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001570 case ISTATE_SEND_TEXTRSP:
1571 atomic_dec(&isert_conn->post_send_buf_count);
1572 cmd->i_state = ISTATE_SENT_STATUS;
1573 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1574 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001575 default:
1576 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1577 dump_stack();
1578 break;
1579 }
1580}
1581
1582static void
1583isert_response_completion(struct iser_tx_desc *tx_desc,
1584 struct isert_cmd *isert_cmd,
1585 struct isert_conn *isert_conn,
1586 struct ib_device *ib_dev)
1587{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001588 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001589
1590 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001591 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001592 cmd->i_state == ISTATE_SEND_REJECT ||
1593 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001594 isert_unmap_tx_desc(tx_desc, ib_dev);
1595
1596 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1597 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1598 return;
1599 }
1600 atomic_dec(&isert_conn->post_send_buf_count);
1601
1602 cmd->i_state = ISTATE_SENT_STATUS;
1603 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1604}
1605
1606static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001607__isert_send_completion(struct iser_tx_desc *tx_desc,
1608 struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001609{
1610 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1611 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1612 struct isert_rdma_wr *wr;
1613
1614 if (!isert_cmd) {
1615 atomic_dec(&isert_conn->post_send_buf_count);
1616 isert_unmap_tx_desc(tx_desc, ib_dev);
1617 return;
1618 }
1619 wr = &isert_cmd->rdma_wr;
1620
1621 switch (wr->iser_ib_op) {
1622 case ISER_IB_RECV:
1623 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1624 dump_stack();
1625 break;
1626 case ISER_IB_SEND:
1627 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1628 isert_response_completion(tx_desc, isert_cmd,
1629 isert_conn, ib_dev);
1630 break;
1631 case ISER_IB_RDMA_WRITE:
1632 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1633 dump_stack();
1634 break;
1635 case ISER_IB_RDMA_READ:
1636 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1637
1638 atomic_dec(&isert_conn->post_send_buf_count);
1639 isert_completion_rdma_read(tx_desc, isert_cmd);
1640 break;
1641 default:
1642 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1643 dump_stack();
1644 break;
1645 }
1646}
1647
1648static void
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001649isert_send_completion(struct iser_tx_desc *tx_desc,
1650 struct isert_conn *isert_conn)
1651{
1652 struct llist_node *llnode = tx_desc->comp_llnode_batch;
1653 struct iser_tx_desc *t;
1654 /*
1655 * Drain coalesced completion llist starting from comp_llnode_batch
1656 * setup in isert_init_send_wr(), and then complete trailing tx_desc.
1657 */
1658 while (llnode) {
1659 t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
1660 llnode = llist_next(llnode);
1661 __isert_send_completion(t, isert_conn);
1662 }
1663 __isert_send_completion(tx_desc, isert_conn);
1664}
1665
1666static void
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001667isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1668{
1669 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1670
1671 if (tx_desc) {
1672 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1673
1674 if (!isert_cmd)
1675 isert_unmap_tx_desc(tx_desc, ib_dev);
1676 else
1677 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1678 }
1679
1680 if (isert_conn->post_recv_buf_count == 0 &&
1681 atomic_read(&isert_conn->post_send_buf_count) == 0) {
1682 pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1683 pr_debug("Calling wake_up from isert_cq_comp_err\n");
1684
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07001685 mutex_lock(&isert_conn->conn_mutex);
1686 if (isert_conn->state != ISER_CONN_DOWN)
1687 isert_conn->state = ISER_CONN_TERMINATING;
1688 mutex_unlock(&isert_conn->conn_mutex);
1689
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001690 wake_up(&isert_conn->conn_wait_comp_err);
1691 }
1692}
1693
1694static void
1695isert_cq_tx_work(struct work_struct *work)
1696{
1697 struct isert_cq_desc *cq_desc = container_of(work,
1698 struct isert_cq_desc, cq_tx_work);
1699 struct isert_device *device = cq_desc->device;
1700 int cq_index = cq_desc->cq_index;
1701 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1702 struct isert_conn *isert_conn;
1703 struct iser_tx_desc *tx_desc;
1704 struct ib_wc wc;
1705
1706 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1707 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1708 isert_conn = wc.qp->qp_context;
1709
1710 if (wc.status == IB_WC_SUCCESS) {
1711 isert_send_completion(tx_desc, isert_conn);
1712 } else {
1713 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1714 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001715 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001716 atomic_dec(&isert_conn->post_send_buf_count);
1717 isert_cq_comp_err(tx_desc, isert_conn);
1718 }
1719 }
1720
1721 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1722}
1723
1724static void
1725isert_cq_tx_callback(struct ib_cq *cq, void *context)
1726{
1727 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1728
1729 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1730 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1731}
1732
1733static void
1734isert_cq_rx_work(struct work_struct *work)
1735{
1736 struct isert_cq_desc *cq_desc = container_of(work,
1737 struct isert_cq_desc, cq_rx_work);
1738 struct isert_device *device = cq_desc->device;
1739 int cq_index = cq_desc->cq_index;
1740 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1741 struct isert_conn *isert_conn;
1742 struct iser_rx_desc *rx_desc;
1743 struct ib_wc wc;
1744 unsigned long xfer_len;
1745
1746 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1747 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1748 isert_conn = wc.qp->qp_context;
1749
1750 if (wc.status == IB_WC_SUCCESS) {
1751 xfer_len = (unsigned long)wc.byte_len;
1752 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1753 } else {
1754 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001755 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001756 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001757 pr_debug("RX wc.vendor_err: 0x%08x\n",
1758 wc.vendor_err);
1759 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001760 isert_conn->post_recv_buf_count--;
1761 isert_cq_comp_err(NULL, isert_conn);
1762 }
1763 }
1764
1765 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1766}
1767
1768static void
1769isert_cq_rx_callback(struct ib_cq *cq, void *context)
1770{
1771 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1772
1773 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1774 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1775}
1776
1777static int
1778isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1779{
1780 struct ib_send_wr *wr_failed;
1781 int ret;
1782
1783 atomic_inc(&isert_conn->post_send_buf_count);
1784
1785 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1786 &wr_failed);
1787 if (ret) {
1788 pr_err("ib_post_send failed with %d\n", ret);
1789 atomic_dec(&isert_conn->post_send_buf_count);
1790 return ret;
1791 }
1792 return ret;
1793}
1794
1795static int
1796isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1797{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001798 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001799 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1800 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1801 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1802 &isert_cmd->tx_desc.iscsi_header;
1803
1804 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1805 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1806 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1807 /*
1808 * Attach SENSE DATA payload to iSCSI Response PDU
1809 */
1810 if (cmd->se_cmd.sense_buffer &&
1811 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1812 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1813 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1814 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001815 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001816
1817 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1818 cmd->sense_buffer);
1819 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1820
1821 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1822 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001823 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001824
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001825 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1826 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001827 DMA_TO_DEVICE);
1828
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001829 isert_cmd->pdu_buf_len = pdu_len;
1830 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1831 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001832 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1833 isert_cmd->tx_desc.num_sge = 2;
1834 }
1835
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001836 isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001837
1838 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1839
1840 return isert_post_response(isert_conn, isert_cmd);
1841}
1842
1843static int
1844isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1845 bool nopout_response)
1846{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001847 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001848 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1849 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1850
1851 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1852 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1853 &isert_cmd->tx_desc.iscsi_header,
1854 nopout_response);
1855 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001856 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001857
Masanari Iida8b513d02013-05-21 23:13:12 +09001858 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001859
1860 return isert_post_response(isert_conn, isert_cmd);
1861}
1862
1863static int
1864isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1865{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001866 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001867 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1868 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1869
1870 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1871 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1872 &isert_cmd->tx_desc.iscsi_header);
1873 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001874 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001875
1876 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1877
1878 return isert_post_response(isert_conn, isert_cmd);
1879}
1880
1881static int
1882isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1883{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001884 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001885 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1886 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1887
1888 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1889 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1890 &isert_cmd->tx_desc.iscsi_header);
1891 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001892 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001893
1894 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1895
1896 return isert_post_response(isert_conn, isert_cmd);
1897}
1898
1899static int
1900isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1901{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001902 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001903 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1904 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001905 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1906 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1907 struct iscsi_reject *hdr =
1908 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001909
1910 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001911 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001912 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001913
1914 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001915 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001916 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1917 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001918 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1919 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001920 tx_dsg->length = ISCSI_HDR_LEN;
1921 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1922 isert_cmd->tx_desc.num_sge = 2;
1923
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001924 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001925
1926 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1927
1928 return isert_post_response(isert_conn, isert_cmd);
1929}
1930
1931static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001932isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1933{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001934 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001935 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1936 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1937 struct iscsi_text_rsp *hdr =
1938 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1939 u32 txt_rsp_len;
1940 int rc;
1941
1942 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1943 rc = iscsit_build_text_rsp(cmd, conn, hdr);
1944 if (rc < 0)
1945 return rc;
1946
1947 txt_rsp_len = rc;
1948 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1949
1950 if (txt_rsp_len) {
1951 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1952 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1953 void *txt_rsp_buf = cmd->buf_ptr;
1954
1955 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1956 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1957
1958 isert_cmd->pdu_buf_len = txt_rsp_len;
1959 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1960 tx_dsg->length = txt_rsp_len;
1961 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1962 isert_cmd->tx_desc.num_sge = 2;
1963 }
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08001964 isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001965
1966 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1967
1968 return isert_post_response(isert_conn, isert_cmd);
1969}
1970
1971static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001972isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1973 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1974 u32 data_left, u32 offset)
1975{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001976 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977 struct scatterlist *sg_start, *tmp_sg;
1978 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1979 u32 sg_off, page_off;
1980 int i = 0, sg_nents;
1981
1982 sg_off = offset / PAGE_SIZE;
1983 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1984 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1985 page_off = offset % PAGE_SIZE;
1986
1987 send_wr->sg_list = ib_sge;
1988 send_wr->num_sge = sg_nents;
1989 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1990 /*
1991 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1992 */
1993 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1994 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1995 (unsigned long long)tmp_sg->dma_address,
1996 tmp_sg->length, page_off);
1997
1998 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1999 ib_sge->length = min_t(u32, data_left,
2000 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
2001 ib_sge->lkey = isert_conn->conn_mr->lkey;
2002
Vu Pham90ecc6e2013-08-28 23:23:33 +03002003 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2004 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002005 page_off = 0;
2006 data_left -= ib_sge->length;
2007 ib_sge++;
2008 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
2009 }
2010
2011 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
2012 send_wr->sg_list, send_wr->num_sge);
2013
2014 return sg_nents;
2015}
2016
2017static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002018isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2019 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002020{
2021 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002022 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002023 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002024 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002025 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002026 struct ib_sge *ib_sge;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002027 struct scatterlist *sg_start;
2028 u32 sg_off = 0, sg_nents;
2029 u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
2030 int ret = 0, count, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002031
Vu Pham90ecc6e2013-08-28 23:23:33 +03002032 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2033 data_left = se_cmd->data_length;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002034 } else {
2035 sg_off = cmd->write_data_done / PAGE_SIZE;
2036 data_left = se_cmd->data_length - cmd->write_data_done;
2037 offset = cmd->write_data_done;
2038 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2039 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002040
Vu Pham90ecc6e2013-08-28 23:23:33 +03002041 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2042 sg_nents = se_cmd->t_data_nents - sg_off;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002043
Vu Pham90ecc6e2013-08-28 23:23:33 +03002044 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2045 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2046 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002047 if (unlikely(!count)) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002048 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002049 return -EINVAL;
2050 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002051 wr->sge = sg_start;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052 wr->num_sge = sg_nents;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002053 wr->cur_rdma_length = data_left;
2054 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2055 isert_cmd, count, sg_start, sg_nents, data_left);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002056
2057 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
2058 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002059 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002060 ret = -ENOMEM;
2061 goto unmap_sg;
2062 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002063 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002064
2065 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
2066 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
2067 GFP_KERNEL);
2068 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03002069 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002070 ret = -ENOMEM;
2071 goto unmap_sg;
2072 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002073
2074 wr->isert_cmd = isert_cmd;
2075 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002076
2077 for (i = 0; i < wr->send_wr_num; i++) {
2078 send_wr = &isert_cmd->rdma_wr.send_wr[i];
2079 data_len = min(data_left, rdma_write_max);
2080
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002081 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002082 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2083 send_wr->opcode = IB_WR_RDMA_WRITE;
2084 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
2085 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2086 if (i + 1 == wr->send_wr_num)
2087 send_wr->next = &isert_cmd->tx_desc.send_wr;
2088 else
2089 send_wr->next = &wr->send_wr[i + 1];
2090 } else {
2091 send_wr->opcode = IB_WR_RDMA_READ;
2092 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
2093 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2094 if (i + 1 == wr->send_wr_num)
2095 send_wr->send_flags = IB_SEND_SIGNALED;
2096 else
2097 send_wr->next = &wr->send_wr[i + 1];
2098 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002099
2100 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
2101 send_wr, data_len, offset);
2102 ib_sge += ib_sge_cnt;
2103
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002104 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002105 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002106 data_left -= data_len;
2107 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002108
2109 return 0;
2110unmap_sg:
2111 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2112 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2113 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2114 return ret;
2115}
2116
2117static int
Vu Pham59464ef2013-08-28 23:23:35 +03002118isert_map_fr_pagelist(struct ib_device *ib_dev,
2119 struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
2120{
2121 u64 start_addr, end_addr, page, chunk_start = 0;
2122 struct scatterlist *tmp_sg;
2123 int i = 0, new_chunk, last_ent, n_pages;
2124
2125 n_pages = 0;
2126 new_chunk = 1;
2127 last_ent = sg_nents - 1;
2128 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
2129 start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
2130 if (new_chunk)
2131 chunk_start = start_addr;
2132 end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
2133
2134 pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
2135 i, (unsigned long long)tmp_sg->dma_address,
2136 tmp_sg->length);
2137
2138 if ((end_addr & ~PAGE_MASK) && i < last_ent) {
2139 new_chunk = 0;
2140 continue;
2141 }
2142 new_chunk = 1;
2143
2144 page = chunk_start & PAGE_MASK;
2145 do {
2146 fr_pl[n_pages++] = page;
2147 pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
2148 n_pages - 1, page);
2149 page += PAGE_SIZE;
2150 } while (page < end_addr);
2151 }
2152
2153 return n_pages;
2154}
2155
2156static int
2157isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
2158 struct isert_cmd *isert_cmd, struct isert_conn *isert_conn,
2159 struct ib_sge *ib_sge, u32 offset, unsigned int data_len)
2160{
2161 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
2162 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2163 struct scatterlist *sg_start;
2164 u32 sg_off, page_off;
2165 struct ib_send_wr fr_wr, inv_wr;
2166 struct ib_send_wr *bad_wr, *wr = NULL;
2167 u8 key;
2168 int ret, sg_nents, pagelist_len;
2169
2170 sg_off = offset / PAGE_SIZE;
2171 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2172 sg_nents = min_t(unsigned int, cmd->se_cmd.t_data_nents - sg_off,
2173 ISCSI_ISER_SG_TABLESIZE);
2174 page_off = offset % PAGE_SIZE;
2175
2176 pr_debug("Cmd: %p use fr_desc %p sg_nents %d sg_off %d offset %u\n",
2177 isert_cmd, fr_desc, sg_nents, sg_off, offset);
2178
2179 pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
2180 &fr_desc->data_frpl->page_list[0]);
2181
2182 if (!fr_desc->valid) {
2183 memset(&inv_wr, 0, sizeof(inv_wr));
2184 inv_wr.opcode = IB_WR_LOCAL_INV;
2185 inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
2186 wr = &inv_wr;
2187 /* Bump the key */
2188 key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
2189 ib_update_fast_reg_key(fr_desc->data_mr, ++key);
2190 }
2191
2192 /* Prepare FASTREG WR */
2193 memset(&fr_wr, 0, sizeof(fr_wr));
2194 fr_wr.opcode = IB_WR_FAST_REG_MR;
2195 fr_wr.wr.fast_reg.iova_start =
2196 fr_desc->data_frpl->page_list[0] + page_off;
2197 fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
2198 fr_wr.wr.fast_reg.page_list_len = pagelist_len;
2199 fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2200 fr_wr.wr.fast_reg.length = data_len;
2201 fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
2202 fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
2203
2204 if (!wr)
2205 wr = &fr_wr;
2206 else
2207 wr->next = &fr_wr;
2208
2209 ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
2210 if (ret) {
2211 pr_err("fast registration failed, ret:%d\n", ret);
2212 return ret;
2213 }
2214 fr_desc->valid = false;
2215
2216 ib_sge->lkey = fr_desc->data_mr->lkey;
2217 ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
2218 ib_sge->length = data_len;
2219
2220 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
2221 ib_sge->addr, ib_sge->length, ib_sge->lkey);
2222
2223 return ret;
2224}
2225
2226static int
2227isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2228 struct isert_rdma_wr *wr)
2229{
2230 struct se_cmd *se_cmd = &cmd->se_cmd;
2231 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
2232 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2233 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
2234 struct ib_send_wr *send_wr;
2235 struct ib_sge *ib_sge;
2236 struct scatterlist *sg_start;
2237 struct fast_reg_descriptor *fr_desc;
2238 u32 sg_off = 0, sg_nents;
2239 u32 offset = 0, data_len, data_left, rdma_write_max;
2240 int ret = 0, count;
2241 unsigned long flags;
2242
2243 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2244 data_left = se_cmd->data_length;
Vu Pham59464ef2013-08-28 23:23:35 +03002245 } else {
2246 sg_off = cmd->write_data_done / PAGE_SIZE;
2247 data_left = se_cmd->data_length - cmd->write_data_done;
2248 offset = cmd->write_data_done;
2249 isert_cmd->tx_desc.isert_cmd = isert_cmd;
2250 }
2251
2252 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
2253 sg_nents = se_cmd->t_data_nents - sg_off;
2254
2255 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
2256 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2257 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2258 if (unlikely(!count)) {
2259 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
2260 return -EINVAL;
2261 }
2262 wr->sge = sg_start;
2263 wr->num_sge = sg_nents;
2264 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
2265 isert_cmd, count, sg_start, sg_nents, data_left);
2266
2267 memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
2268 ib_sge = &wr->s_ib_sge;
2269 wr->ib_sge = ib_sge;
2270
2271 wr->send_wr_num = 1;
2272 memset(&wr->s_send_wr, 0, sizeof(*send_wr));
2273 wr->send_wr = &wr->s_send_wr;
2274
2275 wr->isert_cmd = isert_cmd;
2276 rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
2277
2278 send_wr = &isert_cmd->rdma_wr.s_send_wr;
2279 send_wr->sg_list = ib_sge;
2280 send_wr->num_sge = 1;
2281 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
2282 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
2283 send_wr->opcode = IB_WR_RDMA_WRITE;
2284 send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
2285 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
2286 send_wr->send_flags = 0;
2287 send_wr->next = &isert_cmd->tx_desc.send_wr;
2288 } else {
2289 send_wr->opcode = IB_WR_RDMA_READ;
2290 send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
2291 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
2292 send_wr->send_flags = IB_SEND_SIGNALED;
2293 }
2294
2295 data_len = min(data_left, rdma_write_max);
2296 wr->cur_rdma_length = data_len;
2297
Vu Phamf01b9f72013-11-11 19:04:29 +02002298 /* if there is a single dma entry, dma mr is sufficient */
2299 if (count == 1) {
2300 ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
2301 ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
2302 ib_sge->lkey = isert_conn->conn_mr->lkey;
2303 wr->fr_desc = NULL;
2304 } else {
2305 spin_lock_irqsave(&isert_conn->conn_lock, flags);
2306 fr_desc = list_first_entry(&isert_conn->conn_frwr_pool,
2307 struct fast_reg_descriptor, list);
2308 list_del(&fr_desc->list);
2309 spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
2310 wr->fr_desc = fr_desc;
Vu Pham59464ef2013-08-28 23:23:35 +03002311
Vu Phamf01b9f72013-11-11 19:04:29 +02002312 ret = isert_fast_reg_mr(fr_desc, isert_cmd, isert_conn,
2313 ib_sge, offset, data_len);
2314 if (ret) {
2315 list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
2316 goto unmap_sg;
2317 }
Vu Pham59464ef2013-08-28 23:23:35 +03002318 }
2319
2320 return 0;
2321
2322unmap_sg:
2323 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
2324 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
2325 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2326 return ret;
2327}
2328
2329static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03002330isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
2331{
2332 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham59464ef2013-08-28 23:23:35 +03002333 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002334 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2335 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002336 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002337 struct ib_send_wr *wr_failed;
2338 int rc;
2339
2340 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
2341 isert_cmd, se_cmd->data_length);
2342 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
Vu Phamd40945d2013-08-28 23:23:34 +03002343 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002344 if (rc) {
2345 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2346 return rc;
2347 }
2348
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349 /*
2350 * Build isert_conn->tx_desc for iSCSI response PDU and attach
2351 */
2352 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger04d9cd12013-11-12 18:05:07 -08002353 iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002354 &isert_cmd->tx_desc.iscsi_header);
2355 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger95b60f02013-11-05 13:16:12 -08002356 isert_init_send_wr(isert_conn, isert_cmd,
2357 &isert_cmd->tx_desc.send_wr, true);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002358
2359 atomic_inc(&isert_conn->post_send_buf_count);
2360
2361 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2362 if (rc) {
2363 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
2364 atomic_dec(&isert_conn->post_send_buf_count);
2365 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002366 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
2367 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002368
Vu Pham90ecc6e2013-08-28 23:23:33 +03002369 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002370}
2371
2372static int
2373isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
2374{
2375 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002376 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002377 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
2378 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Phamd40945d2013-08-28 23:23:34 +03002379 struct isert_device *device = isert_conn->conn_device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03002380 struct ib_send_wr *wr_failed;
2381 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002382
Vu Pham90ecc6e2013-08-28 23:23:33 +03002383 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
2384 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002385 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Phamd40945d2013-08-28 23:23:34 +03002386 rc = device->reg_rdma_mem(conn, cmd, wr);
Vu Pham90ecc6e2013-08-28 23:23:33 +03002387 if (rc) {
2388 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
2389 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002390 }
2391
2392 atomic_inc(&isert_conn->post_send_buf_count);
2393
2394 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
2395 if (rc) {
2396 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
2397 atomic_dec(&isert_conn->post_send_buf_count);
2398 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03002399 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2400 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002401
Vu Pham90ecc6e2013-08-28 23:23:33 +03002402 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002403}
2404
2405static int
2406isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2407{
2408 int ret;
2409
2410 switch (state) {
2411 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2412 ret = isert_put_nopin(cmd, conn, false);
2413 break;
2414 default:
2415 pr_err("Unknown immediate state: 0x%02x\n", state);
2416 ret = -EINVAL;
2417 break;
2418 }
2419
2420 return ret;
2421}
2422
2423static int
2424isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2425{
2426 int ret;
2427
2428 switch (state) {
2429 case ISTATE_SEND_LOGOUTRSP:
2430 ret = isert_put_logout_rsp(cmd, conn);
2431 if (!ret) {
2432 pr_debug("Returning iSER Logout -EAGAIN\n");
2433 ret = -EAGAIN;
2434 }
2435 break;
2436 case ISTATE_SEND_NOPIN:
2437 ret = isert_put_nopin(cmd, conn, true);
2438 break;
2439 case ISTATE_SEND_TASKMGTRSP:
2440 ret = isert_put_tm_rsp(cmd, conn);
2441 break;
2442 case ISTATE_SEND_REJECT:
2443 ret = isert_put_reject(cmd, conn);
2444 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002445 case ISTATE_SEND_TEXTRSP:
2446 ret = isert_put_text_rsp(cmd, conn);
2447 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002448 case ISTATE_SEND_STATUS:
2449 /*
2450 * Special case for sending non GOOD SCSI status from TX thread
2451 * context during pre se_cmd excecution failure.
2452 */
2453 ret = isert_put_response(conn, cmd);
2454 break;
2455 default:
2456 pr_err("Unknown response state: 0x%02x\n", state);
2457 ret = -EINVAL;
2458 break;
2459 }
2460
2461 return ret;
2462}
2463
2464static int
2465isert_setup_np(struct iscsi_np *np,
2466 struct __kernel_sockaddr_storage *ksockaddr)
2467{
2468 struct isert_np *isert_np;
2469 struct rdma_cm_id *isert_lid;
2470 struct sockaddr *sa;
2471 int ret;
2472
2473 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2474 if (!isert_np) {
2475 pr_err("Unable to allocate struct isert_np\n");
2476 return -ENOMEM;
2477 }
2478 init_waitqueue_head(&isert_np->np_accept_wq);
2479 mutex_init(&isert_np->np_accept_mutex);
2480 INIT_LIST_HEAD(&isert_np->np_accept_list);
2481 init_completion(&isert_np->np_login_comp);
2482
2483 sa = (struct sockaddr *)ksockaddr;
2484 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2485 /*
2486 * Setup the np->np_sockaddr from the passed sockaddr setup
2487 * in iscsi_target_configfs.c code..
2488 */
2489 memcpy(&np->np_sockaddr, ksockaddr,
2490 sizeof(struct __kernel_sockaddr_storage));
2491
2492 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2493 IB_QPT_RC);
2494 if (IS_ERR(isert_lid)) {
2495 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2496 PTR_ERR(isert_lid));
2497 ret = PTR_ERR(isert_lid);
2498 goto out;
2499 }
2500
2501 ret = rdma_bind_addr(isert_lid, sa);
2502 if (ret) {
2503 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2504 goto out_lid;
2505 }
2506
2507 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2508 if (ret) {
2509 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2510 goto out_lid;
2511 }
2512
2513 isert_np->np_cm_id = isert_lid;
2514 np->np_context = isert_np;
2515 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2516
2517 return 0;
2518
2519out_lid:
2520 rdma_destroy_id(isert_lid);
2521out:
2522 kfree(isert_np);
2523 return ret;
2524}
2525
2526static int
2527isert_check_accept_queue(struct isert_np *isert_np)
2528{
2529 int empty;
2530
2531 mutex_lock(&isert_np->np_accept_mutex);
2532 empty = list_empty(&isert_np->np_accept_list);
2533 mutex_unlock(&isert_np->np_accept_mutex);
2534
2535 return empty;
2536}
2537
2538static int
2539isert_rdma_accept(struct isert_conn *isert_conn)
2540{
2541 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2542 struct rdma_conn_param cp;
2543 int ret;
2544
2545 memset(&cp, 0, sizeof(struct rdma_conn_param));
2546 cp.responder_resources = isert_conn->responder_resources;
2547 cp.initiator_depth = isert_conn->initiator_depth;
2548 cp.retry_count = 7;
2549 cp.rnr_retry_count = 7;
2550
2551 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2552
2553 ret = rdma_accept(cm_id, &cp);
2554 if (ret) {
2555 pr_err("rdma_accept() failed with: %d\n", ret);
2556 return ret;
2557 }
2558
2559 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2560
2561 return 0;
2562}
2563
2564static int
2565isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2566{
2567 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2568 int ret;
2569
2570 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002571 /*
2572 * For login requests after the first PDU, isert_rx_login_req() will
2573 * kick schedule_delayed_work(&conn->login_work) as the packet is
2574 * received, which turns this callback from iscsi_target_do_login_rx()
2575 * into a NOP.
2576 */
2577 if (!login->first_request)
2578 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002579
2580 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2581 if (ret)
2582 return ret;
2583
2584 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2585 return 0;
2586}
2587
2588static void
2589isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2590 struct isert_conn *isert_conn)
2591{
2592 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2593 struct rdma_route *cm_route = &cm_id->route;
2594 struct sockaddr_in *sock_in;
2595 struct sockaddr_in6 *sock_in6;
2596
2597 conn->login_family = np->np_sockaddr.ss_family;
2598
2599 if (np->np_sockaddr.ss_family == AF_INET6) {
2600 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2601 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2602 &sock_in6->sin6_addr.in6_u);
2603 conn->login_port = ntohs(sock_in6->sin6_port);
2604
2605 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2606 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2607 &sock_in6->sin6_addr.in6_u);
2608 conn->local_port = ntohs(sock_in6->sin6_port);
2609 } else {
2610 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2611 sprintf(conn->login_ip, "%pI4",
2612 &sock_in->sin_addr.s_addr);
2613 conn->login_port = ntohs(sock_in->sin_port);
2614
2615 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2616 sprintf(conn->local_ip, "%pI4",
2617 &sock_in->sin_addr.s_addr);
2618 conn->local_port = ntohs(sock_in->sin_port);
2619 }
2620}
2621
2622static int
2623isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2624{
2625 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2626 struct isert_conn *isert_conn;
2627 int max_accept = 0, ret;
2628
2629accept_wait:
2630 ret = wait_event_interruptible(isert_np->np_accept_wq,
2631 !isert_check_accept_queue(isert_np) ||
2632 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2633 if (max_accept > 5)
2634 return -ENODEV;
2635
2636 spin_lock_bh(&np->np_thread_lock);
2637 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2638 spin_unlock_bh(&np->np_thread_lock);
2639 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2640 return -ENODEV;
2641 }
2642 spin_unlock_bh(&np->np_thread_lock);
2643
2644 mutex_lock(&isert_np->np_accept_mutex);
2645 if (list_empty(&isert_np->np_accept_list)) {
2646 mutex_unlock(&isert_np->np_accept_mutex);
2647 max_accept++;
2648 goto accept_wait;
2649 }
2650 isert_conn = list_first_entry(&isert_np->np_accept_list,
2651 struct isert_conn, conn_accept_node);
2652 list_del_init(&isert_conn->conn_accept_node);
2653 mutex_unlock(&isert_np->np_accept_mutex);
2654
2655 conn->context = isert_conn;
2656 isert_conn->conn = conn;
2657 max_accept = 0;
2658
2659 ret = isert_rdma_post_recvl(isert_conn);
2660 if (ret)
2661 return ret;
2662
2663 ret = isert_rdma_accept(isert_conn);
2664 if (ret)
2665 return ret;
2666
2667 isert_set_conn_info(np, conn, isert_conn);
2668
2669 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2670 return 0;
2671}
2672
2673static void
2674isert_free_np(struct iscsi_np *np)
2675{
2676 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2677
2678 rdma_destroy_id(isert_np->np_cm_id);
2679
2680 np->np_context = NULL;
2681 kfree(isert_np);
2682}
2683
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002684static int isert_check_state(struct isert_conn *isert_conn, int state)
2685{
2686 int ret;
2687
2688 mutex_lock(&isert_conn->conn_mutex);
2689 ret = (isert_conn->state == state);
2690 mutex_unlock(&isert_conn->conn_mutex);
2691
2692 return ret;
2693}
2694
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002695static void isert_free_conn(struct iscsi_conn *conn)
2696{
2697 struct isert_conn *isert_conn = conn->context;
2698
2699 pr_debug("isert_free_conn: Starting \n");
2700 /*
2701 * Decrement post_send_buf_count for special case when called
2702 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2703 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002704 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002705 if (isert_conn->logout_posted)
2706 atomic_dec(&isert_conn->post_send_buf_count);
2707
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002708 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2709 pr_debug("Calling rdma_disconnect from isert_free_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002710 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002711 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002712 /*
2713 * Only wait for conn_wait_comp_err if the isert_conn made it
2714 * into full feature phase..
2715 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002716 if (isert_conn->state == ISER_CONN_UP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002717 pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2718 isert_conn->state);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002719 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002720
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002721 wait_event(isert_conn->conn_wait_comp_err,
2722 (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
2723
2724 wait_event(isert_conn->conn_wait,
2725 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2726
2727 isert_put_conn(isert_conn);
2728 return;
2729 }
2730 if (isert_conn->state == ISER_CONN_INIT) {
2731 mutex_unlock(&isert_conn->conn_mutex);
2732 isert_put_conn(isert_conn);
2733 return;
2734 }
2735 pr_debug("isert_free_conn: wait_event conn_wait %d\n",
2736 isert_conn->state);
2737 mutex_unlock(&isert_conn->conn_mutex);
2738
2739 wait_event(isert_conn->conn_wait,
2740 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002741
2742 isert_put_conn(isert_conn);
2743}
2744
2745static struct iscsit_transport iser_target_transport = {
2746 .name = "IB/iSER",
2747 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002748 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002749 .owner = THIS_MODULE,
2750 .iscsit_setup_np = isert_setup_np,
2751 .iscsit_accept_np = isert_accept_np,
2752 .iscsit_free_np = isert_free_np,
2753 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002754 .iscsit_get_login_rx = isert_get_login_rx,
2755 .iscsit_put_login_tx = isert_put_login_tx,
2756 .iscsit_immediate_queue = isert_immediate_queue,
2757 .iscsit_response_queue = isert_response_queue,
2758 .iscsit_get_dataout = isert_get_dataout,
2759 .iscsit_queue_data_in = isert_put_datain,
2760 .iscsit_queue_status = isert_put_response,
2761};
2762
2763static int __init isert_init(void)
2764{
2765 int ret;
2766
2767 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2768 if (!isert_rx_wq) {
2769 pr_err("Unable to allocate isert_rx_wq\n");
2770 return -ENOMEM;
2771 }
2772
2773 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2774 if (!isert_comp_wq) {
2775 pr_err("Unable to allocate isert_comp_wq\n");
2776 ret = -ENOMEM;
2777 goto destroy_rx_wq;
2778 }
2779
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002780 iscsit_register_transport(&iser_target_transport);
2781 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2782 return 0;
2783
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002784destroy_rx_wq:
2785 destroy_workqueue(isert_rx_wq);
2786 return ret;
2787}
2788
2789static void __exit isert_exit(void)
2790{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002791 destroy_workqueue(isert_comp_wq);
2792 destroy_workqueue(isert_rx_wq);
2793 iscsit_unregister_transport(&iser_target_transport);
2794 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2795}
2796
2797MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2798MODULE_VERSION("0.1");
2799MODULE_AUTHOR("nab@Linux-iSCSI.org");
2800MODULE_LICENSE("GPL");
2801
2802module_init(isert_init);
2803module_exit(isert_exit);