2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
37 #include <linux/highmem.h>
38 #include <linux/scatterlist.h>
40 #include "iscsi_iser.h"
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
);
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
);
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
,
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
,
70 void iser_reg_comp(struct ib_cq
*cq
, struct ib_wc
*wc
)
72 iser_err_comp(wc
, "memreg");
75 int iser_assign_reg_ops(struct iser_device
*device
)
77 struct ib_device
*ib_dev
= device
->ib_device
;
79 /* Assign function handles - based on FMR support */
80 if (ib_dev
->ops
.alloc_fmr
&& ib_dev
->ops
.dealloc_fmr
&&
81 ib_dev
->ops
.map_phys_fmr
&& ib_dev
->ops
.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
;
89 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
97 iser_reg_desc_get_fr(struct ib_conn
*ib_conn
)
99 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
100 struct iser_fr_desc
*desc
;
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
);
113 iser_reg_desc_put_fr(struct ib_conn
*ib_conn
,
114 struct iser_fr_desc
*desc
)
116 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
119 spin_lock_irqsave(&fr_pool
->lock
, flags
);
120 list_add(&desc
->list
, &fr_pool
->list
);
121 spin_unlock_irqrestore(&fr_pool
->lock
, flags
);
124 struct iser_fr_desc
*
125 iser_reg_desc_get_fmr(struct ib_conn
*ib_conn
)
127 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
129 return list_first_entry(&fr_pool
->list
,
130 struct iser_fr_desc
, list
);
134 iser_reg_desc_put_fmr(struct ib_conn
*ib_conn
,
135 struct iser_fr_desc
*desc
)
139 static void iser_data_buf_dump(struct iser_data_buf
*data
,
140 struct ib_device
*ibdev
)
142 struct scatterlist
*sg
;
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)sg_dma_address(sg
),
149 sg_page(sg
), sg
->offset
, sg
->length
, sg_dma_len(sg
));
152 static void iser_dump_page_vec(struct iser_page_vec
*page_vec
)
156 iser_err("page vec npages %d data length %lld\n",
157 page_vec
->npages
, page_vec
->fake_mr
.length
);
158 for (i
= 0; i
< page_vec
->npages
; i
++)
159 iser_err("vec[%d]: %llx\n", i
, page_vec
->pages
[i
]);
162 int iser_dma_map_task_data(struct iscsi_iser_task
*iser_task
,
163 struct iser_data_buf
*data
,
164 enum iser_data_dir iser_dir
,
165 enum dma_data_direction dma_dir
)
167 struct ib_device
*dev
;
169 iser_task
->dir
[iser_dir
] = 1;
170 dev
= iser_task
->iser_conn
->ib_conn
.device
->ib_device
;
172 data
->dma_nents
= ib_dma_map_sg(dev
, data
->sg
, data
->size
, dma_dir
);
173 if (unlikely(data
->dma_nents
== 0)) {
174 iser_err("dma_map_sg failed!!!\n");
180 void iser_dma_unmap_task_data(struct iscsi_iser_task
*iser_task
,
181 struct iser_data_buf
*data
,
182 enum dma_data_direction dir
)
184 struct ib_device
*dev
;
186 dev
= iser_task
->iser_conn
->ib_conn
.device
->ib_device
;
187 ib_dma_unmap_sg(dev
, data
->sg
, data
->size
, dir
);
191 iser_reg_dma(struct iser_device
*device
, struct iser_data_buf
*mem
,
192 struct iser_mem_reg
*reg
)
194 struct scatterlist
*sg
= mem
->sg
;
196 reg
->sge
.lkey
= device
->pd
->local_dma_lkey
;
198 * FIXME: rework the registration code path to differentiate
199 * rkey/lkey use cases
202 if (device
->pd
->flags
& IB_PD_UNSAFE_GLOBAL_RKEY
)
203 reg
->rkey
= device
->pd
->unsafe_global_rkey
;
206 reg
->sge
.addr
= sg_dma_address(&sg
[0]);
207 reg
->sge
.length
= sg_dma_len(&sg
[0]);
209 iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
210 " length=0x%x\n", reg
->sge
.lkey
, reg
->rkey
,
211 reg
->sge
.addr
, reg
->sge
.length
);
216 static int iser_set_page(struct ib_mr
*mr
, u64 addr
)
218 struct iser_page_vec
*page_vec
=
219 container_of(mr
, struct iser_page_vec
, fake_mr
);
221 page_vec
->pages
[page_vec
->npages
++] = addr
;
227 int iser_fast_reg_fmr(struct iscsi_iser_task
*iser_task
,
228 struct iser_data_buf
*mem
,
229 struct iser_reg_resources
*rsc
,
230 struct iser_mem_reg
*reg
)
232 struct ib_conn
*ib_conn
= &iser_task
->iser_conn
->ib_conn
;
233 struct iser_device
*device
= ib_conn
->device
;
234 struct iser_page_vec
*page_vec
= rsc
->page_vec
;
235 struct ib_fmr_pool
*fmr_pool
= rsc
->fmr_pool
;
236 struct ib_pool_fmr
*fmr
;
239 page_vec
->npages
= 0;
240 page_vec
->fake_mr
.page_size
= SZ_4K
;
241 plen
= ib_sg_to_pages(&page_vec
->fake_mr
, mem
->sg
,
242 mem
->dma_nents
, NULL
, iser_set_page
);
243 if (unlikely(plen
< mem
->dma_nents
)) {
244 iser_err("page vec too short to hold this SG\n");
245 iser_data_buf_dump(mem
, device
->ib_device
);
246 iser_dump_page_vec(page_vec
);
250 fmr
= ib_fmr_pool_map_phys(fmr_pool
, page_vec
->pages
,
251 page_vec
->npages
, page_vec
->pages
[0]);
254 iser_err("ib_fmr_pool_map_phys failed: %d\n", ret
);
258 reg
->sge
.lkey
= fmr
->fmr
->lkey
;
259 reg
->rkey
= fmr
->fmr
->rkey
;
260 reg
->sge
.addr
= page_vec
->fake_mr
.iova
;
261 reg
->sge
.length
= page_vec
->fake_mr
.length
;
264 iser_dbg("fmr reg: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
265 " length=0x%x\n", reg
->sge
.lkey
, reg
->rkey
,
266 reg
->sge
.addr
, reg
->sge
.length
);
272 * Unregister (previosuly registered using FMR) memory.
273 * If memory is non-FMR does nothing.
275 void iser_unreg_mem_fmr(struct iscsi_iser_task
*iser_task
,
276 enum iser_data_dir cmd_dir
)
278 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
283 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n", reg
->mem_h
);
285 ib_fmr_pool_unmap((struct ib_pool_fmr
*)reg
->mem_h
);
290 void iser_unreg_mem_fastreg(struct iscsi_iser_task
*iser_task
,
291 enum iser_data_dir cmd_dir
)
293 struct iser_device
*device
= iser_task
->iser_conn
->ib_conn
.device
;
294 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
299 device
->reg_ops
->reg_desc_put(&iser_task
->iser_conn
->ib_conn
,
305 iser_set_dif_domain(struct scsi_cmnd
*sc
, struct ib_sig_domain
*domain
)
307 domain
->sig_type
= IB_SIG_TYPE_T10_DIF
;
308 domain
->sig
.dif
.pi_interval
= scsi_prot_interval(sc
);
309 domain
->sig
.dif
.ref_tag
= t10_pi_ref_tag(sc
->request
);
311 * At the moment we hard code those, but in the future
312 * we will take them from sc.
314 domain
->sig
.dif
.apptag_check_mask
= 0xffff;
315 domain
->sig
.dif
.app_escape
= true;
316 domain
->sig
.dif
.ref_escape
= true;
317 if (sc
->prot_flags
& SCSI_PROT_REF_INCREMENT
)
318 domain
->sig
.dif
.ref_remap
= true;
322 iser_set_sig_attrs(struct scsi_cmnd
*sc
, struct ib_sig_attrs
*sig_attrs
)
324 switch (scsi_get_prot_op(sc
)) {
325 case SCSI_PROT_WRITE_INSERT
:
326 case SCSI_PROT_READ_STRIP
:
327 sig_attrs
->mem
.sig_type
= IB_SIG_TYPE_NONE
;
328 iser_set_dif_domain(sc
, &sig_attrs
->wire
);
329 sig_attrs
->wire
.sig
.dif
.bg_type
= IB_T10DIF_CRC
;
331 case SCSI_PROT_READ_INSERT
:
332 case SCSI_PROT_WRITE_STRIP
:
333 sig_attrs
->wire
.sig_type
= IB_SIG_TYPE_NONE
;
334 iser_set_dif_domain(sc
, &sig_attrs
->mem
);
335 sig_attrs
->mem
.sig
.dif
.bg_type
= sc
->prot_flags
& SCSI_PROT_IP_CHECKSUM
?
336 IB_T10DIF_CSUM
: IB_T10DIF_CRC
;
338 case SCSI_PROT_READ_PASS
:
339 case SCSI_PROT_WRITE_PASS
:
340 iser_set_dif_domain(sc
, &sig_attrs
->wire
);
341 sig_attrs
->wire
.sig
.dif
.bg_type
= IB_T10DIF_CRC
;
342 iser_set_dif_domain(sc
, &sig_attrs
->mem
);
343 sig_attrs
->mem
.sig
.dif
.bg_type
= sc
->prot_flags
& SCSI_PROT_IP_CHECKSUM
?
344 IB_T10DIF_CSUM
: IB_T10DIF_CRC
;
347 iser_err("Unsupported PI operation %d\n",
348 scsi_get_prot_op(sc
));
356 iser_set_prot_checks(struct scsi_cmnd
*sc
, u8
*mask
)
359 if (sc
->prot_flags
& SCSI_PROT_REF_CHECK
)
360 *mask
|= IB_SIG_CHECK_REFTAG
;
361 if (sc
->prot_flags
& SCSI_PROT_GUARD_CHECK
)
362 *mask
|= IB_SIG_CHECK_GUARD
;
366 iser_inv_rkey(struct ib_send_wr
*inv_wr
,
369 struct ib_send_wr
*next_wr
)
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;
376 inv_wr
->next
= next_wr
;
380 iser_reg_sig_mr(struct iscsi_iser_task
*iser_task
,
381 struct iser_data_buf
*mem
,
382 struct iser_data_buf
*sig_mem
,
383 struct iser_reg_resources
*rsc
,
384 struct iser_mem_reg
*sig_reg
)
386 struct iser_tx_desc
*tx_desc
= &iser_task
->desc
;
387 struct ib_cqe
*cqe
= &iser_task
->iser_conn
->ib_conn
.reg_cqe
;
388 struct ib_mr
*mr
= rsc
->sig_mr
;
389 struct ib_sig_attrs
*sig_attrs
= mr
->sig_attrs
;
390 struct ib_reg_wr
*wr
= &tx_desc
->reg_wr
;
393 memset(sig_attrs
, 0, sizeof(*sig_attrs
));
394 ret
= iser_set_sig_attrs(iser_task
->sc
, sig_attrs
);
398 iser_set_prot_checks(iser_task
->sc
, &sig_attrs
->check_mask
);
401 iser_inv_rkey(&tx_desc
->inv_wr
, mr
, cqe
, &wr
->wr
);
403 ib_update_fast_reg_key(mr
, ib_inc_rkey(mr
->rkey
));
405 ret
= ib_map_mr_sg_pi(mr
, mem
->sg
, mem
->dma_nents
, NULL
,
406 sig_mem
->sg
, sig_mem
->dma_nents
, NULL
, SZ_4K
);
408 iser_err("failed to map PI sg (%d)\n",
409 mem
->dma_nents
+ sig_mem
->dma_nents
);
413 memset(wr
, 0, sizeof(*wr
));
414 wr
->wr
.next
= &tx_desc
->send_wr
;
415 wr
->wr
.opcode
= IB_WR_REG_MR_INTEGRITY
;
418 wr
->wr
.send_flags
= 0;
421 wr
->access
= IB_ACCESS_LOCAL_WRITE
|
422 IB_ACCESS_REMOTE_READ
|
423 IB_ACCESS_REMOTE_WRITE
;
426 sig_reg
->sge
.lkey
= mr
->lkey
;
427 sig_reg
->rkey
= mr
->rkey
;
428 sig_reg
->sge
.addr
= mr
->iova
;
429 sig_reg
->sge
.length
= mr
->length
;
431 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
432 sig_reg
->sge
.lkey
, sig_reg
->rkey
, sig_reg
->sge
.addr
,
433 sig_reg
->sge
.length
);
438 static int iser_fast_reg_mr(struct iscsi_iser_task
*iser_task
,
439 struct iser_data_buf
*mem
,
440 struct iser_reg_resources
*rsc
,
441 struct iser_mem_reg
*reg
)
443 struct iser_tx_desc
*tx_desc
= &iser_task
->desc
;
444 struct ib_cqe
*cqe
= &iser_task
->iser_conn
->ib_conn
.reg_cqe
;
445 struct ib_mr
*mr
= rsc
->mr
;
446 struct ib_reg_wr
*wr
= &tx_desc
->reg_wr
;
450 iser_inv_rkey(&tx_desc
->inv_wr
, mr
, cqe
, &wr
->wr
);
452 ib_update_fast_reg_key(mr
, ib_inc_rkey(mr
->rkey
));
454 n
= ib_map_mr_sg(mr
, mem
->sg
, mem
->dma_nents
, NULL
, SZ_4K
);
455 if (unlikely(n
!= mem
->dma_nents
)) {
456 iser_err("failed to map sg (%d/%d)\n",
458 return n
< 0 ? n
: -EINVAL
;
461 wr
->wr
.next
= &tx_desc
->send_wr
;
462 wr
->wr
.opcode
= IB_WR_REG_MR
;
464 wr
->wr
.send_flags
= 0;
468 wr
->access
= IB_ACCESS_LOCAL_WRITE
|
469 IB_ACCESS_REMOTE_WRITE
|
470 IB_ACCESS_REMOTE_READ
;
474 reg
->sge
.lkey
= mr
->lkey
;
475 reg
->rkey
= mr
->rkey
;
476 reg
->sge
.addr
= mr
->iova
;
477 reg
->sge
.length
= mr
->length
;
479 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
480 reg
->sge
.lkey
, reg
->rkey
, reg
->sge
.addr
, reg
->sge
.length
);
486 iser_reg_data_sg(struct iscsi_iser_task
*task
,
487 struct iser_data_buf
*mem
,
488 struct iser_fr_desc
*desc
,
490 struct iser_mem_reg
*reg
)
492 struct iser_device
*device
= task
->iser_conn
->ib_conn
.device
;
495 return iser_reg_dma(device
, mem
, reg
);
497 return device
->reg_ops
->reg_mem(task
, mem
, &desc
->rsc
, reg
);
500 int iser_reg_rdma_mem(struct iscsi_iser_task
*task
,
501 enum iser_data_dir dir
,
504 struct ib_conn
*ib_conn
= &task
->iser_conn
->ib_conn
;
505 struct iser_device
*device
= ib_conn
->device
;
506 struct iser_data_buf
*mem
= &task
->data
[dir
];
507 struct iser_mem_reg
*reg
= &task
->rdma_reg
[dir
];
508 struct iser_fr_desc
*desc
= NULL
;
512 use_dma_key
= mem
->dma_nents
== 1 && (all_imm
|| !iser_always_reg
) &&
513 scsi_get_prot_op(task
->sc
) == SCSI_PROT_NORMAL
;
516 desc
= device
->reg_ops
->reg_desc_get(ib_conn
);
520 if (scsi_get_prot_op(task
->sc
) == SCSI_PROT_NORMAL
) {
521 err
= iser_reg_data_sg(task
, mem
, desc
, use_dma_key
, reg
);
525 err
= iser_reg_sig_mr(task
, mem
, &task
->prot
[dir
],
530 desc
->sig_protected
= true;
537 device
->reg_ops
->reg_desc_put(ib_conn
, desc
);
542 void iser_unreg_rdma_mem(struct iscsi_iser_task
*task
,
543 enum iser_data_dir dir
)
545 struct iser_device
*device
= task
->iser_conn
->ib_conn
.device
;
547 device
->reg_ops
->unreg_mem(task
, dir
);