Commit 29c8d9eb authored by Adit Ranadive's avatar Adit Ranadive Committed by Doug Ledford

IB: Add vmw_pvrdma driver

This patch series adds a driver for a paravirtual RDMA device. The
device is developed for VMware's Virtual Machines and allows existing RDMA
applications to continue to use existing Verbs API when deployed in VMs
on ESXi. We recently did a presentation in the OFA Workshop [1] regarding
this device.

Description and RDMA Support
============================
The virtual device is exposed as a dual function PCIe device. One part
is a virtual network device (VMXNet3) which provides networking properties
like MAC, IP addresses to the RDMA part of the device. The networking
properties are used to register GIDs required by RDMA applications to
communicate.

These patches add support and the all required infrastructure for
letting applications use such a device. We support the mandatory Verbs API as
well as the base memory management extensions (Local Inv, Send with Inv and
Fast Register Work Requests). We currently support both Reliable Connected
and Unreliable Datagram QPs but do not support Shared Receive Queues
(SRQs).

Also, we support the following types of Work Requests:
 o Send/Receive (with or without Immediate Data)
 o RDMA Write (with or without Immediate Data)
 o RDMA Read
 o Local Invalidate
 o Send with Invalidate
 o Fast Register Work Requests

This version only adds support for version 1 of RoCE. We will add RoCEv2
support in a future patch. We do support registration of both MAC-based
and IP-based GIDs. I have also created a git tree for our user-level driver
[2].

Testing
=======
We have tested this internally for various types of Guest OS - Red Hat,
Centos, Ubuntu 12.04/14.04/16.04, Oracle Enterprise Linux, SLES 12
using backported versions of this driver. The tests included several
runs of the performance tests (included with OFED), Intel MPI PingPong
benchmark on OpenMPI, krping for FRWRs. Mellanox has been kind enough
to test the backported version of the driver internally on their hardware
using a VMware provided ESX build. I have also applied and tested this
with Doug's k.o/for-4.9 branch (commit 5603910b). Note, that this patch
series should be applied all together. I split out the commits so that
it may be easier to review.

PVRDMA Resources
================
[1] OFA Workshop Presentation -
https://openfabrics.org/images/eventpresos/2016presentations/102parardma.pdf

[2] Libpvrdma User-level library -
http://git.openfabrics.org/?p=~aditr/libpvrdma.git;a=summaryReviewed-by: default avatarJorgen Hansen <jhansen@vmware.com>
Reviewed-by: default avatarGeorge Zhang <georgezhang@vmware.com>
Reviewed-by: default avatarAditya Sarwade <asarwade@vmware.com>
Reviewed-by: default avatarBryan Tan <bryantan@vmware.com>
Reviewed-by: default avatarLeon Romanovsky <leonro@mellanox.com>
Signed-off-by: default avatarAdit Ranadive <aditr@vmware.com>
Signed-off-by: default avatarDoug Ledford <dledford@redhat.com>
parent b1226c7d
......@@ -12928,6 +12928,13 @@ S: Maintained
F: drivers/scsi/vmw_pvscsi.c
F: drivers/scsi/vmw_pvscsi.h
VMWARE PVRDMA DRIVER
M: Adit Ranadive <aditr@vmware.com>
M: VMware PV-Drivers <pv-drivers@vmware.com>
L: linux-rdma@vger.kernel.org
S: Maintained
F: drivers/infiniband/hw/vmw_pvrdma/
VOLTAGE AND CURRENT REGULATOR FRAMEWORK
M: Liam Girdwood <lgirdwood@gmail.com>
M: Mark Brown <broonie@kernel.org>
......
......@@ -73,6 +73,7 @@ source "drivers/infiniband/hw/mlx4/Kconfig"
source "drivers/infiniband/hw/mlx5/Kconfig"
source "drivers/infiniband/hw/nes/Kconfig"
source "drivers/infiniband/hw/ocrdma/Kconfig"
source "drivers/infiniband/hw/vmw_pvrdma/Kconfig"
source "drivers/infiniband/hw/usnic/Kconfig"
source "drivers/infiniband/hw/hns/Kconfig"
......
......@@ -7,6 +7,7 @@ obj-$(CONFIG_MLX4_INFINIBAND) += mlx4/
obj-$(CONFIG_MLX5_INFINIBAND) += mlx5/
obj-$(CONFIG_INFINIBAND_NES) += nes/
obj-$(CONFIG_INFINIBAND_OCRDMA) += ocrdma/
obj-$(CONFIG_INFINIBAND_VMWARE_PVRDMA) += vmw_pvrdma/
obj-$(CONFIG_INFINIBAND_USNIC) += usnic/
obj-$(CONFIG_INFINIBAND_HFI1) += hfi1/
obj-$(CONFIG_INFINIBAND_HNS) += hns/
......
config INFINIBAND_VMWARE_PVRDMA
tristate "VMware Paravirtualized RDMA Driver"
depends on NETDEVICES && ETHERNET && PCI && INET && VMXNET3
---help---
This driver provides low-level support for VMware Paravirtual
RDMA adapter. It interacts with the VMXNet3 driver to provide
Ethernet capabilities.
obj-$(CONFIG_INFINIBAND_VMWARE_PVRDMA) += vmw_pvrdma.o
vmw_pvrdma-y := pvrdma_cmd.o pvrdma_cq.o pvrdma_doorbell.o pvrdma_main.o pvrdma_misc.o pvrdma_mr.o pvrdma_qp.o pvrdma_verbs.o
This diff is collapsed.
/*
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of EITHER the GNU General Public License
* version 2 as published by the Free Software Foundation or the BSD
* 2-Clause License. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*
* You should have received a copy of the GNU General Public License
* along with this program available in the file COPYING in the main
* directory of this source tree.
*
* The BSD 2-Clause License
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/list.h>
#include "pvrdma.h"
#define PVRDMA_CMD_TIMEOUT 10000 /* ms */
static inline int pvrdma_cmd_recv(struct pvrdma_dev *dev,
union pvrdma_cmd_resp *resp,
unsigned resp_code)
{
int err;
dev_dbg(&dev->pdev->dev, "receive response from device\n");
err = wait_for_completion_interruptible_timeout(&dev->cmd_done,
msecs_to_jiffies(PVRDMA_CMD_TIMEOUT));
if (err == 0 || err == -ERESTARTSYS) {
dev_warn(&dev->pdev->dev,
"completion timeout or interrupted\n");
return -ETIMEDOUT;
}
spin_lock(&dev->cmd_lock);
memcpy(resp, dev->resp_slot, sizeof(*resp));
spin_unlock(&dev->cmd_lock);
if (resp->hdr.ack != resp_code) {
dev_warn(&dev->pdev->dev,
"unknown response %#x expected %#x\n",
resp->hdr.ack, resp_code);
return -EFAULT;
}
return 0;
}
int
pvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
union pvrdma_cmd_resp *resp, unsigned resp_code)
{
int err;
dev_dbg(&dev->pdev->dev, "post request to device\n");
/* Serializiation */
down(&dev->cmd_sema);
BUILD_BUG_ON(sizeof(union pvrdma_cmd_req) !=
sizeof(struct pvrdma_cmd_modify_qp));
spin_lock(&dev->cmd_lock);
memcpy(dev->cmd_slot, req, sizeof(*req));
spin_unlock(&dev->cmd_lock);
init_completion(&dev->cmd_done);
pvrdma_write_reg(dev, PVRDMA_REG_REQUEST, 0);
/* Make sure the request is written before reading status. */
mb();
err = pvrdma_read_reg(dev, PVRDMA_REG_ERR);
if (err == 0) {
if (resp != NULL)
err = pvrdma_cmd_recv(dev, resp, resp_code);
} else {
dev_warn(&dev->pdev->dev,
"failed to write request error reg: %d\n", err);
err = -EFAULT;
}
up(&dev->cmd_sema);
return err;
}
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of EITHER the GNU General Public License
* version 2 as published by the Free Software Foundation or the BSD
* 2-Clause License. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*
* You should have received a copy of the GNU General Public License
* along with this program available in the file COPYING in the main
* directory of this source tree.
*
* The BSD 2-Clause License
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/bitmap.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include "pvrdma.h"
int pvrdma_uar_table_init(struct pvrdma_dev *dev)
{
u32 num = dev->dsr->caps.max_uar;
u32 mask = num - 1;
struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
if (!is_power_of_2(num))
return -EINVAL;
tbl->last = 0;
tbl->top = 0;
tbl->max = num;
tbl->mask = mask;
spin_lock_init(&tbl->lock);
tbl->table = kcalloc(BITS_TO_LONGS(num), sizeof(long), GFP_KERNEL);
if (!tbl->table)
return -ENOMEM;
/* 0th UAR is taken by the device. */
set_bit(0, tbl->table);
return 0;
}
void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev)
{
struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
kfree(tbl->table);
}
int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
{
struct pvrdma_id_table *tbl;
unsigned long flags;
u32 obj;
tbl = &dev->uar_table.tbl;
spin_lock_irqsave(&tbl->lock, flags);
obj = find_next_zero_bit(tbl->table, tbl->max, tbl->last);
if (obj >= tbl->max) {
tbl->top = (tbl->top + tbl->max) & tbl->mask;
obj = find_first_zero_bit(tbl->table, tbl->max);
}
if (obj >= tbl->max) {
spin_unlock_irqrestore(&tbl->lock, flags);
return -ENOMEM;
}
set_bit(obj, tbl->table);
obj |= tbl->top;
spin_unlock_irqrestore(&tbl->lock, flags);
uar->index = obj;
uar->pfn = (pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>
PAGE_SHIFT) + uar->index;
return 0;
}
void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
{
struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
unsigned long flags;
u32 obj;
obj = uar->index & (tbl->max - 1);
spin_lock_irqsave(&tbl->lock, flags);
clear_bit(obj, tbl->table);
tbl->last = min(tbl->last, obj);
tbl->top = (tbl->top + tbl->max) & tbl->mask;
spin_unlock_irqrestore(&tbl->lock, flags);
}
This diff is collapsed.
/*
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of EITHER the GNU General Public License
* version 2 as published by the Free Software Foundation or the BSD
* 2-Clause License. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*
* You should have received a copy of the GNU General Public License
* along with this program available in the file COPYING in the main
* directory of this source tree.
*
* The BSD 2-Clause License
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/bitmap.h>
#include "pvrdma.h"
int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
u64 npages, bool alloc_pages)
{
u64 i;
if (npages > PVRDMA_PAGE_DIR_MAX_PAGES)
return -EINVAL;
memset(pdir, 0, sizeof(*pdir));
pdir->dir = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
&pdir->dir_dma, GFP_KERNEL);
if (!pdir->dir)
goto err;
pdir->ntables = PVRDMA_PAGE_DIR_TABLE(npages - 1) + 1;
pdir->tables = kcalloc(pdir->ntables, sizeof(*pdir->tables),
GFP_KERNEL);
if (!pdir->tables)
goto err;
for (i = 0; i < pdir->ntables; i++) {
pdir->tables[i] = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
(dma_addr_t *)&pdir->dir[i],
GFP_KERNEL);
if (!pdir->tables[i])
goto err;
}
pdir->npages = npages;
if (alloc_pages) {
pdir->pages = kcalloc(npages, sizeof(*pdir->pages),
GFP_KERNEL);
if (!pdir->pages)
goto err;
for (i = 0; i < pdir->npages; i++) {
dma_addr_t page_dma;
pdir->pages[i] = dma_alloc_coherent(&dev->pdev->dev,
PAGE_SIZE,
&page_dma,
GFP_KERNEL);
if (!pdir->pages[i])
goto err;
pvrdma_page_dir_insert_dma(pdir, i, page_dma);
}
}
return 0;
err:
pvrdma_page_dir_cleanup(dev, pdir);
return -ENOMEM;
}
static u64 *pvrdma_page_dir_table(struct pvrdma_page_dir *pdir, u64 idx)
{
return pdir->tables[PVRDMA_PAGE_DIR_TABLE(idx)];
}
dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx)
{
return pvrdma_page_dir_table(pdir, idx)[PVRDMA_PAGE_DIR_PAGE(idx)];
}
static void pvrdma_page_dir_cleanup_pages(struct pvrdma_dev *dev,
struct pvrdma_page_dir *pdir)
{
if (pdir->pages) {
u64 i;
for (i = 0; i < pdir->npages && pdir->pages[i]; i++) {
dma_addr_t page_dma = pvrdma_page_dir_get_dma(pdir, i);
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
pdir->pages[i], page_dma);
}
kfree(pdir->pages);
}
}
static void pvrdma_page_dir_cleanup_tables(struct pvrdma_dev *dev,
struct pvrdma_page_dir *pdir)
{
if (pdir->tables) {
int i;
pvrdma_page_dir_cleanup_pages(dev, pdir);
for (i = 0; i < pdir->ntables; i++) {
u64 *table = pdir->tables[i];
if (table)
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
table, pdir->dir[i]);
}
kfree(pdir->tables);
}
}
void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
struct pvrdma_page_dir *pdir)
{
if (pdir->dir) {
pvrdma_page_dir_cleanup_tables(dev, pdir);
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
pdir->dir, pdir->dir_dma);
}
}
int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
dma_addr_t daddr)
{
u64 *table;
if (idx >= pdir->npages)
return -EINVAL;
table = pvrdma_page_dir_table(pdir, idx);
table[PVRDMA_PAGE_DIR_PAGE(idx)] = daddr;
return 0;
}
int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
struct ib_umem *umem, u64 offset)
{
u64 i = offset;
int j, entry;
int ret = 0, len = 0;
struct scatterlist *sg;
if (offset >= pdir->npages)
return -EINVAL;
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
len = sg_dma_len(sg) >> PAGE_SHIFT;
for (j = 0; j < len; j++) {
dma_addr_t addr = sg_dma_address(sg) +
umem->page_size * j;
ret = pvrdma_page_dir_insert_dma(pdir, i, addr);
if (ret)
goto exit;
i++;
}
}
exit:
return ret;
}
int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
u64 *page_list,
int num_pages)
{
int i;
int ret;
if (num_pages > pdir->npages)
return -EINVAL;
for (i = 0; i < num_pages; i++) {
ret = pvrdma_page_dir_insert_dma(pdir, i, page_list[i]);
if (ret)
return ret;
}
return 0;
}
void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst, const struct pvrdma_qp_cap *src)
{
dst->max_send_wr = src->max_send_wr;
dst->max_recv_wr = src->max_recv_wr;
dst->max_send_sge = src->max_send_sge;
dst->max_recv_sge = src->max_recv_sge;
dst->max_inline_data = src->max_inline_data;
}
void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst, const struct ib_qp_cap *src)
{
dst->max_send_wr = src->max_send_wr;
dst->max_recv_wr = src->max_recv_wr;
dst->max_send_sge = src->max_send_sge;
dst->max_recv_sge = src->max_recv_sge;
dst->max_inline_data = src->max_inline_data;
}
void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src)
{
BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
memcpy(dst, src, sizeof(*src));
}
void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src)
{
BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
memcpy(dst, src, sizeof(*src));
}
void pvrdma_global_route_to_ib(struct ib_global_route *dst,
const struct pvrdma_global_route *src)
{
pvrdma_gid_to_ib(&dst->dgid, &src->dgid);
dst->flow_label = src->flow_label;
dst->sgid_index = src->sgid_index;
dst->hop_limit = src->hop_limit;
dst->traffic_class = src->traffic_class;
}
void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
const struct ib_global_route *src)
{
ib_gid_to_pvrdma(&dst->dgid, &src->dgid);
dst->flow_label = src->flow_label;
dst->sgid_index = src->sgid_index;
dst->hop_limit = src->hop_limit;
dst->traffic_class = src->traffic_class;
}
void pvrdma_ah_attr_to_ib(struct ib_ah_attr *dst,
const struct pvrdma_ah_attr *src)
{
pvrdma_global_route_to_ib(&dst->grh, &src->grh);
dst->dlid = src->dlid;
dst->sl = src->sl;
dst->src_path_bits = src->src_path_bits;
dst->static_rate = src->static_rate;
dst->ah_flags = src->ah_flags;
dst->port_num = src->port_num;
memcpy(&dst->dmac, &src->dmac, sizeof(dst->dmac));
}
void ib_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
const struct ib_ah_attr *src)
{
ib_global_route_to_pvrdma(&dst->grh, &src->grh);
dst->dlid = src->dlid;
dst->sl = src->sl;
dst->src_path_bits = src->src_path_bits;
dst->static_rate = src->static_rate;
dst->ah_flags = src->ah_flags;
dst->port_num = src->port_num;
memcpy(&dst->dmac, &src->dmac, sizeof(dst->dmac));
}
/*
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of EITHER the GNU General Public License
* version 2 as published by the Free Software Foundation or the BSD
* 2-Clause License. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*
* You should have received a copy of the GNU General Public License
* along with this program available in the file COPYING in the main
* directory of this source tree.
*
* The BSD 2-Clause License
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/list.h>
#include <linux/slab.h>
#include "pvrdma.h"
/**
* pvrdma_get_dma_mr - get a DMA memory region
* @pd: protection domain
* @acc: access flags
*
* @return: ib_mr pointer on success, otherwise returns an errno.
*/
struct ib_mr *pvrdma_get_dma_mr(struct ib_pd *pd, int acc)
{
struct pvrdma_dev *dev = to_vdev(pd->device);
struct pvrdma_user_mr *mr;
union pvrdma_cmd_req req;
union pvrdma_cmd_resp rsp;
struct pvrdma_cmd_create_mr *cmd = &req.create_mr;
struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp;
int ret;
/* Support only LOCAL_WRITE flag for DMA MRs */
if (acc & ~IB_ACCESS_LOCAL_WRITE) {
dev_warn(&dev->pdev->dev,
"unsupported dma mr access flags %#x\n", acc);
return ERR_PTR(-EOPNOTSUPP);
}
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
if (!mr)
return ERR_PTR(-ENOMEM);
memset(cmd, 0, sizeof(*cmd));
cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR;
cmd->pd_handle = to_vpd(pd)->pd_handle;
cmd->access_flags = acc;
cmd->flags = PVRDMA_MR_FLAG_DMA;
ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_MR_RESP);
if (ret < 0) {
dev_warn(&dev->pdev->dev,
"could not get DMA mem region, error: %d\n", ret);
kfree(mr);
return ERR_PTR(ret);
}
mr->mmr.mr_handle = resp->mr_handle;
mr->ibmr.lkey = resp->lkey;
mr->ibmr.rkey = resp->rkey;
return &mr->ibmr;
}
/**
* pvrdma_reg_user_mr - register a userspace memory region
* @pd: protection domain
* @start: starting address
* @length: length of region
* @virt_addr: I/O virtual address
* @access_flags: access flags for memory region
* @udata: user data
*
* @return: ib_mr pointer on success, otherwise returns an errno.
*/
struct ib_mr *pvrdma_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
u64 virt_addr, int access_flags,
struct ib_udata *udata)
{
struct pvrdma_dev *dev = to_vdev(pd->device);
struct pvrdma_user_mr *mr = NULL;
struct ib_umem *umem;
union pvrdma_cmd_req req;
union pvrdma_cmd_resp rsp;
struct pvrdma_cmd_create_mr *cmd = &req.create_mr;
struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp;
int nchunks;
int ret;
int entry;
struct scatterlist *sg;
if (length == 0 || length > dev->dsr->caps.max_mr_size) {
dev_warn(&dev->pdev->dev, "invalid mem region length\n");
return ERR_PTR(-EINVAL);
}
umem = ib_umem_get(pd->uobject->context, start,
length, access_flags, 0);
if (IS_ERR(umem)) {
dev_warn(&dev->pdev->dev,
"could not get umem for mem region\n");
return ERR_CAST(umem);
}
nchunks = 0;
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry)
nchunks += sg_dma_len(sg) >> PAGE_SHIFT;
if (nchunks < 0 || nchunks > PVRDMA_PAGE_DIR_MAX_PAGES) {
dev_warn(&dev->pdev->dev, "overflow %d pages in mem region\n",
nchunks);
ret = -EINVAL;
goto err_umem;
}
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
if (!mr) {
ret = -ENOMEM;
goto err_umem;
}
mr->mmr.iova = virt_addr;
mr->mmr.size = length;
mr->umem = umem;
ret = pvrdma_page_dir_init(dev, &mr->pdir, nchunks, false);
if (ret) {
dev_warn(&dev->pdev->dev,
"could not allocate page directory\n");
goto err_umem;
}
ret = pvrdma_page_dir_insert_umem(&mr->pdir, mr->umem, 0);
if (ret)
goto err_pdir;
memset(cmd, 0, sizeof(*cmd));
cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR;
cmd->start = start;
cmd->length = length;
cmd->pd_handle = to_vpd(pd)->pd_handle;
cmd->access_flags = access_flags;
cmd->nchunks = nchunks;
cmd->pdir_dma = mr->pdir.dir_dma;
ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_MR_RESP);
if (ret < 0) {
dev_warn(&dev->pdev->dev,
"could not register mem region, error: %d\n", ret);
goto err_pdir;
}
mr->mmr.mr_handle = resp->mr_handle;
mr->ibmr.lkey = resp->lkey;
mr->ibmr.rkey = resp->rkey;
return &mr->ibmr;
err_pdir:
pvrdma_page_dir_cleanup(dev, &mr->pdir);
err_umem:
ib_umem_release(umem);
kfree(mr);
return ERR_PTR(ret);
}
/**
* pvrdma_alloc_mr - allocate a memory region
* @pd: protection domain
* @mr_type: type of memory region
* @max_num_sg: maximum number of pages
*
* @return: ib_mr pointer on success, otherwise returns an errno.
*/
struct ib_mr *pvrdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
u32 max_num_sg)
{
struct pvrdma_dev *dev = to_vdev(pd->device);
struct pvrdma_user_mr *mr;
union pvrdma_cmd_req req;
union pvrdma_cmd_resp rsp;
struct pvrdma_cmd_create_mr *cmd = &req.create_mr;
struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp;
int size = max_num_sg * sizeof(u64);
int ret;
if (mr_type != IB_MR_TYPE_MEM_REG ||
max_num_sg > PVRDMA_MAX_FAST_REG_PAGES)
return ERR_PTR(-EINVAL);
mr = kzalloc(sizeof(*mr), GFP_KERNEL);
if (!mr)
return ERR_PTR(-ENOMEM);
mr->pages = kzalloc(size, GFP_KERNEL);
if (!mr->pages) {
ret = -ENOMEM;
goto freemr;
}
ret = pvrdma_page_dir_init(dev, &mr->pdir, max_num_sg, false);
if (ret) {
dev_warn(&dev->pdev->dev,
"failed to allocate page dir for mr\n");
ret = -ENOMEM;
goto freepages;
}
memset(cmd, 0, sizeof(*cmd));
cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR;
cmd->pd_handle = to_vpd(pd)->pd_handle;
cmd->access_flags = 0;
cmd->flags = PVRDMA_MR_FLAG_FRMR;
cmd->nchunks = max_num_sg;
ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_MR_RESP);
if (ret < 0) {
dev_warn(&dev->pdev->dev,
"could not create FR mem region, error: %d\n", ret);
goto freepdir;
}
mr->max_pages = max_num_sg;
mr->mmr.mr_handle = resp->mr_handle;
mr->ibmr.lkey = resp->lkey;
mr->ibmr.rkey = resp->rkey;
mr->page_shift = PAGE_SHIFT;
mr->umem = NULL;
return &mr->ibmr;
freepdir:
pvrdma_page_dir_cleanup(dev, &mr->pdir);
freepages:
kfree(mr->pages);
freemr:
kfree(mr);
return ERR_PTR(ret);
}
/**
* pvrdma_dereg_mr - deregister a memory region
* @ibmr: memory region
*
* @return: 0 on success.
*/
int pvrdma_dereg_mr(struct ib_mr *ibmr)
{
struct pvrdma_user_mr *mr = to_vmr(ibmr);
struct pvrdma_dev *dev = to_vdev(ibmr->device);
union pvrdma_cmd_req req;
struct pvrdma_cmd_destroy_mr *cmd = &req.destroy_mr;
int ret;
memset(cmd, 0, sizeof(*cmd));
cmd->hdr.cmd = PVRDMA_CMD_DESTROY_MR;
cmd->mr_handle = mr->mmr.mr_handle;
ret = pvrdma_cmd_post(dev, &req, NULL, 0);
if (ret < 0)
dev_warn(&dev->pdev->dev,
"could not deregister mem region, error: %d\n", ret);
pvrdma_page_dir_cleanup(dev, &mr->pdir);
if (mr->umem)
ib_umem_release(mr->umem);
kfree(mr->pages);
kfree(mr);
return 0;
}
static int pvrdma_set_page(struct ib_mr *ibmr, u64 addr)
{
struct pvrdma_user_mr *mr = to_vmr(ibmr);
if (mr->npages == mr->max_pages)
return -ENOMEM;
mr->pages[mr->npages++] = addr;
return 0;
}
int pvrdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
unsigned int *sg_offset)
{
struct pvrdma_user_mr *mr = to_vmr(ibmr);
struct pvrdma_dev *dev = to_vdev(ibmr->device);
int ret;
mr->npages = 0;
ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, pvrdma_set_page);
if (ret < 0)
dev_warn(&dev->pdev->dev, "could not map sg to pages\n");
return ret;
}
This diff is collapsed.
/*
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of EITHER the GNU General Public License
* version 2 as published by the Free Software Foundation or the BSD
* 2-Clause License. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*
* You should have received a copy of the GNU General Public License
* along with this program available in the file COPYING in the main
* directory of this source tree.
*
* The BSD 2-Clause License
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __PVRDMA_RING_H__
#define __PVRDMA_RING_H__
#include <linux/types.h>
#define PVRDMA_INVALID_IDX -1 /* Invalid index. */
struct pvrdma_ring {
atomic_t prod_tail; /* Producer tail. */
atomic_t cons_head; /* Consumer head. */
};
struct pvrdma_ring_state {
struct pvrdma_ring tx; /* Tx ring. */
struct pvrdma_ring rx; /* Rx ring. */
};
static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems)
{
/* Generates fewer instructions than a less-than. */
return (idx & ~((max_elems << 1) - 1)) == 0;
}
static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems)
{
const unsigned int idx = atomic_read(var);
if (pvrdma_idx_valid(idx, max_elems))
return idx & (max_elems - 1);
return PVRDMA_INVALID_IDX;
}
static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems)
{
__u32 idx = atomic_read(var) + 1; /* Increment. */
idx &= (max_elems << 1) - 1; /* Modulo size, flip gen. */
atomic_set(var, idx);
}
static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,
__u32 max_elems, __u32 *out_tail)
{
const __u32 tail = atomic_read(&r->prod_tail);
const __u32 head = atomic_read(&r->cons_head);
if (pvrdma_idx_valid(tail, max_elems) &&
pvrdma_idx_valid(head, max_elems)) {
*out_tail = tail & (max_elems - 1);
return tail != (head ^ max_elems);
}
return PVRDMA_INVALID_IDX;
}
static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r,
__u32 max_elems, __u32 *out_head)
{
const __u32 tail = atomic_read(&r->prod_tail);
const __u32 head = atomic_read(&r->cons_head);
if (pvrdma_idx_valid(tail, max_elems) &&
pvrdma_idx_valid(head, max_elems)) {
*out_head = head & (max_elems - 1);
return tail != head;
}
return PVRDMA_INVALID_IDX;
}
static inline bool pvrdma_idx_ring_is_valid_idx(const struct pvrdma_ring *r,
__u32 max_elems, __u32 *idx)
{
const __u32 tail = atomic_read(&r->prod_tail);
const __u32 head = atomic_read(&r->cons_head);
if (pvrdma_idx_valid(tail, max_elems) &&
pvrdma_idx_valid(head, max_elems) &&
pvrdma_idx_valid(*idx, max_elems)) {
if (tail > head && (*idx < tail && *idx >= head))
return true;
else if (head > tail && (*idx >= head || *idx < tail))
return true;
}
return false;
}
#endif /* __PVRDMA_RING_H__ */
This diff is collapsed.
This diff is collapsed.
......@@ -14,3 +14,4 @@ header-y += mlx5-abi.h
header-y += mthca-abi.h
header-y += nes-abi.h
header-y += ocrdma-abi.h
header-y += vmw_pvrdma-abi.h
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment