2013-07-07 22:25:49 +08:00
|
|
|
/*
|
2015-04-02 22:07:30 +08:00
|
|
|
* Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
|
2013-07-07 22:25:49 +08:00
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <rdma/ib_umem.h>
|
2014-12-11 23:04:21 +08:00
|
|
|
#include <rdma/ib_umem_odp.h>
|
2013-07-07 22:25:49 +08:00
|
|
|
#include "mlx5_ib.h"
|
|
|
|
|
|
|
|
/* @umem: umem object to scan
|
|
|
|
* @addr: ib virtual address requested by the user
|
2016-10-27 21:36:47 +08:00
|
|
|
* @max_page_shift: high limit for page_shift - 0 means no limit
|
2013-07-07 22:25:49 +08:00
|
|
|
* @count: number of PAGE_SIZE pages covered by umem
|
|
|
|
* @shift: page shift for the compound pages found in the region
|
|
|
|
* @ncont: number of compund pages
|
|
|
|
* @order: log2 of the number of compound pages
|
|
|
|
*/
|
2016-10-27 21:36:47 +08:00
|
|
|
void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
|
|
|
|
unsigned long max_page_shift,
|
|
|
|
int *count, int *shift,
|
2013-07-07 22:25:49 +08:00
|
|
|
int *ncont, int *order)
|
|
|
|
{
|
|
|
|
unsigned long tmp;
|
|
|
|
unsigned long m;
|
2017-09-25 02:46:34 +08:00
|
|
|
u64 base = ~0, p = 0;
|
|
|
|
u64 len, pfn;
|
|
|
|
int i = 0;
|
2014-01-28 19:40:15 +08:00
|
|
|
struct scatterlist *sg;
|
|
|
|
int entry;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2019-05-20 14:05:25 +08:00
|
|
|
addr = addr >> PAGE_SHIFT;
|
2013-07-07 22:25:49 +08:00
|
|
|
tmp = (unsigned long)addr;
|
2016-08-26 13:16:17 +08:00
|
|
|
m = find_first_bit(&tmp, BITS_PER_LONG);
|
2016-10-27 21:36:47 +08:00
|
|
|
if (max_page_shift)
|
2019-05-20 14:05:25 +08:00
|
|
|
m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
|
2017-09-25 02:46:34 +08:00
|
|
|
|
2014-01-28 19:40:15 +08:00
|
|
|
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
|
2019-05-20 14:05:25 +08:00
|
|
|
len = sg_dma_len(sg) >> PAGE_SHIFT;
|
|
|
|
pfn = sg_dma_address(sg) >> PAGE_SHIFT;
|
2017-09-25 02:46:34 +08:00
|
|
|
if (base + p != pfn) {
|
|
|
|
/* If either the offset or the new
|
|
|
|
* base are unaligned update m
|
|
|
|
*/
|
|
|
|
tmp = (unsigned long)(pfn | p);
|
|
|
|
if (!IS_ALIGNED(tmp, 1 << m))
|
|
|
|
m = find_first_bit(&tmp, BITS_PER_LONG);
|
|
|
|
|
|
|
|
base = pfn;
|
|
|
|
p = 0;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
2017-09-25 02:46:34 +08:00
|
|
|
|
|
|
|
p += len;
|
|
|
|
i += len;
|
2014-01-28 19:40:15 +08:00
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
if (i) {
|
|
|
|
m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
|
|
|
|
|
|
|
|
if (order)
|
|
|
|
*order = ilog2(roundup_pow_of_two(i) >> m);
|
|
|
|
|
|
|
|
*ncont = DIV_ROUND_UP(i, (1 << m));
|
|
|
|
} else {
|
|
|
|
m = 0;
|
|
|
|
|
|
|
|
if (order)
|
|
|
|
*order = 0;
|
|
|
|
|
|
|
|
*ncont = 0;
|
|
|
|
}
|
2019-05-20 14:05:25 +08:00
|
|
|
*shift = PAGE_SHIFT + m;
|
2013-07-07 22:25:49 +08:00
|
|
|
*count = i;
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:21 +08:00
|
|
|
static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
|
|
|
|
{
|
|
|
|
u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
|
|
|
|
|
|
|
|
if (umem_dma & ODP_READ_ALLOWED_BIT)
|
|
|
|
mtt_entry |= MLX5_IB_MTT_READ;
|
|
|
|
if (umem_dma & ODP_WRITE_ALLOWED_BIT)
|
|
|
|
mtt_entry |= MLX5_IB_MTT_WRITE;
|
|
|
|
|
|
|
|
return mtt_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populate the given array with bus addresses from the umem.
|
|
|
|
*
|
|
|
|
* dev - mlx5_ib device
|
|
|
|
* umem - umem to use to fill the pages
|
|
|
|
* page_shift - determines the page size used in the resulting array
|
2014-12-11 23:04:22 +08:00
|
|
|
* offset - offset into the umem to start from,
|
|
|
|
* only implemented for ODP umems
|
|
|
|
* num_pages - total number of pages to fill
|
2014-12-11 23:04:21 +08:00
|
|
|
* pas - bus addresses array to fill
|
|
|
|
* access_flags - access flags to set on all present pages.
|
|
|
|
use enum mlx5_ib_mtt_access_flags for this.
|
|
|
|
*/
|
2014-12-11 23:04:22 +08:00
|
|
|
void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
|
|
|
|
int page_shift, size_t offset, size_t num_pages,
|
|
|
|
__be64 *pas, int access_flags)
|
2013-07-07 22:25:49 +08:00
|
|
|
{
|
2019-05-20 14:05:25 +08:00
|
|
|
int shift = page_shift - PAGE_SHIFT;
|
2013-07-07 22:25:49 +08:00
|
|
|
int mask = (1 << shift) - 1;
|
2017-01-02 17:37:44 +08:00
|
|
|
int i, k, idx;
|
2013-07-07 22:25:49 +08:00
|
|
|
u64 cur = 0;
|
|
|
|
u64 base;
|
|
|
|
int len;
|
2014-01-28 19:40:15 +08:00
|
|
|
struct scatterlist *sg;
|
|
|
|
int entry;
|
2019-01-08 22:07:26 +08:00
|
|
|
|
2018-09-17 01:48:06 +08:00
|
|
|
if (umem->is_odp) {
|
2014-12-11 23:04:21 +08:00
|
|
|
WARN_ON(shift != 0);
|
|
|
|
WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
|
|
|
|
|
|
|
|
for (i = 0; i < num_pages; ++i) {
|
2018-09-17 01:48:06 +08:00
|
|
|
dma_addr_t pa =
|
|
|
|
to_ib_umem_odp(umem)->dma_list[offset + i];
|
2014-12-11 23:04:21 +08:00
|
|
|
|
|
|
|
pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
i = 0;
|
2014-01-28 19:40:15 +08:00
|
|
|
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
|
2019-05-20 14:05:25 +08:00
|
|
|
len = sg_dma_len(sg) >> PAGE_SHIFT;
|
2014-01-28 19:40:15 +08:00
|
|
|
base = sg_dma_address(sg);
|
2017-01-02 17:37:44 +08:00
|
|
|
|
|
|
|
/* Skip elements below offset */
|
|
|
|
if (i + len < offset << shift) {
|
|
|
|
i += len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip pages below offset */
|
|
|
|
if (i < offset << shift) {
|
|
|
|
k = (offset << shift) - i;
|
|
|
|
i = offset << shift;
|
|
|
|
} else {
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; k < len; k++) {
|
2014-01-28 19:40:15 +08:00
|
|
|
if (!(i & mask)) {
|
2019-05-20 14:05:25 +08:00
|
|
|
cur = base + (k << PAGE_SHIFT);
|
2014-12-11 23:04:21 +08:00
|
|
|
cur |= access_flags;
|
2017-01-02 17:37:44 +08:00
|
|
|
idx = (i >> shift) - offset;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2017-01-02 17:37:44 +08:00
|
|
|
pas[idx] = cpu_to_be64(cur);
|
2014-01-28 19:40:15 +08:00
|
|
|
mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
|
2017-01-02 17:37:44 +08:00
|
|
|
i >> shift, be64_to_cpu(pas[idx]));
|
|
|
|
}
|
2014-01-28 19:40:15 +08:00
|
|
|
i++;
|
2017-01-02 17:37:44 +08:00
|
|
|
|
|
|
|
/* Stop after num_pages reached */
|
|
|
|
if (i >> shift >= offset + num_pages)
|
|
|
|
return;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
2014-01-28 19:40:15 +08:00
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:22 +08:00
|
|
|
void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
|
|
|
|
int page_shift, __be64 *pas, int access_flags)
|
|
|
|
{
|
|
|
|
return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
|
|
|
|
ib_umem_num_pages(umem), pas,
|
|
|
|
access_flags);
|
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
|
|
|
|
{
|
|
|
|
u64 page_size;
|
|
|
|
u64 page_mask;
|
|
|
|
u64 off_size;
|
|
|
|
u64 off_mask;
|
|
|
|
u64 buf_off;
|
|
|
|
|
2014-07-29 04:30:23 +08:00
|
|
|
page_size = (u64)1 << page_shift;
|
2013-07-07 22:25:49 +08:00
|
|
|
page_mask = page_size - 1;
|
|
|
|
buf_off = addr & page_mask;
|
|
|
|
off_size = page_size >> 6;
|
|
|
|
off_mask = off_size - 1;
|
|
|
|
|
|
|
|
if (buf_off & off_mask)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*offset = buf_off >> ilog2(off_size);
|
|
|
|
return 0;
|
|
|
|
}
|