1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
5 #include "opae_hw_api.h"
6 #include "opae_debug.h"
9 /* OPAE Bridge Functions */
12 * opae_bridge_alloc - alloc opae_bridge data structure
14 * @ops: ops of this bridge.
15 * @data: private data of this bridge.
17 * Return opae_bridge on success, otherwise NULL.
20 opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
22 struct opae_bridge *br = opae_zmalloc(sizeof(*br));
31 opae_log("%s %p\n", __func__, br);
37 * opae_bridge_reset - reset opae_bridge
38 * @br: bridge to be reset.
40 * Return: 0 on success, otherwise error code.
42 int opae_bridge_reset(struct opae_bridge *br)
47 if (br->ops && br->ops->reset)
48 return br->ops->reset(br);
50 opae_log("%s no ops\n", __func__);
55 /* Accelerator Functions */
58 * opae_accelerator_alloc - alloc opae_accelerator data structure
59 * @name: accelerator name.
60 * @ops: ops of this accelerator.
61 * @data: private data of this accelerator.
63 * Return: opae_accelerator on success, otherwise NULL.
65 struct opae_accelerator *
66 opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
69 struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));
78 opae_log("%s %p\n", __func__, acc);
84 * opae_acc_reg_read - read accelerator's register from its reg region.
85 * @acc: accelerator to read.
86 * @region_idx: reg region index.
87 * @offset: reg offset.
88 * @byte: read operation width, e.g 4 byte = 32bit read.
89 * @data: data to store the value read from the register.
91 * Return: 0 on success, otherwise error code.
93 int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
94 u64 offset, unsigned int byte, void *data)
99 if (acc->ops && acc->ops->read)
100 return acc->ops->read(acc, region_idx, offset, byte, data);
106 * opae_acc_reg_write - write to accelerator's register from its reg region.
107 * @acc: accelerator to write.
108 * @region_idx: reg region index.
109 * @offset: reg offset.
110 * @byte: write operation width, e.g 4 byte = 32bit write.
111 * @data: data stored the value to write to the register.
113 * Return: 0 on success, otherwise error code.
115 int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
116 u64 offset, unsigned int byte, void *data)
121 if (acc->ops && acc->ops->write)
122 return acc->ops->write(acc, region_idx, offset, byte, data);
128 * opae_acc_get_info - get information of an accelerator.
129 * @acc: targeted accelerator
130 * @info: accelerator info data structure to be filled.
132 * Return: 0 on success, otherwise error code.
134 int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
139 if (acc->ops && acc->ops->get_info)
140 return acc->ops->get_info(acc, info);
146 * opae_acc_get_region_info - get information of an accelerator register region.
147 * @acc: targeted accelerator
148 * @info: accelerator region info data structure to be filled.
150 * Return: 0 on success, otherwise error code.
152 int opae_acc_get_region_info(struct opae_accelerator *acc,
153 struct opae_acc_region_info *info)
158 if (acc->ops && acc->ops->get_region_info)
159 return acc->ops->get_region_info(acc, info);
165 * opae_acc_set_irq - set an accelerator's irq.
166 * @acc: targeted accelerator
167 * @start: start vector number
168 * @count: count of vectors to be set from the start vector
169 * @evtfds: event fds to be notified when corresponding irqs happens
171 * Return: 0 on success, otherwise error code.
173 int opae_acc_set_irq(struct opae_accelerator *acc,
174 u32 start, u32 count, s32 evtfds[])
176 if (!acc || !acc->data)
179 if (start + count <= start)
182 if (acc->ops && acc->ops->set_irq)
183 return acc->ops->set_irq(acc, start, count, evtfds);
189 * opae_acc_get_uuid - get accelerator's UUID.
190 * @acc: targeted accelerator
191 * @uuid: a pointer to UUID
193 * Return: 0 on success, otherwise error code.
195 int opae_acc_get_uuid(struct opae_accelerator *acc,
201 if (acc->ops && acc->ops->get_uuid)
202 return acc->ops->get_uuid(acc, uuid);
207 /* Manager Functions */
210 * opae_manager_alloc - alloc opae_manager data structure
211 * @name: manager name.
212 * @ops: ops of this manager.
213 * @data: private data of this manager.
215 * Return: opae_manager on success, otherwise NULL.
217 struct opae_manager *
218 opae_manager_alloc(const char *name, struct opae_manager_ops *ops, void *data)
220 struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
229 opae_log("%s %p\n", __func__, mgr);
235 * opae_manager_flash - flash a reconfiguration image via opae_manager
236 * @mgr: opae_manager for flash.
237 * @id: id of target region (accelerator).
238 * @buf: image data buffer.
239 * @size: buffer size.
240 * @status: status to store flash result.
242 * Return: 0 on success, otherwise error code.
244 int opae_manager_flash(struct opae_manager *mgr, int id, void *buf, u32 size,
250 if (mgr && mgr->ops && mgr->ops->flash)
251 return mgr->ops->flash(mgr, id, buf, size, status);
256 /* Adapter Functions */
259 * opae_adapter_data_alloc - alloc opae_adapter_data data structure
260 * @type: opae_adapter_type.
262 * Return: opae_adapter_data on success, otherwise NULL.
264 void *opae_adapter_data_alloc(enum opae_adapter_type type)
266 struct opae_adapter_data *data;
271 size = sizeof(struct opae_adapter_data_pci);
274 size = sizeof(struct opae_adapter_data_net);
277 size = sizeof(struct opae_adapter_data);
281 data = opae_zmalloc(size);
290 static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
292 struct opae_adapter_data *data;
294 if (!adapter || !adapter->data)
297 data = adapter->data;
299 if (data->type == OPAE_FPGA_PCI)
300 return &ifpga_adapter_ops;
306 * opae_adapter_init - init opae_adapter data structure
307 * @adapter: pointer of opae_adapter data structure
308 * @name: adapter name.
309 * @data: private data of this adapter.
311 * Return: 0 on success.
313 int opae_adapter_init(struct opae_adapter *adapter,
314 const char *name, void *data)
319 TAILQ_INIT(&adapter->acc_list);
320 adapter->data = data;
321 adapter->name = name;
322 adapter->ops = match_ops(adapter);
328 * opae_adapter_enumerate - enumerate this adapter
329 * @adapter: adapter to enumerate.
331 * Return: 0 on success, otherwise error code.
333 int opae_adapter_enumerate(struct opae_adapter *adapter)
340 if (adapter->ops && adapter->ops->enumerate)
341 ret = adapter->ops->enumerate(adapter);
344 opae_adapter_dump(adapter, 0);
350 * opae_adapter_destroy - destroy this adapter
351 * @adapter: adapter to destroy.
353 * destroy things allocated during adapter enumeration.
355 void opae_adapter_destroy(struct opae_adapter *adapter)
357 if (adapter && adapter->ops && adapter->ops->destroy)
358 adapter->ops->destroy(adapter);
362 * opae_adapter_get_acc - find and return accelerator with matched id
363 * @adapter: adapter to find the accelerator.
364 * @acc_id: id (index) of the accelerator.
366 * destroy things allocated during adapter enumeration.
368 struct opae_accelerator *
369 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
371 struct opae_accelerator *acc = NULL;
376 opae_adapter_for_each_acc(adapter, acc)
377 if (acc->index == acc_id)