blob: 14499991802284bd2bf1283c63f5f6c2eec15cb9 [file] [log] [blame]
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001/*******************************************************************************
2 * This file contains iSCSI extentions for RDMA (iSER) Verbs
3 *
4 * (c) Copyright 2013 RisingTide Systems LLC.
5 *
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>
25#include <rdma/ib_verbs.h>
26#include <rdma/rdma_cm.h>
27#include <target/target_core_base.h>
28#include <target/target_core_fabric.h>
29#include <target/iscsi/iscsi_transport.h>
Sagi Grimberg8a2629a2014-04-29 13:13:45 +030030#include <linux/semaphore.h>
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080031
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;
43static struct kmem_cache *isert_cmd_cache;
44
45static void
46isert_qp_event_callback(struct ib_event *e, void *context)
47{
48 struct isert_conn *isert_conn = (struct isert_conn *)context;
49
50 pr_err("isert_qp_event_callback event: %d\n", e->event);
51 switch (e->event) {
52 case IB_EVENT_COMM_EST:
53 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
54 break;
55 case IB_EVENT_QP_LAST_WQE_REACHED:
56 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
57 break;
58 default:
59 break;
60 }
61}
62
63static int
64isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
65{
66 int ret;
67
68 ret = ib_query_device(ib_dev, devattr);
69 if (ret) {
70 pr_err("ib_query_device() failed: %d\n", ret);
71 return ret;
72 }
73 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
74 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
75
76 return 0;
77}
78
79static int
80isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
81{
82 struct isert_device *device = isert_conn->conn_device;
83 struct ib_qp_init_attr attr;
84 struct ib_device_attr devattr;
85 int ret, index, min_index = 0;
86
87 memset(&devattr, 0, sizeof(struct ib_device_attr));
88 ret = isert_query_device(cma_id->device, &devattr);
89 if (ret)
90 return ret;
91
92 mutex_lock(&device_list_mutex);
93 for (index = 0; index < device->cqs_used; index++)
94 if (device->cq_active_qps[index] <
95 device->cq_active_qps[min_index])
96 min_index = index;
97 device->cq_active_qps[min_index]++;
98 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
99 mutex_unlock(&device_list_mutex);
100
101 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
102 attr.event_handler = isert_qp_event_callback;
103 attr.qp_context = isert_conn;
104 attr.send_cq = device->dev_tx_cq[min_index];
105 attr.recv_cq = device->dev_rx_cq[min_index];
106 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
107 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
108 /*
109 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
110 * work-around for RDMA_READ..
111 */
112 attr.cap.max_send_sge = devattr.max_sge - 2;
113 isert_conn->max_sge = attr.cap.max_send_sge;
114
115 attr.cap.max_recv_sge = 1;
116 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
117 attr.qp_type = IB_QPT_RC;
118
119 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
120 cma_id->device);
121 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
122 isert_conn->conn_pd->device);
123
124 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
125 if (ret) {
126 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
127 return ret;
128 }
129 isert_conn->conn_qp = cma_id->qp;
130 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
131
132 return 0;
133}
134
135static void
136isert_cq_event_callback(struct ib_event *e, void *context)
137{
138 pr_debug("isert_cq_event_callback event: %d\n", e->event);
139}
140
141static int
142isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
143{
144 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
145 struct iser_rx_desc *rx_desc;
146 struct ib_sge *rx_sg;
147 u64 dma_addr;
148 int i, j;
149
150 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
151 sizeof(struct iser_rx_desc), GFP_KERNEL);
152 if (!isert_conn->conn_rx_descs)
153 goto fail;
154
155 rx_desc = isert_conn->conn_rx_descs;
156
157 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
158 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
159 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
160 if (ib_dma_mapping_error(ib_dev, dma_addr))
161 goto dma_map_fail;
162
163 rx_desc->dma_addr = dma_addr;
164
165 rx_sg = &rx_desc->rx_sg;
166 rx_sg->addr = rx_desc->dma_addr;
167 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
168 rx_sg->lkey = isert_conn->conn_mr->lkey;
169 }
170
171 isert_conn->conn_rx_desc_head = 0;
172 return 0;
173
174dma_map_fail:
175 rx_desc = isert_conn->conn_rx_descs;
176 for (j = 0; j < i; j++, rx_desc++) {
177 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
178 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
179 }
180 kfree(isert_conn->conn_rx_descs);
181 isert_conn->conn_rx_descs = NULL;
182fail:
183 return -ENOMEM;
184}
185
186static void
187isert_free_rx_descriptors(struct isert_conn *isert_conn)
188{
189 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
190 struct iser_rx_desc *rx_desc;
191 int i;
192
193 if (!isert_conn->conn_rx_descs)
194 return;
195
196 rx_desc = isert_conn->conn_rx_descs;
197 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
198 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
199 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
200 }
201
202 kfree(isert_conn->conn_rx_descs);
203 isert_conn->conn_rx_descs = NULL;
204}
205
206static void isert_cq_tx_callback(struct ib_cq *, void *);
207static void isert_cq_rx_callback(struct ib_cq *, void *);
208
209static int
210isert_create_device_ib_res(struct isert_device *device)
211{
212 struct ib_device *ib_dev = device->ib_device;
213 struct isert_cq_desc *cq_desc;
214 int ret = 0, i, j;
215
216 device->cqs_used = min_t(int, num_online_cpus(),
217 device->ib_device->num_comp_vectors);
218 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
219 pr_debug("Using %d CQs, device %s supports %d vectors\n",
220 device->cqs_used, device->ib_device->name,
221 device->ib_device->num_comp_vectors);
222 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
223 device->cqs_used, GFP_KERNEL);
224 if (!device->cq_desc) {
225 pr_err("Unable to allocate device->cq_desc\n");
226 return -ENOMEM;
227 }
228 cq_desc = device->cq_desc;
229
230 device->dev_pd = ib_alloc_pd(ib_dev);
231 if (IS_ERR(device->dev_pd)) {
232 ret = PTR_ERR(device->dev_pd);
233 pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
234 goto out_cq_desc;
235 }
236
237 for (i = 0; i < device->cqs_used; i++) {
238 cq_desc[i].device = device;
239 cq_desc[i].cq_index = i;
240
241 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
242 isert_cq_rx_callback,
243 isert_cq_event_callback,
244 (void *)&cq_desc[i],
245 ISER_MAX_RX_CQ_LEN, i);
Wei Yongjun0629b402013-10-29 09:56:34 +0800246 if (IS_ERR(device->dev_rx_cq[i])) {
247 ret = PTR_ERR(device->dev_rx_cq[i]);
248 device->dev_rx_cq[i] = NULL;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800249 goto out_cq;
Wei Yongjun0629b402013-10-29 09:56:34 +0800250 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800251
252 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
253 isert_cq_tx_callback,
254 isert_cq_event_callback,
255 (void *)&cq_desc[i],
256 ISER_MAX_TX_CQ_LEN, i);
Wei Yongjun0629b402013-10-29 09:56:34 +0800257 if (IS_ERR(device->dev_tx_cq[i])) {
258 ret = PTR_ERR(device->dev_tx_cq[i]);
259 device->dev_tx_cq[i] = NULL;
260 goto out_cq;
261 }
262
263 ret = ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP);
264 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800265 goto out_cq;
266
Wei Yongjun0629b402013-10-29 09:56:34 +0800267 ret = ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP);
268 if (ret)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800269 goto out_cq;
270 }
271
272 device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
273 if (IS_ERR(device->dev_mr)) {
274 ret = PTR_ERR(device->dev_mr);
275 pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
276 goto out_cq;
277 }
278
279 return 0;
280
281out_cq:
282 for (j = 0; j < i; j++) {
283 cq_desc = &device->cq_desc[j];
284
285 if (device->dev_rx_cq[j]) {
286 cancel_work_sync(&cq_desc->cq_rx_work);
287 ib_destroy_cq(device->dev_rx_cq[j]);
288 }
289 if (device->dev_tx_cq[j]) {
290 cancel_work_sync(&cq_desc->cq_tx_work);
291 ib_destroy_cq(device->dev_tx_cq[j]);
292 }
293 }
294 ib_dealloc_pd(device->dev_pd);
295
296out_cq_desc:
297 kfree(device->cq_desc);
298
299 return ret;
300}
301
302static void
303isert_free_device_ib_res(struct isert_device *device)
304{
305 struct isert_cq_desc *cq_desc;
306 int i;
307
308 for (i = 0; i < device->cqs_used; i++) {
309 cq_desc = &device->cq_desc[i];
310
311 cancel_work_sync(&cq_desc->cq_rx_work);
312 cancel_work_sync(&cq_desc->cq_tx_work);
313 ib_destroy_cq(device->dev_rx_cq[i]);
314 ib_destroy_cq(device->dev_tx_cq[i]);
315 device->dev_rx_cq[i] = NULL;
316 device->dev_tx_cq[i] = NULL;
317 }
318
319 ib_dereg_mr(device->dev_mr);
320 ib_dealloc_pd(device->dev_pd);
321 kfree(device->cq_desc);
322}
323
324static void
325isert_device_try_release(struct isert_device *device)
326{
327 mutex_lock(&device_list_mutex);
328 device->refcount--;
329 if (!device->refcount) {
330 isert_free_device_ib_res(device);
331 list_del(&device->dev_node);
332 kfree(device);
333 }
334 mutex_unlock(&device_list_mutex);
335}
336
337static struct isert_device *
338isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
339{
340 struct isert_device *device;
341 int ret;
342
343 mutex_lock(&device_list_mutex);
344 list_for_each_entry(device, &device_list, dev_node) {
345 if (device->ib_device->node_guid == cma_id->device->node_guid) {
346 device->refcount++;
347 mutex_unlock(&device_list_mutex);
348 return device;
349 }
350 }
351
352 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
353 if (!device) {
354 mutex_unlock(&device_list_mutex);
355 return ERR_PTR(-ENOMEM);
356 }
357
358 INIT_LIST_HEAD(&device->dev_node);
359
360 device->ib_device = cma_id->device;
361 ret = isert_create_device_ib_res(device);
362 if (ret) {
363 kfree(device);
364 mutex_unlock(&device_list_mutex);
365 return ERR_PTR(ret);
366 }
367
368 device->refcount++;
369 list_add_tail(&device->dev_node, &device_list);
370 mutex_unlock(&device_list_mutex);
371
372 return device;
373}
374
375static int
376isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
377{
378 struct iscsi_np *np = cma_id->context;
379 struct isert_np *isert_np = np->np_context;
380 struct isert_conn *isert_conn;
381 struct isert_device *device;
382 struct ib_device *ib_dev = cma_id->device;
383 int ret = 0;
384
Sagi Grimbergc2334082014-04-29 13:13:47 +0300385 spin_lock_bh(&np->np_thread_lock);
386 if (!np->enabled) {
387 spin_unlock_bh(&np->np_thread_lock);
388 pr_debug("iscsi_np is not enabled, reject connect request\n");
389 return rdma_reject(cma_id, NULL, 0);
390 }
391 spin_unlock_bh(&np->np_thread_lock);
392
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800393 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
394 cma_id, cma_id->context);
395
396 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
397 if (!isert_conn) {
398 pr_err("Unable to allocate isert_conn\n");
399 return -ENOMEM;
400 }
401 isert_conn->state = ISER_CONN_INIT;
402 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
403 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800404 init_completion(&isert_conn->conn_wait);
405 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800406 kref_init(&isert_conn->conn_kref);
407 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700408 mutex_init(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800409
410 cma_id->context = isert_conn;
411 isert_conn->conn_cm_id = cma_id;
412 isert_conn->responder_resources = event->param.conn.responder_resources;
413 isert_conn->initiator_depth = event->param.conn.initiator_depth;
414 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
415 isert_conn->responder_resources, isert_conn->initiator_depth);
416
417 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
418 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
419 if (!isert_conn->login_buf) {
420 pr_err("Unable to allocate isert_conn->login_buf\n");
421 ret = -ENOMEM;
422 goto out;
423 }
424
425 isert_conn->login_req_buf = isert_conn->login_buf;
426 isert_conn->login_rsp_buf = isert_conn->login_buf +
427 ISCSI_DEF_MAX_RECV_SEG_LEN;
428 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
429 isert_conn->login_buf, isert_conn->login_req_buf,
430 isert_conn->login_rsp_buf);
431
432 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
433 (void *)isert_conn->login_req_buf,
434 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
435
436 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
437 if (ret) {
438 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
439 ret);
440 isert_conn->login_req_dma = 0;
441 goto out_login_buf;
442 }
443
444 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
445 (void *)isert_conn->login_rsp_buf,
446 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
447
448 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
449 if (ret) {
450 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
451 ret);
452 isert_conn->login_rsp_dma = 0;
453 goto out_req_dma_map;
454 }
455
456 device = isert_device_find_by_ib_dev(cma_id);
457 if (IS_ERR(device)) {
458 ret = PTR_ERR(device);
459 goto out_rsp_dma_map;
460 }
461
462 isert_conn->conn_device = device;
463 isert_conn->conn_pd = device->dev_pd;
464 isert_conn->conn_mr = device->dev_mr;
465
466 ret = isert_conn_setup_qp(isert_conn, cma_id);
467 if (ret)
468 goto out_conn_dev;
469
470 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg5de94f82014-04-29 13:13:44 +0300471 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800472 mutex_unlock(&isert_np->np_accept_mutex);
473
Sagi Grimberg8a2629a2014-04-29 13:13:45 +0300474 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
475 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800476 return 0;
477
478out_conn_dev:
479 isert_device_try_release(device);
480out_rsp_dma_map:
481 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
482 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
483out_req_dma_map:
484 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
485 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
486out_login_buf:
487 kfree(isert_conn->login_buf);
488out:
489 kfree(isert_conn);
490 return ret;
491}
492
493static void
494isert_connect_release(struct isert_conn *isert_conn)
495{
496 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
497 struct isert_device *device = isert_conn->conn_device;
498 int cq_index;
499
500 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
501
502 if (isert_conn->conn_qp) {
503 cq_index = ((struct isert_cq_desc *)
504 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
505 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
506 isert_conn->conn_device->cq_active_qps[cq_index]--;
507
508 rdma_destroy_qp(isert_conn->conn_cm_id);
509 }
510
511 isert_free_rx_descriptors(isert_conn);
512 rdma_destroy_id(isert_conn->conn_cm_id);
513
514 if (isert_conn->login_buf) {
515 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
516 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
517 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
518 ISCSI_DEF_MAX_RECV_SEG_LEN,
519 DMA_FROM_DEVICE);
520 kfree(isert_conn->login_buf);
521 }
522 kfree(isert_conn);
523
524 if (device)
525 isert_device_try_release(device);
526
527 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
528}
529
530static void
531isert_connected_handler(struct rdma_cm_id *cma_id)
532{
533 return;
534}
535
536static void
537isert_release_conn_kref(struct kref *kref)
538{
539 struct isert_conn *isert_conn = container_of(kref,
540 struct isert_conn, conn_kref);
541
542 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
543 current->comm, current->pid);
544
545 isert_connect_release(isert_conn);
546}
547
548static void
549isert_put_conn(struct isert_conn *isert_conn)
550{
551 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
552}
553
554static void
555isert_disconnect_work(struct work_struct *work)
556{
557 struct isert_conn *isert_conn = container_of(work,
558 struct isert_conn, conn_logout_work);
559
560 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700561 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800562 if (isert_conn->state == ISER_CONN_UP)
563 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800564
565 if (isert_conn->post_recv_buf_count == 0 &&
566 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700567 mutex_unlock(&isert_conn->conn_mutex);
568 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800569 }
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700570 if (!isert_conn->conn_cm_id) {
571 mutex_unlock(&isert_conn->conn_mutex);
572 isert_put_conn(isert_conn);
573 return;
574 }
575 if (!isert_conn->logout_posted) {
576 pr_debug("Calling rdma_disconnect for !logout_posted from"
577 " isert_disconnect_work\n");
578 rdma_disconnect(isert_conn->conn_cm_id);
579 mutex_unlock(&isert_conn->conn_mutex);
580 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
581 goto wake_up;
582 }
583 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800584
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700585wake_up:
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800586 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800587 isert_put_conn(isert_conn);
588}
589
590static void
591isert_disconnected_handler(struct rdma_cm_id *cma_id)
592{
593 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
594
595 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
596 schedule_work(&isert_conn->conn_logout_work);
597}
598
599static int
600isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
601{
602 int ret = 0;
603
604 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
605 event->event, event->status, cma_id->context, cma_id);
606
607 switch (event->event) {
608 case RDMA_CM_EVENT_CONNECT_REQUEST:
609 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
610 ret = isert_connect_request(cma_id, event);
611 break;
612 case RDMA_CM_EVENT_ESTABLISHED:
613 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
614 isert_connected_handler(cma_id);
615 break;
616 case RDMA_CM_EVENT_DISCONNECTED:
617 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
618 isert_disconnected_handler(cma_id);
619 break;
620 case RDMA_CM_EVENT_DEVICE_REMOVAL:
621 case RDMA_CM_EVENT_ADDR_CHANGE:
622 break;
623 case RDMA_CM_EVENT_CONNECT_ERROR:
624 default:
625 pr_err("Unknown RDMA CMA event: %d\n", event->event);
626 break;
627 }
628
629 if (ret != 0) {
630 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
631 event->event, ret);
632 dump_stack();
633 }
634
635 return ret;
636}
637
638static int
639isert_post_recv(struct isert_conn *isert_conn, u32 count)
640{
641 struct ib_recv_wr *rx_wr, *rx_wr_failed;
642 int i, ret;
643 unsigned int rx_head = isert_conn->conn_rx_desc_head;
644 struct iser_rx_desc *rx_desc;
645
646 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
647 rx_desc = &isert_conn->conn_rx_descs[rx_head];
648 rx_wr->wr_id = (unsigned long)rx_desc;
649 rx_wr->sg_list = &rx_desc->rx_sg;
650 rx_wr->num_sge = 1;
651 rx_wr->next = rx_wr + 1;
652 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
653 }
654
655 rx_wr--;
656 rx_wr->next = NULL; /* mark end of work requests list */
657
658 isert_conn->post_recv_buf_count += count;
659 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
660 &rx_wr_failed);
661 if (ret) {
662 pr_err("ib_post_recv() failed with ret: %d\n", ret);
663 isert_conn->post_recv_buf_count -= count;
664 } else {
665 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
666 isert_conn->conn_rx_desc_head = rx_head;
667 }
668 return ret;
669}
670
671static int
672isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
673{
674 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
675 struct ib_send_wr send_wr, *send_wr_failed;
676 int ret;
677
678 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
679 ISER_HEADERS_LEN, DMA_TO_DEVICE);
680
681 send_wr.next = NULL;
682 send_wr.wr_id = (unsigned long)tx_desc;
683 send_wr.sg_list = tx_desc->tx_sg;
684 send_wr.num_sge = tx_desc->num_sge;
685 send_wr.opcode = IB_WR_SEND;
686 send_wr.send_flags = IB_SEND_SIGNALED;
687
688 atomic_inc(&isert_conn->post_send_buf_count);
689
690 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
691 if (ret) {
692 pr_err("ib_post_send() failed, ret: %d\n", ret);
693 atomic_dec(&isert_conn->post_send_buf_count);
694 }
695
696 return ret;
697}
698
699static void
700isert_create_send_desc(struct isert_conn *isert_conn,
701 struct isert_cmd *isert_cmd,
702 struct iser_tx_desc *tx_desc)
703{
704 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
705
706 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
707 ISER_HEADERS_LEN, DMA_TO_DEVICE);
708
709 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
710 tx_desc->iser_header.flags = ISER_VER;
711
712 tx_desc->num_sge = 1;
713 tx_desc->isert_cmd = isert_cmd;
714
715 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
716 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
717 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
718 }
719}
720
721static int
722isert_init_tx_hdrs(struct isert_conn *isert_conn,
723 struct iser_tx_desc *tx_desc)
724{
725 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
726 u64 dma_addr;
727
728 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
729 ISER_HEADERS_LEN, DMA_TO_DEVICE);
730 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
731 pr_err("ib_dma_mapping_error() failed\n");
732 return -ENOMEM;
733 }
734
735 tx_desc->dma_addr = dma_addr;
736 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
737 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
738 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
739
740 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
741 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
742 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
743
744 return 0;
745}
746
747static void
748isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
749{
750 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
751 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
752 send_wr->opcode = IB_WR_SEND;
753 send_wr->send_flags = IB_SEND_SIGNALED;
754 send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
755 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
756}
757
758static int
759isert_rdma_post_recvl(struct isert_conn *isert_conn)
760{
761 struct ib_recv_wr rx_wr, *rx_wr_fail;
762 struct ib_sge sge;
763 int ret;
764
765 memset(&sge, 0, sizeof(struct ib_sge));
766 sge.addr = isert_conn->login_req_dma;
767 sge.length = ISER_RX_LOGIN_SIZE;
768 sge.lkey = isert_conn->conn_mr->lkey;
769
770 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
771 sge.addr, sge.length, sge.lkey);
772
773 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
774 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
775 rx_wr.sg_list = &sge;
776 rx_wr.num_sge = 1;
777
778 isert_conn->post_recv_buf_count++;
779 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
780 if (ret) {
781 pr_err("ib_post_recv() failed: %d\n", ret);
782 isert_conn->post_recv_buf_count--;
783 }
784
785 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
786 return ret;
787}
788
789static int
790isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
791 u32 length)
792{
793 struct isert_conn *isert_conn = conn->context;
794 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
795 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
796 int ret;
797
798 isert_create_send_desc(isert_conn, NULL, tx_desc);
799
800 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
801 sizeof(struct iscsi_hdr));
802
803 isert_init_tx_hdrs(isert_conn, tx_desc);
804
805 if (length > 0) {
806 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
807
808 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
809 length, DMA_TO_DEVICE);
810
811 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
812
813 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
814 length, DMA_TO_DEVICE);
815
816 tx_dsg->addr = isert_conn->login_rsp_dma;
817 tx_dsg->length = length;
818 tx_dsg->lkey = isert_conn->conn_mr->lkey;
819 tx_desc->num_sge = 2;
820 }
821 if (!login->login_failed) {
822 if (login->login_complete) {
823 ret = isert_alloc_rx_descriptors(isert_conn);
824 if (ret)
825 return ret;
826
827 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
828 if (ret)
829 return ret;
830
831 isert_conn->state = ISER_CONN_UP;
832 goto post_send;
833 }
834
835 ret = isert_rdma_post_recvl(isert_conn);
836 if (ret)
837 return ret;
838 }
839post_send:
840 ret = isert_post_send(isert_conn, tx_desc);
841 if (ret)
842 return ret;
843
844 return 0;
845}
846
847static void
848isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
849 struct isert_conn *isert_conn)
850{
851 struct iscsi_conn *conn = isert_conn->conn;
852 struct iscsi_login *login = conn->conn_login;
853 int size;
854
855 if (!login) {
856 pr_err("conn->conn_login is NULL\n");
857 dump_stack();
858 return;
859 }
860
861 if (login->first_request) {
862 struct iscsi_login_req *login_req =
863 (struct iscsi_login_req *)&rx_desc->iscsi_header;
864 /*
865 * Setup the initial iscsi_login values from the leading
866 * login request PDU.
867 */
868 login->leading_connection = (!login_req->tsih) ? 1 : 0;
869 login->current_stage =
870 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
871 >> 2;
872 login->version_min = login_req->min_version;
873 login->version_max = login_req->max_version;
874 memcpy(login->isid, login_req->isid, 6);
875 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
876 login->init_task_tag = login_req->itt;
877 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
878 login->cid = be16_to_cpu(login_req->cid);
879 login->tsih = be16_to_cpu(login_req->tsih);
880 }
881
882 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
883
884 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
885 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
886 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
887 memcpy(login->req_buf, &rx_desc->data[0], size);
888
889 complete(&isert_conn->conn_login_comp);
890}
891
892static void
893isert_release_cmd(struct iscsi_cmd *cmd)
894{
895 struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
896 iscsi_cmd);
897
898 pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
899
900 kfree(cmd->buf_ptr);
901 kfree(cmd->tmr_req);
902
903 kmem_cache_free(isert_cmd_cache, isert_cmd);
904}
905
906static struct iscsi_cmd
907*isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
908{
909 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
910 struct isert_cmd *isert_cmd;
911
912 isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
913 if (!isert_cmd) {
914 pr_err("Unable to allocate isert_cmd\n");
915 return NULL;
916 }
917 isert_cmd->conn = isert_conn;
918 isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
919
920 return &isert_cmd->iscsi_cmd;
921}
922
923static int
924isert_handle_scsi_cmd(struct isert_conn *isert_conn,
925 struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
926 unsigned char *buf)
927{
928 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
929 struct iscsi_conn *conn = isert_conn->conn;
930 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
931 struct scatterlist *sg;
932 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
933 bool dump_payload = false;
934
935 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
936 if (rc < 0)
937 return rc;
938
939 imm_data = cmd->immediate_data;
940 imm_data_len = cmd->first_burst_len;
941 unsol_data = cmd->unsolicited_data;
942
943 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
944 if (rc < 0) {
945 return 0;
946 } else if (rc > 0) {
947 dump_payload = true;
948 goto sequence_cmd;
949 }
950
951 if (!imm_data)
952 return 0;
953
954 sg = &cmd->se_cmd.t_data_sg[0];
955 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
956
957 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
958 sg, sg_nents, &rx_desc->data[0], imm_data_len);
959
960 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
961
962 cmd->write_data_done += imm_data_len;
963
964 if (cmd->write_data_done == cmd->se_cmd.data_length) {
965 spin_lock_bh(&cmd->istate_lock);
966 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
967 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
968 spin_unlock_bh(&cmd->istate_lock);
969 }
970
971sequence_cmd:
Nicholas Bellingeradb97c22013-07-30 04:04:02 +0000972 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800973
974 if (!rc && dump_payload == false && unsol_data)
975 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger553e4c52014-05-23 00:48:35 -0700976 else if (dump_payload && imm_data)
977 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800978
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800979 return 0;
980}
981
982static int
983isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
984 struct iser_rx_desc *rx_desc, unsigned char *buf)
985{
986 struct scatterlist *sg_start;
987 struct iscsi_conn *conn = isert_conn->conn;
988 struct iscsi_cmd *cmd = NULL;
989 struct iscsi_data *hdr = (struct iscsi_data *)buf;
990 u32 unsol_data_len = ntoh24(hdr->dlength);
991 int rc, sg_nents, sg_off, page_off;
992
993 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
994 if (rc < 0)
995 return rc;
996 else if (!cmd)
997 return 0;
998 /*
999 * FIXME: Unexpected unsolicited_data out
1000 */
1001 if (!cmd->unsolicited_data) {
1002 pr_err("Received unexpected solicited data payload\n");
1003 dump_stack();
1004 return -1;
1005 }
1006
1007 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1008 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1009
1010 sg_off = cmd->write_data_done / PAGE_SIZE;
1011 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1012 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1013 page_off = cmd->write_data_done % PAGE_SIZE;
1014 /*
1015 * FIXME: Non page-aligned unsolicited_data out
1016 */
1017 if (page_off) {
1018 pr_err("Received unexpected non-page aligned data payload\n");
1019 dump_stack();
1020 return -1;
1021 }
1022 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1023 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1024
1025 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1026 unsol_data_len);
1027
1028 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1029 if (rc < 0)
1030 return rc;
1031
1032 return 0;
1033}
1034
1035static int
1036isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1037 uint32_t read_stag, uint64_t read_va,
1038 uint32_t write_stag, uint64_t write_va)
1039{
1040 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1041 struct iscsi_conn *conn = isert_conn->conn;
1042 struct iscsi_cmd *cmd;
1043 struct isert_cmd *isert_cmd;
1044 int ret = -EINVAL;
1045 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1046
1047 switch (opcode) {
1048 case ISCSI_OP_SCSI_CMD:
1049 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1050 if (!cmd)
1051 break;
1052
1053 isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1054 isert_cmd->read_stag = read_stag;
1055 isert_cmd->read_va = read_va;
1056 isert_cmd->write_stag = write_stag;
1057 isert_cmd->write_va = write_va;
1058
1059 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
1060 rx_desc, (unsigned char *)hdr);
1061 break;
1062 case ISCSI_OP_NOOP_OUT:
1063 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1064 if (!cmd)
1065 break;
1066
1067 ret = iscsit_handle_nop_out(conn, cmd, (unsigned char *)hdr);
1068 break;
1069 case ISCSI_OP_SCSI_DATA_OUT:
1070 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1071 (unsigned char *)hdr);
1072 break;
1073 case ISCSI_OP_SCSI_TMFUNC:
1074 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1075 if (!cmd)
1076 break;
1077
1078 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1079 (unsigned char *)hdr);
1080 break;
1081 case ISCSI_OP_LOGOUT:
1082 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1083 if (!cmd)
1084 break;
1085
1086 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1087 if (ret > 0)
1088 wait_for_completion_timeout(&conn->conn_logout_comp,
1089 SECONDS_FOR_LOGOUT_COMP *
1090 HZ);
1091 break;
1092 default:
1093 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1094 dump_stack();
1095 break;
1096 }
1097
1098 return ret;
1099}
1100
1101static void
1102isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1103{
1104 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1105 uint64_t read_va = 0, write_va = 0;
1106 uint32_t read_stag = 0, write_stag = 0;
1107 int rc;
1108
1109 switch (iser_hdr->flags & 0xF0) {
1110 case ISCSI_CTRL:
1111 if (iser_hdr->flags & ISER_RSV) {
1112 read_stag = be32_to_cpu(iser_hdr->read_stag);
1113 read_va = be64_to_cpu(iser_hdr->read_va);
1114 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1115 read_stag, (unsigned long long)read_va);
1116 }
1117 if (iser_hdr->flags & ISER_WSV) {
1118 write_stag = be32_to_cpu(iser_hdr->write_stag);
1119 write_va = be64_to_cpu(iser_hdr->write_va);
1120 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1121 write_stag, (unsigned long long)write_va);
1122 }
1123
1124 pr_debug("ISER ISCSI_CTRL PDU\n");
1125 break;
1126 case ISER_HELLO:
1127 pr_err("iSER Hello message\n");
1128 break;
1129 default:
1130 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1131 break;
1132 }
1133
1134 rc = isert_rx_opcode(isert_conn, rx_desc,
1135 read_stag, read_va, write_stag, write_va);
1136}
1137
1138static void
1139isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1140 unsigned long xfer_len)
1141{
1142 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1143 struct iscsi_hdr *hdr;
1144 u64 rx_dma;
1145 int rx_buflen, outstanding;
1146
1147 if ((char *)desc == isert_conn->login_req_buf) {
1148 rx_dma = isert_conn->login_req_dma;
1149 rx_buflen = ISER_RX_LOGIN_SIZE;
1150 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1151 rx_dma, rx_buflen);
1152 } else {
1153 rx_dma = desc->dma_addr;
1154 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1155 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1156 rx_dma, rx_buflen);
1157 }
1158
1159 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1160
1161 hdr = &desc->iscsi_header;
1162 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1163 hdr->opcode, hdr->itt, hdr->flags,
1164 (int)(xfer_len - ISER_HEADERS_LEN));
1165
1166 if ((char *)desc == isert_conn->login_req_buf)
1167 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1168 isert_conn);
1169 else
1170 isert_rx_do_work(desc, isert_conn);
1171
1172 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1173 DMA_FROM_DEVICE);
1174
1175 isert_conn->post_recv_buf_count--;
1176 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1177 isert_conn->post_recv_buf_count);
1178
1179 if ((char *)desc == isert_conn->login_req_buf)
1180 return;
1181
1182 outstanding = isert_conn->post_recv_buf_count;
1183 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1184 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1185 ISERT_MIN_POSTED_RX);
1186 err = isert_post_recv(isert_conn, count);
1187 if (err) {
1188 pr_err("isert_post_recv() count: %d failed, %d\n",
1189 count, err);
1190 }
1191 }
1192}
1193
1194static void
1195isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1196{
1197 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1198 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1199
1200 pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
1201
1202 if (wr->sge) {
1203 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1204 wr->sge = NULL;
1205 }
1206
1207 kfree(wr->send_wr);
1208 wr->send_wr = NULL;
1209
1210 kfree(isert_cmd->ib_sge);
1211 isert_cmd->ib_sge = NULL;
1212}
1213
1214static void
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001215isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001216{
1217 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1218 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001219 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001220
1221 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1222
1223 switch (cmd->iscsi_opcode) {
1224 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001225 spin_lock_bh(&conn->cmd_lock);
1226 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001227 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001228 spin_unlock_bh(&conn->cmd_lock);
1229
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001230 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001231 iscsit_stop_dataout_timer(cmd);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001232 /*
1233 * Check for special case during comp_err where
1234 * WRITE_PENDING has been handed off from core,
1235 * but requires an extra target_put_sess_cmd()
1236 * before transport_generic_free_cmd() below.
1237 */
1238 if (comp_err &&
1239 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1240 struct se_cmd *se_cmd = &cmd->se_cmd;
1241
1242 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1243 }
1244 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001245
1246 isert_unmap_cmd(isert_cmd, isert_conn);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001247 transport_generic_free_cmd(&cmd->se_cmd, 0);
1248 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001249 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001250 spin_lock_bh(&conn->cmd_lock);
1251 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001252 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001253 spin_unlock_bh(&conn->cmd_lock);
1254
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001255 transport_generic_free_cmd(&cmd->se_cmd, 0);
1256 break;
1257 case ISCSI_OP_REJECT:
1258 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001259 spin_lock_bh(&conn->cmd_lock);
1260 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001261 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001262 spin_unlock_bh(&conn->cmd_lock);
1263
1264 /*
1265 * Handle special case for REJECT when iscsi_add_reject*() has
1266 * overwritten the original iscsi_opcode assignment, and the
1267 * associated cmd->se_cmd needs to be released.
1268 */
1269 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001270 pr_debug("Calling transport_generic_free_cmd from"
1271 " isert_put_cmd for 0x%02x\n",
1272 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001273 transport_generic_free_cmd(&cmd->se_cmd, 0);
1274 break;
1275 }
1276 /*
1277 * Fall-through
1278 */
1279 default:
1280 isert_release_cmd(cmd);
1281 break;
1282 }
1283}
1284
1285static void
1286isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1287{
1288 if (tx_desc->dma_addr != 0) {
1289 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1290 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1291 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1292 tx_desc->dma_addr = 0;
1293 }
1294}
1295
1296static void
1297isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001298 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001299{
1300 if (isert_cmd->sense_buf_dma != 0) {
1301 pr_debug("Calling ib_dma_unmap_single for isert_cmd->sense_buf_dma\n");
1302 ib_dma_unmap_single(ib_dev, isert_cmd->sense_buf_dma,
1303 isert_cmd->sense_buf_len, DMA_TO_DEVICE);
1304 isert_cmd->sense_buf_dma = 0;
1305 }
1306
1307 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001308 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001309}
1310
1311static void
1312isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1313 struct isert_cmd *isert_cmd)
1314{
1315 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1316 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1317 struct se_cmd *se_cmd = &cmd->se_cmd;
1318 struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
1319
1320 iscsit_stop_dataout_timer(cmd);
1321
1322 if (wr->sge) {
1323 pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
1324 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1325 wr->sge = NULL;
1326 }
1327
1328 if (isert_cmd->ib_sge) {
1329 pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
1330 kfree(isert_cmd->ib_sge);
1331 isert_cmd->ib_sge = NULL;
1332 }
1333
1334 cmd->write_data_done = se_cmd->data_length;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001335 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001336
1337 pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
1338 spin_lock_bh(&cmd->istate_lock);
1339 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1340 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1341 spin_unlock_bh(&cmd->istate_lock);
1342
1343 target_execute_cmd(se_cmd);
1344}
1345
1346static void
1347isert_do_control_comp(struct work_struct *work)
1348{
1349 struct isert_cmd *isert_cmd = container_of(work,
1350 struct isert_cmd, comp_work);
1351 struct isert_conn *isert_conn = isert_cmd->conn;
1352 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1353 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1354
1355 switch (cmd->i_state) {
1356 case ISTATE_SEND_TASKMGTRSP:
1357 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1358
1359 atomic_dec(&isert_conn->post_send_buf_count);
1360 iscsit_tmr_post_handler(cmd, cmd->conn);
1361
1362 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001363 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001364 break;
1365 case ISTATE_SEND_REJECT:
1366 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1367 atomic_dec(&isert_conn->post_send_buf_count);
1368
1369 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001370 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001371 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001372 case ISTATE_SEND_LOGOUTRSP:
1373 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1374 /*
1375 * Call atomic_dec(&isert_conn->post_send_buf_count)
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001376 * from isert_wait_conn()
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001377 */
1378 isert_conn->logout_posted = true;
1379 iscsit_logout_post_handler(cmd, cmd->conn);
1380 break;
1381 default:
1382 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1383 dump_stack();
1384 break;
1385 }
1386}
1387
1388static void
1389isert_response_completion(struct iser_tx_desc *tx_desc,
1390 struct isert_cmd *isert_cmd,
1391 struct isert_conn *isert_conn,
1392 struct ib_device *ib_dev)
1393{
1394 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001395 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001396
1397 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001398 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1399 cmd->i_state == ISTATE_SEND_REJECT) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400 isert_unmap_tx_desc(tx_desc, ib_dev);
1401
1402 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1403 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1404 return;
1405 }
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001406 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001407
1408 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001409 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001410}
1411
1412static void
1413isert_send_completion(struct iser_tx_desc *tx_desc,
1414 struct isert_conn *isert_conn)
1415{
1416 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1417 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1418 struct isert_rdma_wr *wr;
1419
1420 if (!isert_cmd) {
1421 atomic_dec(&isert_conn->post_send_buf_count);
1422 isert_unmap_tx_desc(tx_desc, ib_dev);
1423 return;
1424 }
1425 wr = &isert_cmd->rdma_wr;
1426
1427 switch (wr->iser_ib_op) {
1428 case ISER_IB_RECV:
1429 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1430 dump_stack();
1431 break;
1432 case ISER_IB_SEND:
1433 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1434 isert_response_completion(tx_desc, isert_cmd,
1435 isert_conn, ib_dev);
1436 break;
1437 case ISER_IB_RDMA_WRITE:
1438 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1439 dump_stack();
1440 break;
1441 case ISER_IB_RDMA_READ:
1442 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1443
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001444 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001445 isert_completion_rdma_read(tx_desc, isert_cmd);
1446 break;
1447 default:
1448 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1449 dump_stack();
1450 break;
1451 }
1452}
1453
1454static void
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001455isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001456{
1457 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001458 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001459
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001460 if (!isert_cmd)
1461 isert_unmap_tx_desc(tx_desc, ib_dev);
1462 else
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001463 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001464}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001465
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001466static void
1467isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1468{
1469 struct iscsi_conn *conn = isert_conn->conn;
1470
1471 if (isert_conn->post_recv_buf_count)
1472 return;
1473
1474 if (conn->sess) {
1475 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1476 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001477 }
1478
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001479 while (atomic_read(&isert_conn->post_send_buf_count))
1480 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001481
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001482 mutex_lock(&isert_conn->conn_mutex);
1483 isert_conn->state = ISER_CONN_DOWN;
1484 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07001485
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001486 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001487}
1488
1489static void
1490isert_cq_tx_work(struct work_struct *work)
1491{
1492 struct isert_cq_desc *cq_desc = container_of(work,
1493 struct isert_cq_desc, cq_tx_work);
1494 struct isert_device *device = cq_desc->device;
1495 int cq_index = cq_desc->cq_index;
1496 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1497 struct isert_conn *isert_conn;
1498 struct iser_tx_desc *tx_desc;
1499 struct ib_wc wc;
1500
1501 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1502 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1503 isert_conn = wc.qp->qp_context;
1504
1505 if (wc.status == IB_WC_SUCCESS) {
1506 isert_send_completion(tx_desc, isert_conn);
1507 } else {
1508 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1509 pr_debug("TX wc.status: 0x%08x\n", wc.status);
1510 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001511 isert_cq_tx_comp_err(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001512 }
1513 }
1514
1515 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1516}
1517
1518static void
1519isert_cq_tx_callback(struct ib_cq *cq, void *context)
1520{
1521 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1522
1523 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1524 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1525}
1526
1527static void
1528isert_cq_rx_work(struct work_struct *work)
1529{
1530 struct isert_cq_desc *cq_desc = container_of(work,
1531 struct isert_cq_desc, cq_rx_work);
1532 struct isert_device *device = cq_desc->device;
1533 int cq_index = cq_desc->cq_index;
1534 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1535 struct isert_conn *isert_conn;
1536 struct iser_rx_desc *rx_desc;
1537 struct ib_wc wc;
1538 unsigned long xfer_len;
1539
1540 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1541 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1542 isert_conn = wc.qp->qp_context;
1543
1544 if (wc.status == IB_WC_SUCCESS) {
1545 xfer_len = (unsigned long)wc.byte_len;
1546 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1547 } else {
1548 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1549 if (wc.status != IB_WC_WR_FLUSH_ERR)
1550 pr_debug("RX wc.status: 0x%08x\n", wc.status);
1551
1552 isert_conn->post_recv_buf_count--;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001553 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001554 }
1555 }
1556
1557 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1558}
1559
1560static void
1561isert_cq_rx_callback(struct ib_cq *cq, void *context)
1562{
1563 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1564
1565 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1566 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1567}
1568
1569static int
1570isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1571{
1572 struct ib_send_wr *wr_failed;
1573 int ret;
1574
1575 atomic_inc(&isert_conn->post_send_buf_count);
1576
1577 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1578 &wr_failed);
1579 if (ret) {
1580 pr_err("ib_post_send failed with %d\n", ret);
1581 atomic_dec(&isert_conn->post_send_buf_count);
1582 return ret;
1583 }
1584 return ret;
1585}
1586
1587static int
1588isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1589{
1590 struct isert_cmd *isert_cmd = container_of(cmd,
1591 struct isert_cmd, iscsi_cmd);
1592 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1593 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1594 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1595 &isert_cmd->tx_desc.iscsi_header;
1596
1597 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1598 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1599 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1600 /*
1601 * Attach SENSE DATA payload to iSCSI Response PDU
1602 */
1603 if (cmd->se_cmd.sense_buffer &&
1604 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1605 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1606 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1607 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1608 u32 padding, sense_len;
1609
1610 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1611 cmd->sense_buffer);
1612 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1613
1614 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1615 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1616 sense_len = cmd->se_cmd.scsi_sense_length + padding;
1617
1618 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1619 (void *)cmd->sense_buffer, sense_len,
1620 DMA_TO_DEVICE);
1621
1622 isert_cmd->sense_buf_len = sense_len;
1623 tx_dsg->addr = isert_cmd->sense_buf_dma;
1624 tx_dsg->length = sense_len;
1625 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1626 isert_cmd->tx_desc.num_sge = 2;
1627 }
1628
1629 isert_init_send_wr(isert_cmd, send_wr);
1630
1631 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1632
1633 return isert_post_response(isert_conn, isert_cmd);
1634}
1635
1636static int
1637isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1638 bool nopout_response)
1639{
1640 struct isert_cmd *isert_cmd = container_of(cmd,
1641 struct isert_cmd, iscsi_cmd);
1642 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1643 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1644
1645 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1646 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1647 &isert_cmd->tx_desc.iscsi_header,
1648 nopout_response);
1649 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1650 isert_init_send_wr(isert_cmd, send_wr);
1651
1652 pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1653
1654 return isert_post_response(isert_conn, isert_cmd);
1655}
1656
1657static int
1658isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1659{
1660 struct isert_cmd *isert_cmd = container_of(cmd,
1661 struct isert_cmd, iscsi_cmd);
1662 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1663 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1664
1665 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1666 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1667 &isert_cmd->tx_desc.iscsi_header);
1668 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1669 isert_init_send_wr(isert_cmd, send_wr);
1670
1671 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1672
1673 return isert_post_response(isert_conn, isert_cmd);
1674}
1675
1676static int
1677isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1678{
1679 struct isert_cmd *isert_cmd = container_of(cmd,
1680 struct isert_cmd, iscsi_cmd);
1681 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1682 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1683
1684 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1685 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1686 &isert_cmd->tx_desc.iscsi_header);
1687 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1688 isert_init_send_wr(isert_cmd, send_wr);
1689
1690 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1691
1692 return isert_post_response(isert_conn, isert_cmd);
1693}
1694
1695static int
1696isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1697{
1698 struct isert_cmd *isert_cmd = container_of(cmd,
1699 struct isert_cmd, iscsi_cmd);
1700 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1701 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001702 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1703 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1704 struct iscsi_reject *hdr =
1705 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001706
1707 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001708 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001709 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001710
1711 hton24(hdr->dlength, ISCSI_HDR_LEN);
1712 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1713 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1714 DMA_TO_DEVICE);
1715 isert_cmd->sense_buf_len = ISCSI_HDR_LEN;
1716 tx_dsg->addr = isert_cmd->sense_buf_dma;
1717 tx_dsg->length = ISCSI_HDR_LEN;
1718 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1719 isert_cmd->tx_desc.num_sge = 2;
1720
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001721 isert_init_send_wr(isert_cmd, send_wr);
1722
1723 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1724
1725 return isert_post_response(isert_conn, isert_cmd);
1726}
1727
1728static int
1729isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1730 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1731 u32 data_left, u32 offset)
1732{
1733 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1734 struct scatterlist *sg_start, *tmp_sg;
1735 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1736 u32 sg_off, page_off;
1737 int i = 0, sg_nents;
1738
1739 sg_off = offset / PAGE_SIZE;
1740 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1741 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1742 page_off = offset % PAGE_SIZE;
1743
1744 send_wr->sg_list = ib_sge;
1745 send_wr->num_sge = sg_nents;
1746 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1747 /*
1748 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1749 */
1750 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1751 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1752 (unsigned long long)tmp_sg->dma_address,
1753 tmp_sg->length, page_off);
1754
1755 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1756 ib_sge->length = min_t(u32, data_left,
1757 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1758 ib_sge->lkey = isert_conn->conn_mr->lkey;
1759
1760 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
1761 ib_sge->addr, ib_sge->length);
1762 page_off = 0;
1763 data_left -= ib_sge->length;
1764 ib_sge++;
1765 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1766 }
1767
1768 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1769 send_wr->sg_list, send_wr->num_sge);
1770
1771 return sg_nents;
1772}
1773
1774static int
1775isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1776{
1777 struct se_cmd *se_cmd = &cmd->se_cmd;
1778 struct isert_cmd *isert_cmd = container_of(cmd,
1779 struct isert_cmd, iscsi_cmd);
1780 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1781 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1782 struct ib_send_wr *wr_failed, *send_wr;
1783 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1784 struct ib_sge *ib_sge;
1785 struct scatterlist *sg;
1786 u32 offset = 0, data_len, data_left, rdma_write_max;
1787 int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
1788
1789 pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
1790
1791 sg = &se_cmd->t_data_sg[0];
1792 sg_nents = se_cmd->t_data_nents;
1793
1794 count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1795 if (unlikely(!count)) {
1796 pr_err("Unable to map put_datain SGs\n");
1797 return -EINVAL;
1798 }
1799 wr->sge = sg;
1800 wr->num_sge = sg_nents;
1801 pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
1802 count, sg, sg_nents);
1803
1804 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1805 if (!ib_sge) {
1806 pr_warn("Unable to allocate datain ib_sge\n");
1807 ret = -ENOMEM;
1808 goto unmap_sg;
1809 }
1810 isert_cmd->ib_sge = ib_sge;
1811
1812 pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
1813 ib_sge, se_cmd->t_data_nents);
1814
1815 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1816 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1817 GFP_KERNEL);
1818 if (!wr->send_wr) {
1819 pr_err("Unable to allocate wr->send_wr\n");
1820 ret = -ENOMEM;
1821 goto unmap_sg;
1822 }
1823 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1824 wr->send_wr, wr->send_wr_num);
1825
1826 iscsit_increment_maxcmdsn(cmd, conn->sess);
1827 cmd->stat_sn = conn->stat_sn++;
1828
1829 wr->isert_cmd = isert_cmd;
1830 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1831 data_left = se_cmd->data_length;
1832
1833 for (i = 0; i < wr->send_wr_num; i++) {
1834 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1835 data_len = min(data_left, rdma_write_max);
1836
1837 send_wr->opcode = IB_WR_RDMA_WRITE;
1838 send_wr->send_flags = 0;
1839 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1840 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1841
1842 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1843 send_wr, data_len, offset);
1844 ib_sge += ib_sge_cnt;
1845
1846 if (i + 1 == wr->send_wr_num)
1847 send_wr->next = &isert_cmd->tx_desc.send_wr;
1848 else
1849 send_wr->next = &wr->send_wr[i + 1];
1850
1851 offset += data_len;
1852 data_left -= data_len;
1853 }
1854 /*
1855 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1856 */
1857 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1858 iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1859 &isert_cmd->tx_desc.iscsi_header);
1860 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1861 isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1862
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001863 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001864
1865 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1866 if (rc) {
1867 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001868 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001869 }
1870 pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
1871 return 1;
1872
1873unmap_sg:
1874 ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1875 return ret;
1876}
1877
1878static int
1879isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1880{
1881 struct se_cmd *se_cmd = &cmd->se_cmd;
1882 struct isert_cmd *isert_cmd = container_of(cmd,
1883 struct isert_cmd, iscsi_cmd);
1884 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1885 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1886 struct ib_send_wr *wr_failed, *send_wr;
1887 struct ib_sge *ib_sge;
1888 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1889 struct scatterlist *sg_start;
1890 u32 sg_off, sg_nents, page_off, va_offset = 0;
1891 u32 offset = 0, data_len, data_left, rdma_write_max;
1892 int rc, ret = 0, count, i, ib_sge_cnt;
1893
1894 pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
1895 se_cmd->data_length, cmd->write_data_done);
1896
1897 sg_off = cmd->write_data_done / PAGE_SIZE;
1898 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1899 page_off = cmd->write_data_done % PAGE_SIZE;
1900
1901 pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
1902 sg_off, sg_start, page_off);
1903
1904 data_left = se_cmd->data_length - cmd->write_data_done;
1905 sg_nents = se_cmd->t_data_nents - sg_off;
1906
1907 pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
1908 data_left, sg_nents);
1909
1910 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1911 if (unlikely(!count)) {
1912 pr_err("Unable to map get_dataout SGs\n");
1913 return -EINVAL;
1914 }
1915 wr->sge = sg_start;
1916 wr->num_sge = sg_nents;
1917 pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
1918 count, sg_start, sg_nents);
1919
1920 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1921 if (!ib_sge) {
1922 pr_warn("Unable to allocate dataout ib_sge\n");
1923 ret = -ENOMEM;
1924 goto unmap_sg;
1925 }
1926 isert_cmd->ib_sge = ib_sge;
1927
1928 pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
1929 ib_sge, sg_nents);
1930
1931 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1932 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1933 GFP_KERNEL);
1934 if (!wr->send_wr) {
1935 pr_debug("Unable to allocate wr->send_wr\n");
1936 ret = -ENOMEM;
1937 goto unmap_sg;
1938 }
1939 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1940 wr->send_wr, wr->send_wr_num);
1941
1942 isert_cmd->tx_desc.isert_cmd = isert_cmd;
1943
1944 wr->iser_ib_op = ISER_IB_RDMA_READ;
1945 wr->isert_cmd = isert_cmd;
1946 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1947 offset = cmd->write_data_done;
1948
1949 for (i = 0; i < wr->send_wr_num; i++) {
1950 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1951 data_len = min(data_left, rdma_write_max);
1952
1953 send_wr->opcode = IB_WR_RDMA_READ;
1954 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
1955 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
1956
1957 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1958 send_wr, data_len, offset);
1959 ib_sge += ib_sge_cnt;
1960
1961 if (i + 1 == wr->send_wr_num)
1962 send_wr->send_flags = IB_SEND_SIGNALED;
1963 else
1964 send_wr->next = &wr->send_wr[i + 1];
1965
1966 offset += data_len;
1967 va_offset += data_len;
1968 data_left -= data_len;
1969 }
1970
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001971 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001972
1973 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1974 if (rc) {
1975 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001976 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001977 }
1978 pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
1979 return 0;
1980
1981unmap_sg:
1982 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1983 return ret;
1984}
1985
1986static int
1987isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1988{
1989 int ret;
1990
1991 switch (state) {
1992 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
1993 ret = isert_put_nopin(cmd, conn, false);
1994 break;
1995 default:
1996 pr_err("Unknown immediate state: 0x%02x\n", state);
1997 ret = -EINVAL;
1998 break;
1999 }
2000
2001 return ret;
2002}
2003
2004static int
2005isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2006{
2007 int ret;
2008
2009 switch (state) {
2010 case ISTATE_SEND_LOGOUTRSP:
2011 ret = isert_put_logout_rsp(cmd, conn);
2012 if (!ret) {
2013 pr_debug("Returning iSER Logout -EAGAIN\n");
2014 ret = -EAGAIN;
2015 }
2016 break;
2017 case ISTATE_SEND_NOPIN:
2018 ret = isert_put_nopin(cmd, conn, true);
2019 break;
2020 case ISTATE_SEND_TASKMGTRSP:
2021 ret = isert_put_tm_rsp(cmd, conn);
2022 break;
2023 case ISTATE_SEND_REJECT:
2024 ret = isert_put_reject(cmd, conn);
2025 break;
2026 case ISTATE_SEND_STATUS:
2027 /*
2028 * Special case for sending non GOOD SCSI status from TX thread
2029 * context during pre se_cmd excecution failure.
2030 */
2031 ret = isert_put_response(conn, cmd);
2032 break;
2033 default:
2034 pr_err("Unknown response state: 0x%02x\n", state);
2035 ret = -EINVAL;
2036 break;
2037 }
2038
2039 return ret;
2040}
2041
2042static int
2043isert_setup_np(struct iscsi_np *np,
2044 struct __kernel_sockaddr_storage *ksockaddr)
2045{
2046 struct isert_np *isert_np;
2047 struct rdma_cm_id *isert_lid;
2048 struct sockaddr *sa;
2049 int ret;
2050
2051 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2052 if (!isert_np) {
2053 pr_err("Unable to allocate struct isert_np\n");
2054 return -ENOMEM;
2055 }
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002056 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002057 mutex_init(&isert_np->np_accept_mutex);
2058 INIT_LIST_HEAD(&isert_np->np_accept_list);
2059 init_completion(&isert_np->np_login_comp);
2060
2061 sa = (struct sockaddr *)ksockaddr;
2062 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2063 /*
2064 * Setup the np->np_sockaddr from the passed sockaddr setup
2065 * in iscsi_target_configfs.c code..
2066 */
2067 memcpy(&np->np_sockaddr, ksockaddr,
2068 sizeof(struct __kernel_sockaddr_storage));
2069
2070 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2071 IB_QPT_RC);
2072 if (IS_ERR(isert_lid)) {
2073 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2074 PTR_ERR(isert_lid));
2075 ret = PTR_ERR(isert_lid);
2076 goto out;
2077 }
2078
2079 ret = rdma_bind_addr(isert_lid, sa);
2080 if (ret) {
2081 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2082 goto out_lid;
2083 }
2084
2085 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2086 if (ret) {
2087 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2088 goto out_lid;
2089 }
2090
2091 isert_np->np_cm_id = isert_lid;
2092 np->np_context = isert_np;
2093 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2094
2095 return 0;
2096
2097out_lid:
2098 rdma_destroy_id(isert_lid);
2099out:
2100 kfree(isert_np);
2101 return ret;
2102}
2103
2104static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002105isert_rdma_accept(struct isert_conn *isert_conn)
2106{
2107 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2108 struct rdma_conn_param cp;
2109 int ret;
2110
2111 memset(&cp, 0, sizeof(struct rdma_conn_param));
2112 cp.responder_resources = isert_conn->responder_resources;
2113 cp.initiator_depth = isert_conn->initiator_depth;
2114 cp.retry_count = 7;
2115 cp.rnr_retry_count = 7;
2116
2117 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2118
2119 ret = rdma_accept(cm_id, &cp);
2120 if (ret) {
2121 pr_err("rdma_accept() failed with: %d\n", ret);
2122 return ret;
2123 }
2124
2125 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2126
2127 return 0;
2128}
2129
2130static int
2131isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2132{
2133 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2134 int ret;
2135
2136 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2137
2138 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2139 if (ret)
2140 return ret;
2141
2142 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2143 return 0;
2144}
2145
2146static void
2147isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2148 struct isert_conn *isert_conn)
2149{
2150 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2151 struct rdma_route *cm_route = &cm_id->route;
2152 struct sockaddr_in *sock_in;
2153 struct sockaddr_in6 *sock_in6;
2154
2155 conn->login_family = np->np_sockaddr.ss_family;
2156
2157 if (np->np_sockaddr.ss_family == AF_INET6) {
2158 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2159 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2160 &sock_in6->sin6_addr.in6_u);
2161 conn->login_port = ntohs(sock_in6->sin6_port);
2162
2163 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2164 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2165 &sock_in6->sin6_addr.in6_u);
2166 conn->local_port = ntohs(sock_in6->sin6_port);
2167 } else {
2168 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2169 sprintf(conn->login_ip, "%pI4",
2170 &sock_in->sin_addr.s_addr);
2171 conn->login_port = ntohs(sock_in->sin_port);
2172
2173 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2174 sprintf(conn->local_ip, "%pI4",
2175 &sock_in->sin_addr.s_addr);
2176 conn->local_port = ntohs(sock_in->sin_port);
2177 }
2178}
2179
2180static int
2181isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2182{
2183 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2184 struct isert_conn *isert_conn;
2185 int max_accept = 0, ret;
2186
2187accept_wait:
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002188 ret = down_interruptible(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002189 if (max_accept > 5)
2190 return -ENODEV;
2191
2192 spin_lock_bh(&np->np_thread_lock);
2193 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2194 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002195 pr_debug("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002196 return -ENODEV;
2197 }
2198 spin_unlock_bh(&np->np_thread_lock);
2199
2200 mutex_lock(&isert_np->np_accept_mutex);
2201 if (list_empty(&isert_np->np_accept_list)) {
2202 mutex_unlock(&isert_np->np_accept_mutex);
2203 max_accept++;
2204 goto accept_wait;
2205 }
2206 isert_conn = list_first_entry(&isert_np->np_accept_list,
2207 struct isert_conn, conn_accept_node);
2208 list_del_init(&isert_conn->conn_accept_node);
2209 mutex_unlock(&isert_np->np_accept_mutex);
2210
2211 conn->context = isert_conn;
2212 isert_conn->conn = conn;
2213 max_accept = 0;
2214
2215 ret = isert_rdma_post_recvl(isert_conn);
2216 if (ret)
2217 return ret;
2218
2219 ret = isert_rdma_accept(isert_conn);
2220 if (ret)
2221 return ret;
2222
2223 isert_set_conn_info(np, conn, isert_conn);
2224
2225 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2226 return 0;
2227}
2228
2229static void
2230isert_free_np(struct iscsi_np *np)
2231{
2232 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2233
2234 rdma_destroy_id(isert_np->np_cm_id);
2235
2236 np->np_context = NULL;
2237 kfree(isert_np);
2238}
2239
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002240static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002241{
2242 struct isert_conn *isert_conn = conn->context;
2243
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002244 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002245 /*
2246 * Decrement post_send_buf_count for special case when called
2247 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2248 */
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002249 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002250 if (isert_conn->logout_posted)
2251 atomic_dec(&isert_conn->post_send_buf_count);
2252
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002253 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002254 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002255 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002256 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002257 /*
2258 * Only wait for conn_wait_comp_err if the isert_conn made it
2259 * into full feature phase..
2260 */
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002261 if (isert_conn->state == ISER_CONN_INIT) {
2262 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002263 return;
2264 }
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002265 if (isert_conn->state == ISER_CONN_UP)
2266 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002267 mutex_unlock(&isert_conn->conn_mutex);
2268
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002269 wait_for_completion(&isert_conn->conn_wait_comp_err);
2270
2271 wait_for_completion(&isert_conn->conn_wait);
2272}
2273
2274static void isert_free_conn(struct iscsi_conn *conn)
2275{
2276 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002277
2278 isert_put_conn(isert_conn);
2279}
2280
2281static struct iscsit_transport iser_target_transport = {
2282 .name = "IB/iSER",
2283 .transport_type = ISCSI_INFINIBAND,
2284 .owner = THIS_MODULE,
2285 .iscsit_setup_np = isert_setup_np,
2286 .iscsit_accept_np = isert_accept_np,
2287 .iscsit_free_np = isert_free_np,
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002288 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002289 .iscsit_free_conn = isert_free_conn,
2290 .iscsit_alloc_cmd = isert_alloc_cmd,
2291 .iscsit_get_login_rx = isert_get_login_rx,
2292 .iscsit_put_login_tx = isert_put_login_tx,
2293 .iscsit_immediate_queue = isert_immediate_queue,
2294 .iscsit_response_queue = isert_response_queue,
2295 .iscsit_get_dataout = isert_get_dataout,
2296 .iscsit_queue_data_in = isert_put_datain,
2297 .iscsit_queue_status = isert_put_response,
2298};
2299
2300static int __init isert_init(void)
2301{
2302 int ret;
2303
2304 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2305 if (!isert_rx_wq) {
2306 pr_err("Unable to allocate isert_rx_wq\n");
2307 return -ENOMEM;
2308 }
2309
2310 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2311 if (!isert_comp_wq) {
2312 pr_err("Unable to allocate isert_comp_wq\n");
2313 ret = -ENOMEM;
2314 goto destroy_rx_wq;
2315 }
2316
2317 isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
2318 sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
2319 0, NULL);
2320 if (!isert_cmd_cache) {
2321 pr_err("Unable to create isert_cmd_cache\n");
2322 ret = -ENOMEM;
2323 goto destroy_tx_cq;
2324 }
2325
2326 iscsit_register_transport(&iser_target_transport);
2327 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2328 return 0;
2329
2330destroy_tx_cq:
2331 destroy_workqueue(isert_comp_wq);
2332destroy_rx_wq:
2333 destroy_workqueue(isert_rx_wq);
2334 return ret;
2335}
2336
2337static void __exit isert_exit(void)
2338{
2339 kmem_cache_destroy(isert_cmd_cache);
2340 destroy_workqueue(isert_comp_wq);
2341 destroy_workqueue(isert_rx_wq);
2342 iscsit_unregister_transport(&iser_target_transport);
2343 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2344}
2345
2346MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2347MODULE_VERSION("0.1");
2348MODULE_AUTHOR("nab@Linux-iSCSI.org");
2349MODULE_LICENSE("GPL");
2350
2351module_init(isert_init);
2352module_exit(isert_exit);