1 /*
2  * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/mm.h>
37 #include <linux/highmem.h>
38 #include <linux/scatterlist.h>
39 
40 #include "iscsi_iser.h"
41 static
42 int iser_fast_reg_fmr(struct iscsi_iser_task *iser_task,
43 		      struct iser_data_buf *mem,
44 		      struct iser_reg_resources *rsc,
45 		      struct iser_mem_reg *mem_reg);
46 static
47 int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
48 		     struct iser_data_buf *mem,
49 		     struct iser_reg_resources *rsc,
50 		     struct iser_mem_reg *mem_reg);
51 
52 static const struct iser_reg_ops fastreg_ops = {
53 	.alloc_reg_res	= iser_alloc_fastreg_pool,
54 	.free_reg_res	= iser_free_fastreg_pool,
55 	.reg_mem	= iser_fast_reg_mr,
56 	.unreg_mem	= iser_unreg_mem_fastreg,
57 	.reg_desc_get	= iser_reg_desc_get_fr,
58 	.reg_desc_put	= iser_reg_desc_put_fr,
59 };
60 
61 static const struct iser_reg_ops fmr_ops = {
62 	.alloc_reg_res	= iser_alloc_fmr_pool,
63 	.free_reg_res	= iser_free_fmr_pool,
64 	.reg_mem	= iser_fast_reg_fmr,
65 	.unreg_mem	= iser_unreg_mem_fmr,
66 	.reg_desc_get	= iser_reg_desc_get_fmr,
67 	.reg_desc_put	= iser_reg_desc_put_fmr,
68 };
69 
70 void iser_reg_comp(struct ib_cq *cq, struct ib_wc *wc)
71 {
72 	iser_err_comp(wc, "memreg");
73 }
74 
75 int iser_assign_reg_ops(struct iser_device *device)
76 {
77 	struct ib_device *ib_dev = device->ib_device;
78 
79 	/* Assign function handles  - based on FMR support */
80 	if (ib_dev->alloc_fmr && ib_dev->dealloc_fmr &&
81 	    ib_dev->map_phys_fmr && ib_dev->unmap_fmr) {
82 		iser_info("FMR supported, using FMR for registration\n");
83 		device->reg_ops = &fmr_ops;
84 	} else if (ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
85 		iser_info("FastReg supported, using FastReg for registration\n");
86 		device->reg_ops = &fastreg_ops;
87 		device->remote_inv_sup = iser_always_reg;
88 	} else {
89 		iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
90 		return -1;
91 	}
92 
93 	return 0;
94 }
95 
96 struct iser_fr_desc *
97 iser_reg_desc_get_fr(struct ib_conn *ib_conn)
98 {
99 	struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
100 	struct iser_fr_desc *desc;
101 	unsigned long flags;
102 
103 	spin_lock_irqsave(&fr_pool->lock, flags);
104 	desc = list_first_entry(&fr_pool->list,
105 				struct iser_fr_desc, list);
106 	list_del(&desc->list);
107 	spin_unlock_irqrestore(&fr_pool->lock, flags);
108 
109 	return desc;
110 }
111 
112 void
113 iser_reg_desc_put_fr(struct ib_conn *ib_conn,
114 		     struct iser_fr_desc *desc)
115 {
116 	struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
117 	unsigned long flags;
118 
119 	spin_lock_irqsave(&fr_pool->lock, flags);
120 	list_add(&desc->list, &fr_pool->list);
121 	spin_unlock_irqrestore(&fr_pool->lock, flags);
122 }
123 
124 struct iser_fr_desc *
125 iser_reg_desc_get_fmr(struct ib_conn *ib_conn)
126 {
127 	struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
128 
129 	return list_first_entry(&fr_pool->list,
130 				struct iser_fr_desc, list);
131 }
132 
133 void
134 iser_reg_desc_put_fmr(struct ib_conn *ib_conn,
135 		      struct iser_fr_desc *desc)
136 {
137 }
138 
139 static void iser_data_buf_dump(struct iser_data_buf *data,
140 			       struct ib_device *ibdev)
141 {
142 	struct scatterlist *sg;
143 	int i;
144 
145 	for_each_sg(data->sg, sg, data->dma_nents, i)
146 		iser_dbg("sg[%d] dma_addr:0x%lX page:0x%p "
147 			 "off:0x%x sz:0x%x dma_len:0x%x\n",
148 			 i, (unsigned long)ib_sg_dma_address(ibdev, sg),
149 			 sg_page(sg), sg->offset,
150 			 sg->length, ib_sg_dma_len(ibdev, sg));
151 }
152 
153 static void iser_dump_page_vec(struct iser_page_vec *page_vec)
154 {
155 	int i;
156 
157 	iser_err("page vec npages %d data length %d\n",
158 		 page_vec->npages, page_vec->fake_mr.length);
159 	for (i = 0; i < page_vec->npages; i++)
160 		iser_err("vec[%d]: %llx\n", i, page_vec->pages[i]);
161 }
162 
163 int iser_dma_map_task_data(struct iscsi_iser_task *iser_task,
164 			    struct iser_data_buf *data,
165 			    enum iser_data_dir iser_dir,
166 			    enum dma_data_direction dma_dir)
167 {
168 	struct ib_device *dev;
169 
170 	iser_task->dir[iser_dir] = 1;
171 	dev = iser_task->iser_conn->ib_conn.device->ib_device;
172 
173 	data->dma_nents = ib_dma_map_sg(dev, data->sg, data->size, dma_dir);
174 	if (data->dma_nents == 0) {
175 		iser_err("dma_map_sg failed!!!\n");
176 		return -EINVAL;
177 	}
178 	return 0;
179 }
180 
181 void iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task,
182 			      struct iser_data_buf *data,
183 			      enum dma_data_direction dir)
184 {
185 	struct ib_device *dev;
186 
187 	dev = iser_task->iser_conn->ib_conn.device->ib_device;
188 	ib_dma_unmap_sg(dev, data->sg, data->size, dir);
189 }
190 
191 static int
192 iser_reg_dma(struct iser_device *device, struct iser_data_buf *mem,
193 	     struct iser_mem_reg *reg)
194 {
195 	struct scatterlist *sg = mem->sg;
196 
197 	reg->sge.lkey = device->pd->local_dma_lkey;
198 	/*
199 	 * FIXME: rework the registration code path to differentiate
200 	 * rkey/lkey use cases
201 	 */
202 	reg->rkey = device->mr ? device->mr->rkey : 0;
203 	reg->sge.addr = ib_sg_dma_address(device->ib_device, &sg[0]);
204 	reg->sge.length = ib_sg_dma_len(device->ib_device, &sg[0]);
205 
206 	iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
207 		 " length=0x%x\n", reg->sge.lkey, reg->rkey,
208 		 reg->sge.addr, reg->sge.length);
209 
210 	return 0;
211 }
212 
213 static int iser_set_page(struct ib_mr *mr, u64 addr)
214 {
215 	struct iser_page_vec *page_vec =
216 		container_of(mr, struct iser_page_vec, fake_mr);
217 
218 	page_vec->pages[page_vec->npages++] = addr;
219 
220 	return 0;
221 }
222 
223 static
224 int iser_fast_reg_fmr(struct iscsi_iser_task *iser_task,
225 		      struct iser_data_buf *mem,
226 		      struct iser_reg_resources *rsc,
227 		      struct iser_mem_reg *reg)
228 {
229 	struct ib_conn *ib_conn = &iser_task->iser_conn->ib_conn;
230 	struct iser_device *device = ib_conn->device;
231 	struct iser_page_vec *page_vec = rsc->page_vec;
232 	struct ib_fmr_pool *fmr_pool = rsc->fmr_pool;
233 	struct ib_pool_fmr *fmr;
234 	int ret, plen;
235 
236 	page_vec->npages = 0;
237 	page_vec->fake_mr.page_size = SIZE_4K;
238 	plen = ib_sg_to_pages(&page_vec->fake_mr, mem->sg,
239 			      mem->size, NULL, iser_set_page);
240 	if (unlikely(plen < mem->size)) {
241 		iser_err("page vec too short to hold this SG\n");
242 		iser_data_buf_dump(mem, device->ib_device);
243 		iser_dump_page_vec(page_vec);
244 		return -EINVAL;
245 	}
246 
247 	fmr  = ib_fmr_pool_map_phys(fmr_pool, page_vec->pages,
248 				    page_vec->npages, page_vec->pages[0]);
249 	if (IS_ERR(fmr)) {
250 		ret = PTR_ERR(fmr);
251 		iser_err("ib_fmr_pool_map_phys failed: %d\n", ret);
252 		return ret;
253 	}
254 
255 	reg->sge.lkey = fmr->fmr->lkey;
256 	reg->rkey = fmr->fmr->rkey;
257 	reg->sge.addr = page_vec->fake_mr.iova;
258 	reg->sge.length = page_vec->fake_mr.length;
259 	reg->mem_h = fmr;
260 
261 	iser_dbg("fmr reg: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
262 		 " length=0x%x\n", reg->sge.lkey, reg->rkey,
263 		 reg->sge.addr, reg->sge.length);
264 
265 	return 0;
266 }
267 
268 /**
269  * Unregister (previosuly registered using FMR) memory.
270  * If memory is non-FMR does nothing.
271  */
272 void iser_unreg_mem_fmr(struct iscsi_iser_task *iser_task,
273 			enum iser_data_dir cmd_dir)
274 {
275 	struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
276 	int ret;
277 
278 	if (!reg->mem_h)
279 		return;
280 
281 	iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n", reg->mem_h);
282 
283 	ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
284 	if (ret)
285 		iser_err("ib_fmr_pool_unmap failed %d\n", ret);
286 
287 	reg->mem_h = NULL;
288 }
289 
290 void iser_unreg_mem_fastreg(struct iscsi_iser_task *iser_task,
291 			    enum iser_data_dir cmd_dir)
292 {
293 	struct iser_device *device = iser_task->iser_conn->ib_conn.device;
294 	struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
295 
296 	if (!reg->mem_h)
297 		return;
298 
299 	device->reg_ops->reg_desc_put(&iser_task->iser_conn->ib_conn,
300 				     reg->mem_h);
301 	reg->mem_h = NULL;
302 }
303 
304 static void
305 iser_set_dif_domain(struct scsi_cmnd *sc, struct ib_sig_attrs *sig_attrs,
306 		    struct ib_sig_domain *domain)
307 {
308 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
309 	domain->sig.dif.pi_interval = scsi_prot_interval(sc);
310 	domain->sig.dif.ref_tag = scsi_prot_ref_tag(sc);
311 	/*
312 	 * At the moment we hard code those, but in the future
313 	 * we will take them from sc.
314 	 */
315 	domain->sig.dif.apptag_check_mask = 0xffff;
316 	domain->sig.dif.app_escape = true;
317 	domain->sig.dif.ref_escape = true;
318 	if (sc->prot_flags & SCSI_PROT_REF_INCREMENT)
319 		domain->sig.dif.ref_remap = true;
320 };
321 
322 static int
323 iser_set_sig_attrs(struct scsi_cmnd *sc, struct ib_sig_attrs *sig_attrs)
324 {
325 	switch (scsi_get_prot_op(sc)) {
326 	case SCSI_PROT_WRITE_INSERT:
327 	case SCSI_PROT_READ_STRIP:
328 		sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
329 		iser_set_dif_domain(sc, sig_attrs, &sig_attrs->wire);
330 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
331 		break;
332 	case SCSI_PROT_READ_INSERT:
333 	case SCSI_PROT_WRITE_STRIP:
334 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
335 		iser_set_dif_domain(sc, sig_attrs, &sig_attrs->mem);
336 		sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
337 						IB_T10DIF_CSUM : IB_T10DIF_CRC;
338 		break;
339 	case SCSI_PROT_READ_PASS:
340 	case SCSI_PROT_WRITE_PASS:
341 		iser_set_dif_domain(sc, sig_attrs, &sig_attrs->wire);
342 		sig_attrs->wire.sig.dif.bg_type = IB_T10DIF_CRC;
343 		iser_set_dif_domain(sc, sig_attrs, &sig_attrs->mem);
344 		sig_attrs->mem.sig.dif.bg_type = sc->prot_flags & SCSI_PROT_IP_CHECKSUM ?
345 						IB_T10DIF_CSUM : IB_T10DIF_CRC;
346 		break;
347 	default:
348 		iser_err("Unsupported PI operation %d\n",
349 			 scsi_get_prot_op(sc));
350 		return -EINVAL;
351 	}
352 
353 	return 0;
354 }
355 
356 static inline void
357 iser_set_prot_checks(struct scsi_cmnd *sc, u8 *mask)
358 {
359 	*mask = 0;
360 	if (sc->prot_flags & SCSI_PROT_REF_CHECK)
361 		*mask |= ISER_CHECK_REFTAG;
362 	if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
363 		*mask |= ISER_CHECK_GUARD;
364 }
365 
366 static inline void
367 iser_inv_rkey(struct ib_send_wr *inv_wr,
368 	      struct ib_mr *mr,
369 	      struct ib_cqe *cqe)
370 {
371 	inv_wr->opcode = IB_WR_LOCAL_INV;
372 	inv_wr->wr_cqe = cqe;
373 	inv_wr->ex.invalidate_rkey = mr->rkey;
374 	inv_wr->send_flags = 0;
375 	inv_wr->num_sge = 0;
376 }
377 
378 static int
379 iser_reg_sig_mr(struct iscsi_iser_task *iser_task,
380 		struct iser_pi_context *pi_ctx,
381 		struct iser_mem_reg *data_reg,
382 		struct iser_mem_reg *prot_reg,
383 		struct iser_mem_reg *sig_reg)
384 {
385 	struct iser_tx_desc *tx_desc = &iser_task->desc;
386 	struct ib_sig_attrs *sig_attrs = &tx_desc->sig_attrs;
387 	struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
388 	struct ib_sig_handover_wr *wr;
389 	struct ib_mr *mr = pi_ctx->sig_mr;
390 	int ret;
391 
392 	memset(sig_attrs, 0, sizeof(*sig_attrs));
393 	ret = iser_set_sig_attrs(iser_task->sc, sig_attrs);
394 	if (ret)
395 		goto err;
396 
397 	iser_set_prot_checks(iser_task->sc, &sig_attrs->check_mask);
398 
399 	if (pi_ctx->sig_mr_valid)
400 		iser_inv_rkey(iser_tx_next_wr(tx_desc), mr, cqe);
401 
402 	ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
403 
404 	wr = sig_handover_wr(iser_tx_next_wr(tx_desc));
405 	wr->wr.opcode = IB_WR_REG_SIG_MR;
406 	wr->wr.wr_cqe = cqe;
407 	wr->wr.sg_list = &data_reg->sge;
408 	wr->wr.num_sge = 1;
409 	wr->wr.send_flags = 0;
410 	wr->sig_attrs = sig_attrs;
411 	wr->sig_mr = mr;
412 	if (scsi_prot_sg_count(iser_task->sc))
413 		wr->prot = &prot_reg->sge;
414 	else
415 		wr->prot = NULL;
416 	wr->access_flags = IB_ACCESS_LOCAL_WRITE |
417 			   IB_ACCESS_REMOTE_READ |
418 			   IB_ACCESS_REMOTE_WRITE;
419 	pi_ctx->sig_mr_valid = 1;
420 
421 	sig_reg->sge.lkey = mr->lkey;
422 	sig_reg->rkey = mr->rkey;
423 	sig_reg->sge.addr = 0;
424 	sig_reg->sge.length = scsi_transfer_length(iser_task->sc);
425 
426 	iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
427 		 sig_reg->sge.lkey, sig_reg->rkey, sig_reg->sge.addr,
428 		 sig_reg->sge.length);
429 err:
430 	return ret;
431 }
432 
433 static int iser_fast_reg_mr(struct iscsi_iser_task *iser_task,
434 			    struct iser_data_buf *mem,
435 			    struct iser_reg_resources *rsc,
436 			    struct iser_mem_reg *reg)
437 {
438 	struct iser_tx_desc *tx_desc = &iser_task->desc;
439 	struct ib_cqe *cqe = &iser_task->iser_conn->ib_conn.reg_cqe;
440 	struct ib_mr *mr = rsc->mr;
441 	struct ib_reg_wr *wr;
442 	int n;
443 
444 	if (rsc->mr_valid)
445 		iser_inv_rkey(iser_tx_next_wr(tx_desc), mr, cqe);
446 
447 	ib_update_fast_reg_key(mr, ib_inc_rkey(mr->rkey));
448 
449 	n = ib_map_mr_sg(mr, mem->sg, mem->size, NULL, SIZE_4K);
450 	if (unlikely(n != mem->size)) {
451 		iser_err("failed to map sg (%d/%d)\n",
452 			 n, mem->size);
453 		return n < 0 ? n : -EINVAL;
454 	}
455 
456 	wr = reg_wr(iser_tx_next_wr(tx_desc));
457 	wr->wr.opcode = IB_WR_REG_MR;
458 	wr->wr.wr_cqe = cqe;
459 	wr->wr.send_flags = 0;
460 	wr->wr.num_sge = 0;
461 	wr->mr = mr;
462 	wr->key = mr->rkey;
463 	wr->access = IB_ACCESS_LOCAL_WRITE  |
464 		     IB_ACCESS_REMOTE_WRITE |
465 		     IB_ACCESS_REMOTE_READ;
466 
467 	rsc->mr_valid = 1;
468 
469 	reg->sge.lkey = mr->lkey;
470 	reg->rkey = mr->rkey;
471 	reg->sge.addr = mr->iova;
472 	reg->sge.length = mr->length;
473 
474 	iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
475 		 reg->sge.lkey, reg->rkey, reg->sge.addr, reg->sge.length);
476 
477 	return 0;
478 }
479 
480 static int
481 iser_reg_prot_sg(struct iscsi_iser_task *task,
482 		 struct iser_data_buf *mem,
483 		 struct iser_fr_desc *desc,
484 		 bool use_dma_key,
485 		 struct iser_mem_reg *reg)
486 {
487 	struct iser_device *device = task->iser_conn->ib_conn.device;
488 
489 	if (use_dma_key)
490 		return iser_reg_dma(device, mem, reg);
491 
492 	return device->reg_ops->reg_mem(task, mem, &desc->pi_ctx->rsc, reg);
493 }
494 
495 static int
496 iser_reg_data_sg(struct iscsi_iser_task *task,
497 		 struct iser_data_buf *mem,
498 		 struct iser_fr_desc *desc,
499 		 bool use_dma_key,
500 		 struct iser_mem_reg *reg)
501 {
502 	struct iser_device *device = task->iser_conn->ib_conn.device;
503 
504 	if (use_dma_key)
505 		return iser_reg_dma(device, mem, reg);
506 
507 	return device->reg_ops->reg_mem(task, mem, &desc->rsc, reg);
508 }
509 
510 int iser_reg_rdma_mem(struct iscsi_iser_task *task,
511 		      enum iser_data_dir dir,
512 		      bool all_imm)
513 {
514 	struct ib_conn *ib_conn = &task->iser_conn->ib_conn;
515 	struct iser_device *device = ib_conn->device;
516 	struct iser_data_buf *mem = &task->data[dir];
517 	struct iser_mem_reg *reg = &task->rdma_reg[dir];
518 	struct iser_mem_reg *data_reg;
519 	struct iser_fr_desc *desc = NULL;
520 	bool use_dma_key;
521 	int err;
522 
523 	use_dma_key = mem->dma_nents == 1 && (all_imm || !iser_always_reg) &&
524 		      scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL;
525 
526 	if (!use_dma_key) {
527 		desc = device->reg_ops->reg_desc_get(ib_conn);
528 		reg->mem_h = desc;
529 	}
530 
531 	if (scsi_get_prot_op(task->sc) == SCSI_PROT_NORMAL)
532 		data_reg = reg;
533 	else
534 		data_reg = &task->desc.data_reg;
535 
536 	err = iser_reg_data_sg(task, mem, desc, use_dma_key, data_reg);
537 	if (unlikely(err))
538 		goto err_reg;
539 
540 	if (scsi_get_prot_op(task->sc) != SCSI_PROT_NORMAL) {
541 		struct iser_mem_reg *prot_reg = &task->desc.prot_reg;
542 
543 		if (scsi_prot_sg_count(task->sc)) {
544 			mem = &task->prot[dir];
545 			err = iser_reg_prot_sg(task, mem, desc,
546 					       use_dma_key, prot_reg);
547 			if (unlikely(err))
548 				goto err_reg;
549 		}
550 
551 		err = iser_reg_sig_mr(task, desc->pi_ctx, data_reg,
552 				      prot_reg, reg);
553 		if (unlikely(err))
554 			goto err_reg;
555 
556 		desc->pi_ctx->sig_protected = 1;
557 	}
558 
559 	return 0;
560 
561 err_reg:
562 	if (desc)
563 		device->reg_ops->reg_desc_put(ib_conn, desc);
564 
565 	return err;
566 }
567 
568 void iser_unreg_rdma_mem(struct iscsi_iser_task *task,
569 			 enum iser_data_dir dir)
570 {
571 	struct iser_device *device = task->iser_conn->ib_conn.device;
572 
573 	device->reg_ops->unreg_mem(task, dir);
574 }
575