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/kref.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/export.h>
|
2013-10-23 14:53:14 +08:00
|
|
|
#include <linux/delay.h>
|
2013-07-07 22:25:49 +08:00
|
|
|
#include <rdma/ib_umem.h>
|
2014-12-11 23:04:26 +08:00
|
|
|
#include <rdma/ib_umem_odp.h>
|
2014-12-11 23:04:11 +08:00
|
|
|
#include <rdma/ib_verbs.h>
|
2013-07-07 22:25:49 +08:00
|
|
|
#include "mlx5_ib.h"
|
2016-03-01 00:05:30 +08:00
|
|
|
#include "user.h"
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
enum {
|
2013-10-23 14:53:14 +08:00
|
|
|
MAX_PENDING_REG_MR = 8,
|
2013-07-07 22:25:49 +08:00
|
|
|
};
|
|
|
|
|
2014-12-11 23:04:22 +08:00
|
|
|
#define MLX5_UMR_ALIGN 2048
|
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
|
|
|
static __be64 mlx5_ib_update_mtt_emergency_buffer[
|
|
|
|
MLX5_UMR_MTT_MIN_CHUNK_SIZE/sizeof(__be64)]
|
|
|
|
__aligned(MLX5_UMR_ALIGN);
|
|
|
|
static DEFINE_MUTEX(mlx5_ib_update_mtt_emergency_buffer_mutex);
|
|
|
|
#endif
|
2013-09-11 21:35:35 +08:00
|
|
|
|
2014-12-11 23:04:23 +08:00
|
|
|
static int clean_mr(struct mlx5_ib_mr *mr);
|
|
|
|
|
2014-12-11 23:04:26 +08:00
|
|
|
static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
|
|
|
|
{
|
2016-03-01 00:05:28 +08:00
|
|
|
int err = mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
|
2014-12-11 23:04:26 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
|
|
|
/* Wait until all page fault handlers using the mr complete. */
|
|
|
|
synchronize_srcu(&dev->mr_srcu);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
static int order2idx(struct mlx5_ib_dev *dev, int order)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
|
|
|
|
if (order < cache->ent[0].order)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return order - cache->ent[0].order;
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:51 +08:00
|
|
|
static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length)
|
|
|
|
{
|
|
|
|
return ((u64)1 << mr->order) * MLX5_ADAPTER_PAGE_SIZE >=
|
|
|
|
length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
|
|
|
static void update_odp_mr(struct mlx5_ib_mr *mr)
|
|
|
|
{
|
|
|
|
if (mr->umem->odp_data) {
|
|
|
|
/*
|
|
|
|
* This barrier prevents the compiler from moving the
|
|
|
|
* setting of umem->odp_data->private to point to our
|
|
|
|
* MR, before reg_umr finished, to ensure that the MR
|
|
|
|
* initialization have finished before starting to
|
|
|
|
* handle invalidations.
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
|
|
|
mr->umem->odp_data->private = mr;
|
|
|
|
/*
|
|
|
|
* Make sure we will see the new
|
|
|
|
* umem->odp_data->private value in the invalidation
|
|
|
|
* routines, before we can get page faults on the
|
|
|
|
* MR. Page faults can happen once we put the MR in
|
|
|
|
* the tree, below this line. Without the barrier,
|
|
|
|
* there can be a fault handling and an invalidation
|
|
|
|
* before umem->odp_data->private == mr is visible to
|
|
|
|
* the invalidation handler.
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
static void reg_mr_callback(int status, void *context)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_mr *mr = context;
|
|
|
|
struct mlx5_ib_dev *dev = mr->dev;
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
int c = order2idx(dev, mr->order);
|
|
|
|
struct mlx5_cache_ent *ent = &cache->ent[c];
|
|
|
|
u8 key;
|
|
|
|
unsigned long flags;
|
2016-03-01 00:05:28 +08:00
|
|
|
struct mlx5_mkey_table *table = &dev->mdev->priv.mkey_table;
|
2014-05-22 19:50:09 +08:00
|
|
|
int err;
|
2013-10-23 14:53:14 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&ent->lock, flags);
|
|
|
|
ent->pending--;
|
|
|
|
spin_unlock_irqrestore(&ent->lock, flags);
|
|
|
|
if (status) {
|
|
|
|
mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
|
|
|
|
kfree(mr);
|
|
|
|
dev->fill_delay = 1;
|
|
|
|
mod_timer(&dev->delay_timer, jiffies + HZ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mr->out.hdr.status) {
|
|
|
|
mlx5_ib_warn(dev, "failed - status %d, syndorme 0x%x\n",
|
|
|
|
mr->out.hdr.status,
|
|
|
|
be32_to_cpu(mr->out.hdr.syndrome));
|
|
|
|
kfree(mr);
|
|
|
|
dev->fill_delay = 1;
|
|
|
|
mod_timer(&dev->delay_timer, jiffies + HZ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-29 04:30:22 +08:00
|
|
|
spin_lock_irqsave(&dev->mdev->priv.mkey_lock, flags);
|
|
|
|
key = dev->mdev->priv.mkey_key++;
|
|
|
|
spin_unlock_irqrestore(&dev->mdev->priv.mkey_lock, flags);
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->mmkey.key = mlx5_idx_to_mkey(be32_to_cpu(mr->out.mkey) & 0xffffff) | key;
|
2013-10-23 14:53:14 +08:00
|
|
|
|
|
|
|
cache->last_add = jiffies;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ent->lock, flags);
|
|
|
|
list_add_tail(&mr->list, &ent->head);
|
|
|
|
ent->cur++;
|
|
|
|
ent->size++;
|
|
|
|
spin_unlock_irqrestore(&ent->lock, flags);
|
2014-05-22 19:50:09 +08:00
|
|
|
|
|
|
|
write_lock_irqsave(&table->lock, flags);
|
2016-03-01 00:05:28 +08:00
|
|
|
err = radix_tree_insert(&table->tree, mlx5_base_mkey(mr->mmkey.key),
|
|
|
|
&mr->mmkey);
|
2014-05-22 19:50:09 +08:00
|
|
|
if (err)
|
2016-03-01 00:05:28 +08:00
|
|
|
pr_err("Error inserting to mkey tree. 0x%x\n", -err);
|
2014-05-22 19:50:09 +08:00
|
|
|
write_unlock_irqrestore(&table->lock, flags);
|
2013-10-23 14:53:14 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent = &cache->ent[c];
|
|
|
|
struct mlx5_create_mkey_mbox_in *in;
|
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
int npages = 1 << ent->order;
|
|
|
|
int err = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
in = kzalloc(sizeof(*in), GFP_KERNEL);
|
|
|
|
if (!in)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
2013-10-23 14:53:14 +08:00
|
|
|
if (ent->pending >= MAX_PENDING_REG_MR) {
|
|
|
|
err = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
|
|
|
|
if (!mr) {
|
|
|
|
err = -ENOMEM;
|
2013-10-23 14:53:14 +08:00
|
|
|
break;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
mr->order = ent->order;
|
|
|
|
mr->umred = 1;
|
2013-10-23 14:53:14 +08:00
|
|
|
mr->dev = dev;
|
2014-12-11 23:04:11 +08:00
|
|
|
in->seg.status = MLX5_MKEY_STATUS_FREE;
|
2013-07-07 22:25:49 +08:00
|
|
|
in->seg.xlt_oct_size = cpu_to_be32((npages + 1) / 2);
|
|
|
|
in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
|
|
|
|
in->seg.flags = MLX5_ACCESS_MODE_MTT | MLX5_PERM_UMR_EN;
|
|
|
|
in->seg.log2_page_size = 12;
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
|
|
|
ent->pending++;
|
|
|
|
spin_unlock_irq(&ent->lock);
|
2016-03-01 00:05:28 +08:00
|
|
|
err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in,
|
2013-10-23 14:53:14 +08:00
|
|
|
sizeof(*in), reg_mr_callback,
|
|
|
|
mr, &mr->out);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err) {
|
2014-12-02 18:26:19 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
|
|
|
ent->pending--;
|
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
mlx5_ib_warn(dev, "create mkey failed %d\n", err);
|
|
|
|
kfree(mr);
|
2013-10-23 14:53:14 +08:00
|
|
|
break;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(in);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_keys(struct mlx5_ib_dev *dev, int c, int num)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent = &cache->ent[c];
|
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (list_empty(&ent->head)) {
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
|
|
|
|
list_del(&mr->list);
|
|
|
|
ent->cur--;
|
|
|
|
ent->size--;
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2014-12-11 23:04:26 +08:00
|
|
|
err = destroy_mkey(dev, mr);
|
2013-09-11 21:35:26 +08:00
|
|
|
if (err)
|
2013-07-07 22:25:49 +08:00
|
|
|
mlx5_ib_warn(dev, "failed destroy mkey\n");
|
2013-09-11 21:35:26 +08:00
|
|
|
else
|
2013-07-07 22:25:49 +08:00
|
|
|
kfree(mr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t size_write(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent = filp->private_data;
|
|
|
|
struct mlx5_ib_dev *dev = ent->dev;
|
|
|
|
char lbuf[20];
|
|
|
|
u32 var;
|
|
|
|
int err;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (copy_from_user(lbuf, buf, sizeof(lbuf)))
|
2013-07-10 18:58:59 +08:00
|
|
|
return -EFAULT;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
c = order2idx(dev, ent->order);
|
|
|
|
lbuf[sizeof(lbuf) - 1] = 0;
|
|
|
|
|
|
|
|
if (sscanf(lbuf, "%u", &var) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (var < ent->limit)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (var > ent->size) {
|
2013-10-23 14:53:14 +08:00
|
|
|
do {
|
|
|
|
err = add_keys(dev, c, var - ent->size);
|
|
|
|
if (err && err != -EAGAIN)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
usleep_range(3000, 5000);
|
|
|
|
} while (err);
|
2013-07-07 22:25:49 +08:00
|
|
|
} else if (var < ent->size) {
|
|
|
|
remove_keys(dev, c, ent->size - var);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
|
|
|
|
loff_t *pos)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent = filp->private_data;
|
|
|
|
char lbuf[20];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (*pos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (copy_to_user(buf, lbuf, err))
|
2013-07-10 18:58:59 +08:00
|
|
|
return -EFAULT;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
*pos += err;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations size_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = simple_open,
|
|
|
|
.write = size_write,
|
|
|
|
.read = size_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t limit_write(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent = filp->private_data;
|
|
|
|
struct mlx5_ib_dev *dev = ent->dev;
|
|
|
|
char lbuf[20];
|
|
|
|
u32 var;
|
|
|
|
int err;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (copy_from_user(lbuf, buf, sizeof(lbuf)))
|
2013-07-10 18:58:59 +08:00
|
|
|
return -EFAULT;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
c = order2idx(dev, ent->order);
|
|
|
|
lbuf[sizeof(lbuf) - 1] = 0;
|
|
|
|
|
|
|
|
if (sscanf(lbuf, "%u", &var) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (var > ent->size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ent->limit = var;
|
|
|
|
|
|
|
|
if (ent->cur < ent->limit) {
|
|
|
|
err = add_keys(dev, c, 2 * ent->limit - ent->cur);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
|
|
|
|
loff_t *pos)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent = filp->private_data;
|
|
|
|
char lbuf[20];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (*pos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (copy_to_user(buf, lbuf, err))
|
2013-07-10 18:58:59 +08:00
|
|
|
return -EFAULT;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
*pos += err;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations limit_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = simple_open,
|
|
|
|
.write = limit_write,
|
|
|
|
.read = limit_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int someone_adding(struct mlx5_mr_cache *cache)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
|
|
|
|
if (cache->ent[i].cur < cache->ent[i].limit)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __cache_work_func(struct mlx5_cache_ent *ent)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = ent->dev;
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
int i = order2idx(dev, ent->order);
|
2013-10-23 14:53:14 +08:00
|
|
|
int err;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
if (cache->stopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ent = &dev->cache.ent[i];
|
2013-10-23 14:53:14 +08:00
|
|
|
if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
|
|
|
|
err = add_keys(dev, i, 1);
|
|
|
|
if (ent->cur < 2 * ent->limit) {
|
|
|
|
if (err == -EAGAIN) {
|
|
|
|
mlx5_ib_dbg(dev, "returned eagain, order %d\n",
|
|
|
|
i + 2);
|
|
|
|
queue_delayed_work(cache->wq, &ent->dwork,
|
|
|
|
msecs_to_jiffies(3));
|
|
|
|
} else if (err) {
|
|
|
|
mlx5_ib_warn(dev, "command failed order %d, err %d\n",
|
|
|
|
i + 2, err);
|
|
|
|
queue_delayed_work(cache->wq, &ent->dwork,
|
|
|
|
msecs_to_jiffies(1000));
|
|
|
|
} else {
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
}
|
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
} else if (ent->cur > 2 * ent->limit) {
|
2015-10-21 14:21:17 +08:00
|
|
|
/*
|
|
|
|
* The remove_keys() logic is performed as garbage collection
|
|
|
|
* task. Such task is intended to be run when no other active
|
|
|
|
* processes are running.
|
|
|
|
*
|
|
|
|
* The need_resched() will return TRUE if there are user tasks
|
|
|
|
* to be activated in near future.
|
|
|
|
*
|
|
|
|
* In such case, we don't execute remove_keys() and postpone
|
|
|
|
* the garbage collection work to try to run in next cycle,
|
|
|
|
* in order to free CPU resources to other tasks.
|
|
|
|
*/
|
|
|
|
if (!need_resched() && !someone_adding(cache) &&
|
2013-10-23 14:53:14 +08:00
|
|
|
time_after(jiffies, cache->last_add + 300 * HZ)) {
|
2013-07-07 22:25:49 +08:00
|
|
|
remove_keys(dev, i, 1);
|
|
|
|
if (ent->cur > ent->limit)
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
} else {
|
2013-10-23 14:53:14 +08:00
|
|
|
queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delayed_cache_work_func(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
|
|
|
|
ent = container_of(work, struct mlx5_cache_ent, dwork.work);
|
|
|
|
__cache_work_func(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_work_func(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
|
|
|
|
ent = container_of(work, struct mlx5_cache_ent, work);
|
|
|
|
__cache_work_func(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_ib_dev *dev, int order)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_ib_mr *mr = NULL;
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
int c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
c = order2idx(dev, order);
|
|
|
|
if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
|
|
|
|
mlx5_ib_warn(dev, "order %d, cache index %d\n", order, c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = c; i < MAX_MR_CACHE_ENTRIES; i++) {
|
|
|
|
ent = &cache->ent[i];
|
|
|
|
|
|
|
|
mlx5_ib_dbg(dev, "order %d, cache index %d\n", ent->order, i);
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (!list_empty(&ent->head)) {
|
|
|
|
mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
|
|
|
|
list);
|
|
|
|
list_del(&mr->list);
|
|
|
|
ent->cur--;
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (ent->cur < ent->limit)
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
break;
|
|
|
|
}
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mr)
|
|
|
|
cache->ent[c].miss++;
|
|
|
|
|
|
|
|
return mr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_cached_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
int shrink = 0;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = order2idx(dev, mr->order);
|
|
|
|
if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
|
|
|
|
mlx5_ib_warn(dev, "order %d, cache index %d\n", mr->order, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ent = &cache->ent[c];
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
list_add_tail(&mr->list, &ent->head);
|
|
|
|
ent->cur++;
|
|
|
|
if (ent->cur > 2 * ent->limit)
|
|
|
|
shrink = 1;
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
if (shrink)
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clean_keys(struct mlx5_ib_dev *dev, int c)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent = &cache->ent[c];
|
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
int err;
|
|
|
|
|
2013-09-11 21:35:23 +08:00
|
|
|
cancel_delayed_work(&ent->dwork);
|
2013-07-07 22:25:49 +08:00
|
|
|
while (1) {
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_lock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (list_empty(&ent->head)) {
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2013-07-07 22:25:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
|
|
|
|
list_del(&mr->list);
|
|
|
|
ent->cur--;
|
|
|
|
ent->size--;
|
2013-10-23 14:53:14 +08:00
|
|
|
spin_unlock_irq(&ent->lock);
|
2014-12-11 23:04:26 +08:00
|
|
|
err = destroy_mkey(dev, mr);
|
2013-09-11 21:35:26 +08:00
|
|
|
if (err)
|
2013-07-07 22:25:49 +08:00
|
|
|
mlx5_ib_warn(dev, "failed destroy mkey\n");
|
2013-09-11 21:35:26 +08:00
|
|
|
else
|
2013-07-07 22:25:49 +08:00
|
|
|
kfree(mr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!mlx5_debugfs_root)
|
|
|
|
return 0;
|
|
|
|
|
2014-07-29 04:30:22 +08:00
|
|
|
cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (!cache->root)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
|
|
|
|
ent = &cache->ent[i];
|
|
|
|
sprintf(ent->name, "%d", ent->order);
|
|
|
|
ent->dir = debugfs_create_dir(ent->name, cache->root);
|
|
|
|
if (!ent->dir)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ent->fsize = debugfs_create_file("size", 0600, ent->dir, ent,
|
|
|
|
&size_fops);
|
|
|
|
if (!ent->fsize)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ent->flimit = debugfs_create_file("limit", 0600, ent->dir, ent,
|
|
|
|
&limit_fops);
|
|
|
|
if (!ent->flimit)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ent->fcur = debugfs_create_u32("cur", 0400, ent->dir,
|
|
|
|
&ent->cur);
|
|
|
|
if (!ent->fcur)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ent->fmiss = debugfs_create_u32("miss", 0600, ent->dir,
|
|
|
|
&ent->miss);
|
|
|
|
if (!ent->fmiss)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
|
|
|
|
{
|
|
|
|
if (!mlx5_debugfs_root)
|
|
|
|
return;
|
|
|
|
|
|
|
|
debugfs_remove_recursive(dev->cache.root);
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
static void delay_time_func(unsigned long ctx)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = (struct mlx5_ib_dev *)ctx;
|
|
|
|
|
|
|
|
dev->fill_delay = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
|
|
|
|
{
|
|
|
|
struct mlx5_mr_cache *cache = &dev->cache;
|
|
|
|
struct mlx5_cache_ent *ent;
|
|
|
|
int limit;
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
cache->wq = create_singlethread_workqueue("mkey_cache");
|
|
|
|
if (!cache->wq) {
|
|
|
|
mlx5_ib_warn(dev, "failed to create work queue\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
setup_timer(&dev->delay_timer, delay_time_func, (unsigned long)dev);
|
2013-07-07 22:25:49 +08:00
|
|
|
for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
|
|
|
|
INIT_LIST_HEAD(&cache->ent[i].head);
|
|
|
|
spin_lock_init(&cache->ent[i].lock);
|
|
|
|
|
|
|
|
ent = &cache->ent[i];
|
|
|
|
INIT_LIST_HEAD(&ent->head);
|
|
|
|
spin_lock_init(&ent->lock);
|
|
|
|
ent->order = i + 2;
|
|
|
|
ent->dev = dev;
|
|
|
|
|
2014-07-29 04:30:22 +08:00
|
|
|
if (dev->mdev->profile->mask & MLX5_PROF_MASK_MR_CACHE)
|
|
|
|
limit = dev->mdev->profile->mr_cache[i].limit;
|
2013-10-24 17:01:00 +08:00
|
|
|
else
|
2013-07-07 22:25:49 +08:00
|
|
|
limit = 0;
|
2013-10-24 17:01:00 +08:00
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
INIT_WORK(&ent->work, cache_work_func);
|
|
|
|
INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
|
|
|
|
ent->limit = limit;
|
|
|
|
queue_work(cache->wq, &ent->work);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mlx5_mr_cache_debugfs_init(dev);
|
|
|
|
if (err)
|
|
|
|
mlx5_ib_warn(dev, "cache debugfs failure\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dev->cache.stopped = 1;
|
2013-09-11 21:35:23 +08:00
|
|
|
flush_workqueue(dev->cache.wq);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
mlx5_mr_cache_debugfs_cleanup(dev);
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
|
|
|
|
clean_keys(dev, i);
|
|
|
|
|
2013-09-11 21:35:23 +08:00
|
|
|
destroy_workqueue(dev->cache.wq);
|
2013-10-23 14:53:14 +08:00
|
|
|
del_timer_sync(&dev->delay_timer);
|
2013-09-11 21:35:23 +08:00
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
2014-07-29 04:30:22 +08:00
|
|
|
struct mlx5_core_dev *mdev = dev->mdev;
|
2013-07-07 22:25:49 +08:00
|
|
|
struct mlx5_create_mkey_mbox_in *in;
|
|
|
|
struct mlx5_mkey_seg *seg;
|
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
|
|
|
|
if (!mr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
in = kzalloc(sizeof(*in), GFP_KERNEL);
|
|
|
|
if (!in) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
seg = &in->seg;
|
|
|
|
seg->flags = convert_access(acc) | MLX5_ACCESS_MODE_PA;
|
|
|
|
seg->flags_pd = cpu_to_be32(to_mpd(pd)->pdn | MLX5_MKEY_LEN64);
|
|
|
|
seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
|
|
|
|
seg->start_addr = 0;
|
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
err = mlx5_core_create_mkey(mdev, &mr->mmkey, in, sizeof(*in), NULL, NULL,
|
2013-10-23 14:53:14 +08:00
|
|
|
NULL);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err)
|
|
|
|
goto err_in;
|
|
|
|
|
|
|
|
kfree(in);
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->ibmr.lkey = mr->mmkey.key;
|
|
|
|
mr->ibmr.rkey = mr->mmkey.key;
|
2013-07-07 22:25:49 +08:00
|
|
|
mr->umem = NULL;
|
|
|
|
|
|
|
|
return &mr->ibmr;
|
|
|
|
|
|
|
|
err_in:
|
|
|
|
kfree(in);
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
kfree(mr);
|
|
|
|
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_octo_len(u64 addr, u64 len, int page_size)
|
|
|
|
{
|
|
|
|
u64 offset;
|
|
|
|
int npages;
|
|
|
|
|
|
|
|
offset = addr & (page_size - 1);
|
|
|
|
npages = ALIGN(len + offset, page_size) >> ilog2(page_size);
|
|
|
|
return (npages + 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int use_umr(int order)
|
|
|
|
{
|
2014-12-11 23:04:21 +08:00
|
|
|
return order <= MLX5_MAX_UMR_SHIFT;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
static int dma_map_mr_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
|
|
|
|
int npages, int page_shift, int *size,
|
|
|
|
__be64 **mr_pas, dma_addr_t *dma)
|
|
|
|
{
|
|
|
|
__be64 *pas;
|
|
|
|
struct device *ddev = dev->ib_dev.dma_device;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
|
|
|
|
* To avoid copying garbage after the pas array, we allocate
|
|
|
|
* a little more.
|
|
|
|
*/
|
|
|
|
*size = ALIGN(sizeof(u64) * npages, MLX5_UMR_MTT_ALIGNMENT);
|
|
|
|
*mr_pas = kmalloc(*size + MLX5_UMR_ALIGN - 1, GFP_KERNEL);
|
|
|
|
if (!(*mr_pas))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pas = PTR_ALIGN(*mr_pas, MLX5_UMR_ALIGN);
|
|
|
|
mlx5_ib_populate_pas(dev, umem, page_shift, pas, MLX5_IB_MTT_PRESENT);
|
|
|
|
/* Clear padding after the actual pages. */
|
|
|
|
memset(pas + npages, 0, *size - npages * sizeof(u64));
|
|
|
|
|
|
|
|
*dma = dma_map_single(ddev, pas, *size, DMA_TO_DEVICE);
|
|
|
|
if (dma_mapping_error(ddev, *dma)) {
|
|
|
|
kfree(*mr_pas);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prep_umr_wqe_common(struct ib_pd *pd, struct ib_send_wr *wr,
|
|
|
|
struct ib_sge *sg, u64 dma, int n, u32 key,
|
|
|
|
int page_shift)
|
2013-07-07 22:25:49 +08:00
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
2015-10-08 16:16:33 +08:00
|
|
|
struct mlx5_umr_wr *umrwr = umr_wr(wr);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
sg->addr = dma;
|
|
|
|
sg->length = ALIGN(sizeof(u64) * n, 64);
|
2015-07-31 07:22:19 +08:00
|
|
|
sg->lkey = dev->umrc.pd->local_dma_lkey;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
wr->next = NULL;
|
|
|
|
wr->sg_list = sg;
|
|
|
|
if (n)
|
|
|
|
wr->num_sge = 1;
|
|
|
|
else
|
|
|
|
wr->num_sge = 0;
|
|
|
|
|
|
|
|
wr->opcode = MLX5_IB_WR_UMR;
|
2014-12-11 23:04:11 +08:00
|
|
|
|
|
|
|
umrwr->npages = n;
|
|
|
|
umrwr->page_shift = page_shift;
|
|
|
|
umrwr->mkey = key;
|
2016-02-29 22:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void prep_umr_reg_wqe(struct ib_pd *pd, struct ib_send_wr *wr,
|
|
|
|
struct ib_sge *sg, u64 dma, int n, u32 key,
|
|
|
|
int page_shift, u64 virt_addr, u64 len,
|
|
|
|
int access_flags)
|
|
|
|
{
|
|
|
|
struct mlx5_umr_wr *umrwr = umr_wr(wr);
|
|
|
|
|
|
|
|
prep_umr_wqe_common(pd, wr, sg, dma, n, key, page_shift);
|
|
|
|
|
|
|
|
wr->send_flags = 0;
|
|
|
|
|
2014-12-11 23:04:11 +08:00
|
|
|
umrwr->target.virt_addr = virt_addr;
|
|
|
|
umrwr->length = len;
|
|
|
|
umrwr->access_flags = access_flags;
|
|
|
|
umrwr->pd = pd;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void prep_umr_unreg_wqe(struct mlx5_ib_dev *dev,
|
|
|
|
struct ib_send_wr *wr, u32 key)
|
|
|
|
{
|
2015-10-08 16:16:33 +08:00
|
|
|
struct mlx5_umr_wr *umrwr = umr_wr(wr);
|
2014-12-11 23:04:11 +08:00
|
|
|
|
|
|
|
wr->send_flags = MLX5_IB_SEND_UMR_UNREG | MLX5_IB_SEND_UMR_FAIL_IF_FREE;
|
2013-07-07 22:25:49 +08:00
|
|
|
wr->opcode = MLX5_IB_WR_UMR;
|
2014-12-11 23:04:11 +08:00
|
|
|
umrwr->mkey = key;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
static struct ib_umem *mr_umem_get(struct ib_pd *pd, u64 start, u64 length,
|
|
|
|
int access_flags, int *npages,
|
|
|
|
int *page_shift, int *ncont, int *order)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct ib_umem *umem = ib_umem_get(pd->uobject->context, start, length,
|
|
|
|
access_flags, 0);
|
|
|
|
if (IS_ERR(umem)) {
|
|
|
|
mlx5_ib_err(dev, "umem get failed (%ld)\n", PTR_ERR(umem));
|
|
|
|
return (void *)umem;
|
|
|
|
}
|
|
|
|
|
|
|
|
mlx5_ib_cont_pages(umem, start, npages, page_shift, ncont, order);
|
|
|
|
if (!*npages) {
|
|
|
|
mlx5_ib_warn(dev, "avoid zero region\n");
|
|
|
|
ib_umem_release(umem);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
mlx5_ib_dbg(dev, "npages %d, ncont %d, order %d, page_shift %d\n",
|
|
|
|
*npages, *ncont, *order, *page_shift);
|
|
|
|
|
|
|
|
return umem;
|
|
|
|
}
|
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
static void mlx5_ib_umr_done(struct ib_cq *cq, struct ib_wc *wc)
|
2013-07-07 22:25:49 +08:00
|
|
|
{
|
2016-03-03 16:38:22 +08:00
|
|
|
struct mlx5_ib_umr_context *context =
|
|
|
|
container_of(wc->wr_cqe, struct mlx5_ib_umr_context, cqe);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
context->status = wc->status;
|
|
|
|
complete(&context->done);
|
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
|
|
|
|
{
|
|
|
|
context->cqe.done = mlx5_ib_umr_done;
|
|
|
|
context->status = -1;
|
|
|
|
init_completion(&context->done);
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
|
|
|
|
u64 virt_addr, u64 len, int npages,
|
|
|
|
int page_shift, int order, int access_flags)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
2013-09-11 21:35:26 +08:00
|
|
|
struct device *ddev = dev->ib_dev.dma_device;
|
2013-07-07 22:25:49 +08:00
|
|
|
struct umr_common *umrc = &dev->umrc;
|
2014-05-22 19:50:12 +08:00
|
|
|
struct mlx5_ib_umr_context umr_context;
|
2016-03-04 00:23:37 +08:00
|
|
|
struct mlx5_umr_wr umrwr = {};
|
2015-10-08 16:16:33 +08:00
|
|
|
struct ib_send_wr *bad;
|
2013-07-07 22:25:49 +08:00
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
struct ib_sge sg;
|
2014-12-11 23:04:21 +08:00
|
|
|
int size;
|
2014-12-11 23:04:10 +08:00
|
|
|
__be64 *mr_pas;
|
|
|
|
dma_addr_t dma;
|
2014-05-22 19:50:08 +08:00
|
|
|
int err = 0;
|
2013-07-07 22:25:49 +08:00
|
|
|
int i;
|
|
|
|
|
2013-10-23 14:53:14 +08:00
|
|
|
for (i = 0; i < 1; i++) {
|
2013-07-07 22:25:49 +08:00
|
|
|
mr = alloc_cached_mr(dev, order);
|
|
|
|
if (mr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = add_keys(dev, order2idx(dev, order), 1);
|
2013-10-23 14:53:14 +08:00
|
|
|
if (err && err != -EAGAIN) {
|
|
|
|
mlx5_ib_warn(dev, "add_keys failed, err %d\n", err);
|
2013-07-07 22:25:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mr)
|
|
|
|
return ERR_PTR(-EAGAIN);
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
err = dma_map_mr_pas(dev, umem, npages, page_shift, &size, &mr_pas,
|
|
|
|
&dma);
|
|
|
|
if (err)
|
2014-05-22 19:50:08 +08:00
|
|
|
goto free_mr;
|
2013-09-11 21:35:26 +08:00
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
mlx5_ib_init_umr_context(&umr_context);
|
|
|
|
|
|
|
|
umrwr.wr.wr_cqe = &umr_context.cqe;
|
2016-03-01 00:05:28 +08:00
|
|
|
prep_umr_reg_wqe(pd, &umrwr.wr, &sg, dma, npages, mr->mmkey.key,
|
2015-10-08 16:16:33 +08:00
|
|
|
page_shift, virt_addr, len, access_flags);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
down(&umrc->sem);
|
2015-10-08 16:16:33 +08:00
|
|
|
err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "post send failed, err %d\n", err);
|
2014-05-22 19:50:08 +08:00
|
|
|
goto unmap_dma;
|
2014-05-22 19:50:12 +08:00
|
|
|
} else {
|
|
|
|
wait_for_completion(&umr_context.done);
|
|
|
|
if (umr_context.status != IB_WC_SUCCESS) {
|
|
|
|
mlx5_ib_warn(dev, "reg umr failed\n");
|
|
|
|
err = -EFAULT;
|
|
|
|
}
|
2014-05-22 19:50:08 +08:00
|
|
|
}
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->mmkey.iova = virt_addr;
|
|
|
|
mr->mmkey.size = len;
|
|
|
|
mr->mmkey.pd = to_mpd(pd)->pdn;
|
2014-05-22 19:50:10 +08:00
|
|
|
|
2014-12-11 23:04:26 +08:00
|
|
|
mr->live = 1;
|
|
|
|
|
2014-05-22 19:50:08 +08:00
|
|
|
unmap_dma:
|
|
|
|
up(&umrc->sem);
|
2014-12-11 23:04:10 +08:00
|
|
|
dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
|
2014-05-22 19:50:08 +08:00
|
|
|
|
2014-12-11 23:04:10 +08:00
|
|
|
kfree(mr_pas);
|
2013-09-11 21:35:26 +08:00
|
|
|
|
2014-05-22 19:50:08 +08:00
|
|
|
free_mr:
|
|
|
|
if (err) {
|
|
|
|
free_cached_mr(dev, mr);
|
|
|
|
return ERR_PTR(err);
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return mr;
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:22 +08:00
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
|
|
|
int mlx5_ib_update_mtt(struct mlx5_ib_mr *mr, u64 start_page_index, int npages,
|
|
|
|
int zap)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = mr->dev;
|
|
|
|
struct device *ddev = dev->ib_dev.dma_device;
|
|
|
|
struct umr_common *umrc = &dev->umrc;
|
|
|
|
struct mlx5_ib_umr_context umr_context;
|
|
|
|
struct ib_umem *umem = mr->umem;
|
|
|
|
int size;
|
|
|
|
__be64 *pas;
|
|
|
|
dma_addr_t dma;
|
2015-10-08 16:16:33 +08:00
|
|
|
struct ib_send_wr *bad;
|
|
|
|
struct mlx5_umr_wr wr;
|
2014-12-11 23:04:22 +08:00
|
|
|
struct ib_sge sg;
|
|
|
|
int err = 0;
|
|
|
|
const int page_index_alignment = MLX5_UMR_MTT_ALIGNMENT / sizeof(u64);
|
|
|
|
const int page_index_mask = page_index_alignment - 1;
|
|
|
|
size_t pages_mapped = 0;
|
|
|
|
size_t pages_to_map = 0;
|
|
|
|
size_t pages_iter = 0;
|
|
|
|
int use_emergency_buf = 0;
|
|
|
|
|
|
|
|
/* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
|
|
|
|
* so we need to align the offset and length accordingly */
|
|
|
|
if (start_page_index & page_index_mask) {
|
|
|
|
npages += start_page_index & page_index_mask;
|
|
|
|
start_page_index &= ~page_index_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
pages_to_map = ALIGN(npages, page_index_alignment);
|
|
|
|
|
|
|
|
if (start_page_index + pages_to_map > MLX5_MAX_UMR_PAGES)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
size = sizeof(u64) * pages_to_map;
|
|
|
|
size = min_t(int, PAGE_SIZE, size);
|
|
|
|
/* We allocate with GFP_ATOMIC to avoid recursion into page-reclaim
|
|
|
|
* code, when we are called from an invalidation. The pas buffer must
|
|
|
|
* be 2k-aligned for Connect-IB. */
|
|
|
|
pas = (__be64 *)get_zeroed_page(GFP_ATOMIC);
|
|
|
|
if (!pas) {
|
|
|
|
mlx5_ib_warn(dev, "unable to allocate memory during MTT update, falling back to slower chunked mechanism.\n");
|
|
|
|
pas = mlx5_ib_update_mtt_emergency_buffer;
|
|
|
|
size = MLX5_UMR_MTT_MIN_CHUNK_SIZE;
|
|
|
|
use_emergency_buf = 1;
|
|
|
|
mutex_lock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
|
|
|
|
memset(pas, 0, size);
|
|
|
|
}
|
|
|
|
pages_iter = size / sizeof(u64);
|
|
|
|
dma = dma_map_single(ddev, pas, size, DMA_TO_DEVICE);
|
|
|
|
if (dma_mapping_error(ddev, dma)) {
|
|
|
|
mlx5_ib_err(dev, "unable to map DMA during MTT update.\n");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto free_pas;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pages_mapped = 0;
|
|
|
|
pages_mapped < pages_to_map && !err;
|
|
|
|
pages_mapped += pages_iter, start_page_index += pages_iter) {
|
|
|
|
dma_sync_single_for_cpu(ddev, dma, size, DMA_TO_DEVICE);
|
|
|
|
|
|
|
|
npages = min_t(size_t,
|
|
|
|
pages_iter,
|
|
|
|
ib_umem_num_pages(umem) - start_page_index);
|
|
|
|
|
|
|
|
if (!zap) {
|
|
|
|
__mlx5_ib_populate_pas(dev, umem, PAGE_SHIFT,
|
|
|
|
start_page_index, npages, pas,
|
|
|
|
MLX5_IB_MTT_PRESENT);
|
|
|
|
/* Clear padding after the pages brought from the
|
|
|
|
* umem. */
|
|
|
|
memset(pas + npages, 0, size - npages * sizeof(u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
dma_sync_single_for_device(ddev, dma, size, DMA_TO_DEVICE);
|
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
mlx5_ib_init_umr_context(&umr_context);
|
|
|
|
|
2014-12-11 23:04:22 +08:00
|
|
|
memset(&wr, 0, sizeof(wr));
|
2016-03-03 16:38:22 +08:00
|
|
|
wr.wr.wr_cqe = &umr_context.cqe;
|
2014-12-11 23:04:22 +08:00
|
|
|
|
|
|
|
sg.addr = dma;
|
|
|
|
sg.length = ALIGN(npages * sizeof(u64),
|
|
|
|
MLX5_UMR_MTT_ALIGNMENT);
|
2015-07-31 07:22:19 +08:00
|
|
|
sg.lkey = dev->umrc.pd->local_dma_lkey;
|
2014-12-11 23:04:22 +08:00
|
|
|
|
2015-10-08 16:16:33 +08:00
|
|
|
wr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE |
|
2014-12-11 23:04:22 +08:00
|
|
|
MLX5_IB_SEND_UMR_UPDATE_MTT;
|
2015-10-08 16:16:33 +08:00
|
|
|
wr.wr.sg_list = &sg;
|
|
|
|
wr.wr.num_sge = 1;
|
|
|
|
wr.wr.opcode = MLX5_IB_WR_UMR;
|
|
|
|
wr.npages = sg.length / sizeof(u64);
|
|
|
|
wr.page_shift = PAGE_SHIFT;
|
2016-03-01 00:05:28 +08:00
|
|
|
wr.mkey = mr->mmkey.key;
|
2015-10-08 16:16:33 +08:00
|
|
|
wr.target.offset = start_page_index;
|
2014-12-11 23:04:22 +08:00
|
|
|
|
|
|
|
down(&umrc->sem);
|
2015-10-08 16:16:33 +08:00
|
|
|
err = ib_post_send(umrc->qp, &wr.wr, &bad);
|
2014-12-11 23:04:22 +08:00
|
|
|
if (err) {
|
|
|
|
mlx5_ib_err(dev, "UMR post send failed, err %d\n", err);
|
|
|
|
} else {
|
|
|
|
wait_for_completion(&umr_context.done);
|
|
|
|
if (umr_context.status != IB_WC_SUCCESS) {
|
|
|
|
mlx5_ib_err(dev, "UMR completion failed, code %d\n",
|
|
|
|
umr_context.status);
|
|
|
|
err = -EFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up(&umrc->sem);
|
|
|
|
}
|
|
|
|
dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
|
|
|
|
|
|
|
|
free_pas:
|
|
|
|
if (!use_emergency_buf)
|
|
|
|
free_page((unsigned long)pas);
|
|
|
|
else
|
|
|
|
mutex_unlock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
/*
|
|
|
|
* If ibmr is NULL it will be allocated by reg_create.
|
|
|
|
* Else, the given ibmr will be used.
|
|
|
|
*/
|
|
|
|
static struct mlx5_ib_mr *reg_create(struct ib_mr *ibmr, struct ib_pd *pd,
|
|
|
|
u64 virt_addr, u64 length,
|
|
|
|
struct ib_umem *umem, int npages,
|
|
|
|
int page_shift, int access_flags)
|
2013-07-07 22:25:49 +08:00
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct mlx5_create_mkey_mbox_in *in;
|
|
|
|
struct mlx5_ib_mr *mr;
|
|
|
|
int inlen;
|
|
|
|
int err;
|
2015-05-29 03:28:41 +08:00
|
|
|
bool pg_cap = !!(MLX5_CAP_GEN(dev->mdev, pg));
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
mr = ibmr ? to_mmr(ibmr) : kzalloc(sizeof(*mr), GFP_KERNEL);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (!mr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
inlen = sizeof(*in) + sizeof(*in->pas) * ((npages + 1) / 2) * 2;
|
|
|
|
in = mlx5_vzalloc(inlen);
|
|
|
|
if (!in) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_1;
|
|
|
|
}
|
2014-12-11 23:04:21 +08:00
|
|
|
mlx5_ib_populate_pas(dev, umem, page_shift, in->pas,
|
|
|
|
pg_cap ? MLX5_IB_MTT_PRESENT : 0);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2014-12-11 23:04:21 +08:00
|
|
|
/* The MLX5_MKEY_INBOX_PG_ACCESS bit allows setting the access flags
|
|
|
|
* in the page list submitted with the command. */
|
|
|
|
in->flags = pg_cap ? cpu_to_be32(MLX5_MKEY_INBOX_PG_ACCESS) : 0;
|
2013-07-07 22:25:49 +08:00
|
|
|
in->seg.flags = convert_access(access_flags) |
|
|
|
|
MLX5_ACCESS_MODE_MTT;
|
|
|
|
in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
|
|
|
|
in->seg.start_addr = cpu_to_be64(virt_addr);
|
|
|
|
in->seg.len = cpu_to_be64(length);
|
|
|
|
in->seg.bsfs_octo_size = 0;
|
|
|
|
in->seg.xlt_oct_size = cpu_to_be32(get_octo_len(virt_addr, length, 1 << page_shift));
|
|
|
|
in->seg.log2_page_size = page_shift;
|
|
|
|
in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
|
2013-10-23 14:53:14 +08:00
|
|
|
in->xlat_oct_act_size = cpu_to_be32(get_octo_len(virt_addr, length,
|
|
|
|
1 << page_shift));
|
2016-03-01 00:05:28 +08:00
|
|
|
err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen, NULL,
|
2013-10-23 14:53:14 +08:00
|
|
|
NULL, NULL);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "create mkey failed\n");
|
|
|
|
goto err_2;
|
|
|
|
}
|
|
|
|
mr->umem = umem;
|
2015-01-06 19:56:01 +08:00
|
|
|
mr->dev = dev;
|
2014-12-11 23:04:26 +08:00
|
|
|
mr->live = 1;
|
2014-11-20 16:13:57 +08:00
|
|
|
kvfree(in);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmkey.key);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
return mr;
|
|
|
|
|
|
|
|
err_2:
|
2014-11-20 16:13:57 +08:00
|
|
|
kvfree(in);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
err_1:
|
2016-02-29 22:46:50 +08:00
|
|
|
if (!ibmr)
|
|
|
|
kfree(mr);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
static void set_mr_fileds(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
|
|
|
|
int npages, u64 length, int access_flags)
|
|
|
|
{
|
|
|
|
mr->npages = npages;
|
|
|
|
atomic_add(npages, &dev->mdev->priv.reg_pages);
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->ibmr.lkey = mr->mmkey.key;
|
|
|
|
mr->ibmr.rkey = mr->mmkey.key;
|
2016-02-29 22:46:50 +08:00
|
|
|
mr->ibmr.length = length;
|
2016-02-29 22:46:51 +08:00
|
|
|
mr->access_flags = access_flags;
|
2016-02-29 22:46:50 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
|
|
|
|
u64 virt_addr, int access_flags,
|
|
|
|
struct ib_udata *udata)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct mlx5_ib_mr *mr = NULL;
|
|
|
|
struct ib_umem *umem;
|
|
|
|
int page_shift;
|
|
|
|
int npages;
|
|
|
|
int ncont;
|
|
|
|
int order;
|
|
|
|
int err;
|
|
|
|
|
2014-09-14 21:47:51 +08:00
|
|
|
mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
|
|
|
|
start, virt_addr, length, access_flags);
|
2016-02-29 22:46:50 +08:00
|
|
|
umem = mr_umem_get(pd, start, length, access_flags, &npages,
|
|
|
|
&page_shift, &ncont, &order);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2016-02-29 22:46:50 +08:00
|
|
|
if (IS_ERR(umem))
|
|
|
|
return (void *)umem;
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
if (use_umr(order)) {
|
|
|
|
mr = reg_umr(pd, umem, virt_addr, length, ncont, page_shift,
|
|
|
|
order, access_flags);
|
|
|
|
if (PTR_ERR(mr) == -EAGAIN) {
|
|
|
|
mlx5_ib_dbg(dev, "cache empty for order %d", order);
|
|
|
|
mr = NULL;
|
|
|
|
}
|
2014-12-11 23:04:23 +08:00
|
|
|
} else if (access_flags & IB_ACCESS_ON_DEMAND) {
|
|
|
|
err = -EINVAL;
|
|
|
|
pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB");
|
|
|
|
goto error;
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mr)
|
2016-02-29 22:46:50 +08:00
|
|
|
mr = reg_create(NULL, pd, virt_addr, length, umem, ncont,
|
|
|
|
page_shift, access_flags);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
if (IS_ERR(mr)) {
|
|
|
|
err = PTR_ERR(mr);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
mr->umem = umem;
|
2016-02-29 22:46:50 +08:00
|
|
|
set_mr_fileds(dev, mr, npages, length, access_flags);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
2014-12-11 23:04:26 +08:00
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
2016-02-29 22:46:50 +08:00
|
|
|
update_odp_mr(mr);
|
2014-12-11 23:04:26 +08:00
|
|
|
#endif
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
return &mr->ibmr;
|
|
|
|
|
|
|
|
error:
|
|
|
|
ib_umem_release(umem);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
|
|
|
|
{
|
|
|
|
struct umr_common *umrc = &dev->umrc;
|
2014-05-22 19:50:12 +08:00
|
|
|
struct mlx5_ib_umr_context umr_context;
|
2016-03-04 00:23:37 +08:00
|
|
|
struct mlx5_umr_wr umrwr = {};
|
2015-10-08 16:16:33 +08:00
|
|
|
struct ib_send_wr *bad;
|
2013-07-07 22:25:49 +08:00
|
|
|
int err;
|
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
mlx5_ib_init_umr_context(&umr_context);
|
|
|
|
|
|
|
|
umrwr.wr.wr_cqe = &umr_context.cqe;
|
2016-03-01 00:05:28 +08:00
|
|
|
prep_umr_unreg_wqe(dev, &umrwr.wr, mr->mmkey.key);
|
2013-07-07 22:25:49 +08:00
|
|
|
|
|
|
|
down(&umrc->sem);
|
2015-10-08 16:16:33 +08:00
|
|
|
err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err) {
|
|
|
|
up(&umrc->sem);
|
|
|
|
mlx5_ib_dbg(dev, "err %d\n", err);
|
|
|
|
goto error;
|
2014-05-22 19:50:12 +08:00
|
|
|
} else {
|
|
|
|
wait_for_completion(&umr_context.done);
|
|
|
|
up(&umrc->sem);
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
2014-05-22 19:50:12 +08:00
|
|
|
if (umr_context.status != IB_WC_SUCCESS) {
|
2013-07-07 22:25:49 +08:00
|
|
|
mlx5_ib_warn(dev, "unreg umr failed\n");
|
|
|
|
err = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:46:51 +08:00
|
|
|
static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr, u64 virt_addr,
|
|
|
|
u64 length, int npages, int page_shift, int order,
|
|
|
|
int access_flags, int flags)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct device *ddev = dev->ib_dev.dma_device;
|
|
|
|
struct mlx5_ib_umr_context umr_context;
|
|
|
|
struct ib_send_wr *bad;
|
|
|
|
struct mlx5_umr_wr umrwr = {};
|
|
|
|
struct ib_sge sg;
|
|
|
|
struct umr_common *umrc = &dev->umrc;
|
|
|
|
dma_addr_t dma = 0;
|
|
|
|
__be64 *mr_pas = NULL;
|
|
|
|
int size;
|
|
|
|
int err;
|
|
|
|
|
2016-03-03 16:38:22 +08:00
|
|
|
mlx5_ib_init_umr_context(&umr_context);
|
|
|
|
|
|
|
|
umrwr.wr.wr_cqe = &umr_context.cqe;
|
2016-02-29 22:46:51 +08:00
|
|
|
umrwr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE;
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_TRANS) {
|
|
|
|
err = dma_map_mr_pas(dev, mr->umem, npages, page_shift, &size,
|
|
|
|
&mr_pas, &dma);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
umrwr.target.virt_addr = virt_addr;
|
|
|
|
umrwr.length = length;
|
|
|
|
umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
prep_umr_wqe_common(pd, &umrwr.wr, &sg, dma, npages, mr->mmkey.key,
|
2016-02-29 22:46:51 +08:00
|
|
|
page_shift);
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_PD) {
|
|
|
|
umrwr.pd = pd;
|
|
|
|
umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_PD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_ACCESS) {
|
|
|
|
umrwr.access_flags = access_flags;
|
|
|
|
umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_ACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post send request to UMR QP */
|
|
|
|
down(&umrc->sem);
|
|
|
|
err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "post send failed, err %d\n", err);
|
|
|
|
} else {
|
|
|
|
wait_for_completion(&umr_context.done);
|
|
|
|
if (umr_context.status != IB_WC_SUCCESS) {
|
|
|
|
mlx5_ib_warn(dev, "reg umr failed (%u)\n",
|
|
|
|
umr_context.status);
|
|
|
|
err = -EFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
up(&umrc->sem);
|
|
|
|
if (flags & IB_MR_REREG_TRANS) {
|
|
|
|
dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
|
|
|
|
kfree(mr_pas);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
|
|
|
|
u64 length, u64 virt_addr, int new_access_flags,
|
|
|
|
struct ib_pd *new_pd, struct ib_udata *udata)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(ib_mr->device);
|
|
|
|
struct mlx5_ib_mr *mr = to_mmr(ib_mr);
|
|
|
|
struct ib_pd *pd = (flags & IB_MR_REREG_PD) ? new_pd : ib_mr->pd;
|
|
|
|
int access_flags = flags & IB_MR_REREG_ACCESS ?
|
|
|
|
new_access_flags :
|
|
|
|
mr->access_flags;
|
|
|
|
u64 addr = (flags & IB_MR_REREG_TRANS) ? virt_addr : mr->umem->address;
|
|
|
|
u64 len = (flags & IB_MR_REREG_TRANS) ? length : mr->umem->length;
|
|
|
|
int page_shift = 0;
|
|
|
|
int npages = 0;
|
|
|
|
int ncont = 0;
|
|
|
|
int order = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
|
|
|
|
start, virt_addr, length, access_flags);
|
|
|
|
|
|
|
|
if (flags != IB_MR_REREG_PD) {
|
|
|
|
/*
|
|
|
|
* Replace umem. This needs to be done whether or not UMR is
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
flags |= IB_MR_REREG_TRANS;
|
|
|
|
ib_umem_release(mr->umem);
|
|
|
|
mr->umem = mr_umem_get(pd, addr, len, access_flags, &npages,
|
|
|
|
&page_shift, &ncont, &order);
|
|
|
|
if (IS_ERR(mr->umem)) {
|
|
|
|
err = PTR_ERR(mr->umem);
|
|
|
|
mr->umem = NULL;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_TRANS && !use_umr_mtt_update(mr, addr, len)) {
|
|
|
|
/*
|
|
|
|
* UMR can't be used - MKey needs to be replaced.
|
|
|
|
*/
|
|
|
|
if (mr->umred) {
|
|
|
|
err = unreg_umr(dev, mr);
|
|
|
|
if (err)
|
|
|
|
mlx5_ib_warn(dev, "Failed to unregister MR\n");
|
|
|
|
} else {
|
|
|
|
err = destroy_mkey(dev, mr);
|
|
|
|
if (err)
|
|
|
|
mlx5_ib_warn(dev, "Failed to destroy MKey\n");
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
mr = reg_create(ib_mr, pd, addr, len, mr->umem, ncont,
|
|
|
|
page_shift, access_flags);
|
|
|
|
|
|
|
|
if (IS_ERR(mr))
|
|
|
|
return PTR_ERR(mr);
|
|
|
|
|
|
|
|
mr->umred = 0;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Send a UMR WQE
|
|
|
|
*/
|
|
|
|
err = rereg_umr(pd, mr, addr, len, npages, page_shift,
|
|
|
|
order, access_flags, flags);
|
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "Failed to rereg UMR\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_PD) {
|
|
|
|
ib_mr->pd = pd;
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->mmkey.pd = to_mpd(pd)->pdn;
|
2016-02-29 22:46:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_ACCESS)
|
|
|
|
mr->access_flags = access_flags;
|
|
|
|
|
|
|
|
if (flags & IB_MR_REREG_TRANS) {
|
|
|
|
atomic_sub(mr->npages, &dev->mdev->priv.reg_pages);
|
|
|
|
set_mr_fileds(dev, mr, npages, len, access_flags);
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->mmkey.iova = addr;
|
|
|
|
mr->mmkey.size = len;
|
2016-02-29 22:46:51 +08:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
|
|
|
update_odp_mr(mr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-14 00:11:26 +08:00
|
|
|
static int
|
|
|
|
mlx5_alloc_priv_descs(struct ib_device *device,
|
|
|
|
struct mlx5_ib_mr *mr,
|
|
|
|
int ndescs,
|
|
|
|
int desc_size)
|
|
|
|
{
|
|
|
|
int size = ndescs * desc_size;
|
|
|
|
int add_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
add_size = max_t(int, MLX5_UMR_ALIGN - ARCH_KMALLOC_MINALIGN, 0);
|
|
|
|
|
|
|
|
mr->descs_alloc = kzalloc(size + add_size, GFP_KERNEL);
|
|
|
|
if (!mr->descs_alloc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mr->descs = PTR_ALIGN(mr->descs_alloc, MLX5_UMR_ALIGN);
|
|
|
|
|
|
|
|
mr->desc_map = dma_map_single(device->dma_device, mr->descs,
|
|
|
|
size, DMA_TO_DEVICE);
|
|
|
|
if (dma_mapping_error(device->dma_device, mr->desc_map)) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
kfree(mr->descs_alloc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mlx5_free_priv_descs(struct mlx5_ib_mr *mr)
|
|
|
|
{
|
|
|
|
if (mr->descs) {
|
|
|
|
struct ib_device *device = mr->ibmr.device;
|
|
|
|
int size = mr->max_descs * mr->desc_size;
|
|
|
|
|
|
|
|
dma_unmap_single(device->dma_device, mr->desc_map,
|
|
|
|
size, DMA_TO_DEVICE);
|
|
|
|
kfree(mr->descs_alloc);
|
|
|
|
mr->descs = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:23 +08:00
|
|
|
static int clean_mr(struct mlx5_ib_mr *mr)
|
2013-07-07 22:25:49 +08:00
|
|
|
{
|
2014-12-11 23:04:23 +08:00
|
|
|
struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
|
2013-07-07 22:25:49 +08:00
|
|
|
int umred = mr->umred;
|
|
|
|
int err;
|
|
|
|
|
2015-07-30 15:32:34 +08:00
|
|
|
if (mr->sig) {
|
|
|
|
if (mlx5_core_destroy_psv(dev->mdev,
|
|
|
|
mr->sig->psv_memory.psv_idx))
|
|
|
|
mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
|
|
|
|
mr->sig->psv_memory.psv_idx);
|
|
|
|
if (mlx5_core_destroy_psv(dev->mdev,
|
|
|
|
mr->sig->psv_wire.psv_idx))
|
|
|
|
mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
|
|
|
|
mr->sig->psv_wire.psv_idx);
|
|
|
|
kfree(mr->sig);
|
|
|
|
mr->sig = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-14 00:11:26 +08:00
|
|
|
mlx5_free_priv_descs(mr);
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
if (!umred) {
|
2014-12-11 23:04:26 +08:00
|
|
|
err = destroy_mkey(dev, mr);
|
2013-07-07 22:25:49 +08:00
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->mmkey.key, err);
|
2013-07-07 22:25:49 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = unreg_umr(dev, mr);
|
|
|
|
if (err) {
|
|
|
|
mlx5_ib_warn(dev, "failed unregister\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
free_cached_mr(dev, mr);
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:23 +08:00
|
|
|
if (!umred)
|
|
|
|
kfree(mr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mlx5_ib_dereg_mr(struct ib_mr *ibmr)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
|
|
|
|
struct mlx5_ib_mr *mr = to_mmr(ibmr);
|
|
|
|
int npages = mr->npages;
|
|
|
|
struct ib_umem *umem = mr->umem;
|
|
|
|
|
|
|
|
#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
|
2014-12-11 23:04:26 +08:00
|
|
|
if (umem && umem->odp_data) {
|
|
|
|
/* Prevent new page faults from succeeding */
|
|
|
|
mr->live = 0;
|
2014-12-11 23:04:23 +08:00
|
|
|
/* Wait for all running page-fault handlers to finish. */
|
|
|
|
synchronize_srcu(&dev->mr_srcu);
|
2014-12-11 23:04:26 +08:00
|
|
|
/* Destroy all page mappings */
|
|
|
|
mlx5_ib_invalidate_range(umem, ib_umem_start(umem),
|
|
|
|
ib_umem_end(umem));
|
|
|
|
/*
|
|
|
|
* We kill the umem before the MR for ODP,
|
|
|
|
* so that there will not be any invalidations in
|
|
|
|
* flight, looking at the *mr struct.
|
|
|
|
*/
|
|
|
|
ib_umem_release(umem);
|
|
|
|
atomic_sub(npages, &dev->mdev->priv.reg_pages);
|
|
|
|
|
|
|
|
/* Avoid double-freeing the umem. */
|
|
|
|
umem = NULL;
|
|
|
|
}
|
2014-12-11 23:04:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
clean_mr(mr);
|
|
|
|
|
2013-07-07 22:25:49 +08:00
|
|
|
if (umem) {
|
|
|
|
ib_umem_release(umem);
|
2014-12-11 23:04:23 +08:00
|
|
|
atomic_sub(npages, &dev->mdev->priv.reg_pages);
|
2013-07-07 22:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:32:35 +08:00
|
|
|
struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
|
|
|
|
enum ib_mr_type mr_type,
|
|
|
|
u32 max_num_sg)
|
2014-02-23 20:19:06 +08:00
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct mlx5_create_mkey_mbox_in *in;
|
|
|
|
struct mlx5_ib_mr *mr;
|
2016-03-01 01:07:33 +08:00
|
|
|
int ndescs = ALIGN(max_num_sg, 4);
|
|
|
|
int err;
|
2014-02-23 20:19:06 +08:00
|
|
|
|
|
|
|
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
|
|
|
|
if (!mr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
in = kzalloc(sizeof(*in), GFP_KERNEL);
|
|
|
|
if (!in) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:11 +08:00
|
|
|
in->seg.status = MLX5_MKEY_STATUS_FREE;
|
2014-02-23 20:19:06 +08:00
|
|
|
in->seg.xlt_oct_size = cpu_to_be32(ndescs);
|
|
|
|
in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
|
|
|
|
in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
|
|
|
|
|
2015-07-30 15:32:35 +08:00
|
|
|
if (mr_type == IB_MR_TYPE_MEM_REG) {
|
2016-03-01 01:07:33 +08:00
|
|
|
mr->access_mode = MLX5_ACCESS_MODE_MTT;
|
2015-07-30 15:32:35 +08:00
|
|
|
in->seg.log2_page_size = PAGE_SHIFT;
|
2015-10-14 00:11:26 +08:00
|
|
|
|
|
|
|
err = mlx5_alloc_priv_descs(pd->device, mr,
|
|
|
|
ndescs, sizeof(u64));
|
|
|
|
if (err)
|
|
|
|
goto err_free_in;
|
|
|
|
|
|
|
|
mr->desc_size = sizeof(u64);
|
|
|
|
mr->max_descs = ndescs;
|
2016-03-01 01:07:33 +08:00
|
|
|
} else if (mr_type == IB_MR_TYPE_SG_GAPS) {
|
|
|
|
mr->access_mode = MLX5_ACCESS_MODE_KLM;
|
|
|
|
|
|
|
|
err = mlx5_alloc_priv_descs(pd->device, mr,
|
|
|
|
ndescs, sizeof(struct mlx5_klm));
|
|
|
|
if (err)
|
|
|
|
goto err_free_in;
|
|
|
|
mr->desc_size = sizeof(struct mlx5_klm);
|
|
|
|
mr->max_descs = ndescs;
|
2015-07-30 15:32:35 +08:00
|
|
|
} else if (mr_type == IB_MR_TYPE_SIGNATURE) {
|
2014-02-23 20:19:06 +08:00
|
|
|
u32 psv_index[2];
|
|
|
|
|
|
|
|
in->seg.flags_pd = cpu_to_be32(be32_to_cpu(in->seg.flags_pd) |
|
|
|
|
MLX5_MKEY_BSF_EN);
|
|
|
|
in->seg.bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
|
|
|
|
mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
|
|
|
|
if (!mr->sig) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_free_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create mem & wire PSVs */
|
2014-07-29 04:30:22 +08:00
|
|
|
err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn,
|
2014-02-23 20:19:06 +08:00
|
|
|
2, psv_index);
|
|
|
|
if (err)
|
|
|
|
goto err_free_sig;
|
|
|
|
|
2016-03-01 01:07:33 +08:00
|
|
|
mr->access_mode = MLX5_ACCESS_MODE_KLM;
|
2014-02-23 20:19:06 +08:00
|
|
|
mr->sig->psv_memory.psv_idx = psv_index[0];
|
|
|
|
mr->sig->psv_wire.psv_idx = psv_index[1];
|
2014-02-23 20:19:12 +08:00
|
|
|
|
|
|
|
mr->sig->sig_status_checked = true;
|
|
|
|
mr->sig->sig_err_exists = false;
|
|
|
|
/* Next UMR, Arm SIGERR */
|
|
|
|
++mr->sig->sigerr_count;
|
2015-07-30 15:32:35 +08:00
|
|
|
} else {
|
|
|
|
mlx5_ib_warn(dev, "Invalid mr type %d\n", mr_type);
|
|
|
|
err = -EINVAL;
|
|
|
|
goto err_free_in;
|
2014-02-23 20:19:06 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 01:07:33 +08:00
|
|
|
in->seg.flags = MLX5_PERM_UMR_EN | mr->access_mode;
|
2016-03-01 00:05:28 +08:00
|
|
|
err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, sizeof(*in),
|
2014-02-23 20:19:06 +08:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (err)
|
|
|
|
goto err_destroy_psv;
|
|
|
|
|
2016-03-01 00:05:28 +08:00
|
|
|
mr->ibmr.lkey = mr->mmkey.key;
|
|
|
|
mr->ibmr.rkey = mr->mmkey.key;
|
2014-02-23 20:19:06 +08:00
|
|
|
mr->umem = NULL;
|
|
|
|
kfree(in);
|
|
|
|
|
|
|
|
return &mr->ibmr;
|
|
|
|
|
|
|
|
err_destroy_psv:
|
|
|
|
if (mr->sig) {
|
2014-07-29 04:30:22 +08:00
|
|
|
if (mlx5_core_destroy_psv(dev->mdev,
|
2014-02-23 20:19:06 +08:00
|
|
|
mr->sig->psv_memory.psv_idx))
|
|
|
|
mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
|
|
|
|
mr->sig->psv_memory.psv_idx);
|
2014-07-29 04:30:22 +08:00
|
|
|
if (mlx5_core_destroy_psv(dev->mdev,
|
2014-02-23 20:19:06 +08:00
|
|
|
mr->sig->psv_wire.psv_idx))
|
|
|
|
mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
|
|
|
|
mr->sig->psv_wire.psv_idx);
|
|
|
|
}
|
2015-10-14 00:11:26 +08:00
|
|
|
mlx5_free_priv_descs(mr);
|
2014-02-23 20:19:06 +08:00
|
|
|
err_free_sig:
|
|
|
|
kfree(mr->sig);
|
|
|
|
err_free_in:
|
|
|
|
kfree(in);
|
|
|
|
err_free:
|
|
|
|
kfree(mr);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:05:30 +08:00
|
|
|
struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
|
|
|
|
struct ib_udata *udata)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_dev *dev = to_mdev(pd->device);
|
|
|
|
struct mlx5_create_mkey_mbox_in *in = NULL;
|
|
|
|
struct mlx5_ib_mw *mw = NULL;
|
|
|
|
int ndescs;
|
|
|
|
int err;
|
|
|
|
struct mlx5_ib_alloc_mw req = {};
|
|
|
|
struct {
|
|
|
|
__u32 comp_mask;
|
|
|
|
__u32 response_length;
|
|
|
|
} resp = {};
|
|
|
|
|
|
|
|
err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
|
|
|
|
if (err)
|
|
|
|
return ERR_PTR(err);
|
|
|
|
|
|
|
|
if (req.comp_mask || req.reserved1 || req.reserved2)
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
|
|
|
|
if (udata->inlen > sizeof(req) &&
|
|
|
|
!ib_is_udata_cleared(udata, sizeof(req),
|
|
|
|
udata->inlen - sizeof(req)))
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
|
|
|
|
ndescs = req.num_klms ? roundup(req.num_klms, 4) : roundup(1, 4);
|
|
|
|
|
|
|
|
mw = kzalloc(sizeof(*mw), GFP_KERNEL);
|
|
|
|
in = kzalloc(sizeof(*in), GFP_KERNEL);
|
|
|
|
if (!mw || !in) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
|
|
|
in->seg.status = MLX5_MKEY_STATUS_FREE;
|
|
|
|
in->seg.xlt_oct_size = cpu_to_be32(ndescs);
|
|
|
|
in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
|
|
|
|
in->seg.flags = MLX5_PERM_UMR_EN | MLX5_ACCESS_MODE_KLM |
|
|
|
|
MLX5_PERM_LOCAL_READ;
|
|
|
|
if (type == IB_MW_TYPE_2)
|
|
|
|
in->seg.flags_pd |= cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
|
|
|
|
in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
|
|
|
|
|
|
|
|
err = mlx5_core_create_mkey(dev->mdev, &mw->mmkey, in, sizeof(*in),
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (err)
|
|
|
|
goto free;
|
|
|
|
|
|
|
|
mw->ibmw.rkey = mw->mmkey.key;
|
|
|
|
|
|
|
|
resp.response_length = min(offsetof(typeof(resp), response_length) +
|
|
|
|
sizeof(resp.response_length), udata->outlen);
|
|
|
|
if (resp.response_length) {
|
|
|
|
err = ib_copy_to_udata(udata, &resp, resp.response_length);
|
|
|
|
if (err) {
|
|
|
|
mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(in);
|
|
|
|
return &mw->ibmw;
|
|
|
|
|
|
|
|
free:
|
|
|
|
kfree(mw);
|
|
|
|
kfree(in);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mlx5_ib_dealloc_mw(struct ib_mw *mw)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_mw *mmw = to_mmw(mw);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mlx5_core_destroy_mkey((to_mdev(mw->device))->mdev,
|
|
|
|
&mmw->mmkey);
|
|
|
|
if (!err)
|
|
|
|
kfree(mmw);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-02-23 20:19:12 +08:00
|
|
|
int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
|
|
|
|
struct ib_mr_status *mr_status)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_mr *mmr = to_mmr(ibmr);
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
|
|
|
|
pr_err("Invalid status check mask\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
mr_status->fail_status = 0;
|
|
|
|
if (check_mask & IB_MR_CHECK_SIG_STATUS) {
|
|
|
|
if (!mmr->sig) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
pr_err("signature status check requested on a non-signature enabled MR\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
mmr->sig->sig_status_checked = true;
|
|
|
|
if (!mmr->sig->sig_err_exists)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (ibmr->lkey == mmr->sig->err_item.key)
|
|
|
|
memcpy(&mr_status->sig_err, &mmr->sig->err_item,
|
|
|
|
sizeof(mr_status->sig_err));
|
|
|
|
else {
|
|
|
|
mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
|
|
|
|
mr_status->sig_err.sig_err_offset = 0;
|
|
|
|
mr_status->sig_err.key = mmr->sig->err_item.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
mmr->sig->sig_err_exists = false;
|
|
|
|
mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-14 00:11:26 +08:00
|
|
|
|
2016-03-01 01:07:33 +08:00
|
|
|
static int
|
|
|
|
mlx5_ib_sg_to_klms(struct mlx5_ib_mr *mr,
|
|
|
|
struct scatterlist *sgl,
|
|
|
|
unsigned short sg_nents)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg = sgl;
|
|
|
|
struct mlx5_klm *klms = mr->descs;
|
|
|
|
u32 lkey = mr->ibmr.pd->local_dma_lkey;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mr->ibmr.iova = sg_dma_address(sg);
|
|
|
|
mr->ibmr.length = 0;
|
|
|
|
mr->ndescs = sg_nents;
|
|
|
|
|
|
|
|
for_each_sg(sgl, sg, sg_nents, i) {
|
|
|
|
if (unlikely(i > mr->max_descs))
|
|
|
|
break;
|
|
|
|
klms[i].va = cpu_to_be64(sg_dma_address(sg));
|
|
|
|
klms[i].bcount = cpu_to_be32(sg_dma_len(sg));
|
|
|
|
klms[i].key = cpu_to_be32(lkey);
|
|
|
|
mr->ibmr.length += sg_dma_len(sg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-10-14 00:11:26 +08:00
|
|
|
static int mlx5_set_page(struct ib_mr *ibmr, u64 addr)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_mr *mr = to_mmr(ibmr);
|
|
|
|
__be64 *descs;
|
|
|
|
|
|
|
|
if (unlikely(mr->ndescs == mr->max_descs))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
descs = mr->descs;
|
|
|
|
descs[mr->ndescs++] = cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mlx5_ib_map_mr_sg(struct ib_mr *ibmr,
|
|
|
|
struct scatterlist *sg,
|
|
|
|
int sg_nents)
|
|
|
|
{
|
|
|
|
struct mlx5_ib_mr *mr = to_mmr(ibmr);
|
|
|
|
int n;
|
|
|
|
|
|
|
|
mr->ndescs = 0;
|
|
|
|
|
|
|
|
ib_dma_sync_single_for_cpu(ibmr->device, mr->desc_map,
|
|
|
|
mr->desc_size * mr->max_descs,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
|
2016-03-01 01:07:33 +08:00
|
|
|
if (mr->access_mode == MLX5_ACCESS_MODE_KLM)
|
|
|
|
n = mlx5_ib_sg_to_klms(mr, sg, sg_nents);
|
|
|
|
else
|
|
|
|
n = ib_sg_to_pages(ibmr, sg, sg_nents, mlx5_set_page);
|
2015-10-14 00:11:26 +08:00
|
|
|
|
|
|
|
ib_dma_sync_single_for_device(ibmr->device, mr->desc_map,
|
|
|
|
mr->desc_size * mr->max_descs,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|