blob: 14fd0f401c3d9342b9ed2733761534ae0ee9267d [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;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -080042
43static void
44isert_qp_event_callback(struct ib_event *e, void *context)
45{
46 struct isert_conn *isert_conn = (struct isert_conn *)context;
47
48 pr_err("isert_qp_event_callback event: %d\n", e->event);
49 switch (e->event) {
50 case IB_EVENT_COMM_EST:
51 rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
52 break;
53 case IB_EVENT_QP_LAST_WQE_REACHED:
54 pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
55 break;
56 default:
57 break;
58 }
59}
60
61static int
62isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
63{
64 int ret;
65
66 ret = ib_query_device(ib_dev, devattr);
67 if (ret) {
68 pr_err("ib_query_device() failed: %d\n", ret);
69 return ret;
70 }
71 pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
72 pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
73
74 return 0;
75}
76
77static int
78isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
79{
80 struct isert_device *device = isert_conn->conn_device;
81 struct ib_qp_init_attr attr;
82 struct ib_device_attr devattr;
83 int ret, index, min_index = 0;
84
85 memset(&devattr, 0, sizeof(struct ib_device_attr));
86 ret = isert_query_device(cma_id->device, &devattr);
87 if (ret)
88 return ret;
89
90 mutex_lock(&device_list_mutex);
91 for (index = 0; index < device->cqs_used; index++)
92 if (device->cq_active_qps[index] <
93 device->cq_active_qps[min_index])
94 min_index = index;
95 device->cq_active_qps[min_index]++;
96 pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
97 mutex_unlock(&device_list_mutex);
98
99 memset(&attr, 0, sizeof(struct ib_qp_init_attr));
100 attr.event_handler = isert_qp_event_callback;
101 attr.qp_context = isert_conn;
102 attr.send_cq = device->dev_tx_cq[min_index];
103 attr.recv_cq = device->dev_rx_cq[min_index];
104 attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
105 attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
106 /*
107 * FIXME: Use devattr.max_sge - 2 for max_send_sge as
108 * work-around for RDMA_READ..
109 */
110 attr.cap.max_send_sge = devattr.max_sge - 2;
111 isert_conn->max_sge = attr.cap.max_send_sge;
112
113 attr.cap.max_recv_sge = 1;
114 attr.sq_sig_type = IB_SIGNAL_REQ_WR;
115 attr.qp_type = IB_QPT_RC;
116
117 pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
118 cma_id->device);
119 pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
120 isert_conn->conn_pd->device);
121
122 ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
123 if (ret) {
124 pr_err("rdma_create_qp failed for cma_id %d\n", ret);
125 return ret;
126 }
127 isert_conn->conn_qp = cma_id->qp;
128 pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
129
130 return 0;
131}
132
133static void
134isert_cq_event_callback(struct ib_event *e, void *context)
135{
136 pr_debug("isert_cq_event_callback event: %d\n", e->event);
137}
138
139static int
140isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
141{
142 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
143 struct iser_rx_desc *rx_desc;
144 struct ib_sge *rx_sg;
145 u64 dma_addr;
146 int i, j;
147
148 isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
149 sizeof(struct iser_rx_desc), GFP_KERNEL);
150 if (!isert_conn->conn_rx_descs)
151 goto fail;
152
153 rx_desc = isert_conn->conn_rx_descs;
154
155 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
156 dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
157 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
158 if (ib_dma_mapping_error(ib_dev, dma_addr))
159 goto dma_map_fail;
160
161 rx_desc->dma_addr = dma_addr;
162
163 rx_sg = &rx_desc->rx_sg;
164 rx_sg->addr = rx_desc->dma_addr;
165 rx_sg->length = ISER_RX_PAYLOAD_SIZE;
166 rx_sg->lkey = isert_conn->conn_mr->lkey;
167 }
168
169 isert_conn->conn_rx_desc_head = 0;
170 return 0;
171
172dma_map_fail:
173 rx_desc = isert_conn->conn_rx_descs;
174 for (j = 0; j < i; j++, rx_desc++) {
175 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
176 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
177 }
178 kfree(isert_conn->conn_rx_descs);
179 isert_conn->conn_rx_descs = NULL;
180fail:
181 return -ENOMEM;
182}
183
184static void
185isert_free_rx_descriptors(struct isert_conn *isert_conn)
186{
187 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
188 struct iser_rx_desc *rx_desc;
189 int i;
190
191 if (!isert_conn->conn_rx_descs)
192 return;
193
194 rx_desc = isert_conn->conn_rx_descs;
195 for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
196 ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
197 ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
198 }
199
200 kfree(isert_conn->conn_rx_descs);
201 isert_conn->conn_rx_descs = NULL;
202}
203
204static void isert_cq_tx_callback(struct ib_cq *, void *);
205static void isert_cq_rx_callback(struct ib_cq *, void *);
206
207static int
208isert_create_device_ib_res(struct isert_device *device)
209{
210 struct ib_device *ib_dev = device->ib_device;
211 struct isert_cq_desc *cq_desc;
212 int ret = 0, i, j;
213
214 device->cqs_used = min_t(int, num_online_cpus(),
215 device->ib_device->num_comp_vectors);
216 device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
217 pr_debug("Using %d CQs, device %s supports %d vectors\n",
218 device->cqs_used, device->ib_device->name,
219 device->ib_device->num_comp_vectors);
220 device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
221 device->cqs_used, GFP_KERNEL);
222 if (!device->cq_desc) {
223 pr_err("Unable to allocate device->cq_desc\n");
224 return -ENOMEM;
225 }
226 cq_desc = device->cq_desc;
227
228 device->dev_pd = ib_alloc_pd(ib_dev);
229 if (IS_ERR(device->dev_pd)) {
230 ret = PTR_ERR(device->dev_pd);
231 pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
232 goto out_cq_desc;
233 }
234
235 for (i = 0; i < device->cqs_used; i++) {
236 cq_desc[i].device = device;
237 cq_desc[i].cq_index = i;
238
239 device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
240 isert_cq_rx_callback,
241 isert_cq_event_callback,
242 (void *)&cq_desc[i],
243 ISER_MAX_RX_CQ_LEN, i);
244 if (IS_ERR(device->dev_rx_cq[i]))
245 goto out_cq;
246
247 device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
248 isert_cq_tx_callback,
249 isert_cq_event_callback,
250 (void *)&cq_desc[i],
251 ISER_MAX_TX_CQ_LEN, i);
252 if (IS_ERR(device->dev_tx_cq[i]))
253 goto out_cq;
254
255 if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
256 goto out_cq;
257
258 if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
259 goto out_cq;
260 }
261
262 device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
263 if (IS_ERR(device->dev_mr)) {
264 ret = PTR_ERR(device->dev_mr);
265 pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
266 goto out_cq;
267 }
268
269 return 0;
270
271out_cq:
272 for (j = 0; j < i; j++) {
273 cq_desc = &device->cq_desc[j];
274
275 if (device->dev_rx_cq[j]) {
276 cancel_work_sync(&cq_desc->cq_rx_work);
277 ib_destroy_cq(device->dev_rx_cq[j]);
278 }
279 if (device->dev_tx_cq[j]) {
280 cancel_work_sync(&cq_desc->cq_tx_work);
281 ib_destroy_cq(device->dev_tx_cq[j]);
282 }
283 }
284 ib_dealloc_pd(device->dev_pd);
285
286out_cq_desc:
287 kfree(device->cq_desc);
288
289 return ret;
290}
291
292static void
293isert_free_device_ib_res(struct isert_device *device)
294{
295 struct isert_cq_desc *cq_desc;
296 int i;
297
298 for (i = 0; i < device->cqs_used; i++) {
299 cq_desc = &device->cq_desc[i];
300
301 cancel_work_sync(&cq_desc->cq_rx_work);
302 cancel_work_sync(&cq_desc->cq_tx_work);
303 ib_destroy_cq(device->dev_rx_cq[i]);
304 ib_destroy_cq(device->dev_tx_cq[i]);
305 device->dev_rx_cq[i] = NULL;
306 device->dev_tx_cq[i] = NULL;
307 }
308
309 ib_dereg_mr(device->dev_mr);
310 ib_dealloc_pd(device->dev_pd);
311 kfree(device->cq_desc);
312}
313
314static void
315isert_device_try_release(struct isert_device *device)
316{
317 mutex_lock(&device_list_mutex);
318 device->refcount--;
319 if (!device->refcount) {
320 isert_free_device_ib_res(device);
321 list_del(&device->dev_node);
322 kfree(device);
323 }
324 mutex_unlock(&device_list_mutex);
325}
326
327static struct isert_device *
328isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
329{
330 struct isert_device *device;
331 int ret;
332
333 mutex_lock(&device_list_mutex);
334 list_for_each_entry(device, &device_list, dev_node) {
335 if (device->ib_device->node_guid == cma_id->device->node_guid) {
336 device->refcount++;
337 mutex_unlock(&device_list_mutex);
338 return device;
339 }
340 }
341
342 device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
343 if (!device) {
344 mutex_unlock(&device_list_mutex);
345 return ERR_PTR(-ENOMEM);
346 }
347
348 INIT_LIST_HEAD(&device->dev_node);
349
350 device->ib_device = cma_id->device;
351 ret = isert_create_device_ib_res(device);
352 if (ret) {
353 kfree(device);
354 mutex_unlock(&device_list_mutex);
355 return ERR_PTR(ret);
356 }
357
358 device->refcount++;
359 list_add_tail(&device->dev_node, &device_list);
360 mutex_unlock(&device_list_mutex);
361
362 return device;
363}
364
365static int
366isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
367{
368 struct iscsi_np *np = cma_id->context;
369 struct isert_np *isert_np = np->np_context;
370 struct isert_conn *isert_conn;
371 struct isert_device *device;
372 struct ib_device *ib_dev = cma_id->device;
373 int ret = 0;
374
375 pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
376 cma_id, cma_id->context);
377
378 isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
379 if (!isert_conn) {
380 pr_err("Unable to allocate isert_conn\n");
381 return -ENOMEM;
382 }
383 isert_conn->state = ISER_CONN_INIT;
384 INIT_LIST_HEAD(&isert_conn->conn_accept_node);
385 init_completion(&isert_conn->conn_login_comp);
386 init_waitqueue_head(&isert_conn->conn_wait);
387 init_waitqueue_head(&isert_conn->conn_wait_comp_err);
388 kref_init(&isert_conn->conn_kref);
389 kref_get(&isert_conn->conn_kref);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700390 mutex_init(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800391
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");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700543 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800544 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");
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700549 mutex_unlock(&isert_conn->conn_mutex);
550 goto wake_up;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800551 }
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700552 if (!isert_conn->conn_cm_id) {
553 mutex_unlock(&isert_conn->conn_mutex);
554 isert_put_conn(isert_conn);
555 return;
556 }
557 if (!isert_conn->logout_posted) {
558 pr_debug("Calling rdma_disconnect for !logout_posted from"
559 " isert_disconnect_work\n");
560 rdma_disconnect(isert_conn->conn_cm_id);
561 mutex_unlock(&isert_conn->conn_mutex);
562 iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
563 goto wake_up;
564 }
565 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800566
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -0700567wake_up:
568 wake_up(&isert_conn->conn_wait);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800569 isert_put_conn(isert_conn);
570}
571
572static void
573isert_disconnected_handler(struct rdma_cm_id *cma_id)
574{
575 struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
576
577 INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
578 schedule_work(&isert_conn->conn_logout_work);
579}
580
581static int
582isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
583{
584 int ret = 0;
585
586 pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
587 event->event, event->status, cma_id->context, cma_id);
588
589 switch (event->event) {
590 case RDMA_CM_EVENT_CONNECT_REQUEST:
591 pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
592 ret = isert_connect_request(cma_id, event);
593 break;
594 case RDMA_CM_EVENT_ESTABLISHED:
595 pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
596 isert_connected_handler(cma_id);
597 break;
598 case RDMA_CM_EVENT_DISCONNECTED:
599 pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
600 isert_disconnected_handler(cma_id);
601 break;
602 case RDMA_CM_EVENT_DEVICE_REMOVAL:
603 case RDMA_CM_EVENT_ADDR_CHANGE:
604 break;
605 case RDMA_CM_EVENT_CONNECT_ERROR:
606 default:
607 pr_err("Unknown RDMA CMA event: %d\n", event->event);
608 break;
609 }
610
611 if (ret != 0) {
612 pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
613 event->event, ret);
614 dump_stack();
615 }
616
617 return ret;
618}
619
620static int
621isert_post_recv(struct isert_conn *isert_conn, u32 count)
622{
623 struct ib_recv_wr *rx_wr, *rx_wr_failed;
624 int i, ret;
625 unsigned int rx_head = isert_conn->conn_rx_desc_head;
626 struct iser_rx_desc *rx_desc;
627
628 for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
629 rx_desc = &isert_conn->conn_rx_descs[rx_head];
630 rx_wr->wr_id = (unsigned long)rx_desc;
631 rx_wr->sg_list = &rx_desc->rx_sg;
632 rx_wr->num_sge = 1;
633 rx_wr->next = rx_wr + 1;
634 rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
635 }
636
637 rx_wr--;
638 rx_wr->next = NULL; /* mark end of work requests list */
639
640 isert_conn->post_recv_buf_count += count;
641 ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
642 &rx_wr_failed);
643 if (ret) {
644 pr_err("ib_post_recv() failed with ret: %d\n", ret);
645 isert_conn->post_recv_buf_count -= count;
646 } else {
647 pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
648 isert_conn->conn_rx_desc_head = rx_head;
649 }
650 return ret;
651}
652
653static int
654isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
655{
656 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
657 struct ib_send_wr send_wr, *send_wr_failed;
658 int ret;
659
660 ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
661 ISER_HEADERS_LEN, DMA_TO_DEVICE);
662
663 send_wr.next = NULL;
664 send_wr.wr_id = (unsigned long)tx_desc;
665 send_wr.sg_list = tx_desc->tx_sg;
666 send_wr.num_sge = tx_desc->num_sge;
667 send_wr.opcode = IB_WR_SEND;
668 send_wr.send_flags = IB_SEND_SIGNALED;
669
670 atomic_inc(&isert_conn->post_send_buf_count);
671
672 ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
673 if (ret) {
674 pr_err("ib_post_send() failed, ret: %d\n", ret);
675 atomic_dec(&isert_conn->post_send_buf_count);
676 }
677
678 return ret;
679}
680
681static void
682isert_create_send_desc(struct isert_conn *isert_conn,
683 struct isert_cmd *isert_cmd,
684 struct iser_tx_desc *tx_desc)
685{
686 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
687
688 ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
689 ISER_HEADERS_LEN, DMA_TO_DEVICE);
690
691 memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
692 tx_desc->iser_header.flags = ISER_VER;
693
694 tx_desc->num_sge = 1;
695 tx_desc->isert_cmd = isert_cmd;
696
697 if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
698 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
699 pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
700 }
701}
702
703static int
704isert_init_tx_hdrs(struct isert_conn *isert_conn,
705 struct iser_tx_desc *tx_desc)
706{
707 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
708 u64 dma_addr;
709
710 dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
711 ISER_HEADERS_LEN, DMA_TO_DEVICE);
712 if (ib_dma_mapping_error(ib_dev, dma_addr)) {
713 pr_err("ib_dma_mapping_error() failed\n");
714 return -ENOMEM;
715 }
716
717 tx_desc->dma_addr = dma_addr;
718 tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
719 tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
720 tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
721
722 pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
723 " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
724 tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
725
726 return 0;
727}
728
729static void
730isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
731{
732 isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
733 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
734 send_wr->opcode = IB_WR_SEND;
735 send_wr->send_flags = IB_SEND_SIGNALED;
736 send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
737 send_wr->num_sge = isert_cmd->tx_desc.num_sge;
738}
739
740static int
741isert_rdma_post_recvl(struct isert_conn *isert_conn)
742{
743 struct ib_recv_wr rx_wr, *rx_wr_fail;
744 struct ib_sge sge;
745 int ret;
746
747 memset(&sge, 0, sizeof(struct ib_sge));
748 sge.addr = isert_conn->login_req_dma;
749 sge.length = ISER_RX_LOGIN_SIZE;
750 sge.lkey = isert_conn->conn_mr->lkey;
751
752 pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
753 sge.addr, sge.length, sge.lkey);
754
755 memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
756 rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
757 rx_wr.sg_list = &sge;
758 rx_wr.num_sge = 1;
759
760 isert_conn->post_recv_buf_count++;
761 ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
762 if (ret) {
763 pr_err("ib_post_recv() failed: %d\n", ret);
764 isert_conn->post_recv_buf_count--;
765 }
766
767 pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
768 return ret;
769}
770
771static int
772isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
773 u32 length)
774{
775 struct isert_conn *isert_conn = conn->context;
776 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
777 struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
778 int ret;
779
780 isert_create_send_desc(isert_conn, NULL, tx_desc);
781
782 memcpy(&tx_desc->iscsi_header, &login->rsp[0],
783 sizeof(struct iscsi_hdr));
784
785 isert_init_tx_hdrs(isert_conn, tx_desc);
786
787 if (length > 0) {
788 struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
789
790 ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
791 length, DMA_TO_DEVICE);
792
793 memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
794
795 ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
796 length, DMA_TO_DEVICE);
797
798 tx_dsg->addr = isert_conn->login_rsp_dma;
799 tx_dsg->length = length;
800 tx_dsg->lkey = isert_conn->conn_mr->lkey;
801 tx_desc->num_sge = 2;
802 }
803 if (!login->login_failed) {
804 if (login->login_complete) {
805 ret = isert_alloc_rx_descriptors(isert_conn);
806 if (ret)
807 return ret;
808
809 ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
810 if (ret)
811 return ret;
812
813 isert_conn->state = ISER_CONN_UP;
814 goto post_send;
815 }
816
817 ret = isert_rdma_post_recvl(isert_conn);
818 if (ret)
819 return ret;
820 }
821post_send:
822 ret = isert_post_send(isert_conn, tx_desc);
823 if (ret)
824 return ret;
825
826 return 0;
827}
828
829static void
830isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
831 struct isert_conn *isert_conn)
832{
833 struct iscsi_conn *conn = isert_conn->conn;
834 struct iscsi_login *login = conn->conn_login;
835 int size;
836
837 if (!login) {
838 pr_err("conn->conn_login is NULL\n");
839 dump_stack();
840 return;
841 }
842
843 if (login->first_request) {
844 struct iscsi_login_req *login_req =
845 (struct iscsi_login_req *)&rx_desc->iscsi_header;
846 /*
847 * Setup the initial iscsi_login values from the leading
848 * login request PDU.
849 */
850 login->leading_connection = (!login_req->tsih) ? 1 : 0;
851 login->current_stage =
852 (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
853 >> 2;
854 login->version_min = login_req->min_version;
855 login->version_max = login_req->max_version;
856 memcpy(login->isid, login_req->isid, 6);
857 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
858 login->init_task_tag = login_req->itt;
859 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
860 login->cid = be16_to_cpu(login_req->cid);
861 login->tsih = be16_to_cpu(login_req->tsih);
862 }
863
864 memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
865
866 size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
867 pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
868 size, rx_buflen, MAX_KEY_VALUE_PAIRS);
869 memcpy(login->req_buf, &rx_desc->data[0], size);
870
Nicholas Bellinger6faaa852013-08-18 16:35:46 -0700871 if (login->first_request) {
872 complete(&isert_conn->conn_login_comp);
873 return;
874 }
875 schedule_delayed_work(&conn->login_work, 0);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800876}
877
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800878static struct iscsi_cmd
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700879*isert_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800880{
881 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
882 struct isert_cmd *isert_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700883 struct iscsi_cmd *cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800884
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700885 cmd = iscsit_allocate_cmd(conn, gfp);
886 if (!cmd) {
887 pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800888 return NULL;
889 }
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700890 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800891 isert_cmd->conn = isert_conn;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700892 isert_cmd->iscsi_cmd = cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800893
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700894 return cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800895}
896
897static int
898isert_handle_scsi_cmd(struct isert_conn *isert_conn,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700899 struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
900 struct iser_rx_desc *rx_desc, unsigned char *buf)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800901{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800902 struct iscsi_conn *conn = isert_conn->conn;
903 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
904 struct scatterlist *sg;
905 int imm_data, imm_data_len, unsol_data, sg_nents, rc;
906 bool dump_payload = false;
907
908 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
909 if (rc < 0)
910 return rc;
911
912 imm_data = cmd->immediate_data;
913 imm_data_len = cmd->first_burst_len;
914 unsol_data = cmd->unsolicited_data;
915
916 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
917 if (rc < 0) {
918 return 0;
919 } else if (rc > 0) {
920 dump_payload = true;
921 goto sequence_cmd;
922 }
923
924 if (!imm_data)
925 return 0;
926
927 sg = &cmd->se_cmd.t_data_sg[0];
928 sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
929
930 pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
931 sg, sg_nents, &rx_desc->data[0], imm_data_len);
932
933 sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
934
935 cmd->write_data_done += imm_data_len;
936
937 if (cmd->write_data_done == cmd->se_cmd.data_length) {
938 spin_lock_bh(&cmd->istate_lock);
939 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
940 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
941 spin_unlock_bh(&cmd->istate_lock);
942 }
943
944sequence_cmd:
Nicholas Bellinger561bf152013-07-03 03:58:58 -0700945 rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800946
947 if (!rc && dump_payload == false && unsol_data)
948 iscsit_set_unsoliticed_dataout(cmd);
949
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -0800950 return 0;
951}
952
953static int
954isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
955 struct iser_rx_desc *rx_desc, unsigned char *buf)
956{
957 struct scatterlist *sg_start;
958 struct iscsi_conn *conn = isert_conn->conn;
959 struct iscsi_cmd *cmd = NULL;
960 struct iscsi_data *hdr = (struct iscsi_data *)buf;
961 u32 unsol_data_len = ntoh24(hdr->dlength);
962 int rc, sg_nents, sg_off, page_off;
963
964 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
965 if (rc < 0)
966 return rc;
967 else if (!cmd)
968 return 0;
969 /*
970 * FIXME: Unexpected unsolicited_data out
971 */
972 if (!cmd->unsolicited_data) {
973 pr_err("Received unexpected solicited data payload\n");
974 dump_stack();
975 return -1;
976 }
977
978 pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
979 unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
980
981 sg_off = cmd->write_data_done / PAGE_SIZE;
982 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
983 sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
984 page_off = cmd->write_data_done % PAGE_SIZE;
985 /*
986 * FIXME: Non page-aligned unsolicited_data out
987 */
988 if (page_off) {
989 pr_err("Received unexpected non-page aligned data payload\n");
990 dump_stack();
991 return -1;
992 }
993 pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
994 sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
995
996 sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
997 unsol_data_len);
998
999 rc = iscsit_check_dataout_payload(cmd, hdr, false);
1000 if (rc < 0)
1001 return rc;
1002
1003 return 0;
1004}
1005
1006static int
Nicholas Bellinger778de362013-06-14 16:07:47 -07001007isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001008 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1009 unsigned char *buf)
Nicholas Bellinger778de362013-06-14 16:07:47 -07001010{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001011 struct iscsi_conn *conn = isert_conn->conn;
1012 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1013 int rc;
1014
1015 rc = iscsit_setup_nop_out(conn, cmd, hdr);
1016 if (rc < 0)
1017 return rc;
1018 /*
1019 * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
1020 */
1021
1022 return iscsit_process_nop_out(conn, cmd, hdr);
1023}
1024
1025static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001026isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001027 struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
1028 struct iscsi_text *hdr)
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001029{
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001030 struct iscsi_conn *conn = isert_conn->conn;
1031 u32 payload_length = ntoh24(hdr->dlength);
1032 int rc;
1033 unsigned char *text_in;
1034
1035 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
1036 if (rc < 0)
1037 return rc;
1038
1039 text_in = kzalloc(payload_length, GFP_KERNEL);
1040 if (!text_in) {
1041 pr_err("Unable to allocate text_in of payload_length: %u\n",
1042 payload_length);
1043 return -ENOMEM;
1044 }
1045 cmd->text_in_ptr = text_in;
1046
1047 memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
1048
1049 return iscsit_process_text_cmd(conn, cmd, hdr);
1050}
1051
1052static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001053isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
1054 uint32_t read_stag, uint64_t read_va,
1055 uint32_t write_stag, uint64_t write_va)
1056{
1057 struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
1058 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001059 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001060 struct iscsi_cmd *cmd;
1061 struct isert_cmd *isert_cmd;
1062 int ret = -EINVAL;
1063 u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
1064
Nicholas Bellingerca40d242013-07-07 17:45:08 -07001065 if (sess->sess_ops->SessionType &&
1066 (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
1067 pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
1068 " ignoring\n", opcode);
1069 return 0;
1070 }
1071
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001072 switch (opcode) {
1073 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001074 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001075 if (!cmd)
1076 break;
1077
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001078 isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001079 isert_cmd->read_stag = read_stag;
1080 isert_cmd->read_va = read_va;
1081 isert_cmd->write_stag = write_stag;
1082 isert_cmd->write_va = write_va;
1083
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001084 ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001085 rx_desc, (unsigned char *)hdr);
1086 break;
1087 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001088 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001089 if (!cmd)
1090 break;
1091
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001092 isert_cmd = iscsit_priv_cmd(cmd);
1093 ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
Nicholas Bellinger778de362013-06-14 16:07:47 -07001094 rx_desc, (unsigned char *)hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001095 break;
1096 case ISCSI_OP_SCSI_DATA_OUT:
1097 ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
1098 (unsigned char *)hdr);
1099 break;
1100 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001101 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001102 if (!cmd)
1103 break;
1104
1105 ret = iscsit_handle_task_mgt_cmd(conn, cmd,
1106 (unsigned char *)hdr);
1107 break;
1108 case ISCSI_OP_LOGOUT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001109 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001110 if (!cmd)
1111 break;
1112
1113 ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
1114 if (ret > 0)
1115 wait_for_completion_timeout(&conn->conn_logout_comp,
1116 SECONDS_FOR_LOGOUT_COMP *
1117 HZ);
1118 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001119 case ISCSI_OP_TEXT:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001120 cmd = isert_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001121 if (!cmd)
1122 break;
1123
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001124 isert_cmd = iscsit_priv_cmd(cmd);
1125 ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001126 rx_desc, (struct iscsi_text *)hdr);
1127 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001128 default:
1129 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
1130 dump_stack();
1131 break;
1132 }
1133
1134 return ret;
1135}
1136
1137static void
1138isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
1139{
1140 struct iser_hdr *iser_hdr = &rx_desc->iser_header;
1141 uint64_t read_va = 0, write_va = 0;
1142 uint32_t read_stag = 0, write_stag = 0;
1143 int rc;
1144
1145 switch (iser_hdr->flags & 0xF0) {
1146 case ISCSI_CTRL:
1147 if (iser_hdr->flags & ISER_RSV) {
1148 read_stag = be32_to_cpu(iser_hdr->read_stag);
1149 read_va = be64_to_cpu(iser_hdr->read_va);
1150 pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
1151 read_stag, (unsigned long long)read_va);
1152 }
1153 if (iser_hdr->flags & ISER_WSV) {
1154 write_stag = be32_to_cpu(iser_hdr->write_stag);
1155 write_va = be64_to_cpu(iser_hdr->write_va);
1156 pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
1157 write_stag, (unsigned long long)write_va);
1158 }
1159
1160 pr_debug("ISER ISCSI_CTRL PDU\n");
1161 break;
1162 case ISER_HELLO:
1163 pr_err("iSER Hello message\n");
1164 break;
1165 default:
1166 pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
1167 break;
1168 }
1169
1170 rc = isert_rx_opcode(isert_conn, rx_desc,
1171 read_stag, read_va, write_stag, write_va);
1172}
1173
1174static void
1175isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
1176 unsigned long xfer_len)
1177{
1178 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1179 struct iscsi_hdr *hdr;
1180 u64 rx_dma;
1181 int rx_buflen, outstanding;
1182
1183 if ((char *)desc == isert_conn->login_req_buf) {
1184 rx_dma = isert_conn->login_req_dma;
1185 rx_buflen = ISER_RX_LOGIN_SIZE;
1186 pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1187 rx_dma, rx_buflen);
1188 } else {
1189 rx_dma = desc->dma_addr;
1190 rx_buflen = ISER_RX_PAYLOAD_SIZE;
1191 pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
1192 rx_dma, rx_buflen);
1193 }
1194
1195 ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
1196
1197 hdr = &desc->iscsi_header;
1198 pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
1199 hdr->opcode, hdr->itt, hdr->flags,
1200 (int)(xfer_len - ISER_HEADERS_LEN));
1201
1202 if ((char *)desc == isert_conn->login_req_buf)
1203 isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
1204 isert_conn);
1205 else
1206 isert_rx_do_work(desc, isert_conn);
1207
1208 ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
1209 DMA_FROM_DEVICE);
1210
1211 isert_conn->post_recv_buf_count--;
1212 pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
1213 isert_conn->post_recv_buf_count);
1214
1215 if ((char *)desc == isert_conn->login_req_buf)
1216 return;
1217
1218 outstanding = isert_conn->post_recv_buf_count;
1219 if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
1220 int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
1221 ISERT_MIN_POSTED_RX);
1222 err = isert_post_recv(isert_conn, count);
1223 if (err) {
1224 pr_err("isert_post_recv() count: %d failed, %d\n",
1225 count, err);
1226 }
1227 }
1228}
1229
1230static void
1231isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
1232{
1233 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1234 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1235
Vu Pham90ecc6e2013-08-28 23:23:33 +03001236 pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001237 if (wr->sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001238 pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
1239 ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
1240 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1241 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001242 wr->sge = NULL;
1243 }
1244
Vu Pham90ecc6e2013-08-28 23:23:33 +03001245 if (wr->send_wr) {
1246 pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
1247 kfree(wr->send_wr);
1248 wr->send_wr = NULL;
1249 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001250
Vu Pham90ecc6e2013-08-28 23:23:33 +03001251 if (wr->ib_sge) {
1252 pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
1253 kfree(wr->ib_sge);
1254 wr->ib_sge = NULL;
1255 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001256}
1257
1258static void
1259isert_put_cmd(struct isert_cmd *isert_cmd)
1260{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001261 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001262 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001263 struct iscsi_conn *conn = isert_conn->conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001264
1265 pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
1266
1267 switch (cmd->iscsi_opcode) {
1268 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001269 spin_lock_bh(&conn->cmd_lock);
1270 if (!list_empty(&cmd->i_conn_node))
1271 list_del(&cmd->i_conn_node);
1272 spin_unlock_bh(&conn->cmd_lock);
1273
1274 if (cmd->data_direction == DMA_TO_DEVICE)
1275 iscsit_stop_dataout_timer(cmd);
1276
1277 isert_unmap_cmd(isert_cmd, isert_conn);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001278 transport_generic_free_cmd(&cmd->se_cmd, 0);
1279 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001280 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001281 spin_lock_bh(&conn->cmd_lock);
1282 if (!list_empty(&cmd->i_conn_node))
1283 list_del(&cmd->i_conn_node);
1284 spin_unlock_bh(&conn->cmd_lock);
1285
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001286 transport_generic_free_cmd(&cmd->se_cmd, 0);
1287 break;
1288 case ISCSI_OP_REJECT:
1289 case ISCSI_OP_NOOP_OUT:
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001290 case ISCSI_OP_TEXT:
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001291 spin_lock_bh(&conn->cmd_lock);
1292 if (!list_empty(&cmd->i_conn_node))
1293 list_del(&cmd->i_conn_node);
1294 spin_unlock_bh(&conn->cmd_lock);
1295
1296 /*
1297 * Handle special case for REJECT when iscsi_add_reject*() has
1298 * overwritten the original iscsi_opcode assignment, and the
1299 * associated cmd->se_cmd needs to be released.
1300 */
1301 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001302 pr_debug("Calling transport_generic_free_cmd from"
1303 " isert_put_cmd for 0x%02x\n",
1304 cmd->iscsi_opcode);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001305 transport_generic_free_cmd(&cmd->se_cmd, 0);
1306 break;
1307 }
1308 /*
1309 * Fall-through
1310 */
1311 default:
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001312 iscsit_release_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001313 break;
1314 }
1315}
1316
1317static void
1318isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
1319{
1320 if (tx_desc->dma_addr != 0) {
1321 pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
1322 ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
1323 ISER_HEADERS_LEN, DMA_TO_DEVICE);
1324 tx_desc->dma_addr = 0;
1325 }
1326}
1327
1328static void
1329isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
1330 struct ib_device *ib_dev)
1331{
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001332 if (isert_cmd->pdu_buf_dma != 0) {
1333 pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
1334 ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
1335 isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
1336 isert_cmd->pdu_buf_dma = 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001337 }
1338
1339 isert_unmap_tx_desc(tx_desc, ib_dev);
1340 isert_put_cmd(isert_cmd);
1341}
1342
1343static void
1344isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
1345 struct isert_cmd *isert_cmd)
1346{
1347 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001348 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001349 struct se_cmd *se_cmd = &cmd->se_cmd;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001350 struct isert_conn *isert_conn = isert_cmd->conn;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001351
1352 iscsit_stop_dataout_timer(cmd);
Vu Pham90ecc6e2013-08-28 23:23:33 +03001353 isert_unmap_cmd(isert_cmd, isert_conn);
1354 cmd->write_data_done = wr->cur_rdma_length;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001355
Vu Pham90ecc6e2013-08-28 23:23:33 +03001356 pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001357 spin_lock_bh(&cmd->istate_lock);
1358 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1359 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1360 spin_unlock_bh(&cmd->istate_lock);
1361
1362 target_execute_cmd(se_cmd);
1363}
1364
1365static void
1366isert_do_control_comp(struct work_struct *work)
1367{
1368 struct isert_cmd *isert_cmd = container_of(work,
1369 struct isert_cmd, comp_work);
1370 struct isert_conn *isert_conn = isert_cmd->conn;
1371 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001372 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001373
1374 switch (cmd->i_state) {
1375 case ISTATE_SEND_TASKMGTRSP:
1376 pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
1377
1378 atomic_dec(&isert_conn->post_send_buf_count);
1379 iscsit_tmr_post_handler(cmd, cmd->conn);
1380
1381 cmd->i_state = ISTATE_SENT_STATUS;
1382 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1383 break;
1384 case ISTATE_SEND_REJECT:
1385 pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
1386 atomic_dec(&isert_conn->post_send_buf_count);
1387
1388 cmd->i_state = ISTATE_SENT_STATUS;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001389 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001390 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001391 case ISTATE_SEND_LOGOUTRSP:
1392 pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
1393 /*
1394 * Call atomic_dec(&isert_conn->post_send_buf_count)
1395 * from isert_free_conn()
1396 */
1397 isert_conn->logout_posted = true;
1398 iscsit_logout_post_handler(cmd, cmd->conn);
1399 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001400 case ISTATE_SEND_TEXTRSP:
1401 atomic_dec(&isert_conn->post_send_buf_count);
1402 cmd->i_state = ISTATE_SENT_STATUS;
1403 isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
1404 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001405 default:
1406 pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
1407 dump_stack();
1408 break;
1409 }
1410}
1411
1412static void
1413isert_response_completion(struct iser_tx_desc *tx_desc,
1414 struct isert_cmd *isert_cmd,
1415 struct isert_conn *isert_conn,
1416 struct ib_device *ib_dev)
1417{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001418 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001419
1420 if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001421 cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001422 cmd->i_state == ISTATE_SEND_REJECT ||
1423 cmd->i_state == ISTATE_SEND_TEXTRSP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001424 isert_unmap_tx_desc(tx_desc, ib_dev);
1425
1426 INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
1427 queue_work(isert_comp_wq, &isert_cmd->comp_work);
1428 return;
1429 }
1430 atomic_dec(&isert_conn->post_send_buf_count);
1431
1432 cmd->i_state = ISTATE_SENT_STATUS;
1433 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1434}
1435
1436static void
1437isert_send_completion(struct iser_tx_desc *tx_desc,
1438 struct isert_conn *isert_conn)
1439{
1440 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1441 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1442 struct isert_rdma_wr *wr;
1443
1444 if (!isert_cmd) {
1445 atomic_dec(&isert_conn->post_send_buf_count);
1446 isert_unmap_tx_desc(tx_desc, ib_dev);
1447 return;
1448 }
1449 wr = &isert_cmd->rdma_wr;
1450
1451 switch (wr->iser_ib_op) {
1452 case ISER_IB_RECV:
1453 pr_err("isert_send_completion: Got ISER_IB_RECV\n");
1454 dump_stack();
1455 break;
1456 case ISER_IB_SEND:
1457 pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
1458 isert_response_completion(tx_desc, isert_cmd,
1459 isert_conn, ib_dev);
1460 break;
1461 case ISER_IB_RDMA_WRITE:
1462 pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
1463 dump_stack();
1464 break;
1465 case ISER_IB_RDMA_READ:
1466 pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
1467
1468 atomic_dec(&isert_conn->post_send_buf_count);
1469 isert_completion_rdma_read(tx_desc, isert_cmd);
1470 break;
1471 default:
1472 pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
1473 dump_stack();
1474 break;
1475 }
1476}
1477
1478static void
1479isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
1480{
1481 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1482
1483 if (tx_desc) {
1484 struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
1485
1486 if (!isert_cmd)
1487 isert_unmap_tx_desc(tx_desc, ib_dev);
1488 else
1489 isert_completion_put(tx_desc, isert_cmd, ib_dev);
1490 }
1491
1492 if (isert_conn->post_recv_buf_count == 0 &&
1493 atomic_read(&isert_conn->post_send_buf_count) == 0) {
1494 pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1495 pr_debug("Calling wake_up from isert_cq_comp_err\n");
1496
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07001497 mutex_lock(&isert_conn->conn_mutex);
1498 if (isert_conn->state != ISER_CONN_DOWN)
1499 isert_conn->state = ISER_CONN_TERMINATING;
1500 mutex_unlock(&isert_conn->conn_mutex);
1501
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001502 wake_up(&isert_conn->conn_wait_comp_err);
1503 }
1504}
1505
1506static void
1507isert_cq_tx_work(struct work_struct *work)
1508{
1509 struct isert_cq_desc *cq_desc = container_of(work,
1510 struct isert_cq_desc, cq_tx_work);
1511 struct isert_device *device = cq_desc->device;
1512 int cq_index = cq_desc->cq_index;
1513 struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
1514 struct isert_conn *isert_conn;
1515 struct iser_tx_desc *tx_desc;
1516 struct ib_wc wc;
1517
1518 while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
1519 tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
1520 isert_conn = wc.qp->qp_context;
1521
1522 if (wc.status == IB_WC_SUCCESS) {
1523 isert_send_completion(tx_desc, isert_conn);
1524 } else {
1525 pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
1526 pr_debug("TX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001527 pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001528 atomic_dec(&isert_conn->post_send_buf_count);
1529 isert_cq_comp_err(tx_desc, isert_conn);
1530 }
1531 }
1532
1533 ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
1534}
1535
1536static void
1537isert_cq_tx_callback(struct ib_cq *cq, void *context)
1538{
1539 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1540
1541 INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
1542 queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
1543}
1544
1545static void
1546isert_cq_rx_work(struct work_struct *work)
1547{
1548 struct isert_cq_desc *cq_desc = container_of(work,
1549 struct isert_cq_desc, cq_rx_work);
1550 struct isert_device *device = cq_desc->device;
1551 int cq_index = cq_desc->cq_index;
1552 struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
1553 struct isert_conn *isert_conn;
1554 struct iser_rx_desc *rx_desc;
1555 struct ib_wc wc;
1556 unsigned long xfer_len;
1557
1558 while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
1559 rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
1560 isert_conn = wc.qp->qp_context;
1561
1562 if (wc.status == IB_WC_SUCCESS) {
1563 xfer_len = (unsigned long)wc.byte_len;
1564 isert_rx_completion(rx_desc, isert_conn, xfer_len);
1565 } else {
1566 pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001567 if (wc.status != IB_WC_WR_FLUSH_ERR) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001568 pr_debug("RX wc.status: 0x%08x\n", wc.status);
Nicholas Bellingerc5a2adb2013-07-01 15:11:21 -07001569 pr_debug("RX wc.vendor_err: 0x%08x\n",
1570 wc.vendor_err);
1571 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001572 isert_conn->post_recv_buf_count--;
1573 isert_cq_comp_err(NULL, isert_conn);
1574 }
1575 }
1576
1577 ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
1578}
1579
1580static void
1581isert_cq_rx_callback(struct ib_cq *cq, void *context)
1582{
1583 struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
1584
1585 INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
1586 queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
1587}
1588
1589static int
1590isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
1591{
1592 struct ib_send_wr *wr_failed;
1593 int ret;
1594
1595 atomic_inc(&isert_conn->post_send_buf_count);
1596
1597 ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
1598 &wr_failed);
1599 if (ret) {
1600 pr_err("ib_post_send failed with %d\n", ret);
1601 atomic_dec(&isert_conn->post_send_buf_count);
1602 return ret;
1603 }
1604 return ret;
1605}
1606
1607static int
1608isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1609{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001610 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001611 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1612 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1613 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
1614 &isert_cmd->tx_desc.iscsi_header;
1615
1616 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1617 iscsit_build_rsp_pdu(cmd, conn, true, hdr);
1618 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1619 /*
1620 * Attach SENSE DATA payload to iSCSI Response PDU
1621 */
1622 if (cmd->se_cmd.sense_buffer &&
1623 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1624 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
1625 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1626 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001627 u32 padding, pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001628
1629 put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
1630 cmd->sense_buffer);
1631 cmd->se_cmd.scsi_sense_length += sizeof(__be16);
1632
1633 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
1634 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001635 pdu_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001636
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001637 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1638 (void *)cmd->sense_buffer, pdu_len,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001639 DMA_TO_DEVICE);
1640
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001641 isert_cmd->pdu_buf_len = pdu_len;
1642 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1643 tx_dsg->length = pdu_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001644 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1645 isert_cmd->tx_desc.num_sge = 2;
1646 }
1647
1648 isert_init_send_wr(isert_cmd, send_wr);
1649
1650 pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1651
1652 return isert_post_response(isert_conn, isert_cmd);
1653}
1654
1655static int
1656isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
1657 bool nopout_response)
1658{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001659 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001660 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1661 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1662
1663 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1664 iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
1665 &isert_cmd->tx_desc.iscsi_header,
1666 nopout_response);
1667 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1668 isert_init_send_wr(isert_cmd, send_wr);
1669
Masanari Iida8b513d02013-05-21 23:13:12 +09001670 pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001671
1672 return isert_post_response(isert_conn, isert_cmd);
1673}
1674
1675static int
1676isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1677{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001678 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001679 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1680 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1681
1682 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1683 iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
1684 &isert_cmd->tx_desc.iscsi_header);
1685 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1686 isert_init_send_wr(isert_cmd, send_wr);
1687
1688 pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1689
1690 return isert_post_response(isert_conn, isert_cmd);
1691}
1692
1693static int
1694isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1695{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001696 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001697 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1698 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1699
1700 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1701 iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
1702 &isert_cmd->tx_desc.iscsi_header);
1703 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1704 isert_init_send_wr(isert_cmd, send_wr);
1705
1706 pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1707
1708 return isert_post_response(isert_conn, isert_cmd);
1709}
1710
1711static int
1712isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1713{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001714 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001715 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1716 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001717 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1718 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1719 struct iscsi_reject *hdr =
1720 (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001721
1722 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001723 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001724 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001725
1726 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001727 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001728 (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
1729 DMA_TO_DEVICE);
Nicholas Bellingerdbbc5d12013-07-03 19:39:37 -07001730 isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
1731 tx_dsg->addr = isert_cmd->pdu_buf_dma;
Nicholas Bellinger3df8f682013-06-26 02:31:42 -07001732 tx_dsg->length = ISCSI_HDR_LEN;
1733 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1734 isert_cmd->tx_desc.num_sge = 2;
1735
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001736 isert_init_send_wr(isert_cmd, send_wr);
1737
1738 pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1739
1740 return isert_post_response(isert_conn, isert_cmd);
1741}
1742
1743static int
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001744isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1745{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001746 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07001747 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1748 struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
1749 struct iscsi_text_rsp *hdr =
1750 (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
1751 u32 txt_rsp_len;
1752 int rc;
1753
1754 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1755 rc = iscsit_build_text_rsp(cmd, conn, hdr);
1756 if (rc < 0)
1757 return rc;
1758
1759 txt_rsp_len = rc;
1760 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1761
1762 if (txt_rsp_len) {
1763 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1764 struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
1765 void *txt_rsp_buf = cmd->buf_ptr;
1766
1767 isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
1768 txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
1769
1770 isert_cmd->pdu_buf_len = txt_rsp_len;
1771 tx_dsg->addr = isert_cmd->pdu_buf_dma;
1772 tx_dsg->length = txt_rsp_len;
1773 tx_dsg->lkey = isert_conn->conn_mr->lkey;
1774 isert_cmd->tx_desc.num_sge = 2;
1775 }
1776 isert_init_send_wr(isert_cmd, send_wr);
1777
1778 pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
1779
1780 return isert_post_response(isert_conn, isert_cmd);
1781}
1782
1783static int
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001784isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
1785 struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
1786 u32 data_left, u32 offset)
1787{
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001788 struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001789 struct scatterlist *sg_start, *tmp_sg;
1790 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
1791 u32 sg_off, page_off;
1792 int i = 0, sg_nents;
1793
1794 sg_off = offset / PAGE_SIZE;
1795 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1796 sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
1797 page_off = offset % PAGE_SIZE;
1798
1799 send_wr->sg_list = ib_sge;
1800 send_wr->num_sge = sg_nents;
1801 send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
1802 /*
1803 * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
1804 */
1805 for_each_sg(sg_start, tmp_sg, sg_nents, i) {
1806 pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
1807 (unsigned long long)tmp_sg->dma_address,
1808 tmp_sg->length, page_off);
1809
1810 ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
1811 ib_sge->length = min_t(u32, data_left,
1812 ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
1813 ib_sge->lkey = isert_conn->conn_mr->lkey;
1814
Vu Pham90ecc6e2013-08-28 23:23:33 +03001815 pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
1816 ib_sge->addr, ib_sge->length, ib_sge->lkey);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001817 page_off = 0;
1818 data_left -= ib_sge->length;
1819 ib_sge++;
1820 pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
1821 }
1822
1823 pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
1824 send_wr->sg_list, send_wr->num_sge);
1825
1826 return sg_nents;
1827}
1828
1829static int
Vu Pham90ecc6e2013-08-28 23:23:33 +03001830isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1831 struct isert_rdma_wr *wr)
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001832{
1833 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001834 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001835 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001836 struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001837 struct ib_send_wr *send_wr;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001838 struct ib_sge *ib_sge;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001839 struct scatterlist *sg_start;
1840 u32 sg_off = 0, sg_nents;
1841 u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
1842 int ret = 0, count, i, ib_sge_cnt;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001843
Vu Pham90ecc6e2013-08-28 23:23:33 +03001844 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
1845 data_left = se_cmd->data_length;
1846 iscsit_increment_maxcmdsn(cmd, conn->sess);
1847 cmd->stat_sn = conn->stat_sn++;
1848 } else {
1849 sg_off = cmd->write_data_done / PAGE_SIZE;
1850 data_left = se_cmd->data_length - cmd->write_data_done;
1851 offset = cmd->write_data_done;
1852 isert_cmd->tx_desc.isert_cmd = isert_cmd;
1853 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001854
Vu Pham90ecc6e2013-08-28 23:23:33 +03001855 sg_start = &cmd->se_cmd.t_data_sg[sg_off];
1856 sg_nents = se_cmd->t_data_nents - sg_off;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001857
Vu Pham90ecc6e2013-08-28 23:23:33 +03001858 count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
1859 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1860 DMA_TO_DEVICE : DMA_FROM_DEVICE);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001861 if (unlikely(!count)) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001862 pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001863 return -EINVAL;
1864 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03001865 wr->sge = sg_start;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001866 wr->num_sge = sg_nents;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001867 wr->cur_rdma_length = data_left;
1868 pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
1869 isert_cmd, count, sg_start, sg_nents, data_left);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001870
1871 ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
1872 if (!ib_sge) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001873 pr_warn("Unable to allocate ib_sge\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001874 ret = -ENOMEM;
1875 goto unmap_sg;
1876 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03001877 wr->ib_sge = ib_sge;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001878
1879 wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
1880 wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
1881 GFP_KERNEL);
1882 if (!wr->send_wr) {
Vu Pham90ecc6e2013-08-28 23:23:33 +03001883 pr_debug("Unable to allocate wr->send_wr\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001884 ret = -ENOMEM;
1885 goto unmap_sg;
1886 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001887
1888 wr->isert_cmd = isert_cmd;
1889 rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001890
1891 for (i = 0; i < wr->send_wr_num; i++) {
1892 send_wr = &isert_cmd->rdma_wr.send_wr[i];
1893 data_len = min(data_left, rdma_write_max);
1894
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001895 send_wr->send_flags = 0;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001896 if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
1897 send_wr->opcode = IB_WR_RDMA_WRITE;
1898 send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
1899 send_wr->wr.rdma.rkey = isert_cmd->read_stag;
1900 if (i + 1 == wr->send_wr_num)
1901 send_wr->next = &isert_cmd->tx_desc.send_wr;
1902 else
1903 send_wr->next = &wr->send_wr[i + 1];
1904 } else {
1905 send_wr->opcode = IB_WR_RDMA_READ;
1906 send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
1907 send_wr->wr.rdma.rkey = isert_cmd->write_stag;
1908 if (i + 1 == wr->send_wr_num)
1909 send_wr->send_flags = IB_SEND_SIGNALED;
1910 else
1911 send_wr->next = &wr->send_wr[i + 1];
1912 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001913
1914 ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
1915 send_wr, data_len, offset);
1916 ib_sge += ib_sge_cnt;
1917
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001918 offset += data_len;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001919 va_offset += data_len;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001920 data_left -= data_len;
1921 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03001922
1923 return 0;
1924unmap_sg:
1925 ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
1926 (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
1927 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1928 return ret;
1929}
1930
1931static int
1932isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
1933{
1934 struct se_cmd *se_cmd = &cmd->se_cmd;
1935 struct isert_cmd *isert_cmd = container_of(cmd,
1936 struct isert_cmd, iscsi_cmd);
1937 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1938 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
1939 struct ib_send_wr *wr_failed;
1940 int rc;
1941
1942 pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
1943 isert_cmd, se_cmd->data_length);
1944 wr->iser_ib_op = ISER_IB_RDMA_WRITE;
1945 rc = isert_map_rdma(conn, cmd, wr);
1946 if (rc) {
1947 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
1948 return rc;
1949 }
1950
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001951 /*
1952 * Build isert_conn->tx_desc for iSCSI response PDU and attach
1953 */
1954 isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
1955 iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
1956 &isert_cmd->tx_desc.iscsi_header);
1957 isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
1958 isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
1959
1960 atomic_inc(&isert_conn->post_send_buf_count);
1961
1962 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1963 if (rc) {
1964 pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
1965 atomic_dec(&isert_conn->post_send_buf_count);
1966 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03001967 pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
1968 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001969
Vu Pham90ecc6e2013-08-28 23:23:33 +03001970 return 1;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001971}
1972
1973static int
1974isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
1975{
1976 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07001977 struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001978 struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
1979 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001980 struct ib_send_wr *wr_failed;
1981 int rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001982
Vu Pham90ecc6e2013-08-28 23:23:33 +03001983 pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
1984 isert_cmd, se_cmd->data_length, cmd->write_data_done);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001985 wr->iser_ib_op = ISER_IB_RDMA_READ;
Vu Pham90ecc6e2013-08-28 23:23:33 +03001986 rc = isert_map_rdma(conn, cmd, wr);
1987 if (rc) {
1988 pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
1989 return rc;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08001990 }
1991
1992 atomic_inc(&isert_conn->post_send_buf_count);
1993
1994 rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
1995 if (rc) {
1996 pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
1997 atomic_dec(&isert_conn->post_send_buf_count);
1998 }
Vu Pham90ecc6e2013-08-28 23:23:33 +03001999 pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
2000 isert_cmd);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002001
Vu Pham90ecc6e2013-08-28 23:23:33 +03002002 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002003}
2004
2005static int
2006isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2007{
2008 int ret;
2009
2010 switch (state) {
2011 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
2012 ret = isert_put_nopin(cmd, conn, false);
2013 break;
2014 default:
2015 pr_err("Unknown immediate state: 0x%02x\n", state);
2016 ret = -EINVAL;
2017 break;
2018 }
2019
2020 return ret;
2021}
2022
2023static int
2024isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
2025{
2026 int ret;
2027
2028 switch (state) {
2029 case ISTATE_SEND_LOGOUTRSP:
2030 ret = isert_put_logout_rsp(cmd, conn);
2031 if (!ret) {
2032 pr_debug("Returning iSER Logout -EAGAIN\n");
2033 ret = -EAGAIN;
2034 }
2035 break;
2036 case ISTATE_SEND_NOPIN:
2037 ret = isert_put_nopin(cmd, conn, true);
2038 break;
2039 case ISTATE_SEND_TASKMGTRSP:
2040 ret = isert_put_tm_rsp(cmd, conn);
2041 break;
2042 case ISTATE_SEND_REJECT:
2043 ret = isert_put_reject(cmd, conn);
2044 break;
Nicholas Bellingeradb54c22013-06-14 16:47:15 -07002045 case ISTATE_SEND_TEXTRSP:
2046 ret = isert_put_text_rsp(cmd, conn);
2047 break;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002048 case ISTATE_SEND_STATUS:
2049 /*
2050 * Special case for sending non GOOD SCSI status from TX thread
2051 * context during pre se_cmd excecution failure.
2052 */
2053 ret = isert_put_response(conn, cmd);
2054 break;
2055 default:
2056 pr_err("Unknown response state: 0x%02x\n", state);
2057 ret = -EINVAL;
2058 break;
2059 }
2060
2061 return ret;
2062}
2063
2064static int
2065isert_setup_np(struct iscsi_np *np,
2066 struct __kernel_sockaddr_storage *ksockaddr)
2067{
2068 struct isert_np *isert_np;
2069 struct rdma_cm_id *isert_lid;
2070 struct sockaddr *sa;
2071 int ret;
2072
2073 isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
2074 if (!isert_np) {
2075 pr_err("Unable to allocate struct isert_np\n");
2076 return -ENOMEM;
2077 }
2078 init_waitqueue_head(&isert_np->np_accept_wq);
2079 mutex_init(&isert_np->np_accept_mutex);
2080 INIT_LIST_HEAD(&isert_np->np_accept_list);
2081 init_completion(&isert_np->np_login_comp);
2082
2083 sa = (struct sockaddr *)ksockaddr;
2084 pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
2085 /*
2086 * Setup the np->np_sockaddr from the passed sockaddr setup
2087 * in iscsi_target_configfs.c code..
2088 */
2089 memcpy(&np->np_sockaddr, ksockaddr,
2090 sizeof(struct __kernel_sockaddr_storage));
2091
2092 isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
2093 IB_QPT_RC);
2094 if (IS_ERR(isert_lid)) {
2095 pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
2096 PTR_ERR(isert_lid));
2097 ret = PTR_ERR(isert_lid);
2098 goto out;
2099 }
2100
2101 ret = rdma_bind_addr(isert_lid, sa);
2102 if (ret) {
2103 pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
2104 goto out_lid;
2105 }
2106
2107 ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
2108 if (ret) {
2109 pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
2110 goto out_lid;
2111 }
2112
2113 isert_np->np_cm_id = isert_lid;
2114 np->np_context = isert_np;
2115 pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
2116
2117 return 0;
2118
2119out_lid:
2120 rdma_destroy_id(isert_lid);
2121out:
2122 kfree(isert_np);
2123 return ret;
2124}
2125
2126static int
2127isert_check_accept_queue(struct isert_np *isert_np)
2128{
2129 int empty;
2130
2131 mutex_lock(&isert_np->np_accept_mutex);
2132 empty = list_empty(&isert_np->np_accept_list);
2133 mutex_unlock(&isert_np->np_accept_mutex);
2134
2135 return empty;
2136}
2137
2138static int
2139isert_rdma_accept(struct isert_conn *isert_conn)
2140{
2141 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2142 struct rdma_conn_param cp;
2143 int ret;
2144
2145 memset(&cp, 0, sizeof(struct rdma_conn_param));
2146 cp.responder_resources = isert_conn->responder_resources;
2147 cp.initiator_depth = isert_conn->initiator_depth;
2148 cp.retry_count = 7;
2149 cp.rnr_retry_count = 7;
2150
2151 pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
2152
2153 ret = rdma_accept(cm_id, &cp);
2154 if (ret) {
2155 pr_err("rdma_accept() failed with: %d\n", ret);
2156 return ret;
2157 }
2158
2159 pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
2160
2161 return 0;
2162}
2163
2164static int
2165isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
2166{
2167 struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
2168 int ret;
2169
2170 pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
Nicholas Bellinger6faaa852013-08-18 16:35:46 -07002171 /*
2172 * For login requests after the first PDU, isert_rx_login_req() will
2173 * kick schedule_delayed_work(&conn->login_work) as the packet is
2174 * received, which turns this callback from iscsi_target_do_login_rx()
2175 * into a NOP.
2176 */
2177 if (!login->first_request)
2178 return 0;
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002179
2180 ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
2181 if (ret)
2182 return ret;
2183
2184 pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
2185 return 0;
2186}
2187
2188static void
2189isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
2190 struct isert_conn *isert_conn)
2191{
2192 struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
2193 struct rdma_route *cm_route = &cm_id->route;
2194 struct sockaddr_in *sock_in;
2195 struct sockaddr_in6 *sock_in6;
2196
2197 conn->login_family = np->np_sockaddr.ss_family;
2198
2199 if (np->np_sockaddr.ss_family == AF_INET6) {
2200 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
2201 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
2202 &sock_in6->sin6_addr.in6_u);
2203 conn->login_port = ntohs(sock_in6->sin6_port);
2204
2205 sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
2206 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
2207 &sock_in6->sin6_addr.in6_u);
2208 conn->local_port = ntohs(sock_in6->sin6_port);
2209 } else {
2210 sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
2211 sprintf(conn->login_ip, "%pI4",
2212 &sock_in->sin_addr.s_addr);
2213 conn->login_port = ntohs(sock_in->sin_port);
2214
2215 sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
2216 sprintf(conn->local_ip, "%pI4",
2217 &sock_in->sin_addr.s_addr);
2218 conn->local_port = ntohs(sock_in->sin_port);
2219 }
2220}
2221
2222static int
2223isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
2224{
2225 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2226 struct isert_conn *isert_conn;
2227 int max_accept = 0, ret;
2228
2229accept_wait:
2230 ret = wait_event_interruptible(isert_np->np_accept_wq,
2231 !isert_check_accept_queue(isert_np) ||
2232 np->np_thread_state == ISCSI_NP_THREAD_RESET);
2233 if (max_accept > 5)
2234 return -ENODEV;
2235
2236 spin_lock_bh(&np->np_thread_lock);
2237 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
2238 spin_unlock_bh(&np->np_thread_lock);
2239 pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
2240 return -ENODEV;
2241 }
2242 spin_unlock_bh(&np->np_thread_lock);
2243
2244 mutex_lock(&isert_np->np_accept_mutex);
2245 if (list_empty(&isert_np->np_accept_list)) {
2246 mutex_unlock(&isert_np->np_accept_mutex);
2247 max_accept++;
2248 goto accept_wait;
2249 }
2250 isert_conn = list_first_entry(&isert_np->np_accept_list,
2251 struct isert_conn, conn_accept_node);
2252 list_del_init(&isert_conn->conn_accept_node);
2253 mutex_unlock(&isert_np->np_accept_mutex);
2254
2255 conn->context = isert_conn;
2256 isert_conn->conn = conn;
2257 max_accept = 0;
2258
2259 ret = isert_rdma_post_recvl(isert_conn);
2260 if (ret)
2261 return ret;
2262
2263 ret = isert_rdma_accept(isert_conn);
2264 if (ret)
2265 return ret;
2266
2267 isert_set_conn_info(np, conn, isert_conn);
2268
2269 pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
2270 return 0;
2271}
2272
2273static void
2274isert_free_np(struct iscsi_np *np)
2275{
2276 struct isert_np *isert_np = (struct isert_np *)np->np_context;
2277
2278 rdma_destroy_id(isert_np->np_cm_id);
2279
2280 np->np_context = NULL;
2281 kfree(isert_np);
2282}
2283
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002284static int isert_check_state(struct isert_conn *isert_conn, int state)
2285{
2286 int ret;
2287
2288 mutex_lock(&isert_conn->conn_mutex);
2289 ret = (isert_conn->state == state);
2290 mutex_unlock(&isert_conn->conn_mutex);
2291
2292 return ret;
2293}
2294
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002295static void isert_free_conn(struct iscsi_conn *conn)
2296{
2297 struct isert_conn *isert_conn = conn->context;
2298
2299 pr_debug("isert_free_conn: Starting \n");
2300 /*
2301 * Decrement post_send_buf_count for special case when called
2302 * from isert_do_control_comp() -> iscsit_logout_post_handler()
2303 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002304 mutex_lock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002305 if (isert_conn->logout_posted)
2306 atomic_dec(&isert_conn->post_send_buf_count);
2307
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002308 if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
2309 pr_debug("Calling rdma_disconnect from isert_free_conn\n");
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002310 rdma_disconnect(isert_conn->conn_cm_id);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002311 }
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002312 /*
2313 * Only wait for conn_wait_comp_err if the isert_conn made it
2314 * into full feature phase..
2315 */
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002316 if (isert_conn->state == ISER_CONN_UP) {
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002317 pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
2318 isert_conn->state);
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002319 mutex_unlock(&isert_conn->conn_mutex);
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002320
Nicholas Bellingerb2cb9642013-07-03 03:05:37 -07002321 wait_event(isert_conn->conn_wait_comp_err,
2322 (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
2323
2324 wait_event(isert_conn->conn_wait,
2325 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
2326
2327 isert_put_conn(isert_conn);
2328 return;
2329 }
2330 if (isert_conn->state == ISER_CONN_INIT) {
2331 mutex_unlock(&isert_conn->conn_mutex);
2332 isert_put_conn(isert_conn);
2333 return;
2334 }
2335 pr_debug("isert_free_conn: wait_event conn_wait %d\n",
2336 isert_conn->state);
2337 mutex_unlock(&isert_conn->conn_mutex);
2338
2339 wait_event(isert_conn->conn_wait,
2340 (isert_check_state(isert_conn, ISER_CONN_DOWN)));
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002341
2342 isert_put_conn(isert_conn);
2343}
2344
2345static struct iscsit_transport iser_target_transport = {
2346 .name = "IB/iSER",
2347 .transport_type = ISCSI_INFINIBAND,
Nicholas Bellingerd703ce22013-08-17 14:27:56 -07002348 .priv_size = sizeof(struct isert_cmd),
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002349 .owner = THIS_MODULE,
2350 .iscsit_setup_np = isert_setup_np,
2351 .iscsit_accept_np = isert_accept_np,
2352 .iscsit_free_np = isert_free_np,
2353 .iscsit_free_conn = isert_free_conn,
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002354 .iscsit_get_login_rx = isert_get_login_rx,
2355 .iscsit_put_login_tx = isert_put_login_tx,
2356 .iscsit_immediate_queue = isert_immediate_queue,
2357 .iscsit_response_queue = isert_response_queue,
2358 .iscsit_get_dataout = isert_get_dataout,
2359 .iscsit_queue_data_in = isert_put_datain,
2360 .iscsit_queue_status = isert_put_response,
2361};
2362
2363static int __init isert_init(void)
2364{
2365 int ret;
2366
2367 isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
2368 if (!isert_rx_wq) {
2369 pr_err("Unable to allocate isert_rx_wq\n");
2370 return -ENOMEM;
2371 }
2372
2373 isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
2374 if (!isert_comp_wq) {
2375 pr_err("Unable to allocate isert_comp_wq\n");
2376 ret = -ENOMEM;
2377 goto destroy_rx_wq;
2378 }
2379
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002380 iscsit_register_transport(&iser_target_transport);
2381 pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
2382 return 0;
2383
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002384destroy_rx_wq:
2385 destroy_workqueue(isert_rx_wq);
2386 return ret;
2387}
2388
2389static void __exit isert_exit(void)
2390{
Nicholas Bellingerb8d26b32013-03-07 00:56:19 -08002391 destroy_workqueue(isert_comp_wq);
2392 destroy_workqueue(isert_rx_wq);
2393 iscsit_unregister_transport(&iser_target_transport);
2394 pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
2395}
2396
2397MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
2398MODULE_VERSION("0.1");
2399MODULE_AUTHOR("nab@Linux-iSCSI.org");
2400MODULE_LICENSE("GPL");
2401
2402module_init(isert_init);
2403module_exit(isert_exit);