blob: 988e29d18bb48f8bdcf172aa43a882b8534a5598 [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
385 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
386 cma_id, cma_id->context);
387
388 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
389 if (!isert_conn) {
390 pr_err("Unable to allocate isert_conn\n");
391 return -ENOMEM;
392 }
393 isert_conn->state = ISER_CONN_INIT;
394 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
395 init_completion(&isert_conn->conn_login_comp);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800396 init_completion(&isert_conn->conn_wait);
397 init_completion(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800398 kref_init(&isert_conn->conn_kref);
399 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700400 mutex_init(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800401
402 cma_id->context = isert_conn;
403 isert_conn->conn_cm_id = cma_id;
404 isert_conn->responder_resources = event->param.conn.responder_resources;
405 isert_conn->initiator_depth = event->param.conn.initiator_depth;
406 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
407 isert_conn->responder_resources, isert_conn->initiator_depth);
408
409 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
410 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
411 if (!isert_conn->login_buf) {
412 pr_err("Unable to allocate isert_conn->login_buf\n");
413 ret = -ENOMEM;
414 goto out;
415 }
416
417 isert_conn->login_req_buf = isert_conn->login_buf;
418 isert_conn->login_rsp_buf = isert_conn->login_buf +
419 ISCSI_DEF_MAX_RECV_SEG_LEN;
420 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
421 isert_conn->login_buf, isert_conn->login_req_buf,
422 isert_conn->login_rsp_buf);
423
424 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
425 (void *)isert_conn->login_req_buf,
426 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
427
428 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
429 if (ret) {
430 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
431 ret);
432 isert_conn->login_req_dma = 0;
433 goto out_login_buf;
434 }
435
436 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
437 (void *)isert_conn->login_rsp_buf,
438 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
439
440 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
441 if (ret) {
442 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
443 ret);
444 isert_conn->login_rsp_dma = 0;
445 goto out_req_dma_map;
446 }
447
448 device = isert_device_find_by_ib_dev(cma_id);
449 if (IS_ERR(device)) {
450 ret = PTR_ERR(device);
451 goto out_rsp_dma_map;
452 }
453
454 isert_conn->conn_device = device;
455 isert_conn->conn_pd = device->dev_pd;
456 isert_conn->conn_mr = device->dev_mr;
457
458 ret = isert_conn_setup_qp(isert_conn, cma_id);
459 if (ret)
460 goto out_conn_dev;
461
462 mutex_lock(&isert_np->np_accept_mutex);
Sagi Grimberg5de94f82014-04-29 13:13:44 +0300463 list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800464 mutex_unlock(&isert_np->np_accept_mutex);
465
Sagi Grimberg8a2629a2014-04-29 13:13:45 +0300466 pr_debug("isert_connect_request() up np_sem np: %p\n", np);
467 up(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800468 return 0;
469
470out_conn_dev:
471 isert_device_try_release(device);
472out_rsp_dma_map:
473 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
474 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
475out_req_dma_map:
476 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
477 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
478out_login_buf:
479 kfree(isert_conn->login_buf);
480out:
481 kfree(isert_conn);
482 return ret;
483}
484
485static void
486isert_connect_release(struct isert_conn *isert_conn)
487{
488 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
489 struct isert_device *device = isert_conn->conn_device;
490 int cq_index;
491
492 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
493
494 if (isert_conn->conn_qp) {
495 cq_index = ((struct isert_cq_desc *)
496 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
497 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
498 isert_conn->conn_device->cq_active_qps[cq_index]--;
499
500 rdma_destroy_qp(isert_conn->conn_cm_id);
501 }
502
503 isert_free_rx_descriptors(isert_conn);
504 rdma_destroy_id(isert_conn->conn_cm_id);
505
506 if (isert_conn->login_buf) {
507 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
508 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
509 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
510 ISCSI_DEF_MAX_RECV_SEG_LEN,
511 DMA_FROM_DEVICE);
512 kfree(isert_conn->login_buf);
513 }
514 kfree(isert_conn);
515
516 if (device)
517 isert_device_try_release(device);
518
519 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
520}
521
522static void
523isert_connected_handler(struct rdma_cm_id *cma_id)
524{
525 return;
526}
527
528static void
529isert_release_conn_kref(struct kref *kref)
530{
531 struct isert_conn *isert_conn = container_of(kref,
532 struct isert_conn, conn_kref);
533
534 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
535 current->comm, current->pid);
536
537 isert_connect_release(isert_conn);
538}
539
540static void
541isert_put_conn(struct isert_conn *isert_conn)
542{
543 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
544}
545
546static void
547isert_disconnect_work(struct work_struct *work)
548{
549 struct isert_conn *isert_conn = container_of(work,
550 struct isert_conn, conn_logout_work);
551
552 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700553 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800554 if (isert_conn->state == ISER_CONN_UP)
555 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800556
557 if (isert_conn->post_recv_buf_count == 0 &&
558 atomic_read(&isert_conn->post_send_buf_count) == 0) {
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700559 mutex_unlock(&isert_conn->conn_mutex);
560 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800561 }
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700562 if (!isert_conn->conn_cm_id) {
563 mutex_unlock(&isert_conn->conn_mutex);
564 isert_put_conn(isert_conn);
565 return;
566 }
567 if (!isert_conn->logout_posted) {
568 pr_debug("Calling rdma_disconnect for !logout_posted from"
569 " isert_disconnect_work\n");
570 rdma_disconnect(isert_conn->conn_cm_id);
571 mutex_unlock(&isert_conn->conn_mutex);
572 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
573 goto wake_up;
574 }
575 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800576
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -0700577wake_up:
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -0800578 complete(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800579 isert_put_conn(isert_conn);
580}
581
582static void
583isert_disconnected_handler(struct rdma_cm_id *cma_id)
584{
585 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
586
587 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
588 schedule_work(&isert_conn->conn_logout_work);
589}
590
591static int
592isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
593{
594 int ret = 0;
595
596 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
597 event->event, event->status, cma_id->context, cma_id);
598
599 switch (event->event) {
600 case RDMA_CM_EVENT_CONNECT_REQUEST:
601 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
602 ret = isert_connect_request(cma_id, event);
603 break;
604 case RDMA_CM_EVENT_ESTABLISHED:
605 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
606 isert_connected_handler(cma_id);
607 break;
608 case RDMA_CM_EVENT_DISCONNECTED:
609 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
610 isert_disconnected_handler(cma_id);
611 break;
612 case RDMA_CM_EVENT_DEVICE_REMOVAL:
613 case RDMA_CM_EVENT_ADDR_CHANGE:
614 break;
615 case RDMA_CM_EVENT_CONNECT_ERROR:
616 default:
617 pr_err("Unknown RDMA CMA event: %d\n", event->event);
618 break;
619 }
620
621 if (ret != 0) {
622 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
623 event->event, ret);
624 dump_stack();
625 }
626
627 return ret;
628}
629
630static int
631isert_post_recv(struct isert_conn *isert_conn, u32 count)
632{
633 struct ib_recv_wr *rx_wr, *rx_wr_failed;
634 int i, ret;
635 unsigned int rx_head = isert_conn->conn_rx_desc_head;
636 struct iser_rx_desc *rx_desc;
637
638 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
639 rx_desc = &isert_conn->conn_rx_descs[rx_head];
640 rx_wr->wr_id = (unsigned long)rx_desc;
641 rx_wr->sg_list = &rx_desc->rx_sg;
642 rx_wr->num_sge = 1;
643 rx_wr->next = rx_wr + 1;
644 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
645 }
646
647 rx_wr--;
648 rx_wr->next = NULL; /* mark end of work requests list */
649
650 isert_conn->post_recv_buf_count += count;
651 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
652 &rx_wr_failed);
653 if (ret) {
654 pr_err("ib_post_recv() failed with ret: %d\n", ret);
655 isert_conn->post_recv_buf_count -= count;
656 } else {
657 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
658 isert_conn->conn_rx_desc_head = rx_head;
659 }
660 return ret;
661}
662
663static int
664isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
665{
666 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
667 struct ib_send_wr send_wr, *send_wr_failed;
668 int ret;
669
670 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
671 ISER_HEADERS_LEN, DMA_TO_DEVICE);
672
673 send_wr.next = NULL;
674 send_wr.wr_id = (unsigned long)tx_desc;
675 send_wr.sg_list = tx_desc->tx_sg;
676 send_wr.num_sge = tx_desc->num_sge;
677 send_wr.opcode = IB_WR_SEND;
678 send_wr.send_flags = IB_SEND_SIGNALED;
679
680 atomic_inc(&isert_conn->post_send_buf_count);
681
682 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
683 if (ret) {
684 pr_err("ib_post_send() failed, ret: %d\n", ret);
685 atomic_dec(&isert_conn->post_send_buf_count);
686 }
687
688 return ret;
689}
690
691static void
692isert_create_send_desc(struct isert_conn *isert_conn,
693 struct isert_cmd *isert_cmd,
694 struct iser_tx_desc *tx_desc)
695{
696 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
697
698 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
699 ISER_HEADERS_LEN, DMA_TO_DEVICE);
700
701 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
702 tx_desc->iser_header.flags = ISER_VER;
703
704 tx_desc->num_sge = 1;
705 tx_desc->isert_cmd = isert_cmd;
706
707 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
708 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
709 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
710 }
711}
712
713static int
714isert_init_tx_hdrs(struct isert_conn *isert_conn,
715 struct iser_tx_desc *tx_desc)
716{
717 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
718 u64 dma_addr;
719
720 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
721 ISER_HEADERS_LEN, DMA_TO_DEVICE);
722 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
723 pr_err("ib_dma_mapping_error() failed\n");
724 return -ENOMEM;
725 }
726
727 tx_desc->dma_addr = dma_addr;
728 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
729 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
730 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
731
732 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
733 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
734 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
735
736 return 0;
737}
738
739static void
740isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
741{
742 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
743 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
744 send_wr->opcode = IB_WR_SEND;
745 send_wr->send_flags = IB_SEND_SIGNALED;
746 send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
747 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
748}
749
750static int
751isert_rdma_post_recvl(struct isert_conn *isert_conn)
752{
753 struct ib_recv_wr rx_wr, *rx_wr_fail;
754 struct ib_sge sge;
755 int ret;
756
757 memset(&sge, 0, sizeof(struct ib_sge));
758 sge.addr = isert_conn->login_req_dma;
759 sge.length = ISER_RX_LOGIN_SIZE;
760 sge.lkey = isert_conn->conn_mr->lkey;
761
762 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
763 sge.addr, sge.length, sge.lkey);
764
765 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
766 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
767 rx_wr.sg_list = &sge;
768 rx_wr.num_sge = 1;
769
770 isert_conn->post_recv_buf_count++;
771 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
772 if (ret) {
773 pr_err("ib_post_recv() failed: %d\n", ret);
774 isert_conn->post_recv_buf_count--;
775 }
776
777 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
778 return ret;
779}
780
781static int
782isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
783 u32 length)
784{
785 struct isert_conn *isert_conn = conn->context;
786 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
787 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
788 int ret;
789
790 isert_create_send_desc(isert_conn, NULL, tx_desc);
791
792 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
793 sizeof(struct iscsi_hdr));
794
795 isert_init_tx_hdrs(isert_conn, tx_desc);
796
797 if (length > 0) {
798 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
799
800 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
801 length, DMA_TO_DEVICE);
802
803 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
804
805 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
806 length, DMA_TO_DEVICE);
807
808 tx_dsg->addr = isert_conn->login_rsp_dma;
809 tx_dsg->length = length;
810 tx_dsg->lkey = isert_conn->conn_mr->lkey;
811 tx_desc->num_sge = 2;
812 }
813 if (!login->login_failed) {
814 if (login->login_complete) {
815 ret = isert_alloc_rx_descriptors(isert_conn);
816 if (ret)
817 return ret;
818
819 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
820 if (ret)
821 return ret;
822
823 isert_conn->state = ISER_CONN_UP;
824 goto post_send;
825 }
826
827 ret = isert_rdma_post_recvl(isert_conn);
828 if (ret)
829 return ret;
830 }
831post_send:
832 ret = isert_post_send(isert_conn, tx_desc);
833 if (ret)
834 return ret;
835
836 return 0;
837}
838
839static void
840isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
841 struct isert_conn *isert_conn)
842{
843 struct iscsi_conn *conn = isert_conn->conn;
844 struct iscsi_login *login = conn->conn_login;
845 int size;
846
847 if (!login) {
848 pr_err("conn->conn_login is NULL\n");
849 dump_stack();
850 return;
851 }
852
853 if (login->first_request) {
854 struct iscsi_login_req *login_req =
855 (struct iscsi_login_req *)&rx_desc->iscsi_header;
856 /*
857 * Setup the initial iscsi_login values from the leading
858 * login request PDU.
859 */
860 login->leading_connection = (!login_req->tsih) ? 1 : 0;
861 login->current_stage =
862 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
863 >> 2;
864 login->version_min = login_req->min_version;
865 login->version_max = login_req->max_version;
866 memcpy(login->isid, login_req->isid, 6);
867 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
868 login->init_task_tag = login_req->itt;
869 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
870 login->cid = be16_to_cpu(login_req->cid);
871 login->tsih = be16_to_cpu(login_req->tsih);
872 }
873
874 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
875
876 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
877 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
878 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
879 memcpy(login->req_buf, &rx_desc->data[0], size);
880
881 complete(&isert_conn->conn_login_comp);
882}
883
884static void
885isert_release_cmd(struct iscsi_cmd *cmd)
886{
887 struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
888 iscsi_cmd);
889
890 pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
891
892 kfree(cmd->buf_ptr);
893 kfree(cmd->tmr_req);
894
895 kmem_cache_free(isert_cmd_cache, isert_cmd);
896}
897
898static struct iscsi_cmd
899*isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
900{
901 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
902 struct isert_cmd *isert_cmd;
903
904 isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
905 if (!isert_cmd) {
906 pr_err("Unable to allocate isert_cmd\n");
907 return NULL;
908 }
909 isert_cmd->conn = isert_conn;
910 isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
911
912 return &isert_cmd->iscsi_cmd;
913}
914
915static int
916isert_handle_scsi_cmd(struct isert_conn *isert_conn,
917 struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
918 unsigned char *buf)
919{
920 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
921 struct iscsi_conn *conn = isert_conn->conn;
922 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
923 struct scatterlist *sg;
924 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
925 bool dump_payload = false;
926
927 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
928 if (rc < 0)
929 return rc;
930
931 imm_data = cmd->immediate_data;
932 imm_data_len = cmd->first_burst_len;
933 unsol_data = cmd->unsolicited_data;
934
935 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
936 if (rc < 0) {
937 return 0;
938 } else if (rc > 0) {
939 dump_payload = true;
940 goto sequence_cmd;
941 }
942
943 if (!imm_data)
944 return 0;
945
946 sg = &cmd->se_cmd.t_data_sg[0];
947 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
948
949 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
950 sg, sg_nents, &rx_desc->data[0], imm_data_len);
951
952 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
953
954 cmd->write_data_done += imm_data_len;
955
956 if (cmd->write_data_done == cmd->se_cmd.data_length) {
957 spin_lock_bh(&cmd->istate_lock);
958 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
959 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
960 spin_unlock_bh(&cmd->istate_lock);
961 }
962
963sequence_cmd:
Nicholas Bellingeradb97c22013-07-30 04:04:02 +0000964 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800965
966 if (!rc && dump_payload == false && unsol_data)
967 iscsit_set_unsoliticed_dataout(cmd);
968
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800969 return 0;
970}
971
972static int
973isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
974 struct iser_rx_desc *rx_desc, unsigned char *buf)
975{
976 struct scatterlist *sg_start;
977 struct iscsi_conn *conn = isert_conn->conn;
978 struct iscsi_cmd *cmd = NULL;
979 struct iscsi_data *hdr = (struct iscsi_data *)buf;
980 u32 unsol_data_len = ntoh24(hdr->dlength);
981 int rc, sg_nents, sg_off, page_off;
982
983 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
984 if (rc < 0)
985 return rc;
986 else if (!cmd)
987 return 0;
988 /*
989 * FIXME: Unexpected unsolicited_data out
990 */
991 if (!cmd->unsolicited_data) {
992 pr_err("Received unexpected solicited data payload\n");
993 dump_stack();
994 return -1;
995 }
996
997 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
998 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
999
1000 sg_off = cmd->write_data_done / PAGE_SIZE;
1001 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1002 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
1003 page_off = cmd->write_data_done % PAGE_SIZE;
1004 /*
1005 * FIXME: Non page-aligned unsolicited_data out
1006 */
1007 if (page_off) {
1008 pr_err("Received unexpected non-page aligned data payload\n");
1009 dump_stack();
1010 return -1;
1011 }
1012 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
1013 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
1014
1015 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
1016 unsol_data_len);
1017
1018 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1019 if (rc < 0)
1020 return rc;
1021
1022 return 0;
1023}
1024
1025static int
1026isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1027 uint32_t read_stag, uint64_t read_va,
1028 uint32_t write_stag, uint64_t write_va)
1029{
1030 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1031 struct iscsi_conn *conn = isert_conn->conn;
1032 struct iscsi_cmd *cmd;
1033 struct isert_cmd *isert_cmd;
1034 int ret = -EINVAL;
1035 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1036
1037 switch (opcode) {
1038 case ISCSI_OP_SCSI_CMD:
1039 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1040 if (!cmd)
1041 break;
1042
1043 isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1044 isert_cmd->read_stag = read_stag;
1045 isert_cmd->read_va = read_va;
1046 isert_cmd->write_stag = write_stag;
1047 isert_cmd->write_va = write_va;
1048
1049 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
1050 rx_desc, (unsigned char *)hdr);
1051 break;
1052 case ISCSI_OP_NOOP_OUT:
1053 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1054 if (!cmd)
1055 break;
1056
1057 ret = iscsit_handle_nop_out(conn, cmd, (unsigned char *)hdr);
1058 break;
1059 case ISCSI_OP_SCSI_DATA_OUT:
1060 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1061 (unsigned char *)hdr);
1062 break;
1063 case ISCSI_OP_SCSI_TMFUNC:
1064 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1065 if (!cmd)
1066 break;
1067
1068 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1069 (unsigned char *)hdr);
1070 break;
1071 case ISCSI_OP_LOGOUT:
1072 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1073 if (!cmd)
1074 break;
1075
1076 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1077 if (ret > 0)
1078 wait_for_completion_timeout(&conn->conn_logout_comp,
1079 SECONDS_FOR_LOGOUT_COMP *
1080 HZ);
1081 break;
1082 default:
1083 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1084 dump_stack();
1085 break;
1086 }
1087
1088 return ret;
1089}
1090
1091static void
1092isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1093{
1094 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1095 uint64_t read_va = 0, write_va = 0;
1096 uint32_t read_stag = 0, write_stag = 0;
1097 int rc;
1098
1099 switch (iser_hdr->flags & 0xF0) {
1100 case ISCSI_CTRL:
1101 if (iser_hdr->flags & ISER_RSV) {
1102 read_stag = be32_to_cpu(iser_hdr->read_stag);
1103 read_va = be64_to_cpu(iser_hdr->read_va);
1104 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1105 read_stag, (unsigned long long)read_va);
1106 }
1107 if (iser_hdr->flags & ISER_WSV) {
1108 write_stag = be32_to_cpu(iser_hdr->write_stag);
1109 write_va = be64_to_cpu(iser_hdr->write_va);
1110 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1111 write_stag, (unsigned long long)write_va);
1112 }
1113
1114 pr_debug("ISER ISCSI_CTRL PDU\n");
1115 break;
1116 case ISER_HELLO:
1117 pr_err("iSER Hello message\n");
1118 break;
1119 default:
1120 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1121 break;
1122 }
1123
1124 rc = isert_rx_opcode(isert_conn, rx_desc,
1125 read_stag, read_va, write_stag, write_va);
1126}
1127
1128static void
1129isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1130 unsigned long xfer_len)
1131{
1132 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1133 struct iscsi_hdr *hdr;
1134 u64 rx_dma;
1135 int rx_buflen, outstanding;
1136
1137 if ((char *)desc == isert_conn->login_req_buf) {
1138 rx_dma = isert_conn->login_req_dma;
1139 rx_buflen = ISER_RX_LOGIN_SIZE;
1140 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1141 rx_dma, rx_buflen);
1142 } else {
1143 rx_dma = desc->dma_addr;
1144 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1145 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1146 rx_dma, rx_buflen);
1147 }
1148
1149 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1150
1151 hdr = &desc->iscsi_header;
1152 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1153 hdr->opcode, hdr->itt, hdr->flags,
1154 (int)(xfer_len - ISER_HEADERS_LEN));
1155
1156 if ((char *)desc == isert_conn->login_req_buf)
1157 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1158 isert_conn);
1159 else
1160 isert_rx_do_work(desc, isert_conn);
1161
1162 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1163 DMA_FROM_DEVICE);
1164
1165 isert_conn->post_recv_buf_count--;
1166 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1167 isert_conn->post_recv_buf_count);
1168
1169 if ((char *)desc == isert_conn->login_req_buf)
1170 return;
1171
1172 outstanding = isert_conn->post_recv_buf_count;
1173 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1174 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1175 ISERT_MIN_POSTED_RX);
1176 err = isert_post_recv(isert_conn, count);
1177 if (err) {
1178 pr_err("isert_post_recv() count: %d failed, %d\n",
1179 count, err);
1180 }
1181 }
1182}
1183
1184static void
1185isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1186{
1187 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1188 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1189
1190 pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
1191
1192 if (wr->sge) {
1193 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1194 wr->sge = NULL;
1195 }
1196
1197 kfree(wr->send_wr);
1198 wr->send_wr = NULL;
1199
1200 kfree(isert_cmd->ib_sge);
1201 isert_cmd->ib_sge = NULL;
1202}
1203
1204static void
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001205isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001206{
1207 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1208 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001209 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001210
1211 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1212
1213 switch (cmd->iscsi_opcode) {
1214 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001215 spin_lock_bh(&conn->cmd_lock);
1216 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001217 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001218 spin_unlock_bh(&conn->cmd_lock);
1219
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001220 if (cmd->data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001221 iscsit_stop_dataout_timer(cmd);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001222 /*
1223 * Check for special case during comp_err where
1224 * WRITE_PENDING has been handed off from core,
1225 * but requires an extra target_put_sess_cmd()
1226 * before transport_generic_free_cmd() below.
1227 */
1228 if (comp_err &&
1229 cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
1230 struct se_cmd *se_cmd = &cmd->se_cmd;
1231
1232 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1233 }
1234 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001235
1236 isert_unmap_cmd(isert_cmd, isert_conn);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001237 transport_generic_free_cmd(&cmd->se_cmd, 0);
1238 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001239 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001240 spin_lock_bh(&conn->cmd_lock);
1241 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001242 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerc6ccbb92013-07-03 03:11:48 -07001243 spin_unlock_bh(&conn->cmd_lock);
1244
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001245 transport_generic_free_cmd(&cmd->se_cmd, 0);
1246 break;
1247 case ISCSI_OP_REJECT:
1248 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001249 spin_lock_bh(&conn->cmd_lock);
1250 if (!list_empty(&cmd->i_conn_node))
Nicholas Bellingeraf737f62014-02-03 12:53:51 -08001251 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001252 spin_unlock_bh(&conn->cmd_lock);
1253
1254 /*
1255 * Handle special case for REJECT when iscsi_add_reject*() has
1256 * overwritten the original iscsi_opcode assignment, and the
1257 * associated cmd->se_cmd needs to be released.
1258 */
1259 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001260 pr_debug("Calling transport_generic_free_cmd from"
1261 " isert_put_cmd for 0x%02x\n",
1262 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001263 transport_generic_free_cmd(&cmd->se_cmd, 0);
1264 break;
1265 }
1266 /*
1267 * Fall-through
1268 */
1269 default:
1270 isert_release_cmd(cmd);
1271 break;
1272 }
1273}
1274
1275static void
1276isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1277{
1278 if (tx_desc->dma_addr != 0) {
1279 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1280 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1281 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1282 tx_desc->dma_addr = 0;
1283 }
1284}
1285
1286static void
1287isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001288 struct ib_device *ib_dev, bool comp_err)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001289{
1290 if (isert_cmd->sense_buf_dma != 0) {
1291 pr_debug("Calling ib_dma_unmap_single for isert_cmd->sense_buf_dma\n");
1292 ib_dma_unmap_single(ib_dev, isert_cmd->sense_buf_dma,
1293 isert_cmd->sense_buf_len, DMA_TO_DEVICE);
1294 isert_cmd->sense_buf_dma = 0;
1295 }
1296
1297 isert_unmap_tx_desc(tx_desc, ib_dev);
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001298 isert_put_cmd(isert_cmd, comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001299}
1300
1301static void
1302isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1303 struct isert_cmd *isert_cmd)
1304{
1305 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1306 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1307 struct se_cmd *se_cmd = &cmd->se_cmd;
1308 struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
1309
1310 iscsit_stop_dataout_timer(cmd);
1311
1312 if (wr->sge) {
1313 pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
1314 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1315 wr->sge = NULL;
1316 }
1317
1318 if (isert_cmd->ib_sge) {
1319 pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
1320 kfree(isert_cmd->ib_sge);
1321 isert_cmd->ib_sge = NULL;
1322 }
1323
1324 cmd->write_data_done = se_cmd->data_length;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001325 wr->send_wr_num = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001326
1327 pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
1328 spin_lock_bh(&cmd->istate_lock);
1329 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1330 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1331 spin_unlock_bh(&cmd->istate_lock);
1332
1333 target_execute_cmd(se_cmd);
1334}
1335
1336static void
1337isert_do_control_comp(struct work_struct *work)
1338{
1339 struct isert_cmd *isert_cmd = container_of(work,
1340 struct isert_cmd, comp_work);
1341 struct isert_conn *isert_conn = isert_cmd->conn;
1342 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1343 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1344
1345 switch (cmd->i_state) {
1346 case ISTATE_SEND_TASKMGTRSP:
1347 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1348
1349 atomic_dec(&isert_conn->post_send_buf_count);
1350 iscsit_tmr_post_handler(cmd, cmd->conn);
1351
1352 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001353 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001354 break;
1355 case ISTATE_SEND_REJECT:
1356 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1357 atomic_dec(&isert_conn->post_send_buf_count);
1358
1359 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001360 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001361 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001362 case ISTATE_SEND_LOGOUTRSP:
1363 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1364 /*
1365 * Call atomic_dec(&isert_conn->post_send_buf_count)
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001366 * from isert_wait_conn()
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001367 */
1368 isert_conn->logout_posted = true;
1369 iscsit_logout_post_handler(cmd, cmd->conn);
1370 break;
1371 default:
1372 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1373 dump_stack();
1374 break;
1375 }
1376}
1377
1378static void
1379isert_response_completion(struct iser_tx_desc *tx_desc,
1380 struct isert_cmd *isert_cmd,
1381 struct isert_conn *isert_conn,
1382 struct ib_device *ib_dev)
1383{
1384 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001385 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001386
1387 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001388 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
1389 cmd->i_state == ISTATE_SEND_REJECT) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001390 isert_unmap_tx_desc(tx_desc, ib_dev);
1391
1392 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1393 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1394 return;
1395 }
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001396 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001397
1398 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001399 isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001400}
1401
1402static void
1403isert_send_completion(struct iser_tx_desc *tx_desc,
1404 struct isert_conn *isert_conn)
1405{
1406 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1407 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1408 struct isert_rdma_wr *wr;
1409
1410 if (!isert_cmd) {
1411 atomic_dec(&isert_conn->post_send_buf_count);
1412 isert_unmap_tx_desc(tx_desc, ib_dev);
1413 return;
1414 }
1415 wr = &isert_cmd->rdma_wr;
1416
1417 switch (wr->iser_ib_op) {
1418 case ISER_IB_RECV:
1419 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1420 dump_stack();
1421 break;
1422 case ISER_IB_SEND:
1423 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1424 isert_response_completion(tx_desc, isert_cmd,
1425 isert_conn, ib_dev);
1426 break;
1427 case ISER_IB_RDMA_WRITE:
1428 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1429 dump_stack();
1430 break;
1431 case ISER_IB_RDMA_READ:
1432 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1433
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001434 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001435 isert_completion_rdma_read(tx_desc, isert_cmd);
1436 break;
1437 default:
1438 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1439 dump_stack();
1440 break;
1441 }
1442}
1443
1444static void
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001445isert_cq_tx_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001446{
1447 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001448 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001449
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001450 if (!isert_cmd)
1451 isert_unmap_tx_desc(tx_desc, ib_dev);
1452 else
Nicholas Bellingerd3066062014-03-30 15:50:03 -07001453 isert_completion_put(tx_desc, isert_cmd, ib_dev, true);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001454}
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001455
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001456static void
1457isert_cq_rx_comp_err(struct isert_conn *isert_conn)
1458{
1459 struct iscsi_conn *conn = isert_conn->conn;
1460
1461 if (isert_conn->post_recv_buf_count)
1462 return;
1463
1464 if (conn->sess) {
1465 target_sess_cmd_list_set_waiting(conn->sess->se_sess);
1466 target_wait_for_sess_cmds(conn->sess->se_sess);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001467 }
1468
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001469 while (atomic_read(&isert_conn->post_send_buf_count))
1470 msleep(3000);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001471
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001472 mutex_lock(&isert_conn->conn_mutex);
1473 isert_conn->state = ISER_CONN_DOWN;
1474 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07001475
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001476 complete(&isert_conn->conn_wait_comp_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001477}
1478
1479static void
1480isert_cq_tx_work(struct work_struct *work)
1481{
1482 struct isert_cq_desc *cq_desc = container_of(work,
1483 struct isert_cq_desc, cq_tx_work);
1484 struct isert_device *device = cq_desc->device;
1485 int cq_index = cq_desc->cq_index;
1486 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1487 struct isert_conn *isert_conn;
1488 struct iser_tx_desc *tx_desc;
1489 struct ib_wc wc;
1490
1491 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1492 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1493 isert_conn = wc.qp->qp_context;
1494
1495 if (wc.status == IB_WC_SUCCESS) {
1496 isert_send_completion(tx_desc, isert_conn);
1497 } else {
1498 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1499 pr_debug("TX wc.status: 0x%08x\n", wc.status);
1500 atomic_dec(&isert_conn->post_send_buf_count);
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001501 isert_cq_tx_comp_err(tx_desc, isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001502 }
1503 }
1504
1505 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1506}
1507
1508static void
1509isert_cq_tx_callback(struct ib_cq *cq, void *context)
1510{
1511 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1512
1513 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1514 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1515}
1516
1517static void
1518isert_cq_rx_work(struct work_struct *work)
1519{
1520 struct isert_cq_desc *cq_desc = container_of(work,
1521 struct isert_cq_desc, cq_rx_work);
1522 struct isert_device *device = cq_desc->device;
1523 int cq_index = cq_desc->cq_index;
1524 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1525 struct isert_conn *isert_conn;
1526 struct iser_rx_desc *rx_desc;
1527 struct ib_wc wc;
1528 unsigned long xfer_len;
1529
1530 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1531 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1532 isert_conn = wc.qp->qp_context;
1533
1534 if (wc.status == IB_WC_SUCCESS) {
1535 xfer_len = (unsigned long)wc.byte_len;
1536 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1537 } else {
1538 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1539 if (wc.status != IB_WC_WR_FLUSH_ERR)
1540 pr_debug("RX wc.status: 0x%08x\n", wc.status);
1541
1542 isert_conn->post_recv_buf_count--;
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08001543 isert_cq_rx_comp_err(isert_conn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001544 }
1545 }
1546
1547 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1548}
1549
1550static void
1551isert_cq_rx_callback(struct ib_cq *cq, void *context)
1552{
1553 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1554
1555 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1556 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1557}
1558
1559static int
1560isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1561{
1562 struct ib_send_wr *wr_failed;
1563 int ret;
1564
1565 atomic_inc(&isert_conn->post_send_buf_count);
1566
1567 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1568 &wr_failed);
1569 if (ret) {
1570 pr_err("ib_post_send failed with %d\n", ret);
1571 atomic_dec(&isert_conn->post_send_buf_count);
1572 return ret;
1573 }
1574 return ret;
1575}
1576
1577static int
1578isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1579{
1580 struct isert_cmd *isert_cmd = container_of(cmd,
1581 struct isert_cmd, iscsi_cmd);
1582 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1583 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1584 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1585 &isert_cmd->tx_desc.iscsi_header;
1586
1587 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1588 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1589 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1590 /*
1591 * Attach SENSE DATA payload to iSCSI Response PDU
1592 */
1593 if (cmd->se_cmd.sense_buffer &&
1594 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1595 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1596 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1597 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1598 u32 padding, sense_len;
1599
1600 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1601 cmd->sense_buffer);
1602 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1603
1604 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1605 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
1606 sense_len = cmd->se_cmd.scsi_sense_length + padding;
1607
1608 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1609 (void *)cmd->sense_buffer, sense_len,
1610 DMA_TO_DEVICE);
1611
1612 isert_cmd->sense_buf_len = sense_len;
1613 tx_dsg->addr = isert_cmd->sense_buf_dma;
1614 tx_dsg->length = sense_len;
1615 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1616 isert_cmd->tx_desc.num_sge = 2;
1617 }
1618
1619 isert_init_send_wr(isert_cmd, send_wr);
1620
1621 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1622
1623 return isert_post_response(isert_conn, isert_cmd);
1624}
1625
1626static int
1627isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1628 bool nopout_response)
1629{
1630 struct isert_cmd *isert_cmd = container_of(cmd,
1631 struct isert_cmd, iscsi_cmd);
1632 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1633 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1634
1635 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1636 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1637 &isert_cmd->tx_desc.iscsi_header,
1638 nopout_response);
1639 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1640 isert_init_send_wr(isert_cmd, send_wr);
1641
1642 pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1643
1644 return isert_post_response(isert_conn, isert_cmd);
1645}
1646
1647static int
1648isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1649{
1650 struct isert_cmd *isert_cmd = container_of(cmd,
1651 struct isert_cmd, iscsi_cmd);
1652 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1653 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1654
1655 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1656 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1657 &isert_cmd->tx_desc.iscsi_header);
1658 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1659 isert_init_send_wr(isert_cmd, send_wr);
1660
1661 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1662
1663 return isert_post_response(isert_conn, isert_cmd);
1664}
1665
1666static int
1667isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1668{
1669 struct isert_cmd *isert_cmd = container_of(cmd,
1670 struct isert_cmd, iscsi_cmd);
1671 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1672 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1673
1674 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1675 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1676 &isert_cmd->tx_desc.iscsi_header);
1677 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1678 isert_init_send_wr(isert_cmd, send_wr);
1679
1680 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1681
1682 return isert_post_response(isert_conn, isert_cmd);
1683}
1684
1685static int
1686isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1687{
1688 struct isert_cmd *isert_cmd = container_of(cmd,
1689 struct isert_cmd, iscsi_cmd);
1690 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1691 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001692 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1693 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1694 struct iscsi_reject *hdr =
1695 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001696
1697 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001698 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001699 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellingerfff98872013-06-26 02:31:42 -07001700
1701 hton24(hdr->dlength, ISCSI_HDR_LEN);
1702 isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
1703 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1704 DMA_TO_DEVICE);
1705 isert_cmd->sense_buf_len = ISCSI_HDR_LEN;
1706 tx_dsg->addr = isert_cmd->sense_buf_dma;
1707 tx_dsg->length = ISCSI_HDR_LEN;
1708 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1709 isert_cmd->tx_desc.num_sge = 2;
1710
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001711 isert_init_send_wr(isert_cmd, send_wr);
1712
1713 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1714
1715 return isert_post_response(isert_conn, isert_cmd);
1716}
1717
1718static int
1719isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1720 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1721 u32 data_left, u32 offset)
1722{
1723 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1724 struct scatterlist *sg_start, *tmp_sg;
1725 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1726 u32 sg_off, page_off;
1727 int i = 0, sg_nents;
1728
1729 sg_off = offset / PAGE_SIZE;
1730 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1731 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1732 page_off = offset % PAGE_SIZE;
1733
1734 send_wr->sg_list = ib_sge;
1735 send_wr->num_sge = sg_nents;
1736 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1737 /*
1738 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1739 */
1740 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1741 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1742 (unsigned long long)tmp_sg->dma_address,
1743 tmp_sg->length, page_off);
1744
1745 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1746 ib_sge->length = min_t(u32, data_left,
1747 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1748 ib_sge->lkey = isert_conn->conn_mr->lkey;
1749
1750 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
1751 ib_sge->addr, ib_sge->length);
1752 page_off = 0;
1753 data_left -= ib_sge->length;
1754 ib_sge++;
1755 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1756 }
1757
1758 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1759 send_wr->sg_list, send_wr->num_sge);
1760
1761 return sg_nents;
1762}
1763
1764static int
1765isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1766{
1767 struct se_cmd *se_cmd = &cmd->se_cmd;
1768 struct isert_cmd *isert_cmd = container_of(cmd,
1769 struct isert_cmd, iscsi_cmd);
1770 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1771 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1772 struct ib_send_wr *wr_failed, *send_wr;
1773 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1774 struct ib_sge *ib_sge;
1775 struct scatterlist *sg;
1776 u32 offset = 0, data_len, data_left, rdma_write_max;
1777 int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
1778
1779 pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
1780
1781 sg = &se_cmd->t_data_sg[0];
1782 sg_nents = se_cmd->t_data_nents;
1783
1784 count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1785 if (unlikely(!count)) {
1786 pr_err("Unable to map put_datain SGs\n");
1787 return -EINVAL;
1788 }
1789 wr->sge = sg;
1790 wr->num_sge = sg_nents;
1791 pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
1792 count, sg, sg_nents);
1793
1794 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1795 if (!ib_sge) {
1796 pr_warn("Unable to allocate datain ib_sge\n");
1797 ret = -ENOMEM;
1798 goto unmap_sg;
1799 }
1800 isert_cmd->ib_sge = ib_sge;
1801
1802 pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
1803 ib_sge, se_cmd->t_data_nents);
1804
1805 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1806 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1807 GFP_KERNEL);
1808 if (!wr->send_wr) {
1809 pr_err("Unable to allocate wr->send_wr\n");
1810 ret = -ENOMEM;
1811 goto unmap_sg;
1812 }
1813 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1814 wr->send_wr, wr->send_wr_num);
1815
1816 iscsit_increment_maxcmdsn(cmd, conn->sess);
1817 cmd->stat_sn = conn->stat_sn++;
1818
1819 wr->isert_cmd = isert_cmd;
1820 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1821 data_left = se_cmd->data_length;
1822
1823 for (i = 0; i < wr->send_wr_num; i++) {
1824 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1825 data_len = min(data_left, rdma_write_max);
1826
1827 send_wr->opcode = IB_WR_RDMA_WRITE;
1828 send_wr->send_flags = 0;
1829 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1830 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1831
1832 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1833 send_wr, data_len, offset);
1834 ib_sge += ib_sge_cnt;
1835
1836 if (i + 1 == wr->send_wr_num)
1837 send_wr->next = &isert_cmd->tx_desc.send_wr;
1838 else
1839 send_wr->next = &wr->send_wr[i + 1];
1840
1841 offset += data_len;
1842 data_left -= data_len;
1843 }
1844 /*
1845 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1846 */
1847 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1848 iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1849 &isert_cmd->tx_desc.iscsi_header);
1850 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1851 isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1852
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001853 atomic_add(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001854
1855 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1856 if (rc) {
1857 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001858 atomic_sub(wr->send_wr_num + 1, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001859 }
1860 pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
1861 return 1;
1862
1863unmap_sg:
1864 ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1865 return ret;
1866}
1867
1868static int
1869isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1870{
1871 struct se_cmd *se_cmd = &cmd->se_cmd;
1872 struct isert_cmd *isert_cmd = container_of(cmd,
1873 struct isert_cmd, iscsi_cmd);
1874 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1875 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1876 struct ib_send_wr *wr_failed, *send_wr;
1877 struct ib_sge *ib_sge;
1878 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1879 struct scatterlist *sg_start;
1880 u32 sg_off, sg_nents, page_off, va_offset = 0;
1881 u32 offset = 0, data_len, data_left, rdma_write_max;
1882 int rc, ret = 0, count, i, ib_sge_cnt;
1883
1884 pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
1885 se_cmd->data_length, cmd->write_data_done);
1886
1887 sg_off = cmd->write_data_done / PAGE_SIZE;
1888 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1889 page_off = cmd->write_data_done % PAGE_SIZE;
1890
1891 pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
1892 sg_off, sg_start, page_off);
1893
1894 data_left = se_cmd->data_length - cmd->write_data_done;
1895 sg_nents = se_cmd->t_data_nents - sg_off;
1896
1897 pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
1898 data_left, sg_nents);
1899
1900 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1901 if (unlikely(!count)) {
1902 pr_err("Unable to map get_dataout SGs\n");
1903 return -EINVAL;
1904 }
1905 wr->sge = sg_start;
1906 wr->num_sge = sg_nents;
1907 pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
1908 count, sg_start, sg_nents);
1909
1910 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1911 if (!ib_sge) {
1912 pr_warn("Unable to allocate dataout ib_sge\n");
1913 ret = -ENOMEM;
1914 goto unmap_sg;
1915 }
1916 isert_cmd->ib_sge = ib_sge;
1917
1918 pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
1919 ib_sge, sg_nents);
1920
1921 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1922 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1923 GFP_KERNEL);
1924 if (!wr->send_wr) {
1925 pr_debug("Unable to allocate wr->send_wr\n");
1926 ret = -ENOMEM;
1927 goto unmap_sg;
1928 }
1929 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1930 wr->send_wr, wr->send_wr_num);
1931
1932 isert_cmd->tx_desc.isert_cmd = isert_cmd;
1933
1934 wr->iser_ib_op = ISER_IB_RDMA_READ;
1935 wr->isert_cmd = isert_cmd;
1936 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1937 offset = cmd->write_data_done;
1938
1939 for (i = 0; i < wr->send_wr_num; i++) {
1940 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1941 data_len = min(data_left, rdma_write_max);
1942
1943 send_wr->opcode = IB_WR_RDMA_READ;
1944 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
1945 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
1946
1947 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1948 send_wr, data_len, offset);
1949 ib_sge += ib_sge_cnt;
1950
1951 if (i + 1 == wr->send_wr_num)
1952 send_wr->send_flags = IB_SEND_SIGNALED;
1953 else
1954 send_wr->next = &wr->send_wr[i + 1];
1955
1956 offset += data_len;
1957 va_offset += data_len;
1958 data_left -= data_len;
1959 }
1960
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001961 atomic_add(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001962
1963 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1964 if (rc) {
1965 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
Nicholas Bellinger0bf95492014-02-27 09:05:03 -08001966 atomic_sub(wr->send_wr_num, &isert_conn->post_send_buf_count);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001967 }
1968 pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
1969 return 0;
1970
1971unmap_sg:
1972 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1973 return ret;
1974}
1975
1976static int
1977isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1978{
1979 int ret;
1980
1981 switch (state) {
1982 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
1983 ret = isert_put_nopin(cmd, conn, false);
1984 break;
1985 default:
1986 pr_err("Unknown immediate state: 0x%02x\n", state);
1987 ret = -EINVAL;
1988 break;
1989 }
1990
1991 return ret;
1992}
1993
1994static int
1995isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1996{
1997 int ret;
1998
1999 switch (state) {
2000 case ISTATE_SEND_LOGOUTRSP:
2001 ret = isert_put_logout_rsp(cmd, conn);
2002 if (!ret) {
2003 pr_debug("Returning iSER Logout -EAGAIN\n");
2004 ret = -EAGAIN;
2005 }
2006 break;
2007 case ISTATE_SEND_NOPIN:
2008 ret = isert_put_nopin(cmd, conn, true);
2009 break;
2010 case ISTATE_SEND_TASKMGTRSP:
2011 ret = isert_put_tm_rsp(cmd, conn);
2012 break;
2013 case ISTATE_SEND_REJECT:
2014 ret = isert_put_reject(cmd, conn);
2015 break;
2016 case ISTATE_SEND_STATUS:
2017 /*
2018 * Special case for sending non GOOD SCSI status from TX thread
2019 * context during pre se_cmd excecution failure.
2020 */
2021 ret = isert_put_response(conn, cmd);
2022 break;
2023 default:
2024 pr_err("Unknown response state: 0x%02x\n", state);
2025 ret = -EINVAL;
2026 break;
2027 }
2028
2029 return ret;
2030}
2031
2032static int
2033isert_setup_np(struct iscsi_np *np,
2034 struct __kernel_sockaddr_storage *ksockaddr)
2035{
2036 struct isert_np *isert_np;
2037 struct rdma_cm_id *isert_lid;
2038 struct sockaddr *sa;
2039 int ret;
2040
2041 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2042 if (!isert_np) {
2043 pr_err("Unable to allocate struct isert_np\n");
2044 return -ENOMEM;
2045 }
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002046 sema_init(&isert_np->np_sem, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002047 mutex_init(&isert_np->np_accept_mutex);
2048 INIT_LIST_HEAD(&isert_np->np_accept_list);
2049 init_completion(&isert_np->np_login_comp);
2050
2051 sa = (struct sockaddr *)ksockaddr;
2052 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2053 /*
2054 * Setup the np->np_sockaddr from the passed sockaddr setup
2055 * in iscsi_target_configfs.c code..
2056 */
2057 memcpy(&np->np_sockaddr, ksockaddr,
2058 sizeof(struct __kernel_sockaddr_storage));
2059
2060 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2061 IB_QPT_RC);
2062 if (IS_ERR(isert_lid)) {
2063 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2064 PTR_ERR(isert_lid));
2065 ret = PTR_ERR(isert_lid);
2066 goto out;
2067 }
2068
2069 ret = rdma_bind_addr(isert_lid, sa);
2070 if (ret) {
2071 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2072 goto out_lid;
2073 }
2074
2075 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2076 if (ret) {
2077 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2078 goto out_lid;
2079 }
2080
2081 isert_np->np_cm_id = isert_lid;
2082 np->np_context = isert_np;
2083 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2084
2085 return 0;
2086
2087out_lid:
2088 rdma_destroy_id(isert_lid);
2089out:
2090 kfree(isert_np);
2091 return ret;
2092}
2093
2094static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002095isert_rdma_accept(struct isert_conn *isert_conn)
2096{
2097 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2098 struct rdma_conn_param cp;
2099 int ret;
2100
2101 memset(&cp, 0, sizeof(struct rdma_conn_param));
2102 cp.responder_resources = isert_conn->responder_resources;
2103 cp.initiator_depth = isert_conn->initiator_depth;
2104 cp.retry_count = 7;
2105 cp.rnr_retry_count = 7;
2106
2107 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2108
2109 ret = rdma_accept(cm_id, &cp);
2110 if (ret) {
2111 pr_err("rdma_accept() failed with: %d\n", ret);
2112 return ret;
2113 }
2114
2115 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2116
2117 return 0;
2118}
2119
2120static int
2121isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2122{
2123 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2124 int ret;
2125
2126 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2127
2128 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2129 if (ret)
2130 return ret;
2131
2132 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2133 return 0;
2134}
2135
2136static void
2137isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2138 struct isert_conn *isert_conn)
2139{
2140 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2141 struct rdma_route *cm_route = &cm_id->route;
2142 struct sockaddr_in *sock_in;
2143 struct sockaddr_in6 *sock_in6;
2144
2145 conn->login_family = np->np_sockaddr.ss_family;
2146
2147 if (np->np_sockaddr.ss_family == AF_INET6) {
2148 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2149 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2150 &sock_in6->sin6_addr.in6_u);
2151 conn->login_port = ntohs(sock_in6->sin6_port);
2152
2153 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2154 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2155 &sock_in6->sin6_addr.in6_u);
2156 conn->local_port = ntohs(sock_in6->sin6_port);
2157 } else {
2158 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2159 sprintf(conn->login_ip, "%pI4",
2160 &sock_in->sin_addr.s_addr);
2161 conn->login_port = ntohs(sock_in->sin_port);
2162
2163 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2164 sprintf(conn->local_ip, "%pI4",
2165 &sock_in->sin_addr.s_addr);
2166 conn->local_port = ntohs(sock_in->sin_port);
2167 }
2168}
2169
2170static int
2171isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2172{
2173 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2174 struct isert_conn *isert_conn;
2175 int max_accept = 0, ret;
2176
2177accept_wait:
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002178 ret = down_interruptible(&isert_np->np_sem);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179 if (max_accept > 5)
2180 return -ENODEV;
2181
2182 spin_lock_bh(&np->np_thread_lock);
2183 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2184 spin_unlock_bh(&np->np_thread_lock);
Sagi Grimberg8a2629a2014-04-29 13:13:45 +03002185 pr_debug("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002186 return -ENODEV;
2187 }
2188 spin_unlock_bh(&np->np_thread_lock);
2189
2190 mutex_lock(&isert_np->np_accept_mutex);
2191 if (list_empty(&isert_np->np_accept_list)) {
2192 mutex_unlock(&isert_np->np_accept_mutex);
2193 max_accept++;
2194 goto accept_wait;
2195 }
2196 isert_conn = list_first_entry(&isert_np->np_accept_list,
2197 struct isert_conn, conn_accept_node);
2198 list_del_init(&isert_conn->conn_accept_node);
2199 mutex_unlock(&isert_np->np_accept_mutex);
2200
2201 conn->context = isert_conn;
2202 isert_conn->conn = conn;
2203 max_accept = 0;
2204
2205 ret = isert_rdma_post_recvl(isert_conn);
2206 if (ret)
2207 return ret;
2208
2209 ret = isert_rdma_accept(isert_conn);
2210 if (ret)
2211 return ret;
2212
2213 isert_set_conn_info(np, conn, isert_conn);
2214
2215 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2216 return 0;
2217}
2218
2219static void
2220isert_free_np(struct iscsi_np *np)
2221{
2222 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2223
2224 rdma_destroy_id(isert_np->np_cm_id);
2225
2226 np->np_context = NULL;
2227 kfree(isert_np);
2228}
2229
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002230static void isert_wait_conn(struct iscsi_conn *conn)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002231{
2232 struct isert_conn *isert_conn = conn->context;
2233
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002234 pr_debug("isert_wait_conn: Starting \n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002235 /*
2236 * Decrement post_send_buf_count for special case when called
2237 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2238 */
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002239 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002240 if (isert_conn->logout_posted)
2241 atomic_dec(&isert_conn->post_send_buf_count);
2242
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002243 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002244 pr_debug("Calling rdma_disconnect from isert_wait_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002245 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002246 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002247 /*
2248 * Only wait for conn_wait_comp_err if the isert_conn made it
2249 * into full feature phase..
2250 */
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002251 if (isert_conn->state == ISER_CONN_INIT) {
2252 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002253 return;
2254 }
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002255 if (isert_conn->state == ISER_CONN_UP)
2256 isert_conn->state = ISER_CONN_TERMINATING;
Nicholas Bellingerd9e507c2013-07-03 03:05:37 -07002257 mutex_unlock(&isert_conn->conn_mutex);
2258
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002259 wait_for_completion(&isert_conn->conn_wait_comp_err);
2260
2261 wait_for_completion(&isert_conn->conn_wait);
2262}
2263
2264static void isert_free_conn(struct iscsi_conn *conn)
2265{
2266 struct isert_conn *isert_conn = conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002267
2268 isert_put_conn(isert_conn);
2269}
2270
2271static struct iscsit_transport iser_target_transport = {
2272 .name = "IB/iSER",
2273 .transport_type = ISCSI_INFINIBAND,
2274 .owner = THIS_MODULE,
2275 .iscsit_setup_np = isert_setup_np,
2276 .iscsit_accept_np = isert_accept_np,
2277 .iscsit_free_np = isert_free_np,
Nicholas Bellingerd8bd97a02014-02-03 12:54:39 -08002278 .iscsit_wait_conn = isert_wait_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002279 .iscsit_free_conn = isert_free_conn,
2280 .iscsit_alloc_cmd = isert_alloc_cmd,
2281 .iscsit_get_login_rx = isert_get_login_rx,
2282 .iscsit_put_login_tx = isert_put_login_tx,
2283 .iscsit_immediate_queue = isert_immediate_queue,
2284 .iscsit_response_queue = isert_response_queue,
2285 .iscsit_get_dataout = isert_get_dataout,
2286 .iscsit_queue_data_in = isert_put_datain,
2287 .iscsit_queue_status = isert_put_response,
2288};
2289
2290static int __init isert_init(void)
2291{
2292 int ret;
2293
2294 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2295 if (!isert_rx_wq) {
2296 pr_err("Unable to allocate isert_rx_wq\n");
2297 return -ENOMEM;
2298 }
2299
2300 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2301 if (!isert_comp_wq) {
2302 pr_err("Unable to allocate isert_comp_wq\n");
2303 ret = -ENOMEM;
2304 goto destroy_rx_wq;
2305 }
2306
2307 isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
2308 sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
2309 0, NULL);
2310 if (!isert_cmd_cache) {
2311 pr_err("Unable to create isert_cmd_cache\n");
2312 ret = -ENOMEM;
2313 goto destroy_tx_cq;
2314 }
2315
2316 iscsit_register_transport(&iser_target_transport);
2317 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2318 return 0;
2319
2320destroy_tx_cq:
2321 destroy_workqueue(isert_comp_wq);
2322destroy_rx_wq:
2323 destroy_workqueue(isert_rx_wq);
2324 return ret;
2325}
2326
2327static void __exit isert_exit(void)
2328{
2329 kmem_cache_destroy(isert_cmd_cache);
2330 destroy_workqueue(isert_comp_wq);
2331 destroy_workqueue(isert_rx_wq);
2332 iscsit_unregister_transport(&iser_target_transport);
2333 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2334}
2335
2336MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2337MODULE_VERSION("0.1");
2338MODULE_AUTHOR("nab@Linux-iSCSI.org");
2339MODULE_LICENSE("GPL");
2340
2341module_init(isert_init);
2342module_exit(isert_exit);