blob: f8007df8f0795f4d0b08363cbe8eb6aad0581a47 [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 }
Sagi Grimberg1813e802014-05-19 17:44:23 +0300575
576 /* Send DREQ/DREP towards our initiator */
577 rdma_disconnect(isert_conn->conn_cm_id);
578
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700579 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800580
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700581wake_up:
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800582 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800583 isert_put_conn(isert_conn);
584}
585
586static void
587isert_disconnected_handler(struct rdma_cm_id *cma_id)
588{
589 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
590
591 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
592 schedule_work(&isert_conn->conn_logout_work);
593}
594
595static int
596isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
597{
598 int ret = 0;
599
600 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
601 event->event, event->status, cma_id->context, cma_id);
602
603 switch (event->event) {
604 case RDMA_CM_EVENT_CONNECT_REQUEST:
605 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
606 ret = isert_connect_request(cma_id, event);
607 break;
608 case RDMA_CM_EVENT_ESTABLISHED:
609 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
610 isert_connected_handler(cma_id);
611 break;
612 case RDMA_CM_EVENT_DISCONNECTED:
613 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
614 isert_disconnected_handler(cma_id);
615 break;
616 case RDMA_CM_EVENT_DEVICE_REMOVAL:
617 case RDMA_CM_EVENT_ADDR_CHANGE:
618 break;
619 case RDMA_CM_EVENT_CONNECT_ERROR:
620 default:
621 pr_err("Unknown RDMA CMA event: %d\n", event->event);
622 break;
623 }
624
625 if (ret != 0) {
626 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
627 event->event, ret);
628 dump_stack();
629 }
630
631 return ret;
632}
633
634static int
635isert_post_recv(struct isert_conn *isert_conn, u32 count)
636{
637 struct ib_recv_wr *rx_wr, *rx_wr_failed;
638 int i, ret;
639 unsigned int rx_head = isert_conn->conn_rx_desc_head;
640 struct iser_rx_desc *rx_desc;
641
642 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
643 rx_desc = &isert_conn->conn_rx_descs[rx_head];
644 rx_wr->wr_id = (unsigned long)rx_desc;
645 rx_wr->sg_list = &rx_desc->rx_sg;
646 rx_wr->num_sge = 1;
647 rx_wr->next = rx_wr + 1;
648 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
649 }
650
651 rx_wr--;
652 rx_wr->next = NULL; /* mark end of work requests list */
653
654 isert_conn->post_recv_buf_count += count;
655 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
656 &rx_wr_failed);
657 if (ret) {
658 pr_err("ib_post_recv() failed with ret: %d\n", ret);
659 isert_conn->post_recv_buf_count -= count;
660 } else {
661 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
662 isert_conn->conn_rx_desc_head = rx_head;
663 }
664 return ret;
665}
666
667static int
668isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
669{
670 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
671 struct ib_send_wr send_wr, *send_wr_failed;
672 int ret;
673
674 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
675 ISER_HEADERS_LEN, DMA_TO_DEVICE);
676
677 send_wr.next = NULL;
678 send_wr.wr_id = (unsigned long)tx_desc;
679 send_wr.sg_list = tx_desc->tx_sg;
680 send_wr.num_sge = tx_desc->num_sge;
681 send_wr.opcode = IB_WR_SEND;
682 send_wr.send_flags = IB_SEND_SIGNALED;
683
684 atomic_inc(&isert_conn->post_send_buf_count);
685
686 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
687 if (ret) {
688 pr_err("ib_post_send() failed, ret: %d\n", ret);
689 atomic_dec(&isert_conn->post_send_buf_count);
690 }
691
692 return ret;
693}
694
695static void
696isert_create_send_desc(struct isert_conn *isert_conn,
697 struct isert_cmd *isert_cmd,
698 struct iser_tx_desc *tx_desc)
699{
700 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
701
702 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
703 ISER_HEADERS_LEN, DMA_TO_DEVICE);
704
705 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
706 tx_desc->iser_header.flags = ISER_VER;
707
708 tx_desc->num_sge = 1;
709 tx_desc->isert_cmd = isert_cmd;
710
711 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
712 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
713 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
714 }
715}
716
717static int
718isert_init_tx_hdrs(struct isert_conn *isert_conn,
719 struct iser_tx_desc *tx_desc)
720{
721 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
722 u64 dma_addr;
723
724 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
725 ISER_HEADERS_LEN, DMA_TO_DEVICE);
726 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
727 pr_err("ib_dma_mapping_error() failed\n");
728 return -ENOMEM;
729 }
730
731 tx_desc->dma_addr = dma_addr;
732 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
733 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
734 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
735
736 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
737 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
738 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
739
740 return 0;
741}
742
743static void
744isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
745{
746 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
747 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
748 send_wr->opcode = IB_WR_SEND;
749 send_wr->send_flags = IB_SEND_SIGNALED;
750 send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
751 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
752}
753
754static int
755isert_rdma_post_recvl(struct isert_conn *isert_conn)
756{
757 struct ib_recv_wr rx_wr, *rx_wr_fail;
758 struct ib_sge sge;
759 int ret;
760
761 memset(&sge, 0, sizeof(struct ib_sge));
762 sge.addr = isert_conn->login_req_dma;
763 sge.length = ISER_RX_LOGIN_SIZE;
764 sge.lkey = isert_conn->conn_mr->lkey;
765
766 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
767 sge.addr, sge.length, sge.lkey);
768
769 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
770 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
771 rx_wr.sg_list = &sge;
772 rx_wr.num_sge = 1;
773
774 isert_conn->post_recv_buf_count++;
775 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
776 if (ret) {
777 pr_err("ib_post_recv() failed: %d\n", ret);
778 isert_conn->post_recv_buf_count--;
779 }
780
781 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
782 return ret;
783}
784
785static int
786isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
787 u32 length)
788{
789 struct isert_conn *isert_conn = conn->context;
790 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
791 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
792 int ret;
793
794 isert_create_send_desc(isert_conn, NULL, tx_desc);
795
796 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
797 sizeof(struct iscsi_hdr));
798
799 isert_init_tx_hdrs(isert_conn, tx_desc);
800
801 if (length > 0) {
802 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
803
804 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
805 length, DMA_TO_DEVICE);
806
807 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
808
809 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
810 length, DMA_TO_DEVICE);
811
812 tx_dsg->addr = isert_conn->login_rsp_dma;
813 tx_dsg->length = length;
814 tx_dsg->lkey = isert_conn->conn_mr->lkey;
815 tx_desc->num_sge = 2;
816 }
817 if (!login->login_failed) {
818 if (login->login_complete) {
819 ret = isert_alloc_rx_descriptors(isert_conn);
820 if (ret)
821 return ret;
822
823 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
824 if (ret)
825 return ret;
826
827 isert_conn->state = ISER_CONN_UP;
828 goto post_send;
829 }
830
831 ret = isert_rdma_post_recvl(isert_conn);
832 if (ret)
833 return ret;
834 }
835post_send:
836 ret = isert_post_send(isert_conn, tx_desc);
837 if (ret)
838 return ret;
839
840 return 0;
841}
842
843static void
844isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
845 struct isert_conn *isert_conn)
846{
847 struct iscsi_conn *conn = isert_conn->conn;
848 struct iscsi_login *login = conn->conn_login;
849 int size;
850
851 if (!login) {
852 pr_err("conn->conn_login is NULL\n");
853 dump_stack();
854 return;
855 }
856
857 if (login->first_request) {
858 struct iscsi_login_req *login_req =
859 (struct iscsi_login_req *)&rx_desc->iscsi_header;
860 /*
861 * Setup the initial iscsi_login values from the leading
862 * login request PDU.
863 */
864 login->leading_connection = (!login_req->tsih) ? 1 : 0;
865 login->current_stage =
866 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
867 >> 2;
868 login->version_min = login_req->min_version;
869 login->version_max = login_req->max_version;
870 memcpy(login->isid, login_req->isid, 6);
871 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
872 login->init_task_tag = login_req->itt;
873 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
874 login->cid = be16_to_cpu(login_req->cid);
875 login->tsih = be16_to_cpu(login_req->tsih);
876 }
877
878 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
879
880 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
881 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
882 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
883 memcpy(login->req_buf, &rx_desc->data[0], size);
884
885 complete(&isert_conn->conn_login_comp);
886}
887
888static void
889isert_release_cmd(struct iscsi_cmd *cmd)
890{
891 struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
892 iscsi_cmd);
893
894 pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
895
896 kfree(cmd->buf_ptr);
897 kfree(cmd->tmr_req);
898
899 kmem_cache_free(isert_cmd_cache, isert_cmd);
900}
901
902static struct iscsi_cmd
903*isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
904{
905 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
906 struct isert_cmd *isert_cmd;
907
908 isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
909 if (!isert_cmd) {
910 pr_err("Unable to allocate isert_cmd\n");
911 return NULL;
912 }
913 isert_cmd->conn = isert_conn;
914 isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
915
916 return &isert_cmd->iscsi_cmd;
917}
918
919static int
920isert_handle_scsi_cmd(struct isert_conn *isert_conn,
921 struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
922 unsigned char *buf)
923{
924 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
925 struct iscsi_conn *conn = isert_conn->conn;
926 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
927 struct scatterlist *sg;
928 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
929 bool dump_payload = false;
930
931 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
932 if (rc < 0)
933 return rc;
934
935 imm_data = cmd->immediate_data;
936 imm_data_len = cmd->first_burst_len;
937 unsol_data = cmd->unsolicited_data;
938
939 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
940 if (rc < 0) {
941 return 0;
942 } else if (rc > 0) {
943 dump_payload = true;
944 goto sequence_cmd;
945 }
946
947 if (!imm_data)
948 return 0;
949
950 sg = &cmd->se_cmd.t_data_sg[0];
951 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
952
953 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
954 sg, sg_nents, &rx_desc->data[0], imm_data_len);
955
956 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
957
958 cmd->write_data_done += imm_data_len;
959
960 if (cmd->write_data_done == cmd->se_cmd.data_length) {
961 spin_lock_bh(&cmd->istate_lock);
962 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
963 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
964 spin_unlock_bh(&cmd->istate_lock);
965 }
966
967sequence_cmd:
Nicholas Bellingeradb97c22013-07-30 04:04:02 +0000968 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969
970 if (!rc && dump_payload == false && unsol_data)
971 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellinger553e4c52014-05-23 00:48:35 -0700972 else if (dump_payload && imm_data)
973 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800974
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800975 return 0;
976}
977
978static int
979isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
980 struct iser_rx_desc *rx_desc, unsigned char *buf)
981{
982 struct scatterlist *sg_start;
983 struct iscsi_conn *conn = isert_conn->conn;
984 struct iscsi_cmd *cmd = NULL;
985 struct iscsi_data *hdr = (struct iscsi_data *)buf;
986 u32 unsol_data_len = ntoh24(hdr->dlength);
987 int rc, sg_nents, sg_off, page_off;
988
989 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
990 if (rc < 0)
991 return rc;
992 else if (!cmd)
993 return 0;
994 /*
995 * FIXME: Unexpected unsolicited_data out
996 */
997 if (!cmd->unsolicited_data) {
998 pr_err("Received unexpected solicited data payload\n");
999 dump_stack();
1000 return -1;
1001 }
1002
1003 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
1004 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
1005
1006 sg_off = cmd->write_data_done / PAGE_SIZE;
1007 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1008 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1009 page_off = cmd->write_data_done % PAGE_SIZE;
1010 /*
1011 * FIXME: Non page-aligned unsolicited_data out
1012 */
1013 if (page_off) {
1014 pr_err("Received unexpected non-page aligned data payload\n");
1015 dump_stack();
1016 return -1;
1017 }
1018 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1019 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1020
1021 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1022 unsol_data_len);
1023
1024 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1025 if (rc < 0)
1026 return rc;
1027
1028 return 0;
1029}
1030
1031static int
1032isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1033 uint32_t read_stag, uint64_t read_va,
1034 uint32_t write_stag, uint64_t write_va)
1035{
1036 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1037 struct iscsi_conn *conn = isert_conn->conn;
1038 struct iscsi_cmd *cmd;
1039 struct isert_cmd *isert_cmd;
1040 int ret = -EINVAL;
1041 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1042
1043 switch (opcode) {
1044 case ISCSI_OP_SCSI_CMD:
1045 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1046 if (!cmd)
1047 break;
1048
1049 isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1050 isert_cmd->read_stag = read_stag;
1051 isert_cmd->read_va = read_va;
1052 isert_cmd->write_stag = write_stag;
1053 isert_cmd->write_va = write_va;
1054
1055 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
1056 rx_desc, (unsigned char *)hdr);
1057 break;
1058 case ISCSI_OP_NOOP_OUT:
1059 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1060 if (!cmd)
1061 break;
1062
1063 ret = iscsit_handle_nop_out(conn, cmd, (unsigned char *)hdr);
1064 break;
1065 case ISCSI_OP_SCSI_DATA_OUT:
1066 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1067 (unsigned char *)hdr);
1068 break;
1069 case ISCSI_OP_SCSI_TMFUNC:
1070 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1071 if (!cmd)
1072 break;
1073
1074 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1075 (unsigned char *)hdr);
1076 break;
1077 case ISCSI_OP_LOGOUT:
1078 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1079 if (!cmd)
1080 break;
1081
1082 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1083 if (ret > 0)
1084 wait_for_completion_timeout(&conn->conn_logout_comp,
1085 SECONDS_FOR_LOGOUT_COMP *
1086 HZ);
1087 break;
1088 default:
1089 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1090 dump_stack();
1091 break;
1092 }
1093
1094 return ret;
1095}
1096
1097static void
1098isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1099{
1100 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1101 uint64_t read_va = 0, write_va = 0;
1102 uint32_t read_stag = 0, write_stag = 0;
1103 int rc;
1104
1105 switch (iser_hdr->flags & 0xF0) {
1106 case ISCSI_CTRL:
1107 if (iser_hdr->flags & ISER_RSV) {
1108 read_stag = be32_to_cpu(iser_hdr->read_stag);
1109 read_va = be64_to_cpu(iser_hdr->read_va);
1110 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1111 read_stag, (unsigned long long)read_va);
1112 }
1113 if (iser_hdr->flags & ISER_WSV) {
1114 write_stag = be32_to_cpu(iser_hdr->write_stag);
1115 write_va = be64_to_cpu(iser_hdr->write_va);
1116 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1117 write_stag, (unsigned long long)write_va);
1118 }
1119
1120 pr_debug("ISER ISCSI_CTRL PDU\n");
1121 break;
1122 case ISER_HELLO:
1123 pr_err("iSER Hello message\n");
1124 break;
1125 default:
1126 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1127 break;
1128 }
1129
1130 rc = isert_rx_opcode(isert_conn, rx_desc,
1131 read_stag, read_va, write_stag, write_va);
1132}
1133
1134static void
1135isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1136 unsigned long xfer_len)
1137{
1138 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1139 struct iscsi_hdr *hdr;
1140 u64 rx_dma;
1141 int rx_buflen, outstanding;
1142
1143 if ((char *)desc == isert_conn->login_req_buf) {
1144 rx_dma = isert_conn->login_req_dma;
1145 rx_buflen = ISER_RX_LOGIN_SIZE;
1146 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1147 rx_dma, rx_buflen);
1148 } else {
1149 rx_dma = desc->dma_addr;
1150 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1151 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1152 rx_dma, rx_buflen);
1153 }
1154
1155 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1156
1157 hdr = &desc->iscsi_header;
1158 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1159 hdr->opcode, hdr->itt, hdr->flags,
1160 (int)(xfer_len - ISER_HEADERS_LEN));
1161
1162 if ((char *)desc == isert_conn->login_req_buf)
1163 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1164 isert_conn);
1165 else
1166 isert_rx_do_work(desc, isert_conn);
1167
1168 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1169 DMA_FROM_DEVICE);
1170
1171 isert_conn->post_recv_buf_count--;
1172 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1173 isert_conn->post_recv_buf_count);
1174
1175 if ((char *)desc == isert_conn->login_req_buf)
1176 return;
1177
1178 outstanding = isert_conn->post_recv_buf_count;
1179 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1180 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1181 ISERT_MIN_POSTED_RX);
1182 err = isert_post_recv(isert_conn, count);
1183 if (err) {
1184 pr_err("isert_post_recv() count: %d failed, %d\n",
1185 count, err);
1186 }
1187 }
1188}
1189
1190static void
1191isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1192{
1193 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1194 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1195
1196 pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
1197
1198 if (wr->sge) {
1199 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1200 wr->sge = NULL;
1201 }
1202
1203 kfree(wr->send_wr);
1204 wr->send_wr = NULL;
1205
1206 kfree(isert_cmd->ib_sge);
1207 isert_cmd->ib_sge = NULL;
1208}
1209
1210static void
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001211isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001212{
1213 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1214 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001215 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001216
1217 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1218
1219 switch (cmd->iscsi_opcode) {
1220 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001221 spin_lock_bh(&conn->cmd_lock);
1222 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001223 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001224 spin_unlock_bh(&conn->cmd_lock);
1225
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001226 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001227 iscsit_stop_dataout_timer(cmd);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001228 /*
1229 * Check for special case during comp_err where
1230 * WRITE_PENDING has been handed off from core,
1231 * but requires an extra target_put_sess_cmd()
1232 * before transport_generic_free_cmd() below.
1233 */
1234 if (comp_err &&
1235 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1236 struct se_cmd *se_cmd = &cmd->se_cmd;
1237
1238 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1239 }
1240 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001241
1242 isert_unmap_cmd(isert_cmd, isert_conn);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001243 transport_generic_free_cmd(&cmd->se_cmd, 0);
1244 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001245 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001246 spin_lock_bh(&conn->cmd_lock);
1247 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001248 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001249 spin_unlock_bh(&conn->cmd_lock);
1250
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001251 transport_generic_free_cmd(&cmd->se_cmd, 0);
1252 break;
1253 case ISCSI_OP_REJECT:
1254 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001255 spin_lock_bh(&conn->cmd_lock);
1256 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001257 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001258 spin_unlock_bh(&conn->cmd_lock);
1259
1260 /*
1261 * Handle special case for REJECT when iscsi_add_reject*() has
1262 * overwritten the original iscsi_opcode assignment, and the
1263 * associated cmd->se_cmd needs to be released.
1264 */
1265 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001266 pr_debug("Calling transport_generic_free_cmd from"
1267 " isert_put_cmd for 0x%02x\n",
1268 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001269 transport_generic_free_cmd(&cmd->se_cmd, 0);
1270 break;
1271 }
1272 /*
1273 * Fall-through
1274 */
1275 default:
1276 isert_release_cmd(cmd);
1277 break;
1278 }
1279}
1280
1281static void
1282isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1283{
1284 if (tx_desc->dma_addr != 0) {
1285 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1286 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1287 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1288 tx_desc->dma_addr = 0;
1289 }
1290}
1291
1292static void
1293isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001294 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001295{
1296 if (isert_cmd->sense_buf_dma != 0) {
1297 pr_debug("Calling ib_dma_unmap_single for isert_cmd->sense_buf_dma\n");
1298 ib_dma_unmap_single(ib_dev, isert_cmd->sense_buf_dma,
1299 isert_cmd->sense_buf_len, DMA_TO_DEVICE);
1300 isert_cmd->sense_buf_dma = 0;
1301 }
1302
1303 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001304 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001305}
1306
1307static void
1308isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1309 struct isert_cmd *isert_cmd)
1310{
1311 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1312 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1313 struct se_cmd *se_cmd = &cmd->se_cmd;
1314 struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
1315
1316 iscsit_stop_dataout_timer(cmd);
1317
1318 if (wr->sge) {
1319 pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
1320 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1321 wr->sge = NULL;
1322 }
1323
1324 if (isert_cmd->ib_sge) {
1325 pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
1326 kfree(isert_cmd->ib_sge);
1327 isert_cmd->ib_sge = NULL;
1328 }
1329
1330 cmd->write_data_done = se_cmd->data_length;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001331 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001332
1333 pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
1334 spin_lock_bh(&cmd->istate_lock);
1335 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1336 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1337 spin_unlock_bh(&cmd->istate_lock);
1338
1339 target_execute_cmd(se_cmd);
1340}
1341
1342static void
1343isert_do_control_comp(struct work_struct *work)
1344{
1345 struct isert_cmd *isert_cmd = container_of(work,
1346 struct isert_cmd, comp_work);
1347 struct isert_conn *isert_conn = isert_cmd->conn;
1348 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1349 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1350
1351 switch (cmd->i_state) {
1352 case ISTATE_SEND_TASKMGTRSP:
1353 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1354
1355 atomic_dec(&isert_conn->post_send_buf_count);
1356 iscsit_tmr_post_handler(cmd, cmd->conn);
1357
1358 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001359 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001360 break;
1361 case ISTATE_SEND_REJECT:
1362 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1363 atomic_dec(&isert_conn->post_send_buf_count);
1364
1365 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001366 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001367 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001368 case ISTATE_SEND_LOGOUTRSP:
1369 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
Sagi Grimberg1813e802014-05-19 17:44:23 +03001370
1371 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001372 iscsit_logout_post_handler(cmd, cmd->conn);
1373 break;
1374 default:
1375 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1376 dump_stack();
1377 break;
1378 }
1379}
1380
1381static void
1382isert_response_completion(struct iser_tx_desc *tx_desc,
1383 struct isert_cmd *isert_cmd,
1384 struct isert_conn *isert_conn,
1385 struct ib_device *ib_dev)
1386{
1387 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001388 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001389
1390 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001391 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1392 cmd->i_state == ISTATE_SEND_REJECT) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001393 isert_unmap_tx_desc(tx_desc, ib_dev);
1394
1395 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1396 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1397 return;
1398 }
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001399 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400
1401 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001402 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001403}
1404
1405static void
1406isert_send_completion(struct iser_tx_desc *tx_desc,
1407 struct isert_conn *isert_conn)
1408{
1409 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1410 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1411 struct isert_rdma_wr *wr;
1412
1413 if (!isert_cmd) {
1414 atomic_dec(&isert_conn->post_send_buf_count);
1415 isert_unmap_tx_desc(tx_desc, ib_dev);
1416 return;
1417 }
1418 wr = &isert_cmd->rdma_wr;
1419
1420 switch (wr->iser_ib_op) {
1421 case ISER_IB_RECV:
1422 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1423 dump_stack();
1424 break;
1425 case ISER_IB_SEND:
1426 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1427 isert_response_completion(tx_desc, isert_cmd,
1428 isert_conn, ib_dev);
1429 break;
1430 case ISER_IB_RDMA_WRITE:
1431 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1432 dump_stack();
1433 break;
1434 case ISER_IB_RDMA_READ:
1435 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1436
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001437 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001438 isert_completion_rdma_read(tx_desc, isert_cmd);
1439 break;
1440 default:
1441 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1442 dump_stack();
1443 break;
1444 }
1445}
1446
1447static void
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001448isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001449{
1450 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001451 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001452
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001453 if (!isert_cmd)
1454 isert_unmap_tx_desc(tx_desc, ib_dev);
1455 else
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001456 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001457}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001458
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001459static void
1460isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1461{
1462 struct iscsi_conn *conn = isert_conn->conn;
1463
1464 if (isert_conn->post_recv_buf_count)
1465 return;
1466
1467 if (conn->sess) {
1468 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1469 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001470 }
1471
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001472 while (atomic_read(&isert_conn->post_send_buf_count))
1473 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001474
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001475 mutex_lock(&isert_conn->conn_mutex);
1476 isert_conn->state = ISER_CONN_DOWN;
1477 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07001478
Sagi Grimberg1813e802014-05-19 17:44:23 +03001479 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
1480
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001481 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001482}
1483
1484static void
1485isert_cq_tx_work(struct work_struct *work)
1486{
1487 struct isert_cq_desc *cq_desc = container_of(work,
1488 struct isert_cq_desc, cq_tx_work);
1489 struct isert_device *device = cq_desc->device;
1490 int cq_index = cq_desc->cq_index;
1491 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1492 struct isert_conn *isert_conn;
1493 struct iser_tx_desc *tx_desc;
1494 struct ib_wc wc;
1495
1496 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1497 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1498 isert_conn = wc.qp->qp_context;
1499
1500 if (wc.status == IB_WC_SUCCESS) {
1501 isert_send_completion(tx_desc, isert_conn);
1502 } else {
1503 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1504 pr_debug("TX wc.status: 0x%08x\n", wc.status);
1505 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001506 isert_cq_tx_comp_err(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001507 }
1508 }
1509
1510 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1511}
1512
1513static void
1514isert_cq_tx_callback(struct ib_cq *cq, void *context)
1515{
1516 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1517
1518 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1519 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1520}
1521
1522static void
1523isert_cq_rx_work(struct work_struct *work)
1524{
1525 struct isert_cq_desc *cq_desc = container_of(work,
1526 struct isert_cq_desc, cq_rx_work);
1527 struct isert_device *device = cq_desc->device;
1528 int cq_index = cq_desc->cq_index;
1529 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1530 struct isert_conn *isert_conn;
1531 struct iser_rx_desc *rx_desc;
1532 struct ib_wc wc;
1533 unsigned long xfer_len;
1534
1535 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1536 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1537 isert_conn = wc.qp->qp_context;
1538
1539 if (wc.status == IB_WC_SUCCESS) {
1540 xfer_len = (unsigned long)wc.byte_len;
1541 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1542 } else {
1543 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1544 if (wc.status != IB_WC_WR_FLUSH_ERR)
1545 pr_debug("RX wc.status: 0x%08x\n", wc.status);
1546
1547 isert_conn->post_recv_buf_count--;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001548 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001549 }
1550 }
1551
1552 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1553}
1554
1555static void
1556isert_cq_rx_callback(struct ib_cq *cq, void *context)
1557{
1558 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1559
1560 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1561 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1562}
1563
1564static int
1565isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1566{
1567 struct ib_send_wr *wr_failed;
1568 int ret;
1569
1570 atomic_inc(&isert_conn->post_send_buf_count);
1571
1572 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1573 &wr_failed);
1574 if (ret) {
1575 pr_err("ib_post_send failed with %d\n", ret);
1576 atomic_dec(&isert_conn->post_send_buf_count);
1577 return ret;
1578 }
1579 return ret;
1580}
1581
1582static int
1583isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1584{
1585 struct isert_cmd *isert_cmd = container_of(cmd,
1586 struct isert_cmd, iscsi_cmd);
1587 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1588 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1589 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1590 &isert_cmd->tx_desc.iscsi_header;
1591
1592 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1593 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1594 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1595 /*
1596 * Attach SENSE DATA payload to iSCSI Response PDU
1597 */
1598 if (cmd->se_cmd.sense_buffer &&
1599 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1600 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1601 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1602 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1603 u32 padding, sense_len;
1604
1605 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1606 cmd->sense_buffer);
1607 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1608
1609 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1610 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1611 sense_len = cmd->se_cmd.scsi_sense_length + padding;
1612
1613 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1614 (void *)cmd->sense_buffer, sense_len,
1615 DMA_TO_DEVICE);
1616
1617 isert_cmd->sense_buf_len = sense_len;
1618 tx_dsg->addr = isert_cmd->sense_buf_dma;
1619 tx_dsg->length = sense_len;
1620 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1621 isert_cmd->tx_desc.num_sge = 2;
1622 }
1623
1624 isert_init_send_wr(isert_cmd, send_wr);
1625
1626 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1627
1628 return isert_post_response(isert_conn, isert_cmd);
1629}
1630
1631static int
1632isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1633 bool nopout_response)
1634{
1635 struct isert_cmd *isert_cmd = container_of(cmd,
1636 struct isert_cmd, iscsi_cmd);
1637 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1638 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1639
1640 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1641 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1642 &isert_cmd->tx_desc.iscsi_header,
1643 nopout_response);
1644 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1645 isert_init_send_wr(isert_cmd, send_wr);
1646
1647 pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1648
1649 return isert_post_response(isert_conn, isert_cmd);
1650}
1651
1652static int
1653isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1654{
1655 struct isert_cmd *isert_cmd = container_of(cmd,
1656 struct isert_cmd, iscsi_cmd);
1657 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1658 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1659
1660 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1661 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1662 &isert_cmd->tx_desc.iscsi_header);
1663 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1664 isert_init_send_wr(isert_cmd, send_wr);
1665
1666 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1667
1668 return isert_post_response(isert_conn, isert_cmd);
1669}
1670
1671static int
1672isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1673{
1674 struct isert_cmd *isert_cmd = container_of(cmd,
1675 struct isert_cmd, iscsi_cmd);
1676 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1677 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1678
1679 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1680 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1681 &isert_cmd->tx_desc.iscsi_header);
1682 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1683 isert_init_send_wr(isert_cmd, send_wr);
1684
1685 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1686
1687 return isert_post_response(isert_conn, isert_cmd);
1688}
1689
1690static int
1691isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1692{
1693 struct isert_cmd *isert_cmd = container_of(cmd,
1694 struct isert_cmd, iscsi_cmd);
1695 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1696 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001697 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1698 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1699 struct iscsi_reject *hdr =
1700 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001701
1702 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001703 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001704 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001705
1706 hton24(hdr->dlength, ISCSI_HDR_LEN);
1707 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1708 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1709 DMA_TO_DEVICE);
1710 isert_cmd->sense_buf_len = ISCSI_HDR_LEN;
1711 tx_dsg->addr = isert_cmd->sense_buf_dma;
1712 tx_dsg->length = ISCSI_HDR_LEN;
1713 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1714 isert_cmd->tx_desc.num_sge = 2;
1715
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001716 isert_init_send_wr(isert_cmd, send_wr);
1717
1718 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1719
1720 return isert_post_response(isert_conn, isert_cmd);
1721}
1722
1723static int
1724isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1725 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1726 u32 data_left, u32 offset)
1727{
1728 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1729 struct scatterlist *sg_start, *tmp_sg;
1730 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1731 u32 sg_off, page_off;
1732 int i = 0, sg_nents;
1733
1734 sg_off = offset / PAGE_SIZE;
1735 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1736 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1737 page_off = offset % PAGE_SIZE;
1738
1739 send_wr->sg_list = ib_sge;
1740 send_wr->num_sge = sg_nents;
1741 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1742 /*
1743 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1744 */
1745 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1746 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1747 (unsigned long long)tmp_sg->dma_address,
1748 tmp_sg->length, page_off);
1749
1750 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1751 ib_sge->length = min_t(u32, data_left,
1752 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1753 ib_sge->lkey = isert_conn->conn_mr->lkey;
1754
1755 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
1756 ib_sge->addr, ib_sge->length);
1757 page_off = 0;
1758 data_left -= ib_sge->length;
1759 ib_sge++;
1760 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1761 }
1762
1763 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1764 send_wr->sg_list, send_wr->num_sge);
1765
1766 return sg_nents;
1767}
1768
1769static int
1770isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1771{
1772 struct se_cmd *se_cmd = &cmd->se_cmd;
1773 struct isert_cmd *isert_cmd = container_of(cmd,
1774 struct isert_cmd, iscsi_cmd);
1775 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1776 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1777 struct ib_send_wr *wr_failed, *send_wr;
1778 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1779 struct ib_sge *ib_sge;
1780 struct scatterlist *sg;
1781 u32 offset = 0, data_len, data_left, rdma_write_max;
1782 int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
1783
1784 pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
1785
1786 sg = &se_cmd->t_data_sg[0];
1787 sg_nents = se_cmd->t_data_nents;
1788
1789 count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1790 if (unlikely(!count)) {
1791 pr_err("Unable to map put_datain SGs\n");
1792 return -EINVAL;
1793 }
1794 wr->sge = sg;
1795 wr->num_sge = sg_nents;
1796 pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
1797 count, sg, sg_nents);
1798
1799 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1800 if (!ib_sge) {
1801 pr_warn("Unable to allocate datain ib_sge\n");
1802 ret = -ENOMEM;
1803 goto unmap_sg;
1804 }
1805 isert_cmd->ib_sge = ib_sge;
1806
1807 pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
1808 ib_sge, se_cmd->t_data_nents);
1809
1810 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1811 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1812 GFP_KERNEL);
1813 if (!wr->send_wr) {
1814 pr_err("Unable to allocate wr->send_wr\n");
1815 ret = -ENOMEM;
1816 goto unmap_sg;
1817 }
1818 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1819 wr->send_wr, wr->send_wr_num);
1820
1821 iscsit_increment_maxcmdsn(cmd, conn->sess);
1822 cmd->stat_sn = conn->stat_sn++;
1823
1824 wr->isert_cmd = isert_cmd;
1825 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1826 data_left = se_cmd->data_length;
1827
1828 for (i = 0; i < wr->send_wr_num; i++) {
1829 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1830 data_len = min(data_left, rdma_write_max);
1831
1832 send_wr->opcode = IB_WR_RDMA_WRITE;
1833 send_wr->send_flags = 0;
1834 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1835 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1836
1837 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1838 send_wr, data_len, offset);
1839 ib_sge += ib_sge_cnt;
1840
1841 if (i + 1 == wr->send_wr_num)
1842 send_wr->next = &isert_cmd->tx_desc.send_wr;
1843 else
1844 send_wr->next = &wr->send_wr[i + 1];
1845
1846 offset += data_len;
1847 data_left -= data_len;
1848 }
1849 /*
1850 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1851 */
1852 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1853 iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1854 &isert_cmd->tx_desc.iscsi_header);
1855 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1856 isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1857
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001858 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001859
1860 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1861 if (rc) {
1862 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001863 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001864 }
1865 pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
1866 return 1;
1867
1868unmap_sg:
1869 ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1870 return ret;
1871}
1872
1873static int
1874isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1875{
1876 struct se_cmd *se_cmd = &cmd->se_cmd;
1877 struct isert_cmd *isert_cmd = container_of(cmd,
1878 struct isert_cmd, iscsi_cmd);
1879 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1880 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1881 struct ib_send_wr *wr_failed, *send_wr;
1882 struct ib_sge *ib_sge;
1883 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1884 struct scatterlist *sg_start;
1885 u32 sg_off, sg_nents, page_off, va_offset = 0;
1886 u32 offset = 0, data_len, data_left, rdma_write_max;
1887 int rc, ret = 0, count, i, ib_sge_cnt;
1888
1889 pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
1890 se_cmd->data_length, cmd->write_data_done);
1891
1892 sg_off = cmd->write_data_done / PAGE_SIZE;
1893 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1894 page_off = cmd->write_data_done % PAGE_SIZE;
1895
1896 pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
1897 sg_off, sg_start, page_off);
1898
1899 data_left = se_cmd->data_length - cmd->write_data_done;
1900 sg_nents = se_cmd->t_data_nents - sg_off;
1901
1902 pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
1903 data_left, sg_nents);
1904
1905 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1906 if (unlikely(!count)) {
1907 pr_err("Unable to map get_dataout SGs\n");
1908 return -EINVAL;
1909 }
1910 wr->sge = sg_start;
1911 wr->num_sge = sg_nents;
1912 pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
1913 count, sg_start, sg_nents);
1914
1915 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1916 if (!ib_sge) {
1917 pr_warn("Unable to allocate dataout ib_sge\n");
1918 ret = -ENOMEM;
1919 goto unmap_sg;
1920 }
1921 isert_cmd->ib_sge = ib_sge;
1922
1923 pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
1924 ib_sge, sg_nents);
1925
1926 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1927 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1928 GFP_KERNEL);
1929 if (!wr->send_wr) {
1930 pr_debug("Unable to allocate wr->send_wr\n");
1931 ret = -ENOMEM;
1932 goto unmap_sg;
1933 }
1934 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1935 wr->send_wr, wr->send_wr_num);
1936
1937 isert_cmd->tx_desc.isert_cmd = isert_cmd;
1938
1939 wr->iser_ib_op = ISER_IB_RDMA_READ;
1940 wr->isert_cmd = isert_cmd;
1941 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1942 offset = cmd->write_data_done;
1943
1944 for (i = 0; i < wr->send_wr_num; i++) {
1945 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1946 data_len = min(data_left, rdma_write_max);
1947
1948 send_wr->opcode = IB_WR_RDMA_READ;
1949 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
1950 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
1951
1952 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1953 send_wr, data_len, offset);
1954 ib_sge += ib_sge_cnt;
1955
1956 if (i + 1 == wr->send_wr_num)
1957 send_wr->send_flags = IB_SEND_SIGNALED;
1958 else
1959 send_wr->next = &wr->send_wr[i + 1];
1960
1961 offset += data_len;
1962 va_offset += data_len;
1963 data_left -= data_len;
1964 }
1965
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001966 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967
1968 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1969 if (rc) {
1970 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001971 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001972 }
1973 pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
1974 return 0;
1975
1976unmap_sg:
1977 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1978 return ret;
1979}
1980
1981static int
1982isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1983{
1984 int ret;
1985
1986 switch (state) {
1987 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
1988 ret = isert_put_nopin(cmd, conn, false);
1989 break;
1990 default:
1991 pr_err("Unknown immediate state: 0x%02x\n", state);
1992 ret = -EINVAL;
1993 break;
1994 }
1995
1996 return ret;
1997}
1998
1999static int
2000isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2001{
2002 int ret;
2003
2004 switch (state) {
2005 case ISTATE_SEND_LOGOUTRSP:
2006 ret = isert_put_logout_rsp(cmd, conn);
2007 if (!ret) {
2008 pr_debug("Returning iSER Logout -EAGAIN\n");
2009 ret = -EAGAIN;
2010 }
2011 break;
2012 case ISTATE_SEND_NOPIN:
2013 ret = isert_put_nopin(cmd, conn, true);
2014 break;
2015 case ISTATE_SEND_TASKMGTRSP:
2016 ret = isert_put_tm_rsp(cmd, conn);
2017 break;
2018 case ISTATE_SEND_REJECT:
2019 ret = isert_put_reject(cmd, conn);
2020 break;
2021 case ISTATE_SEND_STATUS:
2022 /*
2023 * Special case for sending non GOOD SCSI status from TX thread
2024 * context during pre se_cmd excecution failure.
2025 */
2026 ret = isert_put_response(conn, cmd);
2027 break;
2028 default:
2029 pr_err("Unknown response state: 0x%02x\n", state);
2030 ret = -EINVAL;
2031 break;
2032 }
2033
2034 return ret;
2035}
2036
2037static int
2038isert_setup_np(struct iscsi_np *np,
2039 struct __kernel_sockaddr_storage *ksockaddr)
2040{
2041 struct isert_np *isert_np;
2042 struct rdma_cm_id *isert_lid;
2043 struct sockaddr *sa;
2044 int ret;
2045
2046 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2047 if (!isert_np) {
2048 pr_err("Unable to allocate struct isert_np\n");
2049 return -ENOMEM;
2050 }
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002051 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002052 mutex_init(&isert_np->np_accept_mutex);
2053 INIT_LIST_HEAD(&isert_np->np_accept_list);
2054 init_completion(&isert_np->np_login_comp);
2055
2056 sa = (struct sockaddr *)ksockaddr;
2057 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2058 /*
2059 * Setup the np->np_sockaddr from the passed sockaddr setup
2060 * in iscsi_target_configfs.c code..
2061 */
2062 memcpy(&np->np_sockaddr, ksockaddr,
2063 sizeof(struct __kernel_sockaddr_storage));
2064
2065 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2066 IB_QPT_RC);
2067 if (IS_ERR(isert_lid)) {
2068 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2069 PTR_ERR(isert_lid));
2070 ret = PTR_ERR(isert_lid);
2071 goto out;
2072 }
2073
2074 ret = rdma_bind_addr(isert_lid, sa);
2075 if (ret) {
2076 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2077 goto out_lid;
2078 }
2079
2080 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2081 if (ret) {
2082 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2083 goto out_lid;
2084 }
2085
2086 isert_np->np_cm_id = isert_lid;
2087 np->np_context = isert_np;
2088 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2089
2090 return 0;
2091
2092out_lid:
2093 rdma_destroy_id(isert_lid);
2094out:
2095 kfree(isert_np);
2096 return ret;
2097}
2098
2099static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002100isert_rdma_accept(struct isert_conn *isert_conn)
2101{
2102 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2103 struct rdma_conn_param cp;
2104 int ret;
2105
2106 memset(&cp, 0, sizeof(struct rdma_conn_param));
2107 cp.responder_resources = isert_conn->responder_resources;
2108 cp.initiator_depth = isert_conn->initiator_depth;
2109 cp.retry_count = 7;
2110 cp.rnr_retry_count = 7;
2111
2112 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2113
2114 ret = rdma_accept(cm_id, &cp);
2115 if (ret) {
2116 pr_err("rdma_accept() failed with: %d\n", ret);
2117 return ret;
2118 }
2119
2120 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2121
2122 return 0;
2123}
2124
2125static int
2126isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2127{
2128 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2129 int ret;
2130
2131 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2132
2133 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2134 if (ret)
2135 return ret;
2136
2137 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2138 return 0;
2139}
2140
2141static void
2142isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2143 struct isert_conn *isert_conn)
2144{
2145 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2146 struct rdma_route *cm_route = &cm_id->route;
2147 struct sockaddr_in *sock_in;
2148 struct sockaddr_in6 *sock_in6;
2149
2150 conn->login_family = np->np_sockaddr.ss_family;
2151
2152 if (np->np_sockaddr.ss_family == AF_INET6) {
2153 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2154 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2155 &sock_in6->sin6_addr.in6_u);
2156 conn->login_port = ntohs(sock_in6->sin6_port);
2157
2158 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2159 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2160 &sock_in6->sin6_addr.in6_u);
2161 conn->local_port = ntohs(sock_in6->sin6_port);
2162 } else {
2163 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2164 sprintf(conn->login_ip, "%pI4",
2165 &sock_in->sin_addr.s_addr);
2166 conn->login_port = ntohs(sock_in->sin_port);
2167
2168 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2169 sprintf(conn->local_ip, "%pI4",
2170 &sock_in->sin_addr.s_addr);
2171 conn->local_port = ntohs(sock_in->sin_port);
2172 }
2173}
2174
2175static int
2176isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2177{
2178 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2179 struct isert_conn *isert_conn;
2180 int max_accept = 0, ret;
2181
2182accept_wait:
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002183 ret = down_interruptible(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002184 if (max_accept > 5)
2185 return -ENODEV;
2186
2187 spin_lock_bh(&np->np_thread_lock);
Sagi Grimberg3ddb8752014-05-19 17:44:22 +03002188 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002189 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg3ddb8752014-05-19 17:44:22 +03002190 pr_debug("np_thread_state %d for isert_accept_np\n",
2191 np->np_thread_state);
2192 /**
2193 * No point in stalling here when np_thread
2194 * is in state RESET/SHUTDOWN/EXIT - bail
2195 **/
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
Sagi Grimberg1813e802014-05-19 17:44:23 +03002246 mutex_lock(&isert_conn->conn_mutex);
2247 if (isert_conn->conn_cm_id) {
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002248 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002249 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002250 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002251 /*
2252 * Only wait for conn_wait_comp_err if the isert_conn made it
2253 * into full feature phase..
2254 */
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002255 if (isert_conn->state == ISER_CONN_INIT) {
2256 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002257 return;
2258 }
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002259 if (isert_conn->state == ISER_CONN_UP)
2260 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002261 mutex_unlock(&isert_conn->conn_mutex);
2262
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002263 wait_for_completion(&isert_conn->conn_wait_comp_err);
2264
2265 wait_for_completion(&isert_conn->conn_wait);
2266}
2267
2268static void isert_free_conn(struct iscsi_conn *conn)
2269{
2270 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002271
2272 isert_put_conn(isert_conn);
2273}
2274
2275static struct iscsit_transport iser_target_transport = {
2276 .name = "IB/iSER",
2277 .transport_type = ISCSI_INFINIBAND,
2278 .owner = THIS_MODULE,
2279 .iscsit_setup_np = isert_setup_np,
2280 .iscsit_accept_np = isert_accept_np,
2281 .iscsit_free_np = isert_free_np,
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002282 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002283 .iscsit_free_conn = isert_free_conn,
2284 .iscsit_alloc_cmd = isert_alloc_cmd,
2285 .iscsit_get_login_rx = isert_get_login_rx,
2286 .iscsit_put_login_tx = isert_put_login_tx,
2287 .iscsit_immediate_queue = isert_immediate_queue,
2288 .iscsit_response_queue = isert_response_queue,
2289 .iscsit_get_dataout = isert_get_dataout,
2290 .iscsit_queue_data_in = isert_put_datain,
2291 .iscsit_queue_status = isert_put_response,
2292};
2293
2294static int __init isert_init(void)
2295{
2296 int ret;
2297
2298 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2299 if (!isert_rx_wq) {
2300 pr_err("Unable to allocate isert_rx_wq\n");
2301 return -ENOMEM;
2302 }
2303
2304 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2305 if (!isert_comp_wq) {
2306 pr_err("Unable to allocate isert_comp_wq\n");
2307 ret = -ENOMEM;
2308 goto destroy_rx_wq;
2309 }
2310
2311 isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
2312 sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
2313 0, NULL);
2314 if (!isert_cmd_cache) {
2315 pr_err("Unable to create isert_cmd_cache\n");
2316 ret = -ENOMEM;
2317 goto destroy_tx_cq;
2318 }
2319
2320 iscsit_register_transport(&iser_target_transport);
2321 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2322 return 0;
2323
2324destroy_tx_cq:
2325 destroy_workqueue(isert_comp_wq);
2326destroy_rx_wq:
2327 destroy_workqueue(isert_rx_wq);
2328 return ret;
2329}
2330
2331static void __exit isert_exit(void)
2332{
2333 kmem_cache_destroy(isert_cmd_cache);
2334 destroy_workqueue(isert_comp_wq);
2335 destroy_workqueue(isert_rx_wq);
2336 iscsit_unregister_transport(&iser_target_transport);
2337 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2338}
2339
2340MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2341MODULE_VERSION("0.1");
2342MODULE_AUTHOR("nab@Linux-iSCSI.org");
2343MODULE_LICENSE("GPL");
2344
2345module_init(isert_init);
2346module_exit(isert_exit);