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