2016-07-19 16:08:57 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* OWNER 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 16:45:50 +00:00
|
|
|
#include <assert.h>
|
2016-07-19 16:08:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2016-10-03 21:40:06 +00:00
|
|
|
#include <pthread.h>
|
2016-07-19 16:08:57 +00:00
|
|
|
|
2016-11-16 21:20:30 +00:00
|
|
|
#include "spdk_internal/copy_engine.h"
|
|
|
|
|
2016-08-10 17:21:45 +00:00
|
|
|
#include "spdk/env.h"
|
2016-07-19 16:08:57 +00:00
|
|
|
#include "spdk/conf.h"
|
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk/event.h"
|
2016-09-20 20:13:38 +00:00
|
|
|
#include "spdk/io_channel.h"
|
2016-07-19 16:08:57 +00:00
|
|
|
#include "spdk/ioat.h"
|
|
|
|
|
|
|
|
#define IOAT_MAX_CHANNELS 64
|
|
|
|
|
|
|
|
struct ioat_device {
|
|
|
|
struct spdk_ioat_chan *ioat;
|
2016-09-20 22:42:08 +00:00
|
|
|
bool is_allocated;
|
2016-07-19 16:08:57 +00:00
|
|
|
/** linked list pointer for device list */
|
|
|
|
TAILQ_ENTRY(ioat_device) tailq;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TAILQ_HEAD(, ioat_device) g_devices = TAILQ_HEAD_INITIALIZER(g_devices);
|
2016-09-20 22:42:08 +00:00
|
|
|
static pthread_mutex_t g_ioat_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2016-07-19 16:08:57 +00:00
|
|
|
|
2016-09-20 20:13:38 +00:00
|
|
|
struct ioat_io_channel {
|
|
|
|
struct spdk_ioat_chan *ioat_ch;
|
|
|
|
struct ioat_device *ioat_dev;
|
|
|
|
struct spdk_poller *poller;
|
|
|
|
};
|
|
|
|
|
2016-07-19 16:08:57 +00:00
|
|
|
static int
|
2016-10-31 23:07:48 +00:00
|
|
|
ioat_find_dev_by_whitelist_bdf(const struct spdk_pci_addr *pci_addr,
|
|
|
|
const struct spdk_pci_addr *whitelist,
|
2016-07-19 16:08:57 +00:00
|
|
|
int num_whitelist_devices)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_whitelist_devices; i++) {
|
2016-10-31 23:07:48 +00:00
|
|
|
if (spdk_pci_addr_compare(pci_addr, &whitelist[i]) == 0) {
|
2016-07-19 16:08:57 +00:00
|
|
|
return 1;
|
2016-10-31 23:07:48 +00:00
|
|
|
}
|
2016-07-19 16:08:57 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:42:08 +00:00
|
|
|
static struct ioat_device *
|
|
|
|
ioat_allocate_device(void)
|
|
|
|
{
|
|
|
|
struct ioat_device *dev;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_ioat_mutex);
|
|
|
|
TAILQ_FOREACH(dev, &g_devices, tailq) {
|
|
|
|
if (!dev->is_allocated) {
|
|
|
|
dev->is_allocated = true;
|
|
|
|
pthread_mutex_unlock(&g_ioat_mutex);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&g_ioat_mutex);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ioat_free_device(struct ioat_device *dev)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&g_ioat_mutex);
|
|
|
|
dev->is_allocated = false;
|
|
|
|
pthread_mutex_unlock(&g_ioat_mutex);
|
|
|
|
}
|
2016-07-19 16:08:57 +00:00
|
|
|
|
|
|
|
struct ioat_task {
|
2016-11-16 21:11:58 +00:00
|
|
|
spdk_copy_completion_cb cb;
|
2016-07-19 16:08:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int copy_engine_ioat_init(void);
|
|
|
|
static void copy_engine_ioat_exit(void);
|
|
|
|
|
2016-11-16 21:23:57 +00:00
|
|
|
static size_t
|
2016-07-19 16:08:57 +00:00
|
|
|
copy_engine_ioat_get_ctx_size(void)
|
|
|
|
{
|
2016-11-16 21:11:58 +00:00
|
|
|
return sizeof(struct ioat_task) + sizeof(struct spdk_copy_task);
|
2016-07-19 16:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_COPY_MODULE_REGISTER(copy_engine_ioat_init, copy_engine_ioat_exit, NULL,
|
|
|
|
copy_engine_ioat_get_ctx_size)
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_engine_ioat_exit(void)
|
|
|
|
{
|
|
|
|
struct ioat_device *dev;
|
|
|
|
|
|
|
|
while (!TAILQ_EMPTY(&g_devices)) {
|
|
|
|
dev = TAILQ_FIRST(&g_devices);
|
|
|
|
TAILQ_REMOVE(&g_devices, dev, tailq);
|
|
|
|
spdk_ioat_detach(dev->ioat);
|
2016-09-20 22:42:08 +00:00
|
|
|
ioat_free_device(dev);
|
2016-08-17 20:35:18 +00:00
|
|
|
spdk_free(dev);
|
2016-07-19 16:08:57 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ioat_done(void *cb_arg)
|
|
|
|
{
|
2016-11-16 21:11:58 +00:00
|
|
|
struct spdk_copy_task *copy_req;
|
2016-07-19 16:08:57 +00:00
|
|
|
struct ioat_task *ioat_task = cb_arg;
|
|
|
|
|
2016-11-16 21:11:58 +00:00
|
|
|
copy_req = (struct spdk_copy_task *)
|
2016-07-19 16:08:57 +00:00
|
|
|
((uintptr_t)ioat_task -
|
2016-11-16 21:11:58 +00:00
|
|
|
offsetof(struct spdk_copy_task, offload_ctx));
|
2016-07-19 16:08:57 +00:00
|
|
|
|
|
|
|
ioat_task->cb(copy_req, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
2016-09-20 23:18:44 +00:00
|
|
|
ioat_copy_submit(void *cb_arg, struct spdk_io_channel *ch, void *dst, void *src, uint64_t nbytes,
|
2016-11-16 21:11:58 +00:00
|
|
|
spdk_copy_completion_cb cb)
|
2016-07-19 16:08:57 +00:00
|
|
|
{
|
|
|
|
struct ioat_task *ioat_task = (struct ioat_task *)cb_arg;
|
2016-09-20 23:18:44 +00:00
|
|
|
struct ioat_io_channel *ioat_ch = spdk_io_channel_get_ctx(ch);
|
2016-07-19 16:08:57 +00:00
|
|
|
|
2016-08-18 16:45:50 +00:00
|
|
|
assert(ioat_ch->ioat_ch != NULL);
|
2016-07-19 16:08:57 +00:00
|
|
|
|
|
|
|
ioat_task->cb = cb;
|
|
|
|
|
2016-09-20 23:18:44 +00:00
|
|
|
return spdk_ioat_submit_copy(ioat_ch->ioat_ch, ioat_task, ioat_done, dst, src, nbytes);
|
2016-07-19 16:08:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 23:09:49 +00:00
|
|
|
static int64_t
|
2016-09-20 23:18:44 +00:00
|
|
|
ioat_copy_submit_fill(void *cb_arg, struct spdk_io_channel *ch, void *dst, uint8_t fill,
|
2016-11-16 21:11:58 +00:00
|
|
|
uint64_t nbytes, spdk_copy_completion_cb cb)
|
2016-08-26 23:09:49 +00:00
|
|
|
{
|
|
|
|
struct ioat_task *ioat_task = (struct ioat_task *)cb_arg;
|
2016-09-20 23:18:44 +00:00
|
|
|
struct ioat_io_channel *ioat_ch = spdk_io_channel_get_ctx(ch);
|
2016-08-26 23:09:49 +00:00
|
|
|
uint64_t fill64 = 0x0101010101010101ULL * fill;
|
|
|
|
|
2016-08-18 16:45:50 +00:00
|
|
|
assert(ioat_ch->ioat_ch != NULL);
|
2016-08-26 23:09:49 +00:00
|
|
|
|
|
|
|
ioat_task->cb = cb;
|
|
|
|
|
2016-09-20 23:18:44 +00:00
|
|
|
return spdk_ioat_submit_fill(ioat_ch->ioat_ch, ioat_task, ioat_done, dst, fill64, nbytes);
|
2016-08-26 23:09:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:42:08 +00:00
|
|
|
static void
|
|
|
|
ioat_poll(void *arg)
|
|
|
|
{
|
|
|
|
struct spdk_ioat_chan *chan = arg;
|
|
|
|
|
|
|
|
spdk_ioat_process_events(chan);
|
|
|
|
}
|
2016-08-26 23:09:49 +00:00
|
|
|
|
2016-09-20 20:13:38 +00:00
|
|
|
static struct spdk_io_channel *ioat_get_io_channel(uint32_t priority);
|
|
|
|
|
2016-07-19 16:08:57 +00:00
|
|
|
static struct spdk_copy_engine ioat_copy_engine = {
|
|
|
|
.copy = ioat_copy_submit,
|
2016-08-26 23:09:49 +00:00
|
|
|
.fill = ioat_copy_submit_fill,
|
2016-09-20 20:13:38 +00:00
|
|
|
.get_io_channel = ioat_get_io_channel,
|
2016-07-19 16:08:57 +00:00
|
|
|
};
|
|
|
|
|
2016-09-20 20:13:38 +00:00
|
|
|
static int
|
2016-09-27 20:22:15 +00:00
|
|
|
ioat_create_cb(void *io_device, uint32_t priority, void *ctx_buf, void *unique_ctx)
|
2016-09-20 20:13:38 +00:00
|
|
|
{
|
|
|
|
struct ioat_io_channel *ch = ctx_buf;
|
|
|
|
struct ioat_device *ioat_dev;
|
|
|
|
|
|
|
|
ioat_dev = ioat_allocate_device();
|
|
|
|
if (ioat_dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch->ioat_dev = ioat_dev;
|
|
|
|
ch->ioat_ch = ioat_dev->ioat;
|
|
|
|
spdk_poller_register(&ch->poller, ioat_poll, ch->ioat_ch,
|
2017-01-04 23:52:03 +00:00
|
|
|
spdk_app_get_current_core(), 0);
|
2016-09-20 20:13:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ioat_destroy_cb(void *io_device, void *ctx_buf)
|
|
|
|
{
|
|
|
|
struct ioat_io_channel *ch = ctx_buf;
|
|
|
|
|
|
|
|
ioat_free_device(ch->ioat_dev);
|
|
|
|
spdk_poller_unregister(&ch->poller, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_io_channel *
|
|
|
|
ioat_get_io_channel(uint32_t priority)
|
|
|
|
{
|
2016-09-27 20:22:15 +00:00
|
|
|
return spdk_get_io_channel(&ioat_copy_engine, priority, false, NULL);
|
2016-09-20 20:13:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:08:57 +00:00
|
|
|
struct ioat_probe_ctx {
|
|
|
|
int num_whitelist_devices;
|
2016-10-31 23:07:48 +00:00
|
|
|
struct spdk_pci_addr whitelist[IOAT_MAX_CHANNELS];
|
2016-07-19 16:08:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
probe_cb(void *cb_ctx, struct spdk_pci_device *pci_dev)
|
|
|
|
{
|
|
|
|
struct ioat_probe_ctx *ctx = cb_ctx;
|
2016-10-31 23:01:52 +00:00
|
|
|
struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr(pci_dev);
|
2016-07-19 16:08:57 +00:00
|
|
|
|
2017-02-10 23:55:14 +00:00
|
|
|
SPDK_NOTICELOG(" Found matching device at %04x:%02x:%02x.%x vendor:0x%04x device:0x%04x\n",
|
|
|
|
pci_addr.domain,
|
2016-10-31 23:01:52 +00:00
|
|
|
pci_addr.bus,
|
|
|
|
pci_addr.dev,
|
|
|
|
pci_addr.func,
|
2016-07-19 16:08:57 +00:00
|
|
|
spdk_pci_device_get_vendor_id(pci_dev),
|
2016-11-02 17:50:28 +00:00
|
|
|
spdk_pci_device_get_device_id(pci_dev));
|
2016-07-19 16:08:57 +00:00
|
|
|
|
|
|
|
if (ctx->num_whitelist_devices > 0 &&
|
2016-10-31 23:07:48 +00:00
|
|
|
!ioat_find_dev_by_whitelist_bdf(&pci_addr, ctx->whitelist, ctx->num_whitelist_devices)) {
|
2016-07-19 16:08:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Claim the device in case conflict with other process */
|
2016-10-31 23:01:52 +00:00
|
|
|
if (spdk_pci_device_claim(&pci_addr) != 0) {
|
2016-07-19 16:08:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
attach_cb(void *cb_ctx, struct spdk_pci_device *pci_dev, struct spdk_ioat_chan *ioat)
|
|
|
|
{
|
|
|
|
struct ioat_device *dev;
|
|
|
|
|
2016-08-17 20:35:18 +00:00
|
|
|
dev = spdk_zmalloc(sizeof(*dev), 0, NULL);
|
2016-07-19 16:08:57 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to allocate device struct\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->ioat = ioat;
|
|
|
|
TAILQ_INSERT_TAIL(&g_devices, dev, tailq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
copy_engine_ioat_init(void)
|
|
|
|
{
|
|
|
|
struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Ioat");
|
|
|
|
const char *val, *pci_bdf;
|
|
|
|
int i;
|
|
|
|
struct ioat_probe_ctx probe_ctx = {};
|
|
|
|
|
|
|
|
if (sp != NULL) {
|
|
|
|
val = spdk_conf_section_get_val(sp, "Disable");
|
|
|
|
if (val != NULL) {
|
|
|
|
/* Disable Ioat */
|
|
|
|
if (!strcmp(val, "Yes")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*Init the whitelist*/
|
|
|
|
for (i = 0; i < IOAT_MAX_CHANNELS; i++) {
|
|
|
|
pci_bdf = spdk_conf_section_get_nmval(sp, "Whitelist", i, 0);
|
|
|
|
if (!pci_bdf)
|
|
|
|
break;
|
2016-10-31 23:29:52 +00:00
|
|
|
|
|
|
|
if (spdk_pci_addr_parse(&probe_ctx.whitelist[probe_ctx.num_whitelist_devices], pci_bdf) < 0) {
|
|
|
|
SPDK_ERRLOG("Invalid Ioat Whitelist address %s\n", pci_bdf);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-19 16:08:57 +00:00
|
|
|
probe_ctx.num_whitelist_devices++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_ioat_probe(&probe_ctx, probe_cb, attach_cb) != 0) {
|
|
|
|
SPDK_ERRLOG("spdk_ioat_probe() failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_NOTICELOG("Ioat Copy Engine Offload Enabled\n");
|
|
|
|
spdk_copy_engine_register(&ioat_copy_engine);
|
2016-09-20 20:13:38 +00:00
|
|
|
spdk_io_device_register(&ioat_copy_engine, ioat_create_cb, ioat_destroy_cb,
|
|
|
|
sizeof(struct ioat_io_channel));
|
2016-07-19 16:08:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|