New upstream version 18.08
[deb_dpdk.git] / drivers / raw / ifpga_rawdev / base / opae_hw_api.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include "opae_hw_api.h"
6 #include "opae_debug.h"
7 #include "ifpga_api.h"
8
9 /* OPAE Bridge Functions */
10
11 /**
12  * opae_bridge_alloc - alloc opae_bridge data structure
13  * @name: bridge name.
14  * @ops: ops of this bridge.
15  * @data: private data of this bridge.
16  *
17  * Return opae_bridge on success, otherwise NULL.
18  */
19 struct opae_bridge *
20 opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
21 {
22         struct opae_bridge *br = opae_zmalloc(sizeof(*br));
23
24         if (!br)
25                 return NULL;
26
27         br->name = name;
28         br->ops = ops;
29         br->data = data;
30
31         opae_log("%s %p\n", __func__, br);
32
33         return br;
34 }
35
36 /**
37  * opae_bridge_reset -  reset opae_bridge
38  * @br: bridge to be reset.
39  *
40  * Return: 0 on success, otherwise error code.
41  */
42 int opae_bridge_reset(struct opae_bridge *br)
43 {
44         if (!br)
45                 return -EINVAL;
46
47         if (br->ops && br->ops->reset)
48                 return br->ops->reset(br);
49
50         opae_log("%s no ops\n", __func__);
51
52         return -ENOENT;
53 }
54
55 /* Accelerator Functions */
56
57 /**
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.
62  *
63  * Return: opae_accelerator on success, otherwise NULL.
64  */
65 struct opae_accelerator *
66 opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
67                        void *data)
68 {
69         struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));
70
71         if (!acc)
72                 return NULL;
73
74         acc->name = name;
75         acc->ops = ops;
76         acc->data = data;
77
78         opae_log("%s %p\n", __func__, acc);
79
80         return acc;
81 }
82
83 /**
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.
90  *
91  * Return: 0 on success, otherwise error code.
92  */
93 int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
94                       u64 offset, unsigned int byte, void *data)
95 {
96         if (!acc || !data)
97                 return -EINVAL;
98
99         if (acc->ops && acc->ops->read)
100                 return acc->ops->read(acc, region_idx, offset, byte, data);
101
102         return -ENOENT;
103 }
104
105 /**
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.
112  *
113  * Return: 0 on success, otherwise error code.
114  */
115 int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
116                        u64 offset, unsigned int byte, void *data)
117 {
118         if (!acc || !data)
119                 return -EINVAL;
120
121         if (acc->ops && acc->ops->write)
122                 return acc->ops->write(acc, region_idx, offset, byte, data);
123
124         return -ENOENT;
125 }
126
127 /**
128  * opae_acc_get_info - get information of an accelerator.
129  * @acc: targeted accelerator
130  * @info: accelerator info data structure to be filled.
131  *
132  * Return: 0 on success, otherwise error code.
133  */
134 int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
135 {
136         if (!acc || !info)
137                 return -EINVAL;
138
139         if (acc->ops && acc->ops->get_info)
140                 return acc->ops->get_info(acc, info);
141
142         return -ENOENT;
143 }
144
145 /**
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.
149  *
150  * Return: 0 on success, otherwise error code.
151  */
152 int opae_acc_get_region_info(struct opae_accelerator *acc,
153                              struct opae_acc_region_info *info)
154 {
155         if (!acc || !info)
156                 return -EINVAL;
157
158         if (acc->ops && acc->ops->get_region_info)
159                 return acc->ops->get_region_info(acc, info);
160
161         return -ENOENT;
162 }
163
164 /**
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
170  *
171  * Return: 0 on success, otherwise error code.
172  */
173 int opae_acc_set_irq(struct opae_accelerator *acc,
174                      u32 start, u32 count, s32 evtfds[])
175 {
176         if (!acc || !acc->data)
177                 return -EINVAL;
178
179         if (start + count <= start)
180                 return -EINVAL;
181
182         if (acc->ops && acc->ops->set_irq)
183                 return acc->ops->set_irq(acc, start, count, evtfds);
184
185         return -ENOENT;
186 }
187
188 /**
189  * opae_acc_get_uuid -  get accelerator's UUID.
190  * @acc: targeted accelerator
191  * @uuid: a pointer to UUID
192  *
193  * Return: 0 on success, otherwise error code.
194  */
195 int opae_acc_get_uuid(struct opae_accelerator *acc,
196                       struct uuid *uuid)
197 {
198         if (!acc || !uuid)
199                 return -EINVAL;
200
201         if (acc->ops && acc->ops->get_uuid)
202                 return acc->ops->get_uuid(acc, uuid);
203
204         return -ENOENT;
205 }
206
207 /* Manager Functions */
208
209 /**
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.
214  *
215  * Return: opae_manager on success, otherwise NULL.
216  */
217 struct opae_manager *
218 opae_manager_alloc(const char *name, struct opae_manager_ops *ops, void *data)
219 {
220         struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
221
222         if (!mgr)
223                 return NULL;
224
225         mgr->name = name;
226         mgr->ops = ops;
227         mgr->data = data;
228
229         opae_log("%s %p\n", __func__, mgr);
230
231         return mgr;
232 }
233
234 /**
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.
241  *
242  * Return: 0 on success, otherwise error code.
243  */
244 int opae_manager_flash(struct opae_manager *mgr, int id, void *buf, u32 size,
245                        u64 *status)
246 {
247         if (!mgr)
248                 return -EINVAL;
249
250         if (mgr && mgr->ops && mgr->ops->flash)
251                 return mgr->ops->flash(mgr, id, buf, size, status);
252
253         return -ENOENT;
254 }
255
256 /* Adapter Functions */
257
258 /**
259  * opae_adapter_data_alloc - alloc opae_adapter_data data structure
260  * @type: opae_adapter_type.
261  *
262  * Return: opae_adapter_data on success, otherwise NULL.
263  */
264 void *opae_adapter_data_alloc(enum opae_adapter_type type)
265 {
266         struct opae_adapter_data *data;
267         int size;
268
269         switch (type) {
270         case OPAE_FPGA_PCI:
271                 size = sizeof(struct opae_adapter_data_pci);
272                 break;
273         case OPAE_FPGA_NET:
274                 size = sizeof(struct opae_adapter_data_net);
275                 break;
276         default:
277                 size = sizeof(struct opae_adapter_data);
278                 break;
279         }
280
281         data = opae_zmalloc(size);
282         if (!data)
283                 return NULL;
284
285         data->type = type;
286
287         return data;
288 }
289
290 static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
291 {
292         struct opae_adapter_data *data;
293
294         if (!adapter || !adapter->data)
295                 return NULL;
296
297         data = adapter->data;
298
299         if (data->type == OPAE_FPGA_PCI)
300                 return &ifpga_adapter_ops;
301
302         return NULL;
303 }
304
305 /**
306  * opae_adapter_data_alloc - alloc opae_adapter_data data structure
307  * @name: adapter name.
308  * @data: private data of this adapter.
309  *
310  * Return: opae_adapter on success, otherwise NULL.
311  */
312 struct opae_adapter *opae_adapter_alloc(const char *name, void *data)
313 {
314         struct opae_adapter *adapter = opae_zmalloc(sizeof(*adapter));
315
316         if (!adapter)
317                 return NULL;
318
319         TAILQ_INIT(&adapter->acc_list);
320         adapter->data = data;
321         adapter->name = name;
322         adapter->ops = match_ops(adapter);
323
324         return adapter;
325 }
326
327 /**
328  * opae_adapter_enumerate - enumerate this adapter
329  * @adapter: adapter to enumerate.
330  *
331  * Return: 0 on success, otherwise error code.
332  */
333 int opae_adapter_enumerate(struct opae_adapter *adapter)
334 {
335         int ret = -ENOENT;
336
337         if (!adapter)
338                 return -EINVAL;
339
340         if (adapter->ops && adapter->ops->enumerate)
341                 ret = adapter->ops->enumerate(adapter);
342
343         if (!ret)
344                 opae_adapter_dump(adapter, 1);
345
346         return ret;
347 }
348
349 /**
350  * opae_adapter_destroy - destroy this adapter
351  * @adapter: adapter to destroy.
352  *
353  * destroy things allocated during adapter enumeration.
354  */
355 void opae_adapter_destroy(struct opae_adapter *adapter)
356 {
357         if (adapter && adapter->ops && adapter->ops->destroy)
358                 adapter->ops->destroy(adapter);
359 }
360
361 /**
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.
365  *
366  * destroy things allocated during adapter enumeration.
367  */
368 struct opae_accelerator *
369 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
370 {
371         struct opae_accelerator *acc = NULL;
372
373         if (!adapter)
374                 return NULL;
375
376         opae_adapter_for_each_acc(adapter, acc)
377                 if (acc->index == acc_id)
378                         return acc;
379
380         return NULL;
381 }