112515907SHans Petter Selasky /*-
221bc3710SHans Petter Selasky * Copyright (c) 2013-2020, Mellanox Technologies, Ltd. All rights reserved.
312515907SHans Petter Selasky *
412515907SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
512515907SHans Petter Selasky * modification, are permitted provided that the following conditions
612515907SHans Petter Selasky * are met:
712515907SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
812515907SHans Petter Selasky * notice, this list of conditions and the following disclaimer.
912515907SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
1012515907SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
1112515907SHans Petter Selasky * documentation and/or other materials provided with the distribution.
1212515907SHans Petter Selasky *
1312515907SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
1412515907SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1512515907SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1612515907SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1712515907SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1812515907SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1912515907SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2012515907SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2112515907SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2212515907SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2312515907SHans Petter Selasky * SUCH DAMAGE.
2412515907SHans Petter Selasky */
2512515907SHans Petter Selasky
2670600979SKonstantin Belousov #include "opt_rss.h"
2770600979SKonstantin Belousov #include "opt_ratelimit.h"
2870600979SKonstantin Belousov
2912515907SHans Petter Selasky #include <linux/module.h>
3012515907SHans Petter Selasky #include <rdma/ib_umem.h>
318e6e287fSHans Petter Selasky #include <rdma/ib_umem_odp.h>
32028130b8SKonstantin Belousov #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
3312515907SHans Petter Selasky
3412515907SHans Petter Selasky /* @umem: umem object to scan
3512515907SHans Petter Selasky * @addr: ib virtual address requested by the user
36565cb4e8SHans Petter Selasky * @max_page_shift: high limit for page_shift - 0 means no limit
3712515907SHans Petter Selasky * @count: number of PAGE_SIZE pages covered by umem
3812515907SHans Petter Selasky * @shift: page shift for the compound pages found in the region
3912515907SHans Petter Selasky * @ncont: number of compund pages
4012515907SHans Petter Selasky * @order: log2 of the number of compound pages
4112515907SHans Petter Selasky */
mlx5_ib_cont_pages(struct ib_umem * umem,u64 addr,unsigned long max_page_shift,int * count,int * shift,int * ncont,int * order)42565cb4e8SHans Petter Selasky void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
43565cb4e8SHans Petter Selasky unsigned long max_page_shift,
44565cb4e8SHans Petter Selasky int *count, int *shift,
4512515907SHans Petter Selasky int *ncont, int *order)
4612515907SHans Petter Selasky {
478ac4c959SHans Petter Selasky unsigned long tmp;
4812515907SHans Petter Selasky unsigned long m;
4921bc3710SHans Petter Selasky u64 base = ~0, p = 0;
5021bc3710SHans Petter Selasky u64 len, pfn;
5121bc3710SHans Petter Selasky int i = 0;
5212515907SHans Petter Selasky struct scatterlist *sg;
5312515907SHans Petter Selasky int entry;
5412515907SHans Petter Selasky
5521bc3710SHans Petter Selasky addr = addr >> PAGE_SHIFT;
568e6e287fSHans Petter Selasky tmp = (unsigned long)addr;
578e6e287fSHans Petter Selasky m = find_first_bit(&tmp, BITS_PER_LONG);
58565cb4e8SHans Petter Selasky if (max_page_shift)
59565cb4e8SHans Petter Selasky m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
6021bc3710SHans Petter Selasky
6112515907SHans Petter Selasky for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
6221bc3710SHans Petter Selasky len = sg_dma_len(sg) >> PAGE_SHIFT;
6321bc3710SHans Petter Selasky pfn = sg_dma_address(sg) >> PAGE_SHIFT;
6412515907SHans Petter Selasky if (base + p != pfn) {
6521bc3710SHans Petter Selasky /* If either the offset or the new
6621bc3710SHans Petter Selasky * base are unaligned update m
6721bc3710SHans Petter Selasky */
6821bc3710SHans Petter Selasky tmp = (unsigned long)(pfn | p);
6921bc3710SHans Petter Selasky if (!IS_ALIGNED(tmp, 1 << m))
708e6e287fSHans Petter Selasky m = find_first_bit(&tmp, BITS_PER_LONG);
7121bc3710SHans Petter Selasky
7212515907SHans Petter Selasky base = pfn;
7312515907SHans Petter Selasky p = 0;
7412515907SHans Petter Selasky }
7521bc3710SHans Petter Selasky
7621bc3710SHans Petter Selasky p += len;
7721bc3710SHans Petter Selasky i += len;
7812515907SHans Petter Selasky }
7912515907SHans Petter Selasky
8012515907SHans Petter Selasky if (i) {
81*5a5da24fSDoug Moore m = min_t(unsigned long, order_base_2(i), m);
8212515907SHans Petter Selasky
8312515907SHans Petter Selasky if (order)
84*5a5da24fSDoug Moore *order = order_base_2(i) - m;
8512515907SHans Petter Selasky
8612515907SHans Petter Selasky *ncont = DIV_ROUND_UP(i, (1 << m));
8712515907SHans Petter Selasky } else {
8812515907SHans Petter Selasky m = 0;
8912515907SHans Petter Selasky
9012515907SHans Petter Selasky if (order)
9112515907SHans Petter Selasky *order = 0;
9212515907SHans Petter Selasky
9312515907SHans Petter Selasky *ncont = 0;
9412515907SHans Petter Selasky }
9521bc3710SHans Petter Selasky *shift = PAGE_SHIFT + m;
9612515907SHans Petter Selasky *count = i;
9712515907SHans Petter Selasky }
9812515907SHans Petter Selasky
998e6e287fSHans Petter Selasky #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
umem_dma_to_mtt(dma_addr_t umem_dma)1008e6e287fSHans Petter Selasky static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
1018e6e287fSHans Petter Selasky {
1028e6e287fSHans Petter Selasky u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
1038e6e287fSHans Petter Selasky
1048e6e287fSHans Petter Selasky if (umem_dma & ODP_READ_ALLOWED_BIT)
1058e6e287fSHans Petter Selasky mtt_entry |= MLX5_IB_MTT_READ;
1068e6e287fSHans Petter Selasky if (umem_dma & ODP_WRITE_ALLOWED_BIT)
1078e6e287fSHans Petter Selasky mtt_entry |= MLX5_IB_MTT_WRITE;
1088e6e287fSHans Petter Selasky
1098e6e287fSHans Petter Selasky return mtt_entry;
1108e6e287fSHans Petter Selasky }
1118e6e287fSHans Petter Selasky #endif
1128e6e287fSHans Petter Selasky
11312515907SHans Petter Selasky /*
11412515907SHans Petter Selasky * Populate the given array with bus addresses from the umem.
11512515907SHans Petter Selasky *
11612515907SHans Petter Selasky * dev - mlx5_ib device
11712515907SHans Petter Selasky * umem - umem to use to fill the pages
11812515907SHans Petter Selasky * page_shift - determines the page size used in the resulting array
11912515907SHans Petter Selasky * offset - offset into the umem to start from,
12012515907SHans Petter Selasky * only implemented for ODP umems
12112515907SHans Petter Selasky * num_pages - total number of pages to fill
12212515907SHans Petter Selasky * pas - bus addresses array to fill
12312515907SHans Petter Selasky * access_flags - access flags to set on all present pages.
12412515907SHans Petter Selasky use enum mlx5_ib_mtt_access_flags for this.
12512515907SHans Petter Selasky */
__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)1268e6e287fSHans Petter Selasky void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
1278e6e287fSHans Petter Selasky int page_shift, size_t offset, size_t num_pages,
12812515907SHans Petter Selasky __be64 *pas, int access_flags)
12912515907SHans Petter Selasky {
13012515907SHans Petter Selasky unsigned long umem_page_shift = ilog2(umem->page_size);
13112515907SHans Petter Selasky int shift = page_shift - umem_page_shift;
13212515907SHans Petter Selasky int mask = (1 << shift) - 1;
13312515907SHans Petter Selasky int i, k;
13412515907SHans Petter Selasky u64 cur = 0;
13512515907SHans Petter Selasky u64 base;
13612515907SHans Petter Selasky int len;
13712515907SHans Petter Selasky struct scatterlist *sg;
13812515907SHans Petter Selasky int entry;
1398e6e287fSHans Petter Selasky #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1408e6e287fSHans Petter Selasky const bool odp = umem->odp_data != NULL;
1418e6e287fSHans Petter Selasky
1428e6e287fSHans Petter Selasky if (odp) {
1438e6e287fSHans Petter Selasky WARN_ON(shift != 0);
1448e6e287fSHans Petter Selasky WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
1458e6e287fSHans Petter Selasky
1468e6e287fSHans Petter Selasky for (i = 0; i < num_pages; ++i) {
1478e6e287fSHans Petter Selasky dma_addr_t pa = umem->odp_data->dma_list[offset + i];
1488e6e287fSHans Petter Selasky
1498e6e287fSHans Petter Selasky pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
1508e6e287fSHans Petter Selasky }
1518e6e287fSHans Petter Selasky return;
1528e6e287fSHans Petter Selasky }
1538e6e287fSHans Petter Selasky #endif
15412515907SHans Petter Selasky
15512515907SHans Petter Selasky i = 0;
15612515907SHans Petter Selasky for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
15712515907SHans Petter Selasky len = sg_dma_len(sg) >> umem_page_shift;
15812515907SHans Petter Selasky base = sg_dma_address(sg);
15912515907SHans Petter Selasky for (k = 0; k < len; k++) {
16012515907SHans Petter Selasky if (!(i & mask)) {
16112515907SHans Petter Selasky cur = base + (k << umem_page_shift);
16212515907SHans Petter Selasky cur |= access_flags;
16312515907SHans Petter Selasky
16412515907SHans Petter Selasky pas[i >> shift] = cpu_to_be64(cur);
16512515907SHans Petter Selasky mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
1668e6e287fSHans Petter Selasky i >> shift, (long long)be64_to_cpu(pas[i >> shift]));
16712515907SHans Petter Selasky } else
16812515907SHans Petter Selasky mlx5_ib_dbg(dev, "=====> 0x%llx\n",
1698e6e287fSHans Petter Selasky (long long)(base + (k << umem_page_shift)));
17012515907SHans Petter Selasky i++;
17112515907SHans Petter Selasky }
17212515907SHans Petter Selasky }
17312515907SHans Petter Selasky }
17412515907SHans Petter Selasky
mlx5_ib_populate_pas(struct mlx5_ib_dev * dev,struct ib_umem * umem,int page_shift,__be64 * pas,int access_flags)17512515907SHans Petter Selasky void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
17612515907SHans Petter Selasky int page_shift, __be64 *pas, int access_flags)
17712515907SHans Petter Selasky {
17812515907SHans Petter Selasky return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
1798e6e287fSHans Petter Selasky ib_umem_num_pages(umem), pas,
18012515907SHans Petter Selasky access_flags);
18112515907SHans Petter Selasky }
mlx5_ib_get_buf_offset(u64 addr,int page_shift,u32 * offset)18212515907SHans Petter Selasky int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
18312515907SHans Petter Selasky {
18412515907SHans Petter Selasky u64 page_size;
18512515907SHans Petter Selasky u64 page_mask;
18612515907SHans Petter Selasky u64 off_size;
18712515907SHans Petter Selasky u64 off_mask;
18812515907SHans Petter Selasky u64 buf_off;
18912515907SHans Petter Selasky
19012515907SHans Petter Selasky page_size = (u64)1 << page_shift;
19112515907SHans Petter Selasky page_mask = page_size - 1;
19212515907SHans Petter Selasky buf_off = addr & page_mask;
19312515907SHans Petter Selasky off_size = page_size >> 6;
19412515907SHans Petter Selasky off_mask = off_size - 1;
19512515907SHans Petter Selasky
19612515907SHans Petter Selasky if (buf_off & off_mask)
19712515907SHans Petter Selasky return -EINVAL;
19812515907SHans Petter Selasky
1998e6e287fSHans Petter Selasky *offset = buf_off >> ilog2(off_size);
20012515907SHans Petter Selasky return 0;
20112515907SHans Petter Selasky }
202