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 * @network_ops: ops of network management.
214 * @data: private data of this manager.
216 * Return: opae_manager on success, otherwise NULL.
218 struct opae_manager *
219 opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
220 struct opae_manager_networking_ops *network_ops, void *data)
222 struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
229 mgr->network_ops = network_ops;
232 opae_log("%s %p\n", __func__, mgr);
238 * opae_manager_flash - flash a reconfiguration image via opae_manager
239 * @mgr: opae_manager for flash.
240 * @id: id of target region (accelerator).
241 * @buf: image data buffer.
242 * @size: buffer size.
243 * @status: status to store flash result.
245 * Return: 0 on success, otherwise error code.
247 int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
248 u32 size, u64 *status)
253 if (mgr && mgr->ops && mgr->ops->flash)
254 return mgr->ops->flash(mgr, id, buf, size, status);
259 /* Adapter Functions */
262 * opae_adapter_data_alloc - alloc opae_adapter_data data structure
263 * @type: opae_adapter_type.
265 * Return: opae_adapter_data on success, otherwise NULL.
267 void *opae_adapter_data_alloc(enum opae_adapter_type type)
269 struct opae_adapter_data *data;
274 size = sizeof(struct opae_adapter_data_pci);
277 size = sizeof(struct opae_adapter_data_net);
280 size = sizeof(struct opae_adapter_data);
284 data = opae_zmalloc(size);
293 static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
295 struct opae_adapter_data *data;
297 if (!adapter || !adapter->data)
300 data = adapter->data;
302 if (data->type == OPAE_FPGA_PCI)
303 return &ifpga_adapter_ops;
309 * opae_adapter_init - init opae_adapter data structure
310 * @adapter: pointer of opae_adapter data structure
311 * @name: adapter name.
312 * @data: private data of this adapter.
314 * Return: 0 on success.
316 int opae_adapter_init(struct opae_adapter *adapter,
317 const char *name, void *data)
322 TAILQ_INIT(&adapter->acc_list);
323 adapter->data = data;
324 adapter->name = name;
325 adapter->ops = match_ops(adapter);
331 * opae_adapter_enumerate - enumerate this adapter
332 * @adapter: adapter to enumerate.
334 * Return: 0 on success, otherwise error code.
336 int opae_adapter_enumerate(struct opae_adapter *adapter)
343 if (adapter->ops && adapter->ops->enumerate)
344 ret = adapter->ops->enumerate(adapter);
347 opae_adapter_dump(adapter, 0);
353 * opae_adapter_destroy - destroy this adapter
354 * @adapter: adapter to destroy.
356 * destroy things allocated during adapter enumeration.
358 void opae_adapter_destroy(struct opae_adapter *adapter)
360 if (adapter && adapter->ops && adapter->ops->destroy)
361 adapter->ops->destroy(adapter);
365 * opae_adapter_get_acc - find and return accelerator with matched id
366 * @adapter: adapter to find the accelerator.
367 * @acc_id: id (index) of the accelerator.
369 * destroy things allocated during adapter enumeration.
371 struct opae_accelerator *
372 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
374 struct opae_accelerator *acc = NULL;
379 opae_adapter_for_each_acc(adapter, acc)
380 if (acc->index == acc_id)
387 * opae_manager_read_mac_rom - read the content of the MAC ROM
388 * @mgr: opae_manager for MAC ROM
389 * @port: the port number of retimer
390 * @addr: buffer of the MAC address
392 * Return: return the bytes of read successfully
394 int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
395 struct opae_ether_addr *addr)
397 if (!mgr || !mgr->network_ops)
400 if (mgr->network_ops->read_mac_rom)
401 return mgr->network_ops->read_mac_rom(mgr,
402 port * sizeof(struct opae_ether_addr),
403 addr, sizeof(struct opae_ether_addr));
409 * opae_manager_write_mac_rom - write data into MAC ROM
410 * @mgr: opae_manager for MAC ROM
411 * @port: the port number of the retimer
412 * @addr: data of the MAC address
414 * Return: return written bytes
416 int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
417 struct opae_ether_addr *addr)
419 if (!mgr || !mgr->network_ops)
422 if (mgr->network_ops && mgr->network_ops->write_mac_rom)
423 return mgr->network_ops->write_mac_rom(mgr,
424 port * sizeof(struct opae_ether_addr),
425 addr, sizeof(struct opae_ether_addr));
431 * opae_manager_get_eth_group_nums - get eth group numbers
432 * @mgr: opae_manager for eth group
434 * Return: the numbers of eth group
436 int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
438 if (!mgr || !mgr->network_ops)
441 if (mgr->network_ops->get_retimer_info)
442 return mgr->network_ops->get_eth_group_nums(mgr);
448 * opae_manager_get_eth_group_info - get eth group info
449 * @mgr: opae_manager for eth group
450 * @group_id: id for eth group
451 * @info: info return to caller
453 * Return: 0 on success, otherwise error code
455 int opae_manager_get_eth_group_info(struct opae_manager *mgr,
456 u8 group_id, struct opae_eth_group_info *info)
458 if (!mgr || !mgr->network_ops)
461 if (mgr->network_ops->get_retimer_info)
462 return mgr->network_ops->get_eth_group_info(mgr,
469 * opae_manager_get_eth_group_region_info
470 * @mgr: opae_manager for flash.
471 * @info: the memory region info for eth group
473 * Return: 0 on success, otherwise error code.
475 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
476 u8 group_id, struct opae_eth_group_region_info *info)
481 if (group_id >= MAX_ETH_GROUP_DEVICES)
484 info->group_id = group_id;
486 if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
487 return mgr->ops->get_eth_group_region_info(mgr, info);
493 * opae_manager_eth_group_read_reg - read ETH group register
494 * @mgr: opae_manager for ETH Group
495 * @group_id: ETH group id
497 * @index: port index in eth group device
498 * @addr: register address of ETH Group
501 * Return: 0 on success, otherwise error code
503 int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
504 u8 type, u8 index, u16 addr, u32 *data)
506 if (!mgr || !mgr->network_ops)
509 if (mgr->network_ops->eth_group_reg_read)
510 return mgr->network_ops->eth_group_reg_read(mgr, group_id,
511 type, index, addr, data);
517 * opae_manager_eth_group_write_reg - write ETH group register
518 * @mgr: opae_manager for ETH Group
519 * @group_id: ETH group id
521 * @index: port index in eth group device
522 * @addr: register address of ETH Group
523 * @data: data will write to register
525 * Return: 0 on success, otherwise error code
527 int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
528 u8 type, u8 index, u16 addr, u32 data)
530 if (!mgr || !mgr->network_ops)
533 if (mgr->network_ops->eth_group_reg_write)
534 return mgr->network_ops->eth_group_reg_write(mgr, group_id,
535 type, index, addr, data);
541 * opae_manager_get_retimer_info - get retimer info like PKVL chip
542 * @mgr: opae_manager for retimer
543 * @info: info return to caller
545 * Return: 0 on success, otherwise error code
547 int opae_manager_get_retimer_info(struct opae_manager *mgr,
548 struct opae_retimer_info *info)
550 if (!mgr || !mgr->network_ops)
553 if (mgr->network_ops->get_retimer_info)
554 return mgr->network_ops->get_retimer_info(mgr, info);
560 * opae_manager_get_retimer_status - get retimer status
561 * @mgr: opae_manager of retimer
562 * @status: status of retimer
564 * Return: 0 on success, otherwise error code
566 int opae_manager_get_retimer_status(struct opae_manager *mgr,
567 struct opae_retimer_status *status)
569 if (!mgr || !mgr->network_ops)
572 if (mgr->network_ops->get_retimer_status)
573 return mgr->network_ops->get_retimer_status(mgr,
580 * opae_manager_get_sensor_by_id - get sensor device
581 * @id: the id of the sensor
583 * Return: the pointer of the opae_sensor_info
585 struct opae_sensor_info *
586 opae_mgr_get_sensor_by_id(unsigned int id)
588 struct opae_sensor_info *sensor;
590 opae_mgr_for_each_sensor(sensor)
591 if (sensor->id == id)
598 * opae_manager_get_sensor_by_name - get sensor device
599 * @name: the name of the sensor
601 * Return: the pointer of the opae_sensor_info
603 struct opae_sensor_info *
604 opae_mgr_get_sensor_by_name(const char *name)
606 struct opae_sensor_info *sensor;
608 opae_mgr_for_each_sensor(sensor)
609 if (!strcmp(sensor->name, name))
616 * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
618 * @mgr: opae_manager for sensor.
619 * @name: the name of the sensor
620 * @value: the readout sensor value
622 * Return: 0 on success, otherwise error code
625 opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
626 const char *name, unsigned int *value)
628 struct opae_sensor_info *sensor;
633 sensor = opae_mgr_get_sensor_by_name(name);
637 if (mgr->ops && mgr->ops->get_sensor_value)
638 return mgr->ops->get_sensor_value(mgr, sensor, value);
644 * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
646 * @mgr: opae_manager for sensor
647 * @id: the id of the sensor
648 * @value: the readout sensor value
650 * Return: 0 on success, otherwise error code
653 opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
654 unsigned int id, unsigned int *value)
656 struct opae_sensor_info *sensor;
661 sensor = opae_mgr_get_sensor_by_id(id);
665 if (mgr->ops && mgr->ops->get_sensor_value)
666 return mgr->ops->get_sensor_value(mgr, sensor, value);
672 * opae_manager_get_sensor_value - get the current
674 * @mgr: opae_manager for sensor
675 * @sensor: opae_sensor_info for sensor
676 * @value: the readout sensor value
678 * Return: 0 on success, otherwise error code
681 opae_mgr_get_sensor_value(struct opae_manager *mgr,
682 struct opae_sensor_info *sensor,
688 if (mgr->ops && mgr->ops->get_sensor_value)
689 return mgr->ops->get_sensor_value(mgr, sensor, value);