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 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 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 int iser_assign_reg_ops(struct iser_device
*device
)
72 struct ib_device_attr
*dev_attr
= &device
->dev_attr
;
74 /* Assign function handles - based on FMR support */
75 if (device
->ib_device
->alloc_fmr
&& device
->ib_device
->dealloc_fmr
&&
76 device
->ib_device
->map_phys_fmr
&& device
->ib_device
->unmap_fmr
) {
77 iser_info("FMR supported, using FMR for registration\n");
78 device
->reg_ops
= &fmr_ops
;
80 if (dev_attr
->device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
81 iser_info("FastReg supported, using FastReg for registration\n");
82 device
->reg_ops
= &fastreg_ops
;
84 iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
92 iser_reg_desc_get_fr(struct ib_conn
*ib_conn
)
94 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
95 struct iser_fr_desc
*desc
;
98 spin_lock_irqsave(&fr_pool
->lock
, flags
);
99 desc
= list_first_entry(&fr_pool
->list
,
100 struct iser_fr_desc
, list
);
101 list_del(&desc
->list
);
102 spin_unlock_irqrestore(&fr_pool
->lock
, flags
);
108 iser_reg_desc_put_fr(struct ib_conn
*ib_conn
,
109 struct iser_fr_desc
*desc
)
111 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
114 spin_lock_irqsave(&fr_pool
->lock
, flags
);
115 list_add(&desc
->list
, &fr_pool
->list
);
116 spin_unlock_irqrestore(&fr_pool
->lock
, flags
);
119 struct iser_fr_desc
*
120 iser_reg_desc_get_fmr(struct ib_conn
*ib_conn
)
122 struct iser_fr_pool
*fr_pool
= &ib_conn
->fr_pool
;
124 return list_first_entry(&fr_pool
->list
,
125 struct iser_fr_desc
, list
);
129 iser_reg_desc_put_fmr(struct ib_conn
*ib_conn
,
130 struct iser_fr_desc
*desc
)
134 #define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & ~MASK_4K) == 0)
137 * iser_sg_to_page_vec - Translates scatterlist entries to physical addresses
138 * and returns the length of resulting physical address array (may be less than
139 * the original due to possible compaction).
141 * we build a "page vec" under the assumption that the SG meets the RDMA
142 * alignment requirements. Other then the first and last SG elements, all
143 * the "internal" elements can be compacted into a list whose elements are
144 * dma addresses of physical pages. The code supports also the weird case
145 * where --few fragments of the same page-- are present in the SG as
146 * consecutive elements. Also, it handles one entry SG.
149 static int iser_sg_to_page_vec(struct iser_data_buf
*data
,
150 struct ib_device
*ibdev
, u64
*pages
,
151 int *offset
, int *data_size
)
153 struct scatterlist
*sg
, *sgl
= data
->sg
;
154 u64 start_addr
, end_addr
, page
, chunk_start
= 0;
155 unsigned long total_sz
= 0;
156 unsigned int dma_len
;
157 int i
, new_chunk
, cur_page
, last_ent
= data
->dma_nents
- 1;
159 /* compute the offset of first element */
160 *offset
= (u64
) sgl
[0].offset
& ~MASK_4K
;
164 for_each_sg(sgl
, sg
, data
->dma_nents
, i
) {
165 start_addr
= ib_sg_dma_address(ibdev
, sg
);
167 chunk_start
= start_addr
;
168 dma_len
= ib_sg_dma_len(ibdev
, sg
);
169 end_addr
= start_addr
+ dma_len
;
172 /* collect page fragments until aligned or end of SG list */
173 if (!IS_4K_ALIGNED(end_addr
) && i
< last_ent
) {
179 /* address of the first page in the contiguous chunk;
180 masking relevant for the very first SG entry,
181 which might be unaligned */
182 page
= chunk_start
& MASK_4K
;
184 pages
[cur_page
++] = page
;
186 } while (page
< end_addr
);
189 *data_size
= total_sz
;
190 iser_dbg("page_vec->data_size:%d cur_page %d\n",
191 *data_size
, cur_page
);
195 static void iser_data_buf_dump(struct iser_data_buf
*data
,
196 struct ib_device
*ibdev
)
198 struct scatterlist
*sg
;
201 for_each_sg(data
->sg
, sg
, data
->dma_nents
, i
)
202 iser_dbg("sg[%d] dma_addr:0x%lX page:0x%p "
203 "off:0x%x sz:0x%x dma_len:0x%x\n",
204 i
, (unsigned long)ib_sg_dma_address(ibdev
, sg
),
205 sg_page(sg
), sg
->offset
,
206 sg
->length
, ib_sg_dma_len(ibdev
, sg
));
209 static void iser_dump_page_vec(struct iser_page_vec
*page_vec
)
213 iser_err("page vec length %d data size %d\n",
214 page_vec
->length
, page_vec
->data_size
);
215 for (i
= 0; i
< page_vec
->length
; i
++)
216 iser_err("%d %lx\n",i
,(unsigned long)page_vec
->pages
[i
]);
219 int iser_dma_map_task_data(struct iscsi_iser_task
*iser_task
,
220 struct iser_data_buf
*data
,
221 enum iser_data_dir iser_dir
,
222 enum dma_data_direction dma_dir
)
224 struct ib_device
*dev
;
226 iser_task
->dir
[iser_dir
] = 1;
227 dev
= iser_task
->iser_conn
->ib_conn
.device
->ib_device
;
229 data
->dma_nents
= ib_dma_map_sg(dev
, data
->sg
, data
->size
, dma_dir
);
230 if (data
->dma_nents
== 0) {
231 iser_err("dma_map_sg failed!!!\n");
237 void iser_dma_unmap_task_data(struct iscsi_iser_task
*iser_task
,
238 struct iser_data_buf
*data
,
239 enum dma_data_direction dir
)
241 struct ib_device
*dev
;
243 dev
= iser_task
->iser_conn
->ib_conn
.device
->ib_device
;
244 ib_dma_unmap_sg(dev
, data
->sg
, data
->size
, dir
);
248 iser_reg_dma(struct iser_device
*device
, struct iser_data_buf
*mem
,
249 struct iser_mem_reg
*reg
)
251 struct scatterlist
*sg
= mem
->sg
;
253 reg
->sge
.lkey
= device
->pd
->local_dma_lkey
;
254 reg
->rkey
= device
->mr
->rkey
;
255 reg
->sge
.addr
= ib_sg_dma_address(device
->ib_device
, &sg
[0]);
256 reg
->sge
.length
= ib_sg_dma_len(device
->ib_device
, &sg
[0]);
258 iser_dbg("Single DMA entry: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
259 " length=0x%x\n", reg
->sge
.lkey
, reg
->rkey
,
260 reg
->sge
.addr
, reg
->sge
.length
);
266 * iser_reg_page_vec - Register physical memory
268 * returns: 0 on success, errno code on failure
271 int iser_fast_reg_fmr(struct iscsi_iser_task
*iser_task
,
272 struct iser_data_buf
*mem
,
273 struct iser_reg_resources
*rsc
,
274 struct iser_mem_reg
*reg
)
276 struct ib_conn
*ib_conn
= &iser_task
->iser_conn
->ib_conn
;
277 struct iser_device
*device
= ib_conn
->device
;
278 struct iser_page_vec
*page_vec
= rsc
->page_vec
;
279 struct ib_fmr_pool
*fmr_pool
= rsc
->fmr_pool
;
280 struct ib_pool_fmr
*fmr
;
283 plen
= iser_sg_to_page_vec(mem
, device
->ib_device
,
286 &page_vec
->data_size
);
287 page_vec
->length
= plen
;
288 if (plen
* SIZE_4K
< page_vec
->data_size
) {
289 iser_err("page vec too short to hold this SG\n");
290 iser_data_buf_dump(mem
, device
->ib_device
);
291 iser_dump_page_vec(page_vec
);
295 fmr
= ib_fmr_pool_map_phys(fmr_pool
,
301 iser_err("ib_fmr_pool_map_phys failed: %d\n", ret
);
305 reg
->sge
.lkey
= fmr
->fmr
->lkey
;
306 reg
->rkey
= fmr
->fmr
->rkey
;
307 reg
->sge
.addr
= page_vec
->pages
[0] + page_vec
->offset
;
308 reg
->sge
.length
= page_vec
->data_size
;
311 iser_dbg("fmr reg: lkey=0x%x, rkey=0x%x, addr=0x%llx,"
312 " length=0x%x\n", reg
->sge
.lkey
, reg
->rkey
,
313 reg
->sge
.addr
, reg
->sge
.length
);
319 * Unregister (previosuly registered using FMR) memory.
320 * If memory is non-FMR does nothing.
322 void iser_unreg_mem_fmr(struct iscsi_iser_task
*iser_task
,
323 enum iser_data_dir cmd_dir
)
325 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
331 iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n", reg
->mem_h
);
333 ret
= ib_fmr_pool_unmap((struct ib_pool_fmr
*)reg
->mem_h
);
335 iser_err("ib_fmr_pool_unmap failed %d\n", ret
);
340 void iser_unreg_mem_fastreg(struct iscsi_iser_task
*iser_task
,
341 enum iser_data_dir cmd_dir
)
343 struct iser_device
*device
= iser_task
->iser_conn
->ib_conn
.device
;
344 struct iser_mem_reg
*reg
= &iser_task
->rdma_reg
[cmd_dir
];
349 device
->reg_ops
->reg_desc_put(&iser_task
->iser_conn
->ib_conn
,
355 iser_set_dif_domain(struct scsi_cmnd
*sc
, struct ib_sig_attrs
*sig_attrs
,
356 struct ib_sig_domain
*domain
)
358 domain
->sig_type
= IB_SIG_TYPE_T10_DIF
;
359 domain
->sig
.dif
.pi_interval
= scsi_prot_interval(sc
);
360 domain
->sig
.dif
.ref_tag
= scsi_prot_ref_tag(sc
);
362 * At the moment we hard code those, but in the future
363 * we will take them from sc.
365 domain
->sig
.dif
.apptag_check_mask
= 0xffff;
366 domain
->sig
.dif
.app_escape
= true;
367 domain
->sig
.dif
.ref_escape
= true;
368 if (sc
->prot_flags
& SCSI_PROT_REF_INCREMENT
)
369 domain
->sig
.dif
.ref_remap
= true;
373 iser_set_sig_attrs(struct scsi_cmnd
*sc
, struct ib_sig_attrs
*sig_attrs
)
375 switch (scsi_get_prot_op(sc
)) {
376 case SCSI_PROT_WRITE_INSERT
:
377 case SCSI_PROT_READ_STRIP
:
378 sig_attrs
->mem
.sig_type
= IB_SIG_TYPE_NONE
;
379 iser_set_dif_domain(sc
, sig_attrs
, &sig_attrs
->wire
);
380 sig_attrs
->wire
.sig
.dif
.bg_type
= IB_T10DIF_CRC
;
382 case SCSI_PROT_READ_INSERT
:
383 case SCSI_PROT_WRITE_STRIP
:
384 sig_attrs
->wire
.sig_type
= IB_SIG_TYPE_NONE
;
385 iser_set_dif_domain(sc
, sig_attrs
, &sig_attrs
->mem
);
386 sig_attrs
->mem
.sig
.dif
.bg_type
= sc
->prot_flags
& SCSI_PROT_IP_CHECKSUM
?
387 IB_T10DIF_CSUM
: IB_T10DIF_CRC
;
389 case SCSI_PROT_READ_PASS
:
390 case SCSI_PROT_WRITE_PASS
:
391 iser_set_dif_domain(sc
, sig_attrs
, &sig_attrs
->wire
);
392 sig_attrs
->wire
.sig
.dif
.bg_type
= IB_T10DIF_CRC
;
393 iser_set_dif_domain(sc
, sig_attrs
, &sig_attrs
->mem
);
394 sig_attrs
->mem
.sig
.dif
.bg_type
= sc
->prot_flags
& SCSI_PROT_IP_CHECKSUM
?
395 IB_T10DIF_CSUM
: IB_T10DIF_CRC
;
398 iser_err("Unsupported PI operation %d\n",
399 scsi_get_prot_op(sc
));
407 iser_set_prot_checks(struct scsi_cmnd
*sc
, u8
*mask
)
410 if (sc
->prot_flags
& SCSI_PROT_REF_CHECK
)
411 *mask
|= ISER_CHECK_REFTAG
;
412 if (sc
->prot_flags
& SCSI_PROT_GUARD_CHECK
)
413 *mask
|= ISER_CHECK_GUARD
;
417 iser_inv_rkey(struct ib_send_wr
*inv_wr
, struct ib_mr
*mr
)
421 inv_wr
->opcode
= IB_WR_LOCAL_INV
;
422 inv_wr
->wr_id
= ISER_FASTREG_LI_WRID
;
423 inv_wr
->ex
.invalidate_rkey
= mr
->rkey
;
424 inv_wr
->send_flags
= 0;
427 rkey
= ib_inc_rkey(mr
->rkey
);
428 ib_update_fast_reg_key(mr
, rkey
);
432 iser_reg_sig_mr(struct iscsi_iser_task
*iser_task
,
433 struct iser_pi_context
*pi_ctx
,
434 struct iser_mem_reg
*data_reg
,
435 struct iser_mem_reg
*prot_reg
,
436 struct iser_mem_reg
*sig_reg
)
438 struct iser_tx_desc
*tx_desc
= &iser_task
->desc
;
439 struct ib_sig_attrs
*sig_attrs
= &tx_desc
->sig_attrs
;
440 struct ib_sig_handover_wr
*wr
;
443 memset(sig_attrs
, 0, sizeof(*sig_attrs
));
444 ret
= iser_set_sig_attrs(iser_task
->sc
, sig_attrs
);
448 iser_set_prot_checks(iser_task
->sc
, &sig_attrs
->check_mask
);
450 if (!pi_ctx
->sig_mr_valid
)
451 iser_inv_rkey(iser_tx_next_wr(tx_desc
), pi_ctx
->sig_mr
);
453 wr
= sig_handover_wr(iser_tx_next_wr(tx_desc
));
454 wr
->wr
.opcode
= IB_WR_REG_SIG_MR
;
455 wr
->wr
.wr_id
= ISER_FASTREG_LI_WRID
;
456 wr
->wr
.sg_list
= &data_reg
->sge
;
458 wr
->wr
.send_flags
= 0;
459 wr
->sig_attrs
= sig_attrs
;
460 wr
->sig_mr
= pi_ctx
->sig_mr
;
461 if (scsi_prot_sg_count(iser_task
->sc
))
462 wr
->prot
= &prot_reg
->sge
;
465 wr
->access_flags
= IB_ACCESS_LOCAL_WRITE
|
466 IB_ACCESS_REMOTE_READ
|
467 IB_ACCESS_REMOTE_WRITE
;
468 pi_ctx
->sig_mr_valid
= 0;
470 sig_reg
->sge
.lkey
= pi_ctx
->sig_mr
->lkey
;
471 sig_reg
->rkey
= pi_ctx
->sig_mr
->rkey
;
472 sig_reg
->sge
.addr
= 0;
473 sig_reg
->sge
.length
= scsi_transfer_length(iser_task
->sc
);
475 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=%u\n",
476 sig_reg
->sge
.lkey
, sig_reg
->rkey
, sig_reg
->sge
.addr
,
477 sig_reg
->sge
.length
);
482 static int iser_fast_reg_mr(struct iscsi_iser_task
*iser_task
,
483 struct iser_data_buf
*mem
,
484 struct iser_reg_resources
*rsc
,
485 struct iser_mem_reg
*reg
)
487 struct iser_tx_desc
*tx_desc
= &iser_task
->desc
;
488 struct ib_mr
*mr
= rsc
->mr
;
489 struct ib_reg_wr
*wr
;
493 iser_inv_rkey(iser_tx_next_wr(tx_desc
), mr
);
495 n
= ib_map_mr_sg(mr
, mem
->sg
, mem
->size
, SIZE_4K
);
496 if (unlikely(n
!= mem
->size
)) {
497 iser_err("failed to map sg (%d/%d)\n",
499 return n
< 0 ? n
: -EINVAL
;
502 wr
= reg_wr(iser_tx_next_wr(tx_desc
));
503 wr
->wr
.opcode
= IB_WR_REG_MR
;
504 wr
->wr
.wr_id
= ISER_FASTREG_LI_WRID
;
505 wr
->wr
.send_flags
= 0;
509 wr
->access
= IB_ACCESS_LOCAL_WRITE
|
510 IB_ACCESS_REMOTE_WRITE
|
511 IB_ACCESS_REMOTE_READ
;
515 reg
->sge
.lkey
= mr
->lkey
;
516 reg
->rkey
= mr
->rkey
;
517 reg
->sge
.addr
= mr
->iova
;
518 reg
->sge
.length
= mr
->length
;
520 iser_dbg("lkey=0x%x rkey=0x%x addr=0x%llx length=0x%x\n",
521 reg
->sge
.lkey
, reg
->rkey
, reg
->sge
.addr
, reg
->sge
.length
);
527 iser_reg_prot_sg(struct iscsi_iser_task
*task
,
528 struct iser_data_buf
*mem
,
529 struct iser_fr_desc
*desc
,
531 struct iser_mem_reg
*reg
)
533 struct iser_device
*device
= task
->iser_conn
->ib_conn
.device
;
536 return iser_reg_dma(device
, mem
, reg
);
538 return device
->reg_ops
->reg_mem(task
, mem
, &desc
->pi_ctx
->rsc
, reg
);
542 iser_reg_data_sg(struct iscsi_iser_task
*task
,
543 struct iser_data_buf
*mem
,
544 struct iser_fr_desc
*desc
,
546 struct iser_mem_reg
*reg
)
548 struct iser_device
*device
= task
->iser_conn
->ib_conn
.device
;
551 return iser_reg_dma(device
, mem
, reg
);
553 return device
->reg_ops
->reg_mem(task
, mem
, &desc
->rsc
, reg
);
556 int iser_reg_rdma_mem(struct iscsi_iser_task
*task
,
557 enum iser_data_dir dir
)
559 struct ib_conn
*ib_conn
= &task
->iser_conn
->ib_conn
;
560 struct iser_device
*device
= ib_conn
->device
;
561 struct iser_data_buf
*mem
= &task
->data
[dir
];
562 struct iser_mem_reg
*reg
= &task
->rdma_reg
[dir
];
563 struct iser_mem_reg
*data_reg
;
564 struct iser_fr_desc
*desc
= NULL
;
568 use_dma_key
= (mem
->dma_nents
== 1 && !iser_always_reg
&&
569 scsi_get_prot_op(task
->sc
) == SCSI_PROT_NORMAL
);
572 desc
= device
->reg_ops
->reg_desc_get(ib_conn
);
576 if (scsi_get_prot_op(task
->sc
) == SCSI_PROT_NORMAL
)
579 data_reg
= &task
->desc
.data_reg
;
581 err
= iser_reg_data_sg(task
, mem
, desc
, use_dma_key
, data_reg
);
585 if (scsi_get_prot_op(task
->sc
) != SCSI_PROT_NORMAL
) {
586 struct iser_mem_reg
*prot_reg
= &task
->desc
.prot_reg
;
588 if (scsi_prot_sg_count(task
->sc
)) {
589 mem
= &task
->prot
[dir
];
590 err
= iser_reg_prot_sg(task
, mem
, desc
,
591 use_dma_key
, prot_reg
);
596 err
= iser_reg_sig_mr(task
, desc
->pi_ctx
, data_reg
,
601 desc
->pi_ctx
->sig_protected
= 1;
608 device
->reg_ops
->reg_desc_put(ib_conn
, desc
);
613 void iser_unreg_rdma_mem(struct iscsi_iser_task
*task
,
614 enum iser_data_dir dir
)
616 struct iser_device
*device
= task
->iser_conn
->ib_conn
.device
;
618 device
->reg_ops
->unreg_mem(task
, dir
);