2015-09-21 08:52:41 -07:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
2016-01-26 10:47:22 -07:00
|
|
|
* Copyright (c) Intel Corporation.
|
2015-09-21 08:52:41 -07:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 11:18:25 -07:00
|
|
|
#include "spdk/stdinc.h"
|
2016-03-01 16:51:02 -07:00
|
|
|
|
2017-08-03 08:07:51 -07:00
|
|
|
#include "spdk_internal/mock.h"
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
#include "spdk/env.h"
|
2016-02-03 14:36:26 -07:00
|
|
|
|
2017-08-03 08:07:51 -07:00
|
|
|
/*
|
|
|
|
* NOTE:
|
|
|
|
* Functions in this file are mocks for SPDK based functions
|
|
|
|
* and work conceptually in the same way that mocks work in
|
|
|
|
* /lib/ut_mock. However, the globals that control the behavior
|
|
|
|
* of the mock are defined here, with each function, as
|
|
|
|
* opposed to being defined as part of the macro that defines
|
|
|
|
* the stub or wrapper for other types of functions. Be sure
|
|
|
|
* to use the correct global variable naming convention when
|
|
|
|
* working with these functions. See /lib/ut_mock for details.
|
|
|
|
*/
|
|
|
|
|
2017-08-03 10:21:48 -07:00
|
|
|
/*
|
|
|
|
* these stubs have a return value set with one of the MOCK_SET macros
|
|
|
|
*/
|
|
|
|
DEFINE_STUB(spdk_process_is_primary, bool, (void), true)
|
|
|
|
|
|
|
|
DEFINE_STUB_VP(spdk_memzone_lookup, (const char *name), NULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* these mocks don't fit well with the library macro model because
|
|
|
|
* they do 'something' other than just return a pre-set value
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* setup the mock control to pass thru by default */
|
|
|
|
void *ut_p_spdk_memzone_reserve = MOCK_PASS_THRU_P;
|
|
|
|
void *
|
|
|
|
spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags)
|
|
|
|
{
|
|
|
|
if (ut_p_spdk_memzone_reserve &&
|
|
|
|
ut_p_spdk_memzone_reserve == MOCK_PASS_THRU_P) {
|
|
|
|
return malloc(len);
|
|
|
|
} else {
|
|
|
|
return ut_p_spdk_memzone_reserve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
void *
|
2017-05-25 14:21:30 -04:00
|
|
|
spdk_dma_malloc(size_t size, size_t align, uint64_t *phys_addr)
|
2015-09-24 10:04:33 -07:00
|
|
|
{
|
2016-06-27 09:35:05 -07:00
|
|
|
void *buf = NULL;
|
|
|
|
if (posix_memalign(&buf, align, size)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-13 22:58:19 -04:00
|
|
|
if (phys_addr) {
|
|
|
|
*phys_addr = (uint64_t)buf;
|
|
|
|
}
|
2015-09-24 10:04:33 -07:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-08-03 08:07:51 -07:00
|
|
|
int ut_spdk_dma_zmalloc = (int)MOCK_PASS_THRU;
|
|
|
|
void *ut_p_spdk_dma_zmalloc = &ut_spdk_dma_zmalloc;
|
2017-02-08 11:43:02 -07:00
|
|
|
void *
|
2017-05-25 14:21:30 -04:00
|
|
|
spdk_dma_zmalloc(size_t size, size_t align, uint64_t *phys_addr)
|
2017-02-08 11:43:02 -07:00
|
|
|
{
|
2017-08-03 08:07:51 -07:00
|
|
|
if (ut_p_spdk_dma_zmalloc &&
|
|
|
|
ut_spdk_dma_zmalloc == (int)MOCK_PASS_THRU) {
|
|
|
|
void *buf = spdk_dma_malloc(size, align, phys_addr);
|
2017-02-08 11:43:02 -07:00
|
|
|
|
2017-08-03 08:07:51 -07:00
|
|
|
if (buf != NULL) {
|
|
|
|
memset(buf, 0, size);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
} else {
|
|
|
|
return ut_p_spdk_dma_zmalloc;
|
2017-02-08 11:43:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 10:31:53 -07:00
|
|
|
void *
|
2017-05-25 14:21:30 -04:00
|
|
|
spdk_dma_malloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id)
|
2017-05-04 10:31:53 -07:00
|
|
|
{
|
2017-05-25 14:21:30 -04:00
|
|
|
return spdk_dma_malloc(size, align, phys_addr);
|
2017-05-04 10:31:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2017-05-25 14:21:30 -04:00
|
|
|
spdk_dma_zmalloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id)
|
2017-05-04 10:31:53 -07:00
|
|
|
{
|
2017-05-25 14:21:30 -04:00
|
|
|
return spdk_dma_zmalloc(size, align, phys_addr);
|
2017-05-04 10:31:53 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 03:19:31 -05:00
|
|
|
void *
|
2017-05-25 14:21:30 -04:00
|
|
|
spdk_dma_realloc(void *buf, size_t size, size_t align, uint64_t *phys_addr)
|
2016-11-07 03:19:31 -05:00
|
|
|
{
|
|
|
|
return realloc(buf, size);
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:21:30 -04:00
|
|
|
void spdk_dma_free(void *buf)
|
2016-08-12 10:24:34 -07:00
|
|
|
{
|
2017-08-03 08:07:51 -07:00
|
|
|
if (ut_p_spdk_dma_zmalloc &&
|
|
|
|
ut_spdk_dma_zmalloc == (int)MOCK_PASS_THRU) {
|
|
|
|
free(buf);
|
|
|
|
}
|
2016-08-12 10:24:34 -07:00
|
|
|
}
|
2015-11-02 12:58:19 -07:00
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
bool ut_fail_vtophys = false;
|
|
|
|
uint64_t spdk_vtophys(void *buf)
|
|
|
|
{
|
|
|
|
if (ut_fail_vtophys) {
|
|
|
|
return (uint64_t) - 1;
|
|
|
|
} else {
|
|
|
|
return (uintptr_t)buf;
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 16:01:36 -07:00
|
|
|
|
2016-10-03 14:26:07 -07:00
|
|
|
void
|
|
|
|
spdk_memzone_dump(FILE *f)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
int
|
|
|
|
spdk_memzone_free(const char *name)
|
2016-08-04 22:59:38 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
struct spdk_mempool *
|
|
|
|
spdk_mempool_create(const char *name, size_t count,
|
2017-01-25 13:35:40 -07:00
|
|
|
size_t ele_size, size_t cache_size, int socket_id)
|
2016-08-04 22:59:38 -04:00
|
|
|
{
|
2016-08-12 10:24:34 -07:00
|
|
|
static int mp = 0;
|
2016-08-04 22:59:38 -04:00
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
return (struct spdk_mempool *)∓
|
|
|
|
}
|
2016-08-09 04:17:09 -04:00
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
void
|
|
|
|
spdk_mempool_free(struct spdk_mempool *mp)
|
2016-08-09 04:17:09 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
void *
|
|
|
|
spdk_mempool_get(struct spdk_mempool *mp)
|
2016-08-09 04:17:09 -04:00
|
|
|
{
|
2016-09-28 07:58:51 -07:00
|
|
|
void *buf;
|
|
|
|
|
|
|
|
if (posix_memalign(&buf, 64, 0x1000)) {
|
|
|
|
buf = NULL;
|
2016-09-27 15:34:34 -07:00
|
|
|
}
|
2016-09-28 07:58:51 -07:00
|
|
|
|
|
|
|
return buf;
|
2016-08-09 04:17:09 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
void
|
|
|
|
spdk_mempool_put(struct spdk_mempool *mp, void *ele)
|
2016-08-09 04:17:09 -04:00
|
|
|
{
|
2016-08-12 10:24:34 -07:00
|
|
|
free(ele);
|
|
|
|
}
|
|
|
|
|
2017-06-29 15:33:56 -07:00
|
|
|
size_t
|
|
|
|
spdk_mempool_count(const struct spdk_mempool *mp)
|
|
|
|
{
|
|
|
|
return 1024;
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
uint64_t ut_tsc = 0;
|
|
|
|
uint64_t spdk_get_ticks(void)
|
|
|
|
{
|
|
|
|
return ut_tsc;
|
2016-08-09 04:17:09 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:24:34 -07:00
|
|
|
uint64_t spdk_get_ticks_hz(void)
|
|
|
|
{
|
|
|
|
return 1000000;
|
|
|
|
}
|
2016-12-09 14:23:55 -07:00
|
|
|
|
2017-04-14 16:38:03 -07:00
|
|
|
void spdk_delay_us(unsigned int us)
|
|
|
|
{
|
|
|
|
ut_tsc += us;
|
|
|
|
}
|
|
|
|
|
2016-12-09 14:23:55 -07:00
|
|
|
int
|
|
|
|
spdk_pci_addr_parse(struct spdk_pci_addr *addr, const char *bdf)
|
|
|
|
{
|
|
|
|
unsigned domain, bus, dev, func;
|
|
|
|
|
|
|
|
if (addr == NULL || bdf == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:01:55 +08:00
|
|
|
if ((sscanf(bdf, "%x:%x:%x.%x", &domain, &bus, &dev, &func) == 4) ||
|
|
|
|
(sscanf(bdf, "%x.%x.%x.%x", &domain, &bus, &dev, &func) == 4)) {
|
2016-12-09 14:23:55 -07:00
|
|
|
/* Matched a full address - all variables are initialized */
|
|
|
|
} else if (sscanf(bdf, "%x:%x:%x", &domain, &bus, &dev) == 3) {
|
|
|
|
func = 0;
|
2017-04-17 10:01:55 +08:00
|
|
|
} else if ((sscanf(bdf, "%x:%x.%x", &bus, &dev, &func) == 3) ||
|
|
|
|
(sscanf(bdf, "%x.%x.%x", &bus, &dev, &func) == 3)) {
|
2016-12-09 14:23:55 -07:00
|
|
|
domain = 0;
|
2017-04-17 10:01:55 +08:00
|
|
|
} else if ((sscanf(bdf, "%x:%x", &bus, &dev) == 2) ||
|
|
|
|
(sscanf(bdf, "%x.%x", &bus, &dev) == 2)) {
|
2016-12-09 14:23:55 -07:00
|
|
|
domain = 0;
|
|
|
|
func = 0;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:05:47 -07:00
|
|
|
if (bus > 0xFF || dev > 0x1F || func > 7) {
|
2016-12-09 14:23:55 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr->domain = domain;
|
|
|
|
addr->bus = bus;
|
|
|
|
addr->dev = dev;
|
|
|
|
addr->func = func;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_pci_addr_fmt(char *bdf, size_t sz, const struct spdk_pci_addr *addr)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = snprintf(bdf, sz, "%04x:%02x:%02x.%x",
|
|
|
|
addr->domain, addr->bus,
|
|
|
|
addr->dev, addr->func);
|
|
|
|
|
|
|
|
if (rc > 0 && (size_t)rc < sz) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-29 15:33:56 -07:00
|
|
|
|
|
|
|
uint32_t
|
|
|
|
spdk_env_get_core_count(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|