]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/test_env.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / spdk / test / lib / test_env.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38
39 #include "spdk/env.h"
40
41 void *
42 spdk_malloc(size_t size, size_t align, uint64_t *phys_addr)
43 {
44 void *buf = NULL;
45 if (posix_memalign(&buf, align, size)) {
46 return NULL;
47 }
48 if (phys_addr) {
49 *phys_addr = (uint64_t)buf;
50 }
51 return buf;
52 }
53
54 void *
55 spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr)
56 {
57 void *buf = spdk_malloc(size, align, phys_addr);
58
59 if (buf != NULL) {
60 memset(buf, 0, size);
61 }
62 return buf;
63 }
64
65 void *
66 spdk_realloc(void *buf, size_t size, size_t align, uint64_t *phys_addr)
67 {
68 return realloc(buf, size);
69 }
70
71 void spdk_free(void *buf)
72 {
73 free(buf);
74 }
75
76 bool ut_fail_vtophys = false;
77 uint64_t spdk_vtophys(void *buf)
78 {
79 if (ut_fail_vtophys) {
80 return (uint64_t) - 1;
81 } else {
82 return (uintptr_t)buf;
83 }
84 }
85
86 void *
87 spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags)
88 {
89 return malloc(len);
90 }
91
92 void *
93 spdk_memzone_lookup(const char *name)
94 {
95 return NULL;
96 }
97
98 void
99 spdk_memzone_dump(FILE *f)
100 {
101 return;
102 }
103
104 int
105 spdk_memzone_free(const char *name)
106 {
107 return 0;
108 }
109
110 struct spdk_mempool *
111 spdk_mempool_create(const char *name, size_t count,
112 size_t ele_size, size_t cache_size, int socket_id)
113 {
114 static int mp = 0;
115
116 return (struct spdk_mempool *)&mp;
117 }
118
119 void
120 spdk_mempool_free(struct spdk_mempool *mp)
121 {
122
123 }
124
125 void *
126 spdk_mempool_get(struct spdk_mempool *mp)
127 {
128 void *buf;
129
130 if (posix_memalign(&buf, 64, 0x1000)) {
131 buf = NULL;
132 }
133
134 return buf;
135 }
136
137 void
138 spdk_mempool_put(struct spdk_mempool *mp, void *ele)
139 {
140 free(ele);
141 }
142
143 bool
144 spdk_process_is_primary(void)
145 {
146 return true;
147 }
148
149 uint64_t ut_tsc = 0;
150 uint64_t spdk_get_ticks(void)
151 {
152 return ut_tsc;
153 }
154
155 uint64_t spdk_get_ticks_hz(void)
156 {
157 return 1000000;
158 }
159
160 int
161 spdk_pci_addr_parse(struct spdk_pci_addr *addr, const char *bdf)
162 {
163 unsigned domain, bus, dev, func;
164
165 if (addr == NULL || bdf == NULL) {
166 return -EINVAL;
167 }
168
169 if (sscanf(bdf, "%x:%x:%x.%x", &domain, &bus, &dev, &func) == 4) {
170 /* Matched a full address - all variables are initialized */
171 } else if (sscanf(bdf, "%x:%x:%x", &domain, &bus, &dev) == 3) {
172 func = 0;
173 } else if (sscanf(bdf, "%x:%x.%x", &bus, &dev, &func) == 3) {
174 domain = 0;
175 } else if (sscanf(bdf, "%x:%x", &bus, &dev) == 2) {
176 domain = 0;
177 func = 0;
178 } else {
179 return -EINVAL;
180 }
181
182 if (domain > 0xFFFF || bus > 0xFF || dev > 0x1F || func > 7) {
183 return -EINVAL;
184 }
185
186 addr->domain = domain;
187 addr->bus = bus;
188 addr->dev = dev;
189 addr->func = func;
190
191 return 0;
192 }
193
194 int
195 spdk_pci_addr_fmt(char *bdf, size_t sz, const struct spdk_pci_addr *addr)
196 {
197 int rc;
198
199 rc = snprintf(bdf, sz, "%04x:%02x:%02x.%x",
200 addr->domain, addr->bus,
201 addr->dev, addr->func);
202
203 if (rc > 0 && (size_t)rc < sz) {
204 return 0;
205 }
206
207 return -1;
208 }