blob: c48c9481025c98c8b7a9a6bc36c44289215d40c4 [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);
391
392 cma_id->context = isert_conn;
393 isert_conn->conn_cm_id = cma_id;
394 isert_conn->responder_resources = event->param.conn.responder_resources;
395 isert_conn->initiator_depth = event->param.conn.initiator_depth;
396 pr_debug("Using responder_resources: %u initiator_depth: %u\n",
397 isert_conn->responder_resources, isert_conn->initiator_depth);
398
399 isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
400 ISER_RX_LOGIN_SIZE, GFP_KERNEL);
401 if (!isert_conn->login_buf) {
402 pr_err("Unable to allocate isert_conn->login_buf\n");
403 ret = -ENOMEM;
404 goto out;
405 }
406
407 isert_conn->login_req_buf = isert_conn->login_buf;
408 isert_conn->login_rsp_buf = isert_conn->login_buf +
409 ISCSI_DEF_MAX_RECV_SEG_LEN;
410 pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
411 isert_conn->login_buf, isert_conn->login_req_buf,
412 isert_conn->login_rsp_buf);
413
414 isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
415 (void *)isert_conn->login_req_buf,
416 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
417
418 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
419 if (ret) {
420 pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
421 ret);
422 isert_conn->login_req_dma = 0;
423 goto out_login_buf;
424 }
425
426 isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
427 (void *)isert_conn->login_rsp_buf,
428 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
429
430 ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
431 if (ret) {
432 pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
433 ret);
434 isert_conn->login_rsp_dma = 0;
435 goto out_req_dma_map;
436 }
437
438 device = isert_device_find_by_ib_dev(cma_id);
439 if (IS_ERR(device)) {
440 ret = PTR_ERR(device);
441 goto out_rsp_dma_map;
442 }
443
444 isert_conn->conn_device = device;
445 isert_conn->conn_pd = device->dev_pd;
446 isert_conn->conn_mr = device->dev_mr;
447
448 ret = isert_conn_setup_qp(isert_conn, cma_id);
449 if (ret)
450 goto out_conn_dev;
451
452 mutex_lock(&isert_np->np_accept_mutex);
453 list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
454 mutex_unlock(&isert_np->np_accept_mutex);
455
456 pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
457 wake_up(&isert_np->np_accept_wq);
458 return 0;
459
460out_conn_dev:
461 isert_device_try_release(device);
462out_rsp_dma_map:
463 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
464 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
465out_req_dma_map:
466 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
467 ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
468out_login_buf:
469 kfree(isert_conn->login_buf);
470out:
471 kfree(isert_conn);
472 return ret;
473}
474
475static void
476isert_connect_release(struct isert_conn *isert_conn)
477{
478 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
479 struct isert_device *device = isert_conn->conn_device;
480 int cq_index;
481
482 pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
483
484 if (isert_conn->conn_qp) {
485 cq_index = ((struct isert_cq_desc *)
486 isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
487 pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
488 isert_conn->conn_device->cq_active_qps[cq_index]--;
489
490 rdma_destroy_qp(isert_conn->conn_cm_id);
491 }
492
493 isert_free_rx_descriptors(isert_conn);
494 rdma_destroy_id(isert_conn->conn_cm_id);
495
496 if (isert_conn->login_buf) {
497 ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
498 ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
499 ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
500 ISCSI_DEF_MAX_RECV_SEG_LEN,
501 DMA_FROM_DEVICE);
502 kfree(isert_conn->login_buf);
503 }
504 kfree(isert_conn);
505
506 if (device)
507 isert_device_try_release(device);
508
509 pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
510}
511
512static void
513isert_connected_handler(struct rdma_cm_id *cma_id)
514{
515 return;
516}
517
518static void
519isert_release_conn_kref(struct kref *kref)
520{
521 struct isert_conn *isert_conn = container_of(kref,
522 struct isert_conn, conn_kref);
523
524 pr_debug("Calling isert_connect_release for final kref %s/%d\n",
525 current->comm, current->pid);
526
527 isert_connect_release(isert_conn);
528}
529
530static void
531isert_put_conn(struct isert_conn *isert_conn)
532{
533 kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
534}
535
536static void
537isert_disconnect_work(struct work_struct *work)
538{
539 struct isert_conn *isert_conn = container_of(work,
540 struct isert_conn, conn_logout_work);
541
542 pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
543
544 isert_conn->state = ISER_CONN_DOWN;
545
546 if (isert_conn->post_recv_buf_count == 0 &&
547 atomic_read(&isert_conn->post_send_buf_count) == 0) {
548 pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
549 wake_up(&isert_conn->conn_wait);
550 }
551
552 isert_put_conn(isert_conn);
553}
554
555static void
556isert_disconnected_handler(struct rdma_cm_id *cma_id)
557{
558 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
559
560 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
561 schedule_work(&isert_conn->conn_logout_work);
562}
563
564static int
565isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
566{
567 int ret = 0;
568
569 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
570 event->event, event->status, cma_id->context, cma_id);
571
572 switch (event->event) {
573 case RDMA_CM_EVENT_CONNECT_REQUEST:
574 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
575 ret = isert_connect_request(cma_id, event);
576 break;
577 case RDMA_CM_EVENT_ESTABLISHED:
578 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
579 isert_connected_handler(cma_id);
580 break;
581 case RDMA_CM_EVENT_DISCONNECTED:
582 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
583 isert_disconnected_handler(cma_id);
584 break;
585 case RDMA_CM_EVENT_DEVICE_REMOVAL:
586 case RDMA_CM_EVENT_ADDR_CHANGE:
587 break;
588 case RDMA_CM_EVENT_CONNECT_ERROR:
589 default:
590 pr_err("Unknown RDMA CMA event: %d\n", event->event);
591 break;
592 }
593
594 if (ret != 0) {
595 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
596 event->event, ret);
597 dump_stack();
598 }
599
600 return ret;
601}
602
603static int
604isert_post_recv(struct isert_conn *isert_conn, u32 count)
605{
606 struct ib_recv_wr *rx_wr, *rx_wr_failed;
607 int i, ret;
608 unsigned int rx_head = isert_conn->conn_rx_desc_head;
609 struct iser_rx_desc *rx_desc;
610
611 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
612 rx_desc = &isert_conn->conn_rx_descs[rx_head];
613 rx_wr->wr_id = (unsigned long)rx_desc;
614 rx_wr->sg_list = &rx_desc->rx_sg;
615 rx_wr->num_sge = 1;
616 rx_wr->next = rx_wr + 1;
617 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
618 }
619
620 rx_wr--;
621 rx_wr->next = NULL; /* mark end of work requests list */
622
623 isert_conn->post_recv_buf_count += count;
624 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
625 &rx_wr_failed);
626 if (ret) {
627 pr_err("ib_post_recv() failed with ret: %d\n", ret);
628 isert_conn->post_recv_buf_count -= count;
629 } else {
630 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
631 isert_conn->conn_rx_desc_head = rx_head;
632 }
633 return ret;
634}
635
636static int
637isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
638{
639 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
640 struct ib_send_wr send_wr, *send_wr_failed;
641 int ret;
642
643 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
644 ISER_HEADERS_LEN, DMA_TO_DEVICE);
645
646 send_wr.next = NULL;
647 send_wr.wr_id = (unsigned long)tx_desc;
648 send_wr.sg_list = tx_desc->tx_sg;
649 send_wr.num_sge = tx_desc->num_sge;
650 send_wr.opcode = IB_WR_SEND;
651 send_wr.send_flags = IB_SEND_SIGNALED;
652
653 atomic_inc(&isert_conn->post_send_buf_count);
654
655 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
656 if (ret) {
657 pr_err("ib_post_send() failed, ret: %d\n", ret);
658 atomic_dec(&isert_conn->post_send_buf_count);
659 }
660
661 return ret;
662}
663
664static void
665isert_create_send_desc(struct isert_conn *isert_conn,
666 struct isert_cmd *isert_cmd,
667 struct iser_tx_desc *tx_desc)
668{
669 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
670
671 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
672 ISER_HEADERS_LEN, DMA_TO_DEVICE);
673
674 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
675 tx_desc->iser_header.flags = ISER_VER;
676
677 tx_desc->num_sge = 1;
678 tx_desc->isert_cmd = isert_cmd;
679
680 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
681 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
682 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
683 }
684}
685
686static int
687isert_init_tx_hdrs(struct isert_conn *isert_conn,
688 struct iser_tx_desc *tx_desc)
689{
690 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
691 u64 dma_addr;
692
693 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
694 ISER_HEADERS_LEN, DMA_TO_DEVICE);
695 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
696 pr_err("ib_dma_mapping_error() failed\n");
697 return -ENOMEM;
698 }
699
700 tx_desc->dma_addr = dma_addr;
701 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
702 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
703 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
704
705 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
706 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
707 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
708
709 return 0;
710}
711
712static void
713isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
714{
715 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
716 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
717 send_wr->opcode = IB_WR_SEND;
718 send_wr->send_flags = IB_SEND_SIGNALED;
719 send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
720 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
721}
722
723static int
724isert_rdma_post_recvl(struct isert_conn *isert_conn)
725{
726 struct ib_recv_wr rx_wr, *rx_wr_fail;
727 struct ib_sge sge;
728 int ret;
729
730 memset(&sge, 0, sizeof(struct ib_sge));
731 sge.addr = isert_conn->login_req_dma;
732 sge.length = ISER_RX_LOGIN_SIZE;
733 sge.lkey = isert_conn->conn_mr->lkey;
734
735 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
736 sge.addr, sge.length, sge.lkey);
737
738 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
739 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
740 rx_wr.sg_list = &sge;
741 rx_wr.num_sge = 1;
742
743 isert_conn->post_recv_buf_count++;
744 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
745 if (ret) {
746 pr_err("ib_post_recv() failed: %d\n", ret);
747 isert_conn->post_recv_buf_count--;
748 }
749
750 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
751 return ret;
752}
753
754static int
755isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
756 u32 length)
757{
758 struct isert_conn *isert_conn = conn->context;
759 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
760 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
761 int ret;
762
763 isert_create_send_desc(isert_conn, NULL, tx_desc);
764
765 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
766 sizeof(struct iscsi_hdr));
767
768 isert_init_tx_hdrs(isert_conn, tx_desc);
769
770 if (length > 0) {
771 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
772
773 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
774 length, DMA_TO_DEVICE);
775
776 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
777
778 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
779 length, DMA_TO_DEVICE);
780
781 tx_dsg->addr = isert_conn->login_rsp_dma;
782 tx_dsg->length = length;
783 tx_dsg->lkey = isert_conn->conn_mr->lkey;
784 tx_desc->num_sge = 2;
785 }
786 if (!login->login_failed) {
787 if (login->login_complete) {
788 ret = isert_alloc_rx_descriptors(isert_conn);
789 if (ret)
790 return ret;
791
792 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
793 if (ret)
794 return ret;
795
796 isert_conn->state = ISER_CONN_UP;
797 goto post_send;
798 }
799
800 ret = isert_rdma_post_recvl(isert_conn);
801 if (ret)
802 return ret;
803 }
804post_send:
805 ret = isert_post_send(isert_conn, tx_desc);
806 if (ret)
807 return ret;
808
809 return 0;
810}
811
812static void
813isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
814 struct isert_conn *isert_conn)
815{
816 struct iscsi_conn *conn = isert_conn->conn;
817 struct iscsi_login *login = conn->conn_login;
818 int size;
819
820 if (!login) {
821 pr_err("conn->conn_login is NULL\n");
822 dump_stack();
823 return;
824 }
825
826 if (login->first_request) {
827 struct iscsi_login_req *login_req =
828 (struct iscsi_login_req *)&rx_desc->iscsi_header;
829 /*
830 * Setup the initial iscsi_login values from the leading
831 * login request PDU.
832 */
833 login->leading_connection = (!login_req->tsih) ? 1 : 0;
834 login->current_stage =
835 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
836 >> 2;
837 login->version_min = login_req->min_version;
838 login->version_max = login_req->max_version;
839 memcpy(login->isid, login_req->isid, 6);
840 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
841 login->init_task_tag = login_req->itt;
842 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
843 login->cid = be16_to_cpu(login_req->cid);
844 login->tsih = be16_to_cpu(login_req->tsih);
845 }
846
847 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
848
849 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
850 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
851 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
852 memcpy(login->req_buf, &rx_desc->data[0], size);
853
854 complete(&isert_conn->conn_login_comp);
855}
856
857static void
858isert_release_cmd(struct iscsi_cmd *cmd)
859{
860 struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
861 iscsi_cmd);
862
863 pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
864
865 kfree(cmd->buf_ptr);
866 kfree(cmd->tmr_req);
867
868 kmem_cache_free(isert_cmd_cache, isert_cmd);
869}
870
871static struct iscsi_cmd
872*isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
873{
874 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
875 struct isert_cmd *isert_cmd;
876
877 isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
878 if (!isert_cmd) {
879 pr_err("Unable to allocate isert_cmd\n");
880 return NULL;
881 }
882 isert_cmd->conn = isert_conn;
883 isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
884
885 return &isert_cmd->iscsi_cmd;
886}
887
888static int
889isert_handle_scsi_cmd(struct isert_conn *isert_conn,
890 struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
891 unsigned char *buf)
892{
893 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
894 struct iscsi_conn *conn = isert_conn->conn;
895 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
896 struct scatterlist *sg;
897 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
898 bool dump_payload = false;
899
900 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
901 if (rc < 0)
902 return rc;
903
904 imm_data = cmd->immediate_data;
905 imm_data_len = cmd->first_burst_len;
906 unsol_data = cmd->unsolicited_data;
907
908 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
909 if (rc < 0) {
910 return 0;
911 } else if (rc > 0) {
912 dump_payload = true;
913 goto sequence_cmd;
914 }
915
916 if (!imm_data)
917 return 0;
918
919 sg = &cmd->se_cmd.t_data_sg[0];
920 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
921
922 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
923 sg, sg_nents, &rx_desc->data[0], imm_data_len);
924
925 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
926
927 cmd->write_data_done += imm_data_len;
928
929 if (cmd->write_data_done == cmd->se_cmd.data_length) {
930 spin_lock_bh(&cmd->istate_lock);
931 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
932 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
933 spin_unlock_bh(&cmd->istate_lock);
934 }
935
936sequence_cmd:
937 rc = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
938
939 if (!rc && dump_payload == false && unsol_data)
940 iscsit_set_unsoliticed_dataout(cmd);
941
942 if (rc == CMDSN_ERROR_CANNOT_RECOVER)
943 return iscsit_add_reject_from_cmd(
944 ISCSI_REASON_PROTOCOL_ERROR,
945 1, 0, (unsigned char *)hdr, cmd);
946
947 return 0;
948}
949
950static int
951isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
952 struct iser_rx_desc *rx_desc, unsigned char *buf)
953{
954 struct scatterlist *sg_start;
955 struct iscsi_conn *conn = isert_conn->conn;
956 struct iscsi_cmd *cmd = NULL;
957 struct iscsi_data *hdr = (struct iscsi_data *)buf;
958 u32 unsol_data_len = ntoh24(hdr->dlength);
959 int rc, sg_nents, sg_off, page_off;
960
961 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
962 if (rc < 0)
963 return rc;
964 else if (!cmd)
965 return 0;
966 /*
967 * FIXME: Unexpected unsolicited_data out
968 */
969 if (!cmd->unsolicited_data) {
970 pr_err("Received unexpected solicited data payload\n");
971 dump_stack();
972 return -1;
973 }
974
975 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
976 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
977
978 sg_off = cmd->write_data_done / PAGE_SIZE;
979 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
980 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
981 page_off = cmd->write_data_done % PAGE_SIZE;
982 /*
983 * FIXME: Non page-aligned unsolicited_data out
984 */
985 if (page_off) {
986 pr_err("Received unexpected non-page aligned data payload\n");
987 dump_stack();
988 return -1;
989 }
990 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
991 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
992
993 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
994 unsol_data_len);
995
996 rc = iscsit_check_dataout_payload(cmd, hdr, false);
997 if (rc < 0)
998 return rc;
999
1000 return 0;
1001}
1002
1003static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001004isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1005 struct iser_rx_desc *rx_desc, unsigned char *buf)
1006{
1007 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1008 struct iscsi_conn *conn = isert_conn->conn;
1009 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1010 int rc;
1011
1012 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1013 if (rc < 0)
1014 return rc;
1015 /*
1016 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1017 */
1018
1019 return iscsit_process_nop_out(conn, cmd, hdr);
1020}
1021
1022static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001023isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1024 uint32_t read_stag, uint64_t read_va,
1025 uint32_t write_stag, uint64_t write_va)
1026{
1027 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1028 struct iscsi_conn *conn = isert_conn->conn;
1029 struct iscsi_cmd *cmd;
1030 struct isert_cmd *isert_cmd;
1031 int ret = -EINVAL;
1032 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1033
1034 switch (opcode) {
1035 case ISCSI_OP_SCSI_CMD:
1036 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1037 if (!cmd)
1038 break;
1039
1040 isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1041 isert_cmd->read_stag = read_stag;
1042 isert_cmd->read_va = read_va;
1043 isert_cmd->write_stag = write_stag;
1044 isert_cmd->write_va = write_va;
1045
1046 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
1047 rx_desc, (unsigned char *)hdr);
1048 break;
1049 case ISCSI_OP_NOOP_OUT:
1050 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1051 if (!cmd)
1052 break;
1053
Nicholas Bellinger778de362013-06-14 16:07:47 -07001054 isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
1055 ret = isert_handle_nop_out(isert_conn, isert_cmd,
1056 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001057 break;
1058 case ISCSI_OP_SCSI_DATA_OUT:
1059 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1060 (unsigned char *)hdr);
1061 break;
1062 case ISCSI_OP_SCSI_TMFUNC:
1063 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1064 if (!cmd)
1065 break;
1066
1067 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1068 (unsigned char *)hdr);
1069 break;
1070 case ISCSI_OP_LOGOUT:
1071 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1072 if (!cmd)
1073 break;
1074
1075 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1076 if (ret > 0)
1077 wait_for_completion_timeout(&conn->conn_logout_comp,
1078 SECONDS_FOR_LOGOUT_COMP *
1079 HZ);
1080 break;
1081 default:
1082 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1083 dump_stack();
1084 break;
1085 }
1086
1087 return ret;
1088}
1089
1090static void
1091isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1092{
1093 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1094 uint64_t read_va = 0, write_va = 0;
1095 uint32_t read_stag = 0, write_stag = 0;
1096 int rc;
1097
1098 switch (iser_hdr->flags & 0xF0) {
1099 case ISCSI_CTRL:
1100 if (iser_hdr->flags & ISER_RSV) {
1101 read_stag = be32_to_cpu(iser_hdr->read_stag);
1102 read_va = be64_to_cpu(iser_hdr->read_va);
1103 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1104 read_stag, (unsigned long long)read_va);
1105 }
1106 if (iser_hdr->flags & ISER_WSV) {
1107 write_stag = be32_to_cpu(iser_hdr->write_stag);
1108 write_va = be64_to_cpu(iser_hdr->write_va);
1109 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1110 write_stag, (unsigned long long)write_va);
1111 }
1112
1113 pr_debug("ISER ISCSI_CTRL PDU\n");
1114 break;
1115 case ISER_HELLO:
1116 pr_err("iSER Hello message\n");
1117 break;
1118 default:
1119 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1120 break;
1121 }
1122
1123 rc = isert_rx_opcode(isert_conn, rx_desc,
1124 read_stag, read_va, write_stag, write_va);
1125}
1126
1127static void
1128isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1129 unsigned long xfer_len)
1130{
1131 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1132 struct iscsi_hdr *hdr;
1133 u64 rx_dma;
1134 int rx_buflen, outstanding;
1135
1136 if ((char *)desc == isert_conn->login_req_buf) {
1137 rx_dma = isert_conn->login_req_dma;
1138 rx_buflen = ISER_RX_LOGIN_SIZE;
1139 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1140 rx_dma, rx_buflen);
1141 } else {
1142 rx_dma = desc->dma_addr;
1143 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1144 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1145 rx_dma, rx_buflen);
1146 }
1147
1148 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1149
1150 hdr = &desc->iscsi_header;
1151 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1152 hdr->opcode, hdr->itt, hdr->flags,
1153 (int)(xfer_len - ISER_HEADERS_LEN));
1154
1155 if ((char *)desc == isert_conn->login_req_buf)
1156 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1157 isert_conn);
1158 else
1159 isert_rx_do_work(desc, isert_conn);
1160
1161 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1162 DMA_FROM_DEVICE);
1163
1164 isert_conn->post_recv_buf_count--;
1165 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1166 isert_conn->post_recv_buf_count);
1167
1168 if ((char *)desc == isert_conn->login_req_buf)
1169 return;
1170
1171 outstanding = isert_conn->post_recv_buf_count;
1172 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1173 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1174 ISERT_MIN_POSTED_RX);
1175 err = isert_post_recv(isert_conn, count);
1176 if (err) {
1177 pr_err("isert_post_recv() count: %d failed, %d\n",
1178 count, err);
1179 }
1180 }
1181}
1182
1183static void
1184isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1185{
1186 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1187 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1188
1189 pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
1190
1191 if (wr->sge) {
1192 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1193 wr->sge = NULL;
1194 }
1195
1196 kfree(wr->send_wr);
1197 wr->send_wr = NULL;
1198
1199 kfree(isert_cmd->ib_sge);
1200 isert_cmd->ib_sge = NULL;
1201}
1202
1203static void
1204isert_put_cmd(struct isert_cmd *isert_cmd)
1205{
1206 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1207 struct isert_conn *isert_conn = isert_cmd->conn;
1208 struct iscsi_conn *conn;
1209
1210 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1211
1212 switch (cmd->iscsi_opcode) {
1213 case ISCSI_OP_SCSI_CMD:
1214 conn = isert_conn->conn;
1215
1216 spin_lock_bh(&conn->cmd_lock);
1217 if (!list_empty(&cmd->i_conn_node))
1218 list_del(&cmd->i_conn_node);
1219 spin_unlock_bh(&conn->cmd_lock);
1220
1221 if (cmd->data_direction == DMA_TO_DEVICE)
1222 iscsit_stop_dataout_timer(cmd);
1223
1224 isert_unmap_cmd(isert_cmd, isert_conn);
1225 /*
1226 * Fall-through
1227 */
1228 case ISCSI_OP_SCSI_TMFUNC:
1229 transport_generic_free_cmd(&cmd->se_cmd, 0);
1230 break;
1231 case ISCSI_OP_REJECT:
1232 case ISCSI_OP_NOOP_OUT:
1233 conn = isert_conn->conn;
1234
1235 spin_lock_bh(&conn->cmd_lock);
1236 if (!list_empty(&cmd->i_conn_node))
1237 list_del(&cmd->i_conn_node);
1238 spin_unlock_bh(&conn->cmd_lock);
1239
1240 /*
1241 * Handle special case for REJECT when iscsi_add_reject*() has
1242 * overwritten the original iscsi_opcode assignment, and the
1243 * associated cmd->se_cmd needs to be released.
1244 */
1245 if (cmd->se_cmd.se_tfo != NULL) {
1246 transport_generic_free_cmd(&cmd->se_cmd, 0);
1247 break;
1248 }
1249 /*
1250 * Fall-through
1251 */
1252 default:
1253 isert_release_cmd(cmd);
1254 break;
1255 }
1256}
1257
1258static void
1259isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1260{
1261 if (tx_desc->dma_addr != 0) {
1262 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1263 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1264 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1265 tx_desc->dma_addr = 0;
1266 }
1267}
1268
1269static void
1270isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1271 struct ib_device *ib_dev)
1272{
1273 if (isert_cmd->sense_buf_dma != 0) {
1274 pr_debug("Calling ib_dma_unmap_single for isert_cmd->sense_buf_dma\n");
1275 ib_dma_unmap_single(ib_dev, isert_cmd->sense_buf_dma,
1276 isert_cmd->sense_buf_len, DMA_TO_DEVICE);
1277 isert_cmd->sense_buf_dma = 0;
1278 }
1279
1280 isert_unmap_tx_desc(tx_desc, ib_dev);
1281 isert_put_cmd(isert_cmd);
1282}
1283
1284static void
1285isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1286 struct isert_cmd *isert_cmd)
1287{
1288 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1289 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1290 struct se_cmd *se_cmd = &cmd->se_cmd;
1291 struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
1292
1293 iscsit_stop_dataout_timer(cmd);
1294
1295 if (wr->sge) {
1296 pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
1297 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
1298 wr->sge = NULL;
1299 }
1300
1301 if (isert_cmd->ib_sge) {
1302 pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
1303 kfree(isert_cmd->ib_sge);
1304 isert_cmd->ib_sge = NULL;
1305 }
1306
1307 cmd->write_data_done = se_cmd->data_length;
1308
1309 pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
1310 spin_lock_bh(&cmd->istate_lock);
1311 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1312 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1313 spin_unlock_bh(&cmd->istate_lock);
1314
1315 target_execute_cmd(se_cmd);
1316}
1317
1318static void
1319isert_do_control_comp(struct work_struct *work)
1320{
1321 struct isert_cmd *isert_cmd = container_of(work,
1322 struct isert_cmd, comp_work);
1323 struct isert_conn *isert_conn = isert_cmd->conn;
1324 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1325 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1326
1327 switch (cmd->i_state) {
1328 case ISTATE_SEND_TASKMGTRSP:
1329 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1330
1331 atomic_dec(&isert_conn->post_send_buf_count);
1332 iscsit_tmr_post_handler(cmd, cmd->conn);
1333
1334 cmd->i_state = ISTATE_SENT_STATUS;
1335 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1336 break;
1337 case ISTATE_SEND_REJECT:
1338 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1339 atomic_dec(&isert_conn->post_send_buf_count);
1340
1341 cmd->i_state = ISTATE_SENT_STATUS;
1342 complete(&cmd->reject_comp);
1343 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1344 case ISTATE_SEND_LOGOUTRSP:
1345 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1346 /*
1347 * Call atomic_dec(&isert_conn->post_send_buf_count)
1348 * from isert_free_conn()
1349 */
1350 isert_conn->logout_posted = true;
1351 iscsit_logout_post_handler(cmd, cmd->conn);
1352 break;
1353 default:
1354 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1355 dump_stack();
1356 break;
1357 }
1358}
1359
1360static void
1361isert_response_completion(struct iser_tx_desc *tx_desc,
1362 struct isert_cmd *isert_cmd,
1363 struct isert_conn *isert_conn,
1364 struct ib_device *ib_dev)
1365{
1366 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1367
1368 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
1369 cmd->i_state == ISTATE_SEND_LOGOUTRSP) {
1370 isert_unmap_tx_desc(tx_desc, ib_dev);
1371
1372 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1373 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1374 return;
1375 }
1376 atomic_dec(&isert_conn->post_send_buf_count);
1377
1378 cmd->i_state = ISTATE_SENT_STATUS;
1379 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1380}
1381
1382static void
1383isert_send_completion(struct iser_tx_desc *tx_desc,
1384 struct isert_conn *isert_conn)
1385{
1386 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1387 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1388 struct isert_rdma_wr *wr;
1389
1390 if (!isert_cmd) {
1391 atomic_dec(&isert_conn->post_send_buf_count);
1392 isert_unmap_tx_desc(tx_desc, ib_dev);
1393 return;
1394 }
1395 wr = &isert_cmd->rdma_wr;
1396
1397 switch (wr->iser_ib_op) {
1398 case ISER_IB_RECV:
1399 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1400 dump_stack();
1401 break;
1402 case ISER_IB_SEND:
1403 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1404 isert_response_completion(tx_desc, isert_cmd,
1405 isert_conn, ib_dev);
1406 break;
1407 case ISER_IB_RDMA_WRITE:
1408 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1409 dump_stack();
1410 break;
1411 case ISER_IB_RDMA_READ:
1412 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1413
1414 atomic_dec(&isert_conn->post_send_buf_count);
1415 isert_completion_rdma_read(tx_desc, isert_cmd);
1416 break;
1417 default:
1418 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1419 dump_stack();
1420 break;
1421 }
1422}
1423
1424static void
1425isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1426{
1427 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1428
1429 if (tx_desc) {
1430 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1431
1432 if (!isert_cmd)
1433 isert_unmap_tx_desc(tx_desc, ib_dev);
1434 else
1435 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1436 }
1437
1438 if (isert_conn->post_recv_buf_count == 0 &&
1439 atomic_read(&isert_conn->post_send_buf_count) == 0) {
1440 pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1441 pr_debug("Calling wake_up from isert_cq_comp_err\n");
1442
1443 isert_conn->state = ISER_CONN_TERMINATING;
1444 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;
1661
1662 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1663 iscsit_build_reject(cmd, conn, (struct iscsi_reject *)
1664 &isert_cmd->tx_desc.iscsi_header);
1665 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1666 isert_init_send_wr(isert_cmd, send_wr);
1667
1668 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1669
1670 return isert_post_response(isert_conn, isert_cmd);
1671}
1672
1673static int
1674isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1675 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1676 u32 data_left, u32 offset)
1677{
1678 struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
1679 struct scatterlist *sg_start, *tmp_sg;
1680 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1681 u32 sg_off, page_off;
1682 int i = 0, sg_nents;
1683
1684 sg_off = offset / PAGE_SIZE;
1685 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1686 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1687 page_off = offset % PAGE_SIZE;
1688
1689 send_wr->sg_list = ib_sge;
1690 send_wr->num_sge = sg_nents;
1691 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1692 /*
1693 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1694 */
1695 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1696 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1697 (unsigned long long)tmp_sg->dma_address,
1698 tmp_sg->length, page_off);
1699
1700 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1701 ib_sge->length = min_t(u32, data_left,
1702 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1703 ib_sge->lkey = isert_conn->conn_mr->lkey;
1704
1705 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
1706 ib_sge->addr, ib_sge->length);
1707 page_off = 0;
1708 data_left -= ib_sge->length;
1709 ib_sge++;
1710 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1711 }
1712
1713 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1714 send_wr->sg_list, send_wr->num_sge);
1715
1716 return sg_nents;
1717}
1718
1719static int
1720isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1721{
1722 struct se_cmd *se_cmd = &cmd->se_cmd;
1723 struct isert_cmd *isert_cmd = container_of(cmd,
1724 struct isert_cmd, iscsi_cmd);
1725 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1726 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1727 struct ib_send_wr *wr_failed, *send_wr;
1728 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1729 struct ib_sge *ib_sge;
1730 struct scatterlist *sg;
1731 u32 offset = 0, data_len, data_left, rdma_write_max;
1732 int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
1733
1734 pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
1735
1736 sg = &se_cmd->t_data_sg[0];
1737 sg_nents = se_cmd->t_data_nents;
1738
1739 count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1740 if (unlikely(!count)) {
1741 pr_err("Unable to map put_datain SGs\n");
1742 return -EINVAL;
1743 }
1744 wr->sge = sg;
1745 wr->num_sge = sg_nents;
1746 pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
1747 count, sg, sg_nents);
1748
1749 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1750 if (!ib_sge) {
1751 pr_warn("Unable to allocate datain ib_sge\n");
1752 ret = -ENOMEM;
1753 goto unmap_sg;
1754 }
1755 isert_cmd->ib_sge = ib_sge;
1756
1757 pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
1758 ib_sge, se_cmd->t_data_nents);
1759
1760 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1761 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1762 GFP_KERNEL);
1763 if (!wr->send_wr) {
1764 pr_err("Unable to allocate wr->send_wr\n");
1765 ret = -ENOMEM;
1766 goto unmap_sg;
1767 }
1768 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1769 wr->send_wr, wr->send_wr_num);
1770
1771 iscsit_increment_maxcmdsn(cmd, conn->sess);
1772 cmd->stat_sn = conn->stat_sn++;
1773
1774 wr->isert_cmd = isert_cmd;
1775 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1776 data_left = se_cmd->data_length;
1777
1778 for (i = 0; i < wr->send_wr_num; i++) {
1779 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1780 data_len = min(data_left, rdma_write_max);
1781
1782 send_wr->opcode = IB_WR_RDMA_WRITE;
1783 send_wr->send_flags = 0;
1784 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1785 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1786
1787 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1788 send_wr, data_len, offset);
1789 ib_sge += ib_sge_cnt;
1790
1791 if (i + 1 == wr->send_wr_num)
1792 send_wr->next = &isert_cmd->tx_desc.send_wr;
1793 else
1794 send_wr->next = &wr->send_wr[i + 1];
1795
1796 offset += data_len;
1797 data_left -= data_len;
1798 }
1799 /*
1800 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1801 */
1802 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1803 iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1804 &isert_cmd->tx_desc.iscsi_header);
1805 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1806 isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1807
1808 atomic_inc(&isert_conn->post_send_buf_count);
1809
1810 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1811 if (rc) {
1812 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
1813 atomic_dec(&isert_conn->post_send_buf_count);
1814 }
1815 pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
1816 return 1;
1817
1818unmap_sg:
1819 ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
1820 return ret;
1821}
1822
1823static int
1824isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1825{
1826 struct se_cmd *se_cmd = &cmd->se_cmd;
1827 struct isert_cmd *isert_cmd = container_of(cmd,
1828 struct isert_cmd, iscsi_cmd);
1829 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1830 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1831 struct ib_send_wr *wr_failed, *send_wr;
1832 struct ib_sge *ib_sge;
1833 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1834 struct scatterlist *sg_start;
1835 u32 sg_off, sg_nents, page_off, va_offset = 0;
1836 u32 offset = 0, data_len, data_left, rdma_write_max;
1837 int rc, ret = 0, count, i, ib_sge_cnt;
1838
1839 pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
1840 se_cmd->data_length, cmd->write_data_done);
1841
1842 sg_off = cmd->write_data_done / PAGE_SIZE;
1843 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1844 page_off = cmd->write_data_done % PAGE_SIZE;
1845
1846 pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
1847 sg_off, sg_start, page_off);
1848
1849 data_left = se_cmd->data_length - cmd->write_data_done;
1850 sg_nents = se_cmd->t_data_nents - sg_off;
1851
1852 pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
1853 data_left, sg_nents);
1854
1855 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1856 if (unlikely(!count)) {
1857 pr_err("Unable to map get_dataout SGs\n");
1858 return -EINVAL;
1859 }
1860 wr->sge = sg_start;
1861 wr->num_sge = sg_nents;
1862 pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
1863 count, sg_start, sg_nents);
1864
1865 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1866 if (!ib_sge) {
1867 pr_warn("Unable to allocate dataout ib_sge\n");
1868 ret = -ENOMEM;
1869 goto unmap_sg;
1870 }
1871 isert_cmd->ib_sge = ib_sge;
1872
1873 pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
1874 ib_sge, sg_nents);
1875
1876 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1877 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1878 GFP_KERNEL);
1879 if (!wr->send_wr) {
1880 pr_debug("Unable to allocate wr->send_wr\n");
1881 ret = -ENOMEM;
1882 goto unmap_sg;
1883 }
1884 pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
1885 wr->send_wr, wr->send_wr_num);
1886
1887 isert_cmd->tx_desc.isert_cmd = isert_cmd;
1888
1889 wr->iser_ib_op = ISER_IB_RDMA_READ;
1890 wr->isert_cmd = isert_cmd;
1891 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
1892 offset = cmd->write_data_done;
1893
1894 for (i = 0; i < wr->send_wr_num; i++) {
1895 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1896 data_len = min(data_left, rdma_write_max);
1897
1898 send_wr->opcode = IB_WR_RDMA_READ;
1899 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
1900 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
1901
1902 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1903 send_wr, data_len, offset);
1904 ib_sge += ib_sge_cnt;
1905
1906 if (i + 1 == wr->send_wr_num)
1907 send_wr->send_flags = IB_SEND_SIGNALED;
1908 else
1909 send_wr->next = &wr->send_wr[i + 1];
1910
1911 offset += data_len;
1912 va_offset += data_len;
1913 data_left -= data_len;
1914 }
1915
1916 atomic_inc(&isert_conn->post_send_buf_count);
1917
1918 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1919 if (rc) {
1920 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
1921 atomic_dec(&isert_conn->post_send_buf_count);
1922 }
1923 pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
1924 return 0;
1925
1926unmap_sg:
1927 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
1928 return ret;
1929}
1930
1931static int
1932isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1933{
1934 int ret;
1935
1936 switch (state) {
1937 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
1938 ret = isert_put_nopin(cmd, conn, false);
1939 break;
1940 default:
1941 pr_err("Unknown immediate state: 0x%02x\n", state);
1942 ret = -EINVAL;
1943 break;
1944 }
1945
1946 return ret;
1947}
1948
1949static int
1950isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
1951{
1952 int ret;
1953
1954 switch (state) {
1955 case ISTATE_SEND_LOGOUTRSP:
1956 ret = isert_put_logout_rsp(cmd, conn);
1957 if (!ret) {
1958 pr_debug("Returning iSER Logout -EAGAIN\n");
1959 ret = -EAGAIN;
1960 }
1961 break;
1962 case ISTATE_SEND_NOPIN:
1963 ret = isert_put_nopin(cmd, conn, true);
1964 break;
1965 case ISTATE_SEND_TASKMGTRSP:
1966 ret = isert_put_tm_rsp(cmd, conn);
1967 break;
1968 case ISTATE_SEND_REJECT:
1969 ret = isert_put_reject(cmd, conn);
1970 break;
1971 case ISTATE_SEND_STATUS:
1972 /*
1973 * Special case for sending non GOOD SCSI status from TX thread
1974 * context during pre se_cmd excecution failure.
1975 */
1976 ret = isert_put_response(conn, cmd);
1977 break;
1978 default:
1979 pr_err("Unknown response state: 0x%02x\n", state);
1980 ret = -EINVAL;
1981 break;
1982 }
1983
1984 return ret;
1985}
1986
1987static int
1988isert_setup_np(struct iscsi_np *np,
1989 struct __kernel_sockaddr_storage *ksockaddr)
1990{
1991 struct isert_np *isert_np;
1992 struct rdma_cm_id *isert_lid;
1993 struct sockaddr *sa;
1994 int ret;
1995
1996 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
1997 if (!isert_np) {
1998 pr_err("Unable to allocate struct isert_np\n");
1999 return -ENOMEM;
2000 }
2001 init_waitqueue_head(&isert_np->np_accept_wq);
2002 mutex_init(&isert_np->np_accept_mutex);
2003 INIT_LIST_HEAD(&isert_np->np_accept_list);
2004 init_completion(&isert_np->np_login_comp);
2005
2006 sa = (struct sockaddr *)ksockaddr;
2007 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2008 /*
2009 * Setup the np->np_sockaddr from the passed sockaddr setup
2010 * in iscsi_target_configfs.c code..
2011 */
2012 memcpy(&np->np_sockaddr, ksockaddr,
2013 sizeof(struct __kernel_sockaddr_storage));
2014
2015 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2016 IB_QPT_RC);
2017 if (IS_ERR(isert_lid)) {
2018 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2019 PTR_ERR(isert_lid));
2020 ret = PTR_ERR(isert_lid);
2021 goto out;
2022 }
2023
2024 ret = rdma_bind_addr(isert_lid, sa);
2025 if (ret) {
2026 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2027 goto out_lid;
2028 }
2029
2030 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2031 if (ret) {
2032 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2033 goto out_lid;
2034 }
2035
2036 isert_np->np_cm_id = isert_lid;
2037 np->np_context = isert_np;
2038 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2039
2040 return 0;
2041
2042out_lid:
2043 rdma_destroy_id(isert_lid);
2044out:
2045 kfree(isert_np);
2046 return ret;
2047}
2048
2049static int
2050isert_check_accept_queue(struct isert_np *isert_np)
2051{
2052 int empty;
2053
2054 mutex_lock(&isert_np->np_accept_mutex);
2055 empty = list_empty(&isert_np->np_accept_list);
2056 mutex_unlock(&isert_np->np_accept_mutex);
2057
2058 return empty;
2059}
2060
2061static int
2062isert_rdma_accept(struct isert_conn *isert_conn)
2063{
2064 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2065 struct rdma_conn_param cp;
2066 int ret;
2067
2068 memset(&cp, 0, sizeof(struct rdma_conn_param));
2069 cp.responder_resources = isert_conn->responder_resources;
2070 cp.initiator_depth = isert_conn->initiator_depth;
2071 cp.retry_count = 7;
2072 cp.rnr_retry_count = 7;
2073
2074 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2075
2076 ret = rdma_accept(cm_id, &cp);
2077 if (ret) {
2078 pr_err("rdma_accept() failed with: %d\n", ret);
2079 return ret;
2080 }
2081
2082 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2083
2084 return 0;
2085}
2086
2087static int
2088isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2089{
2090 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2091 int ret;
2092
2093 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
2094
2095 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2096 if (ret)
2097 return ret;
2098
2099 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2100 return 0;
2101}
2102
2103static void
2104isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2105 struct isert_conn *isert_conn)
2106{
2107 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2108 struct rdma_route *cm_route = &cm_id->route;
2109 struct sockaddr_in *sock_in;
2110 struct sockaddr_in6 *sock_in6;
2111
2112 conn->login_family = np->np_sockaddr.ss_family;
2113
2114 if (np->np_sockaddr.ss_family == AF_INET6) {
2115 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2116 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2117 &sock_in6->sin6_addr.in6_u);
2118 conn->login_port = ntohs(sock_in6->sin6_port);
2119
2120 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2121 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2122 &sock_in6->sin6_addr.in6_u);
2123 conn->local_port = ntohs(sock_in6->sin6_port);
2124 } else {
2125 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2126 sprintf(conn->login_ip, "%pI4",
2127 &sock_in->sin_addr.s_addr);
2128 conn->login_port = ntohs(sock_in->sin_port);
2129
2130 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2131 sprintf(conn->local_ip, "%pI4",
2132 &sock_in->sin_addr.s_addr);
2133 conn->local_port = ntohs(sock_in->sin_port);
2134 }
2135}
2136
2137static int
2138isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2139{
2140 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2141 struct isert_conn *isert_conn;
2142 int max_accept = 0, ret;
2143
2144accept_wait:
2145 ret = wait_event_interruptible(isert_np->np_accept_wq,
2146 !isert_check_accept_queue(isert_np) ||
2147 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2148 if (max_accept > 5)
2149 return -ENODEV;
2150
2151 spin_lock_bh(&np->np_thread_lock);
2152 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2153 spin_unlock_bh(&np->np_thread_lock);
2154 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2155 return -ENODEV;
2156 }
2157 spin_unlock_bh(&np->np_thread_lock);
2158
2159 mutex_lock(&isert_np->np_accept_mutex);
2160 if (list_empty(&isert_np->np_accept_list)) {
2161 mutex_unlock(&isert_np->np_accept_mutex);
2162 max_accept++;
2163 goto accept_wait;
2164 }
2165 isert_conn = list_first_entry(&isert_np->np_accept_list,
2166 struct isert_conn, conn_accept_node);
2167 list_del_init(&isert_conn->conn_accept_node);
2168 mutex_unlock(&isert_np->np_accept_mutex);
2169
2170 conn->context = isert_conn;
2171 isert_conn->conn = conn;
2172 max_accept = 0;
2173
2174 ret = isert_rdma_post_recvl(isert_conn);
2175 if (ret)
2176 return ret;
2177
2178 ret = isert_rdma_accept(isert_conn);
2179 if (ret)
2180 return ret;
2181
2182 isert_set_conn_info(np, conn, isert_conn);
2183
2184 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2185 return 0;
2186}
2187
2188static void
2189isert_free_np(struct iscsi_np *np)
2190{
2191 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2192
2193 rdma_destroy_id(isert_np->np_cm_id);
2194
2195 np->np_context = NULL;
2196 kfree(isert_np);
2197}
2198
2199static void isert_free_conn(struct iscsi_conn *conn)
2200{
2201 struct isert_conn *isert_conn = conn->context;
2202
2203 pr_debug("isert_free_conn: Starting \n");
2204 /*
2205 * Decrement post_send_buf_count for special case when called
2206 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2207 */
2208 if (isert_conn->logout_posted)
2209 atomic_dec(&isert_conn->post_send_buf_count);
2210
2211 if (isert_conn->conn_cm_id)
2212 rdma_disconnect(isert_conn->conn_cm_id);
2213 /*
2214 * Only wait for conn_wait_comp_err if the isert_conn made it
2215 * into full feature phase..
2216 */
2217 if (isert_conn->state > ISER_CONN_INIT) {
2218 pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2219 isert_conn->state);
2220 wait_event(isert_conn->conn_wait_comp_err,
2221 isert_conn->state == ISER_CONN_TERMINATING);
2222 pr_debug("isert_free_conn: After wait_event #1 >>>>>>>>>>>>\n");
2223 }
2224
2225 pr_debug("isert_free_conn: wait_event conn_wait %d\n", isert_conn->state);
2226 wait_event(isert_conn->conn_wait, isert_conn->state == ISER_CONN_DOWN);
2227 pr_debug("isert_free_conn: After wait_event #2 >>>>>>>>>>>>>>>>>>>>\n");
2228
2229 isert_put_conn(isert_conn);
2230}
2231
2232static struct iscsit_transport iser_target_transport = {
2233 .name = "IB/iSER",
2234 .transport_type = ISCSI_INFINIBAND,
2235 .owner = THIS_MODULE,
2236 .iscsit_setup_np = isert_setup_np,
2237 .iscsit_accept_np = isert_accept_np,
2238 .iscsit_free_np = isert_free_np,
2239 .iscsit_free_conn = isert_free_conn,
2240 .iscsit_alloc_cmd = isert_alloc_cmd,
2241 .iscsit_get_login_rx = isert_get_login_rx,
2242 .iscsit_put_login_tx = isert_put_login_tx,
2243 .iscsit_immediate_queue = isert_immediate_queue,
2244 .iscsit_response_queue = isert_response_queue,
2245 .iscsit_get_dataout = isert_get_dataout,
2246 .iscsit_queue_data_in = isert_put_datain,
2247 .iscsit_queue_status = isert_put_response,
2248};
2249
2250static int __init isert_init(void)
2251{
2252 int ret;
2253
2254 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2255 if (!isert_rx_wq) {
2256 pr_err("Unable to allocate isert_rx_wq\n");
2257 return -ENOMEM;
2258 }
2259
2260 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2261 if (!isert_comp_wq) {
2262 pr_err("Unable to allocate isert_comp_wq\n");
2263 ret = -ENOMEM;
2264 goto destroy_rx_wq;
2265 }
2266
2267 isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
2268 sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
2269 0, NULL);
2270 if (!isert_cmd_cache) {
2271 pr_err("Unable to create isert_cmd_cache\n");
2272 ret = -ENOMEM;
2273 goto destroy_tx_cq;
2274 }
2275
2276 iscsit_register_transport(&iser_target_transport);
2277 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2278 return 0;
2279
2280destroy_tx_cq:
2281 destroy_workqueue(isert_comp_wq);
2282destroy_rx_wq:
2283 destroy_workqueue(isert_rx_wq);
2284 return ret;
2285}
2286
2287static void __exit isert_exit(void)
2288{
2289 kmem_cache_destroy(isert_cmd_cache);
2290 destroy_workqueue(isert_comp_wq);
2291 destroy_workqueue(isert_rx_wq);
2292 iscsit_unregister_transport(&iser_target_transport);
2293 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2294}
2295
2296MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2297MODULE_VERSION("0.1");
2298MODULE_AUTHOR("nab@Linux-iSCSI.org");
2299MODULE_LICENSE("GPL");
2300
2301module_init(isert_init);
2302module_exit(isert_exit);