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