Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2020 Intel Corporation.
3 : : * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
4 : : * All rights reserved.
5 : : */
6 : :
7 : : #include "accel_ioat.h"
8 : :
9 : : #include "spdk/stdinc.h"
10 : :
11 : : #include "spdk/accel_module.h"
12 : : #include "spdk/log.h"
13 : : #include "spdk/likely.h"
14 : :
15 : : #include "spdk/env.h"
16 : : #include "spdk/event.h"
17 : : #include "spdk/thread.h"
18 : : #include "spdk/ioat.h"
19 : :
20 : : static bool g_ioat_enable = false;
21 : : static bool g_ioat_initialized = false;
22 : :
23 : : struct ioat_device {
24 : : struct spdk_ioat_chan *ioat;
25 : : bool is_allocated;
26 : : /** linked list pointer for device list */
27 : : TAILQ_ENTRY(ioat_device) tailq;
28 : : };
29 : :
30 : : struct pci_device {
31 : : struct spdk_pci_device *pci_dev;
32 : : TAILQ_ENTRY(pci_device) tailq;
33 : : };
34 : :
35 : : static TAILQ_HEAD(, ioat_device) g_devices = TAILQ_HEAD_INITIALIZER(g_devices);
36 : : static pthread_mutex_t g_ioat_mutex = PTHREAD_MUTEX_INITIALIZER;
37 : :
38 : : static TAILQ_HEAD(, pci_device) g_pci_devices = TAILQ_HEAD_INITIALIZER(g_pci_devices);
39 : :
40 : : struct ioat_io_channel {
41 : : struct spdk_ioat_chan *ioat_ch;
42 : : struct ioat_device *ioat_dev;
43 : : struct spdk_poller *poller;
44 : : };
45 : :
46 : : static struct ioat_device *
47 : 0 : ioat_allocate_device(void)
48 : : {
49 : : struct ioat_device *dev;
50 : :
51 [ # # ]: 0 : pthread_mutex_lock(&g_ioat_mutex);
52 [ # # ]: 0 : TAILQ_FOREACH(dev, &g_devices, tailq) {
53 [ # # # # ]: 0 : if (!dev->is_allocated) {
54 : 0 : dev->is_allocated = true;
55 [ # # ]: 0 : pthread_mutex_unlock(&g_ioat_mutex);
56 : 0 : return dev;
57 : : }
58 : : }
59 [ # # ]: 0 : pthread_mutex_unlock(&g_ioat_mutex);
60 : :
61 : 0 : return NULL;
62 : : }
63 : :
64 : : static void
65 : 0 : ioat_free_device(struct ioat_device *dev)
66 : : {
67 [ # # ]: 0 : pthread_mutex_lock(&g_ioat_mutex);
68 : 0 : dev->is_allocated = false;
69 [ # # ]: 0 : pthread_mutex_unlock(&g_ioat_mutex);
70 : 0 : }
71 : :
72 : : static int accel_ioat_init(void);
73 : : static void accel_ioat_exit(void *ctx);
74 : : static bool ioat_supports_opcode(enum spdk_accel_opcode opc);
75 : : static struct spdk_io_channel *ioat_get_io_channel(void);
76 : : static int ioat_submit_tasks(struct spdk_io_channel *ch, struct spdk_accel_task *accel_task);
77 : :
78 : : static size_t
79 : 0 : accel_ioat_get_ctx_size(void)
80 : : {
81 : 0 : return sizeof(struct spdk_accel_task);
82 : : }
83 : :
84 : : static struct spdk_accel_module_if g_ioat_module = {
85 : : .module_init = accel_ioat_init,
86 : : .module_fini = accel_ioat_exit,
87 : : .write_config_json = NULL,
88 : : .get_ctx_size = accel_ioat_get_ctx_size,
89 : : .name = "ioat",
90 : : .supports_opcode = ioat_supports_opcode,
91 : : .get_io_channel = ioat_get_io_channel,
92 : : .submit_tasks = ioat_submit_tasks
93 : : };
94 : :
95 : : static void
96 : 0 : ioat_done(void *cb_arg)
97 : : {
98 : 0 : struct spdk_accel_task *accel_task = cb_arg;
99 : :
100 : 0 : spdk_accel_task_complete(accel_task, 0);
101 : 0 : }
102 : :
103 : : static int
104 : 0 : ioat_poll(void *arg)
105 : : {
106 : 0 : struct spdk_ioat_chan *chan = arg;
107 : :
108 : 0 : return spdk_ioat_process_events(chan) != 0 ? SPDK_POLLER_BUSY :
109 : : SPDK_POLLER_IDLE;
110 : : }
111 : :
112 : : static struct spdk_io_channel *ioat_get_io_channel(void);
113 : :
114 : : static bool
115 : 0 : ioat_supports_opcode(enum spdk_accel_opcode opc)
116 : : {
117 [ # # # # ]: 0 : if (!g_ioat_initialized) {
118 : 0 : assert(0);
119 : : return false;
120 : : }
121 : :
122 [ # # ]: 0 : switch (opc) {
123 : 0 : case SPDK_ACCEL_OPC_COPY:
124 : : case SPDK_ACCEL_OPC_FILL:
125 : 0 : return true;
126 : 0 : default:
127 : 0 : return false;
128 : : }
129 : : }
130 : :
131 : : static int
132 : 0 : ioat_submit_fill(struct ioat_io_channel *ioat_ch, struct spdk_accel_task *task)
133 : : {
134 [ # # ]: 0 : if (spdk_unlikely(task->d.iovcnt != 1)) {
135 : 0 : return -EINVAL;
136 : : }
137 : :
138 : 0 : return spdk_ioat_build_fill(ioat_ch->ioat_ch, task, ioat_done,
139 : 0 : task->d.iovs[0].iov_base, task->fill_pattern,
140 : 0 : task->d.iovs[0].iov_len);
141 : : }
142 : :
143 : : static int
144 : 0 : ioat_submit_copy(struct ioat_io_channel *ioat_ch, struct spdk_accel_task *task)
145 : : {
146 [ # # # # ]: 0 : if (spdk_unlikely(task->d.iovcnt != 1 || task->s.iovcnt != 1)) {
147 : 0 : return -EINVAL;
148 : : }
149 : :
150 [ # # ]: 0 : if (spdk_unlikely(task->d.iovs[0].iov_len != task->s.iovs[0].iov_len)) {
151 : 0 : return -EINVAL;
152 : : }
153 : :
154 : 0 : return spdk_ioat_build_copy(ioat_ch->ioat_ch, task, ioat_done,
155 : 0 : task->d.iovs[0].iov_base, task->s.iovs[0].iov_base,
156 : 0 : task->d.iovs[0].iov_len);
157 : : }
158 : :
159 : : static int
160 : 0 : ioat_submit_tasks(struct spdk_io_channel *ch, struct spdk_accel_task *accel_task)
161 : : {
162 : 0 : struct ioat_io_channel *ioat_ch = spdk_io_channel_get_ctx(ch);
163 : : struct spdk_accel_task *tmp;
164 : 0 : int rc = 0;
165 : :
166 : : do {
167 [ # # # ]: 0 : switch (accel_task->op_code) {
168 : 0 : case SPDK_ACCEL_OPC_FILL:
169 : 0 : rc = ioat_submit_fill(ioat_ch, accel_task);
170 : 0 : break;
171 : 0 : case SPDK_ACCEL_OPC_COPY:
172 : 0 : rc = ioat_submit_copy(ioat_ch, accel_task);
173 : 0 : break;
174 : 0 : default:
175 : 0 : assert(false);
176 : : break;
177 : : }
178 : :
179 : 0 : tmp = STAILQ_NEXT(accel_task, link);
180 : :
181 : : /* Report any build errors via the callback now. */
182 [ # # ]: 0 : if (rc) {
183 : 0 : spdk_accel_task_complete(accel_task, rc);
184 : : }
185 : :
186 : 0 : accel_task = tmp;
187 [ # # ]: 0 : } while (accel_task);
188 : :
189 : 0 : spdk_ioat_flush(ioat_ch->ioat_ch);
190 : :
191 : 0 : return 0;
192 : : }
193 : :
194 : : static int
195 : 0 : ioat_create_cb(void *io_device, void *ctx_buf)
196 : : {
197 : 0 : struct ioat_io_channel *ch = ctx_buf;
198 : : struct ioat_device *ioat_dev;
199 : :
200 : 0 : ioat_dev = ioat_allocate_device();
201 [ # # ]: 0 : if (ioat_dev == NULL) {
202 : 0 : return -1;
203 : : }
204 : :
205 : 0 : ch->ioat_dev = ioat_dev;
206 : 0 : ch->ioat_ch = ioat_dev->ioat;
207 : 0 : ch->poller = SPDK_POLLER_REGISTER(ioat_poll, ch->ioat_ch, 0);
208 : :
209 : 0 : return 0;
210 : : }
211 : :
212 : : static void
213 : 0 : ioat_destroy_cb(void *io_device, void *ctx_buf)
214 : : {
215 : 0 : struct ioat_io_channel *ch = ctx_buf;
216 : :
217 : 0 : ioat_free_device(ch->ioat_dev);
218 : 0 : spdk_poller_unregister(&ch->poller);
219 : 0 : }
220 : :
221 : : static struct spdk_io_channel *
222 : 0 : ioat_get_io_channel(void)
223 : : {
224 : 0 : return spdk_get_io_channel(&g_ioat_module);
225 : : }
226 : :
227 : : static bool
228 : 0 : probe_cb(void *cb_ctx, struct spdk_pci_device *pci_dev)
229 : : {
230 : 0 : struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr(pci_dev);
231 : : struct pci_device *pdev;
232 : :
233 [ # # # # ]: 0 : SPDK_INFOLOG(accel_ioat,
234 : : " Found matching device at %04x:%02x:%02x.%x vendor:0x%04x device:0x%04x\n",
235 : : pci_addr.domain,
236 : : pci_addr.bus,
237 : : pci_addr.dev,
238 : : pci_addr.func,
239 : : spdk_pci_device_get_vendor_id(pci_dev),
240 : : spdk_pci_device_get_device_id(pci_dev));
241 : :
242 : 0 : pdev = calloc(1, sizeof(*pdev));
243 [ # # ]: 0 : if (pdev == NULL) {
244 : 0 : return false;
245 : : }
246 : 0 : pdev->pci_dev = pci_dev;
247 : 0 : TAILQ_INSERT_TAIL(&g_pci_devices, pdev, tailq);
248 : :
249 : : /* Claim the device in case conflict with other process */
250 [ # # ]: 0 : if (spdk_pci_device_claim(pci_dev) < 0) {
251 : 0 : return false;
252 : : }
253 : :
254 : 0 : return true;
255 : : }
256 : :
257 : : static void
258 : 0 : attach_cb(void *cb_ctx, struct spdk_pci_device *pci_dev, struct spdk_ioat_chan *ioat)
259 : : {
260 : : struct ioat_device *dev;
261 : :
262 : 0 : dev = calloc(1, sizeof(*dev));
263 [ # # ]: 0 : if (dev == NULL) {
264 : 0 : SPDK_ERRLOG("Failed to allocate device struct\n");
265 : 0 : return;
266 : : }
267 : :
268 : 0 : dev->ioat = ioat;
269 : 0 : TAILQ_INSERT_TAIL(&g_devices, dev, tailq);
270 : : }
271 : :
272 : : void
273 : 0 : accel_ioat_enable_probe(void)
274 : : {
275 : 0 : g_ioat_enable = true;
276 : 0 : spdk_accel_module_list_add(&g_ioat_module);
277 : 0 : }
278 : :
279 : : static int
280 : 0 : accel_ioat_init(void)
281 : : {
282 [ # # # # ]: 0 : if (!g_ioat_enable) {
283 : 0 : assert(0);
284 : : return 0;
285 : : }
286 : :
287 [ # # ]: 0 : if (spdk_ioat_probe(NULL, probe_cb, attach_cb) != 0) {
288 : 0 : SPDK_ERRLOG("spdk_ioat_probe() failed\n");
289 : 0 : return -1;
290 : : }
291 : :
292 [ # # ]: 0 : if (TAILQ_EMPTY(&g_devices)) {
293 : 0 : SPDK_NOTICELOG("No available ioat devices\n");
294 : 0 : return -1;
295 : : }
296 : :
297 : 0 : g_ioat_initialized = true;
298 : 0 : spdk_io_device_register(&g_ioat_module, ioat_create_cb, ioat_destroy_cb,
299 : : sizeof(struct ioat_io_channel), "ioat_accel_module");
300 : 0 : return 0;
301 : : }
302 : :
303 : : static void
304 : 0 : _device_unregister_cb(void *io_device)
305 : : {
306 : 0 : struct ioat_device *dev = io_device;
307 : : struct pci_device *pci_dev;
308 : :
309 [ # # ]: 0 : while (!TAILQ_EMPTY(&g_devices)) {
310 : 0 : dev = TAILQ_FIRST(&g_devices);
311 [ # # ]: 0 : TAILQ_REMOVE(&g_devices, dev, tailq);
312 : 0 : spdk_ioat_detach(dev->ioat);
313 : 0 : free(dev);
314 : : }
315 : :
316 [ # # ]: 0 : while (!TAILQ_EMPTY(&g_pci_devices)) {
317 : 0 : pci_dev = TAILQ_FIRST(&g_pci_devices);
318 [ # # ]: 0 : TAILQ_REMOVE(&g_pci_devices, pci_dev, tailq);
319 : 0 : spdk_pci_device_detach(pci_dev->pci_dev);
320 : 0 : free(pci_dev);
321 : : }
322 : :
323 : 0 : g_ioat_initialized = false;
324 : :
325 : 0 : spdk_accel_module_finish();
326 : 0 : }
327 : :
328 : : static void
329 : 0 : accel_ioat_exit(void *ctx)
330 : : {
331 [ # # # # ]: 0 : if (g_ioat_initialized) {
332 : 0 : spdk_io_device_unregister(&g_ioat_module, _device_unregister_cb);
333 : : } else {
334 : 0 : spdk_accel_module_finish();
335 : : }
336 : 0 : }
337 : :
338 : 2817 : SPDK_LOG_REGISTER_COMPONENT(accel_ioat)
|