1 /*
2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
4 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
38
39 #include "iscsi_iser.h"
40
41 #define ISCSI_ISER_MAX_CONN 8
42 #define ISER_MAX_RX_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
43 #define ISER_MAX_TX_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
44 #define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
45 ISCSI_ISER_MAX_CONN)
46
iser_qp_event_callback(struct ib_event *cause, void *context)47 static void iser_qp_event_callback(struct ib_event *cause, void *context)
48 {
49 iser_err("qp event %s (%d)\n",
50 ib_event_msg(cause->event), cause->event);
51 }
52
iser_event_handler(struct ib_event_handler *handler, struct ib_event *event)53 static void iser_event_handler(struct ib_event_handler *handler,
54 struct ib_event *event)
55 {
56 iser_err("async event %s (%d) on device %s port %d\n",
57 ib_event_msg(event->event), event->event,
58 dev_name(&event->device->dev), event->element.port_num);
59 }
60
61 /*
62 * iser_create_device_ib_res - creates Protection Domain (PD), Completion
63 * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
64 * the adaptor.
65 *
66 * Return: 0 on success, -1 on failure
67 */
iser_create_device_ib_res(struct iser_device *device)68 static int iser_create_device_ib_res(struct iser_device *device)
69 {
70 struct ib_device *ib_dev = device->ib_device;
71
72 if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) {
73 iser_err("IB device does not support memory registrations\n");
74 return -1;
75 }
76
77 device->pd = ib_alloc_pd(ib_dev,
78 iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
79 if (IS_ERR(device->pd))
80 goto pd_err;
81
82 INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
83 iser_event_handler);
84 ib_register_event_handler(&device->event_handler);
85 return 0;
86
87 pd_err:
88 iser_err("failed to allocate an IB resource\n");
89 return -1;
90 }
91
92 /*
93 * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
94 * CQ and PD created with the device associated with the adaptor.
95 */
iser_free_device_ib_res(struct iser_device *device)96 static void iser_free_device_ib_res(struct iser_device *device)
97 {
98 ib_unregister_event_handler(&device->event_handler);
99 ib_dealloc_pd(device->pd);
100
101 device->pd = NULL;
102 }
103
104 static struct iser_fr_desc *
iser_create_fastreg_desc(struct iser_device *device, struct ib_pd *pd, bool pi_enable, unsigned int size)105 iser_create_fastreg_desc(struct iser_device *device,
106 struct ib_pd *pd,
107 bool pi_enable,
108 unsigned int size)
109 {
110 struct iser_fr_desc *desc;
111 struct ib_device *ib_dev = device->ib_device;
112 enum ib_mr_type mr_type;
113 int ret;
114
115 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
116 if (!desc)
117 return ERR_PTR(-ENOMEM);
118
119 if (ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
120 mr_type = IB_MR_TYPE_SG_GAPS;
121 else
122 mr_type = IB_MR_TYPE_MEM_REG;
123
124 desc->rsc.mr = ib_alloc_mr(pd, mr_type, size);
125 if (IS_ERR(desc->rsc.mr)) {
126 ret = PTR_ERR(desc->rsc.mr);
127 iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
128 goto err_alloc_mr;
129 }
130
131 if (pi_enable) {
132 desc->rsc.sig_mr = ib_alloc_mr_integrity(pd, size, size);
133 if (IS_ERR(desc->rsc.sig_mr)) {
134 ret = PTR_ERR(desc->rsc.sig_mr);
135 iser_err("Failed to allocate sig_mr err=%d\n", ret);
136 goto err_alloc_mr_integrity;
137 }
138 }
139
140 return desc;
141
142 err_alloc_mr_integrity:
143 ib_dereg_mr(desc->rsc.mr);
144 err_alloc_mr:
145 kfree(desc);
146
147 return ERR_PTR(ret);
148 }
149
iser_destroy_fastreg_desc(struct iser_fr_desc *desc)150 static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc)
151 {
152 struct iser_reg_resources *res = &desc->rsc;
153
154 ib_dereg_mr(res->mr);
155 if (res->sig_mr) {
156 ib_dereg_mr(res->sig_mr);
157 res->sig_mr = NULL;
158 }
159 kfree(desc);
160 }
161
162 /**
163 * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
164 * for fast registration work requests.
165 * @ib_conn: connection RDMA resources
166 * @cmds_max: max number of SCSI commands for this connection
167 * @size: max number of pages per map request
168 *
169 * Return: 0 on success, or errno code on failure
170 */
iser_alloc_fastreg_pool(struct ib_conn *ib_conn, unsigned cmds_max, unsigned int size)171 int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
172 unsigned cmds_max,
173 unsigned int size)
174 {
175 struct iser_device *device = ib_conn->device;
176 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
177 struct iser_fr_desc *desc;
178 int i, ret;
179
180 INIT_LIST_HEAD(&fr_pool->list);
181 INIT_LIST_HEAD(&fr_pool->all_list);
182 spin_lock_init(&fr_pool->lock);
183 fr_pool->size = 0;
184 for (i = 0; i < cmds_max; i++) {
185 desc = iser_create_fastreg_desc(device, device->pd,
186 ib_conn->pi_support, size);
187 if (IS_ERR(desc)) {
188 ret = PTR_ERR(desc);
189 goto err;
190 }
191
192 list_add_tail(&desc->list, &fr_pool->list);
193 list_add_tail(&desc->all_list, &fr_pool->all_list);
194 fr_pool->size++;
195 }
196
197 return 0;
198
199 err:
200 iser_free_fastreg_pool(ib_conn);
201 return ret;
202 }
203
204 /**
205 * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
206 * @ib_conn: connection RDMA resources
207 */
iser_free_fastreg_pool(struct ib_conn *ib_conn)208 void iser_free_fastreg_pool(struct ib_conn *ib_conn)
209 {
210 struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
211 struct iser_fr_desc *desc, *tmp;
212 int i = 0;
213
214 if (list_empty(&fr_pool->all_list))
215 return;
216
217 iser_info("freeing conn %p fr pool\n", ib_conn);
218
219 list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
220 list_del(&desc->all_list);
221 iser_destroy_fastreg_desc(desc);
222 ++i;
223 }
224
225 if (i < fr_pool->size)
226 iser_warn("pool still has %d regions registered\n",
227 fr_pool->size - i);
228 }
229
230 /*
231 * iser_create_ib_conn_res - Queue-Pair (QP)
232 *
233 * Return: 0 on success, -1 on failure
234 */
iser_create_ib_conn_res(struct ib_conn *ib_conn)235 static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
236 {
237 struct iser_conn *iser_conn = to_iser_conn(ib_conn);
238 struct iser_device *device;
239 struct ib_device *ib_dev;
240 struct ib_qp_init_attr init_attr;
241 int ret = -ENOMEM;
242 unsigned int max_send_wr, cq_size;
243
244 BUG_ON(ib_conn->device == NULL);
245
246 device = ib_conn->device;
247 ib_dev = device->ib_device;
248
249 if (ib_conn->pi_support)
250 max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
251 else
252 max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
253 max_send_wr = min_t(unsigned int, max_send_wr,
254 (unsigned int)ib_dev->attrs.max_qp_wr);
255
256 cq_size = max_send_wr + ISER_QP_MAX_RECV_DTOS;
257 ib_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_SOFTIRQ);
258 if (IS_ERR(ib_conn->cq)) {
259 ret = PTR_ERR(ib_conn->cq);
260 goto cq_err;
261 }
262 ib_conn->cq_size = cq_size;
263
264 memset(&init_attr, 0, sizeof(init_attr));
265
266 init_attr.event_handler = iser_qp_event_callback;
267 init_attr.qp_context = (void *)ib_conn;
268 init_attr.send_cq = ib_conn->cq;
269 init_attr.recv_cq = ib_conn->cq;
270 init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
271 init_attr.cap.max_send_sge = 2;
272 init_attr.cap.max_recv_sge = 1;
273 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
274 init_attr.qp_type = IB_QPT_RC;
275 init_attr.cap.max_send_wr = max_send_wr;
276 if (ib_conn->pi_support)
277 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
278 iser_conn->max_cmds = ISER_GET_MAX_XMIT_CMDS(max_send_wr - 1);
279
280 ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
281 if (ret)
282 goto out_err;
283
284 ib_conn->qp = ib_conn->cma_id->qp;
285 iser_info("setting conn %p cma_id %p qp %p max_send_wr %d\n",
286 ib_conn, ib_conn->cma_id,
287 ib_conn->cma_id->qp, max_send_wr);
288 return ret;
289
290 out_err:
291 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
292 cq_err:
293 iser_err("unable to alloc mem or create resource, err %d\n", ret);
294
295 return ret;
296 }
297
298 /*
299 * based on the resolved device node GUID see if there already allocated
300 * device for this device. If there's no such, create one.
301 */
302 static
iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)303 struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
304 {
305 struct iser_device *device;
306
307 mutex_lock(&ig.device_list_mutex);
308
309 list_for_each_entry(device, &ig.device_list, ig_list)
310 /* find if there's a match using the node GUID */
311 if (device->ib_device->node_guid == cma_id->device->node_guid)
312 goto inc_refcnt;
313
314 device = kzalloc(sizeof *device, GFP_KERNEL);
315 if (device == NULL)
316 goto out;
317
318 /* assign this device to the device */
319 device->ib_device = cma_id->device;
320 /* init the device and link it into ig device list */
321 if (iser_create_device_ib_res(device)) {
322 kfree(device);
323 device = NULL;
324 goto out;
325 }
326 list_add(&device->ig_list, &ig.device_list);
327
328 inc_refcnt:
329 device->refcount++;
330 out:
331 mutex_unlock(&ig.device_list_mutex);
332 return device;
333 }
334
335 /* if there's no demand for this device, release it */
iser_device_try_release(struct iser_device *device)336 static void iser_device_try_release(struct iser_device *device)
337 {
338 mutex_lock(&ig.device_list_mutex);
339 device->refcount--;
340 iser_info("device %p refcount %d\n", device, device->refcount);
341 if (!device->refcount) {
342 iser_free_device_ib_res(device);
343 list_del(&device->ig_list);
344 kfree(device);
345 }
346 mutex_unlock(&ig.device_list_mutex);
347 }
348
349 /*
350 * Called with state mutex held
351 */
iser_conn_state_comp_exch(struct iser_conn *iser_conn, enum iser_conn_state comp, enum iser_conn_state exch)352 static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
353 enum iser_conn_state comp,
354 enum iser_conn_state exch)
355 {
356 int ret;
357
358 ret = (iser_conn->state == comp);
359 if (ret)
360 iser_conn->state = exch;
361
362 return ret;
363 }
364
iser_release_work(struct work_struct *work)365 void iser_release_work(struct work_struct *work)
366 {
367 struct iser_conn *iser_conn;
368
369 iser_conn = container_of(work, struct iser_conn, release_work);
370
371 /* Wait for conn_stop to complete */
372 wait_for_completion(&iser_conn->stop_completion);
373 /* Wait for IB resouces cleanup to complete */
374 wait_for_completion(&iser_conn->ib_completion);
375
376 mutex_lock(&iser_conn->state_mutex);
377 iser_conn->state = ISER_CONN_DOWN;
378 mutex_unlock(&iser_conn->state_mutex);
379
380 iser_conn_release(iser_conn);
381 }
382
383 /**
384 * iser_free_ib_conn_res - release IB related resources
385 * @iser_conn: iser connection struct
386 * @destroy: indicator if we need to try to release the
387 * iser device and memory regoins pool (only iscsi
388 * shutdown and DEVICE_REMOVAL will use this).
389 *
390 * This routine is called with the iser state mutex held
391 * so the cm_id removal is out of here. It is Safe to
392 * be invoked multiple times.
393 */
iser_free_ib_conn_res(struct iser_conn *iser_conn, bool destroy)394 static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
395 bool destroy)
396 {
397 struct ib_conn *ib_conn = &iser_conn->ib_conn;
398 struct iser_device *device = ib_conn->device;
399
400 iser_info("freeing conn %p cma_id %p qp %p\n",
401 iser_conn, ib_conn->cma_id, ib_conn->qp);
402
403 if (ib_conn->qp != NULL) {
404 rdma_destroy_qp(ib_conn->cma_id);
405 ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
406 ib_conn->qp = NULL;
407 }
408
409 if (destroy) {
410 if (iser_conn->rx_descs)
411 iser_free_rx_descriptors(iser_conn);
412
413 if (device != NULL) {
414 iser_device_try_release(device);
415 ib_conn->device = NULL;
416 }
417 }
418 }
419
420 /**
421 * iser_conn_release - Frees all conn objects and deallocs conn descriptor
422 * @iser_conn: iSER connection context
423 */
iser_conn_release(struct iser_conn *iser_conn)424 void iser_conn_release(struct iser_conn *iser_conn)
425 {
426 struct ib_conn *ib_conn = &iser_conn->ib_conn;
427
428 mutex_lock(&ig.connlist_mutex);
429 list_del(&iser_conn->conn_list);
430 mutex_unlock(&ig.connlist_mutex);
431
432 mutex_lock(&iser_conn->state_mutex);
433 /* In case we endup here without ep_disconnect being invoked. */
434 if (iser_conn->state != ISER_CONN_DOWN) {
435 iser_warn("iser conn %p state %d, expected state down.\n",
436 iser_conn, iser_conn->state);
437 iscsi_destroy_endpoint(iser_conn->ep);
438 iser_conn->state = ISER_CONN_DOWN;
439 }
440 /*
441 * In case we never got to bind stage, we still need to
442 * release IB resources (which is safe to call more than once).
443 */
444 iser_free_ib_conn_res(iser_conn, true);
445 mutex_unlock(&iser_conn->state_mutex);
446
447 if (ib_conn->cma_id != NULL) {
448 rdma_destroy_id(ib_conn->cma_id);
449 ib_conn->cma_id = NULL;
450 }
451
452 kfree(iser_conn);
453 }
454
455 /**
456 * iser_conn_terminate - triggers start of the disconnect procedures and
457 * waits for them to be done
458 * @iser_conn: iSER connection context
459 *
460 * Called with state mutex held
461 */
iser_conn_terminate(struct iser_conn *iser_conn)462 int iser_conn_terminate(struct iser_conn *iser_conn)
463 {
464 struct ib_conn *ib_conn = &iser_conn->ib_conn;
465 int err = 0;
466
467 /* terminate the iser conn only if the conn state is UP */
468 if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
469 ISER_CONN_TERMINATING))
470 return 0;
471
472 iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
473
474 /* suspend queuing of new iscsi commands */
475 if (iser_conn->iscsi_conn)
476 iscsi_suspend_queue(iser_conn->iscsi_conn);
477
478 /*
479 * In case we didn't already clean up the cma_id (peer initiated
480 * a disconnection), we need to Cause the CMA to change the QP
481 * state to ERROR.
482 */
483 if (ib_conn->cma_id) {
484 err = rdma_disconnect(ib_conn->cma_id);
485 if (err)
486 iser_err("Failed to disconnect, conn: 0x%p err %d\n",
487 iser_conn, err);
488
489 /* block until all flush errors are consumed */
490 ib_drain_sq(ib_conn->qp);
491 }
492
493 return 1;
494 }
495
496 /*
497 * Called with state mutex held
498 */
iser_connect_error(struct rdma_cm_id *cma_id)499 static void iser_connect_error(struct rdma_cm_id *cma_id)
500 {
501 struct iser_conn *iser_conn;
502
503 iser_conn = (struct iser_conn *)cma_id->context;
504 iser_conn->state = ISER_CONN_TERMINATING;
505 }
506
507 static void
iser_calc_scsi_params(struct iser_conn *iser_conn, unsigned int max_sectors)508 iser_calc_scsi_params(struct iser_conn *iser_conn,
509 unsigned int max_sectors)
510 {
511 struct iser_device *device = iser_conn->ib_conn.device;
512 struct ib_device_attr *attr = &device->ib_device->attrs;
513 unsigned short sg_tablesize, sup_sg_tablesize;
514 unsigned short reserved_mr_pages;
515 u32 max_num_sg;
516
517 /*
518 * FRs without SG_GAPS can only map up to a (device) page per entry,
519 * but if the first entry is misaligned we'll end up using two entries
520 * (head and tail) for a single page worth data, so one additional
521 * entry is required.
522 */
523 if (attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
524 reserved_mr_pages = 0;
525 else
526 reserved_mr_pages = 1;
527
528 if (iser_conn->ib_conn.pi_support)
529 max_num_sg = attr->max_pi_fast_reg_page_list_len;
530 else
531 max_num_sg = attr->max_fast_reg_page_list_len;
532
533 sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K);
534 sup_sg_tablesize = min_t(uint, ISCSI_ISER_MAX_SG_TABLESIZE,
535 max_num_sg - reserved_mr_pages);
536 iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
537 iser_conn->pages_per_mr =
538 iser_conn->scsi_sg_tablesize + reserved_mr_pages;
539 }
540
541 /*
542 * Called with state mutex held
543 */
iser_addr_handler(struct rdma_cm_id *cma_id)544 static void iser_addr_handler(struct rdma_cm_id *cma_id)
545 {
546 struct iser_device *device;
547 struct iser_conn *iser_conn;
548 struct ib_conn *ib_conn;
549 int ret;
550
551 iser_conn = (struct iser_conn *)cma_id->context;
552 if (iser_conn->state != ISER_CONN_PENDING)
553 /* bailout */
554 return;
555
556 ib_conn = &iser_conn->ib_conn;
557 device = iser_device_find_by_ib_device(cma_id);
558 if (!device) {
559 iser_err("device lookup/creation failed\n");
560 iser_connect_error(cma_id);
561 return;
562 }
563
564 ib_conn->device = device;
565
566 /* connection T10-PI support */
567 if (iser_pi_enable) {
568 if (!(device->ib_device->attrs.device_cap_flags &
569 IB_DEVICE_INTEGRITY_HANDOVER)) {
570 iser_warn("T10-PI requested but not supported on %s, "
571 "continue without T10-PI\n",
572 dev_name(&ib_conn->device->ib_device->dev));
573 ib_conn->pi_support = false;
574 } else {
575 ib_conn->pi_support = true;
576 }
577 }
578
579 iser_calc_scsi_params(iser_conn, iser_max_sectors);
580
581 ret = rdma_resolve_route(cma_id, 1000);
582 if (ret) {
583 iser_err("resolve route failed: %d\n", ret);
584 iser_connect_error(cma_id);
585 return;
586 }
587 }
588
589 /*
590 * Called with state mutex held
591 */
iser_route_handler(struct rdma_cm_id *cma_id)592 static void iser_route_handler(struct rdma_cm_id *cma_id)
593 {
594 struct rdma_conn_param conn_param;
595 int ret;
596 struct iser_cm_hdr req_hdr;
597 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
598 struct ib_conn *ib_conn = &iser_conn->ib_conn;
599 struct ib_device *ib_dev = ib_conn->device->ib_device;
600
601 if (iser_conn->state != ISER_CONN_PENDING)
602 /* bailout */
603 return;
604
605 ret = iser_create_ib_conn_res(ib_conn);
606 if (ret)
607 goto failure;
608
609 memset(&conn_param, 0, sizeof conn_param);
610 conn_param.responder_resources = ib_dev->attrs.max_qp_rd_atom;
611 conn_param.initiator_depth = 1;
612 conn_param.retry_count = 7;
613 conn_param.rnr_retry_count = 6;
614
615 memset(&req_hdr, 0, sizeof(req_hdr));
616 req_hdr.flags = ISER_ZBVA_NOT_SUP;
617 if (!iser_always_reg)
618 req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
619 conn_param.private_data = (void *)&req_hdr;
620 conn_param.private_data_len = sizeof(struct iser_cm_hdr);
621
622 ret = rdma_connect_locked(cma_id, &conn_param);
623 if (ret) {
624 iser_err("failure connecting: %d\n", ret);
625 goto failure;
626 }
627
628 return;
629 failure:
630 iser_connect_error(cma_id);
631 }
632
iser_connected_handler(struct rdma_cm_id *cma_id, const void *private_data)633 static void iser_connected_handler(struct rdma_cm_id *cma_id,
634 const void *private_data)
635 {
636 struct iser_conn *iser_conn;
637 struct ib_qp_attr attr;
638 struct ib_qp_init_attr init_attr;
639
640 iser_conn = (struct iser_conn *)cma_id->context;
641 if (iser_conn->state != ISER_CONN_PENDING)
642 /* bailout */
643 return;
644
645 (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
646 iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
647
648 if (private_data) {
649 u8 flags = *(u8 *)private_data;
650
651 iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
652 }
653
654 iser_info("conn %p: negotiated %s invalidation\n",
655 iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
656
657 iser_conn->state = ISER_CONN_UP;
658 complete(&iser_conn->up_completion);
659 }
660
iser_disconnected_handler(struct rdma_cm_id *cma_id)661 static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
662 {
663 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
664
665 if (iser_conn_terminate(iser_conn)) {
666 if (iser_conn->iscsi_conn)
667 iscsi_conn_failure(iser_conn->iscsi_conn,
668 ISCSI_ERR_CONN_FAILED);
669 else
670 iser_err("iscsi_iser connection isn't bound\n");
671 }
672 }
673
iser_cleanup_handler(struct rdma_cm_id *cma_id, bool destroy)674 static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
675 bool destroy)
676 {
677 struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
678
679 /*
680 * We are not guaranteed that we visited disconnected_handler
681 * by now, call it here to be safe that we handle CM drep
682 * and flush errors.
683 */
684 iser_disconnected_handler(cma_id);
685 iser_free_ib_conn_res(iser_conn, destroy);
686 complete(&iser_conn->ib_completion);
687 };
688
iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)689 static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
690 {
691 struct iser_conn *iser_conn;
692 int ret = 0;
693
694 iser_conn = (struct iser_conn *)cma_id->context;
695 iser_info("%s (%d): status %d conn %p id %p\n",
696 rdma_event_msg(event->event), event->event,
697 event->status, cma_id->context, cma_id);
698
699 mutex_lock(&iser_conn->state_mutex);
700 switch (event->event) {
701 case RDMA_CM_EVENT_ADDR_RESOLVED:
702 iser_addr_handler(cma_id);
703 break;
704 case RDMA_CM_EVENT_ROUTE_RESOLVED:
705 iser_route_handler(cma_id);
706 break;
707 case RDMA_CM_EVENT_ESTABLISHED:
708 iser_connected_handler(cma_id, event->param.conn.private_data);
709 break;
710 case RDMA_CM_EVENT_REJECTED:
711 iser_info("Connection rejected: %s\n",
712 rdma_reject_msg(cma_id, event->status));
713 fallthrough;
714 case RDMA_CM_EVENT_ADDR_ERROR:
715 case RDMA_CM_EVENT_ROUTE_ERROR:
716 case RDMA_CM_EVENT_CONNECT_ERROR:
717 case RDMA_CM_EVENT_UNREACHABLE:
718 iser_connect_error(cma_id);
719 break;
720 case RDMA_CM_EVENT_DISCONNECTED:
721 case RDMA_CM_EVENT_ADDR_CHANGE:
722 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
723 iser_cleanup_handler(cma_id, false);
724 break;
725 case RDMA_CM_EVENT_DEVICE_REMOVAL:
726 /*
727 * we *must* destroy the device as we cannot rely
728 * on iscsid to be around to initiate error handling.
729 * also if we are not in state DOWN implicitly destroy
730 * the cma_id.
731 */
732 iser_cleanup_handler(cma_id, true);
733 if (iser_conn->state != ISER_CONN_DOWN) {
734 iser_conn->ib_conn.cma_id = NULL;
735 ret = 1;
736 }
737 break;
738 default:
739 iser_err("Unexpected RDMA CM event: %s (%d)\n",
740 rdma_event_msg(event->event), event->event);
741 break;
742 }
743 mutex_unlock(&iser_conn->state_mutex);
744
745 return ret;
746 }
747
iser_conn_init(struct iser_conn *iser_conn)748 void iser_conn_init(struct iser_conn *iser_conn)
749 {
750 struct ib_conn *ib_conn = &iser_conn->ib_conn;
751
752 iser_conn->state = ISER_CONN_INIT;
753 init_completion(&iser_conn->stop_completion);
754 init_completion(&iser_conn->ib_completion);
755 init_completion(&iser_conn->up_completion);
756 INIT_LIST_HEAD(&iser_conn->conn_list);
757 mutex_init(&iser_conn->state_mutex);
758
759 ib_conn->post_recv_buf_count = 0;
760 ib_conn->reg_cqe.done = iser_reg_comp;
761 }
762
763 /**
764 * starts the process of connecting to the target
765 * sleeps until the connection is established or rejected
766 */
iser_connect(struct iser_conn *iser_conn, struct sockaddr *src_addr, struct sockaddr *dst_addr, int non_blocking)767 int iser_connect(struct iser_conn *iser_conn,
768 struct sockaddr *src_addr,
769 struct sockaddr *dst_addr,
770 int non_blocking)
771 {
772 struct ib_conn *ib_conn = &iser_conn->ib_conn;
773 int err = 0;
774
775 mutex_lock(&iser_conn->state_mutex);
776
777 sprintf(iser_conn->name, "%pISp", dst_addr);
778
779 iser_info("connecting to: %s\n", iser_conn->name);
780
781 /* the device is known only --after-- address resolution */
782 ib_conn->device = NULL;
783
784 iser_conn->state = ISER_CONN_PENDING;
785
786 ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
787 (void *)iser_conn,
788 RDMA_PS_TCP, IB_QPT_RC);
789 if (IS_ERR(ib_conn->cma_id)) {
790 err = PTR_ERR(ib_conn->cma_id);
791 iser_err("rdma_create_id failed: %d\n", err);
792 goto id_failure;
793 }
794
795 err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
796 if (err) {
797 iser_err("rdma_resolve_addr failed: %d\n", err);
798 goto addr_failure;
799 }
800
801 if (!non_blocking) {
802 wait_for_completion_interruptible(&iser_conn->up_completion);
803
804 if (iser_conn->state != ISER_CONN_UP) {
805 err = -EIO;
806 goto connect_failure;
807 }
808 }
809 mutex_unlock(&iser_conn->state_mutex);
810
811 mutex_lock(&ig.connlist_mutex);
812 list_add(&iser_conn->conn_list, &ig.connlist);
813 mutex_unlock(&ig.connlist_mutex);
814 return 0;
815
816 id_failure:
817 ib_conn->cma_id = NULL;
818 addr_failure:
819 iser_conn->state = ISER_CONN_DOWN;
820 connect_failure:
821 mutex_unlock(&iser_conn->state_mutex);
822 iser_conn_release(iser_conn);
823 return err;
824 }
825
iser_post_recvl(struct iser_conn *iser_conn)826 int iser_post_recvl(struct iser_conn *iser_conn)
827 {
828 struct ib_conn *ib_conn = &iser_conn->ib_conn;
829 struct iser_login_desc *desc = &iser_conn->login_desc;
830 struct ib_recv_wr wr;
831 int ib_ret;
832
833 desc->sge.addr = desc->rsp_dma;
834 desc->sge.length = ISER_RX_LOGIN_SIZE;
835 desc->sge.lkey = ib_conn->device->pd->local_dma_lkey;
836
837 desc->cqe.done = iser_login_rsp;
838 wr.wr_cqe = &desc->cqe;
839 wr.sg_list = &desc->sge;
840 wr.num_sge = 1;
841 wr.next = NULL;
842
843 ib_conn->post_recv_buf_count++;
844 ib_ret = ib_post_recv(ib_conn->qp, &wr, NULL);
845 if (ib_ret) {
846 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
847 ib_conn->post_recv_buf_count--;
848 }
849
850 return ib_ret;
851 }
852
iser_post_recvm(struct iser_conn *iser_conn, int count)853 int iser_post_recvm(struct iser_conn *iser_conn, int count)
854 {
855 struct ib_conn *ib_conn = &iser_conn->ib_conn;
856 unsigned int my_rx_head = iser_conn->rx_desc_head;
857 struct iser_rx_desc *rx_desc;
858 struct ib_recv_wr *wr;
859 int i, ib_ret;
860
861 for (wr = ib_conn->rx_wr, i = 0; i < count; i++, wr++) {
862 rx_desc = &iser_conn->rx_descs[my_rx_head];
863 rx_desc->cqe.done = iser_task_rsp;
864 wr->wr_cqe = &rx_desc->cqe;
865 wr->sg_list = &rx_desc->rx_sg;
866 wr->num_sge = 1;
867 wr->next = wr + 1;
868 my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
869 }
870
871 wr--;
872 wr->next = NULL; /* mark end of work requests list */
873
874 ib_conn->post_recv_buf_count += count;
875 ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, NULL);
876 if (unlikely(ib_ret)) {
877 iser_err("ib_post_recv failed ret=%d\n", ib_ret);
878 ib_conn->post_recv_buf_count -= count;
879 } else
880 iser_conn->rx_desc_head = my_rx_head;
881
882 return ib_ret;
883 }
884
885
886 /**
887 * iser_post_send - Initiate a Send DTO operation
888 * @ib_conn: connection RDMA resources
889 * @tx_desc: iSER TX descriptor
890 * @signal: true to send work request as SIGNALED
891 *
892 * Return: 0 on success, -1 on failure
893 */
iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc, bool signal)894 int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
895 bool signal)
896 {
897 struct ib_send_wr *wr = &tx_desc->send_wr;
898 struct ib_send_wr *first_wr;
899 int ib_ret;
900
901 ib_dma_sync_single_for_device(ib_conn->device->ib_device,
902 tx_desc->dma_addr, ISER_HEADERS_LEN,
903 DMA_TO_DEVICE);
904
905 wr->next = NULL;
906 wr->wr_cqe = &tx_desc->cqe;
907 wr->sg_list = tx_desc->tx_sg;
908 wr->num_sge = tx_desc->num_sge;
909 wr->opcode = IB_WR_SEND;
910 wr->send_flags = signal ? IB_SEND_SIGNALED : 0;
911
912 if (tx_desc->inv_wr.next)
913 first_wr = &tx_desc->inv_wr;
914 else if (tx_desc->reg_wr.wr.next)
915 first_wr = &tx_desc->reg_wr.wr;
916 else
917 first_wr = wr;
918
919 ib_ret = ib_post_send(ib_conn->qp, first_wr, NULL);
920 if (unlikely(ib_ret))
921 iser_err("ib_post_send failed, ret:%d opcode:%d\n",
922 ib_ret, wr->opcode);
923
924 return ib_ret;
925 }
926
iser_check_task_pi_status(struct iscsi_iser_task *iser_task, enum iser_data_dir cmd_dir, sector_t *sector)927 u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
928 enum iser_data_dir cmd_dir, sector_t *sector)
929 {
930 struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
931 struct iser_fr_desc *desc = reg->mem_h;
932 unsigned long sector_size = iser_task->sc->device->sector_size;
933 struct ib_mr_status mr_status;
934 int ret;
935
936 if (desc && desc->sig_protected) {
937 desc->sig_protected = false;
938 ret = ib_check_mr_status(desc->rsc.sig_mr,
939 IB_MR_CHECK_SIG_STATUS, &mr_status);
940 if (ret) {
941 iser_err("ib_check_mr_status failed, ret %d\n", ret);
942 /* Not a lot we can do, return ambiguous guard error */
943 *sector = 0;
944 return 0x1;
945 }
946
947 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
948 sector_t sector_off = mr_status.sig_err.sig_err_offset;
949
950 sector_div(sector_off, sector_size + 8);
951 *sector = scsi_get_lba(iser_task->sc) + sector_off;
952
953 iser_err("PI error found type %d at sector %llx "
954 "expected %x vs actual %x\n",
955 mr_status.sig_err.err_type,
956 (unsigned long long)*sector,
957 mr_status.sig_err.expected,
958 mr_status.sig_err.actual);
959
960 switch (mr_status.sig_err.err_type) {
961 case IB_SIG_BAD_GUARD:
962 return 0x1;
963 case IB_SIG_BAD_REFTAG:
964 return 0x3;
965 case IB_SIG_BAD_APPTAG:
966 return 0x2;
967 }
968 }
969 }
970
971 return 0;
972 }
973
iser_err_comp(struct ib_wc *wc, const char *type)974 void iser_err_comp(struct ib_wc *wc, const char *type)
975 {
976 if (wc->status != IB_WC_WR_FLUSH_ERR) {
977 struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
978
979 iser_err("%s failure: %s (%d) vend_err %#x\n", type,
980 ib_wc_status_msg(wc->status), wc->status,
981 wc->vendor_err);
982
983 if (iser_conn->iscsi_conn)
984 iscsi_conn_failure(iser_conn->iscsi_conn,
985 ISCSI_ERR_CONN_FAILED);
986 } else {
987 iser_dbg("%s failure: %s (%d)\n", type,
988 ib_wc_status_msg(wc->status), wc->status);
989 }
990 }
991