1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
9 #include "opae_hw_api.h"
10 #include "opae_debug.h"
11 #include "ifpga_api.h"
13 /* OPAE Bridge Functions */
16 * opae_bridge_alloc - alloc opae_bridge data structure
18 * @ops: ops of this bridge.
19 * @data: private data of this bridge.
21 * Return opae_bridge on success, otherwise NULL.
24 opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
26 struct opae_bridge *br = opae_zmalloc(sizeof(*br));
35 opae_log("%s %p\n", __func__, br);
41 * opae_bridge_reset - reset opae_bridge
42 * @br: bridge to be reset.
44 * Return: 0 on success, otherwise error code.
46 int opae_bridge_reset(struct opae_bridge *br)
51 if (br->ops && br->ops->reset)
52 return br->ops->reset(br);
54 opae_log("%s no ops\n", __func__);
59 /* Accelerator Functions */
62 * opae_accelerator_alloc - alloc opae_accelerator data structure
63 * @name: accelerator name.
64 * @ops: ops of this accelerator.
65 * @data: private data of this accelerator.
67 * Return: opae_accelerator on success, otherwise NULL.
69 struct opae_accelerator *
70 opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
73 struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));
82 opae_log("%s %p\n", __func__, acc);
88 * opae_acc_reg_read - read accelerator's register from its reg region.
89 * @acc: accelerator to read.
90 * @region_idx: reg region index.
91 * @offset: reg offset.
92 * @byte: read operation width, e.g 4 byte = 32bit read.
93 * @data: data to store the value read from the register.
95 * Return: 0 on success, otherwise error code.
97 int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
98 u64 offset, unsigned int byte, void *data)
103 if (acc->ops && acc->ops->read)
104 return acc->ops->read(acc, region_idx, offset, byte, data);
110 * opae_acc_reg_write - write to accelerator's register from its reg region.
111 * @acc: accelerator to write.
112 * @region_idx: reg region index.
113 * @offset: reg offset.
114 * @byte: write operation width, e.g 4 byte = 32bit write.
115 * @data: data stored the value to write to the register.
117 * Return: 0 on success, otherwise error code.
119 int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
120 u64 offset, unsigned int byte, void *data)
125 if (acc->ops && acc->ops->write)
126 return acc->ops->write(acc, region_idx, offset, byte, data);
132 * opae_acc_get_info - get information of an accelerator.
133 * @acc: targeted accelerator
134 * @info: accelerator info data structure to be filled.
136 * Return: 0 on success, otherwise error code.
138 int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
143 if (acc->ops && acc->ops->get_info)
144 return acc->ops->get_info(acc, info);
150 * opae_acc_get_region_info - get information of an accelerator register region.
151 * @acc: targeted accelerator
152 * @info: accelerator region info data structure to be filled.
154 * Return: 0 on success, otherwise error code.
156 int opae_acc_get_region_info(struct opae_accelerator *acc,
157 struct opae_acc_region_info *info)
162 if (acc->ops && acc->ops->get_region_info)
163 return acc->ops->get_region_info(acc, info);
169 * opae_acc_set_irq - set an accelerator's irq.
170 * @acc: targeted accelerator
171 * @start: start vector number
172 * @count: count of vectors to be set from the start vector
173 * @evtfds: event fds to be notified when corresponding irqs happens
175 * Return: 0 on success, otherwise error code.
177 int opae_acc_set_irq(struct opae_accelerator *acc,
178 u32 start, u32 count, s32 evtfds[])
180 if (!acc || !acc->data)
183 if (start + count <= start)
186 if (acc->ops && acc->ops->set_irq)
187 return acc->ops->set_irq(acc, start, count, evtfds);
193 * opae_acc_get_uuid - get accelerator's UUID.
194 * @acc: targeted accelerator
195 * @uuid: a pointer to UUID
197 * Return: 0 on success, otherwise error code.
199 int opae_acc_get_uuid(struct opae_accelerator *acc,
205 if (acc->ops && acc->ops->get_uuid)
206 return acc->ops->get_uuid(acc, uuid);
211 /* Manager Functions */
214 * opae_manager_alloc - alloc opae_manager data structure
215 * @name: manager name.
216 * @ops: ops of this manager.
217 * @network_ops: ops of network management.
218 * @data: private data of this manager.
220 * Return: opae_manager on success, otherwise NULL.
222 struct opae_manager *
223 opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
224 struct opae_manager_networking_ops *network_ops, void *data)
226 struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
233 mgr->network_ops = network_ops;
236 opae_log("%s %p\n", __func__, mgr);
242 * opae_manager_flash - flash a reconfiguration image via opae_manager
243 * @mgr: opae_manager for flash.
244 * @id: id of target region (accelerator).
245 * @buf: image data buffer.
246 * @size: buffer size.
247 * @status: status to store flash result.
249 * Return: 0 on success, otherwise error code.
251 int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
252 u32 size, u64 *status)
257 if (mgr && mgr->ops && mgr->ops->flash)
258 return mgr->ops->flash(mgr, id, buf, size, status);
263 /* Adapter Functions */
266 * opae_adapter_data_alloc - alloc opae_adapter_data data structure
267 * @type: opae_adapter_type.
269 * Return: opae_adapter_data on success, otherwise NULL.
271 void *opae_adapter_data_alloc(enum opae_adapter_type type)
273 struct opae_adapter_data *data;
278 size = sizeof(struct opae_adapter_data_pci);
281 size = sizeof(struct opae_adapter_data_net);
284 size = sizeof(struct opae_adapter_data);
288 data = opae_zmalloc(size);
297 static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
299 struct opae_adapter_data *data;
301 if (!adapter || !adapter->data)
304 data = adapter->data;
306 if (data->type == OPAE_FPGA_PCI)
307 return &ifpga_adapter_ops;
312 static void opae_mutex_init(pthread_mutex_t *mutex)
314 pthread_mutexattr_t mattr;
316 pthread_mutexattr_init(&mattr);
317 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
318 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
319 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
320 pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
321 pthread_mutex_init(mutex, &mattr);
322 pthread_mutexattr_destroy(&mattr);
325 static int opae_shm_open(char *shm_name, u32 size, int *new_shm)
330 shm_id = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0666);
332 if (errno == EEXIST) {
333 dev_info(NULL, "shared memory %s already exist\n",
335 shm_id = shm_open(shm_name, O_RDWR, 0666);
337 dev_err(NULL, "failed to create shared memory %s\n",
343 ret = ftruncate(shm_id, size);
346 "failed to set shared memory size to %u\n",
348 ret = shm_unlink(shm_name);
351 "failed to unlink shared memory %s\n",
361 static pthread_mutex_t *opae_adapter_mutex_open(struct opae_adapter *adapter)
370 adapter->lock = NULL;
372 snprintf(shm_name, sizeof(shm_name), "/mutex.IFPGA:%s", adapter->name);
373 shm_id = opae_shm_open(shm_name, sizeof(pthread_mutex_t), &new_shm);
375 dev_err(NULL, "failed to open shared memory %s\n", shm_name);
377 dev_info(NULL, "shared memory %s id is %d\n",
379 ptr = mmap(NULL, sizeof(pthread_mutex_t),
380 PROT_READ | PROT_WRITE, MAP_SHARED,
382 adapter->lock = (pthread_mutex_t *)ptr;
385 "shared memory %s address is %p\n",
388 opae_mutex_init(adapter->lock);
390 dev_err(NULL, "failed to map shared memory %s\n",
395 return adapter->lock;
398 static void opae_adapter_mutex_close(struct opae_adapter *adapter)
406 snprintf(shm_name, sizeof(shm_name), "/mutex.IFPGA:%s", adapter->name);
408 ret = munmap(adapter->lock, sizeof(pthread_mutex_t));
410 dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
412 adapter->lock = NULL;
416 * opae_adapter_lock - lock this adapter
417 * @adapter: adapter to lock.
418 * @timeout: maximum time to wait for lock done
419 * -1 wait until the lock is available
420 * 0 do not wait and return immediately
421 * t positive time in second to wait
423 * Return: 0 on success, otherwise error code.
425 int opae_adapter_lock(struct opae_adapter *adapter, int timeout)
430 if (adapter && adapter->lock) {
432 ret = pthread_mutex_lock(adapter->lock);
433 } else if (timeout == 0) {
434 ret = pthread_mutex_trylock(adapter->lock);
436 clock_gettime(CLOCK_REALTIME, &t);
438 ret = pthread_mutex_timedlock(adapter->lock, &t);
445 * opae_adapter_unlock - unlock this adapter
446 * @adapter: adapter to unlock.
448 * Return: 0 on success, otherwise error code.
450 int opae_adapter_unlock(struct opae_adapter *adapter)
454 if (adapter && adapter->lock)
455 ret = pthread_mutex_unlock(adapter->lock);
460 static void opae_adapter_shm_init(struct opae_adapter *adapter)
464 if (!adapter->shm.ptr)
467 sd = (opae_share_data *)adapter->shm.ptr;
468 dev_info(NULL, "initialize shared memory\n");
469 opae_mutex_init(&sd->spi_mutex);
470 opae_mutex_init(&sd->i2c_mutex);
472 sd->dtb_size = SHM_BLK_SIZE;
475 static void *opae_adapter_shm_alloc(struct opae_adapter *adapter)
479 u32 size = sizeof(opae_share_data);
486 snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
487 adapter->shm.ptr = NULL;
489 opae_adapter_lock(adapter, -1);
490 shm_id = opae_shm_open(shm_name, size, &new_shm);
492 dev_err(NULL, "failed to open shared memory %s\n", shm_name);
494 dev_info(NULL, "shared memory %s id is %d\n",
496 adapter->shm.id = shm_id;
497 adapter->shm.size = size;
498 adapter->shm.ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
499 MAP_SHARED, shm_id, 0);
500 if (adapter->shm.ptr) {
502 "shared memory %s address is %p\n",
503 shm_name, adapter->shm.ptr);
505 opae_adapter_shm_init(adapter);
506 sd = (opae_share_data *)adapter->shm.ptr;
509 dev_err(NULL, "failed to map shared memory %s\n",
513 opae_adapter_unlock(adapter);
515 return adapter->shm.ptr;
518 static void opae_adapter_shm_free(struct opae_adapter *adapter)
525 if (!adapter->shm.ptr)
528 sd = (opae_share_data *)adapter->shm.ptr;
529 snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
531 opae_adapter_lock(adapter, -1);
532 ref_cnt = --sd->ref_cnt;
533 ret = munmap(adapter->shm.ptr, adapter->shm.size);
535 dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
537 adapter->shm.ptr = NULL;
540 dev_info(NULL, "unlink shared memory %s\n", shm_name);
541 ret = shm_unlink(shm_name);
543 dev_err(NULL, "failed to unlink shared memory %s\n",
547 opae_adapter_unlock(adapter);
551 * opae_adapter_init - init opae_adapter data structure
552 * @adapter: pointer of opae_adapter data structure
553 * @name: adapter name.
554 * @data: private data of this adapter.
556 * Return: 0 on success.
558 int opae_adapter_init(struct opae_adapter *adapter,
559 const char *name, void *data)
564 TAILQ_INIT(&adapter->acc_list);
565 adapter->data = data;
566 adapter->name = name;
567 adapter->ops = match_ops(adapter);
569 if (!opae_adapter_mutex_open(adapter))
572 if (!opae_adapter_shm_alloc(adapter))
579 * opae_adapter_enumerate - enumerate this adapter
580 * @adapter: adapter to enumerate.
582 * Return: 0 on success, otherwise error code.
584 int opae_adapter_enumerate(struct opae_adapter *adapter)
591 if (adapter->ops && adapter->ops->enumerate)
592 ret = adapter->ops->enumerate(adapter);
595 opae_adapter_dump(adapter, 0);
601 * opae_adapter_destroy - destroy this adapter
602 * @adapter: adapter to destroy.
604 * destroy things allocated during adapter enumeration.
606 void opae_adapter_destroy(struct opae_adapter *adapter)
609 if (adapter->ops && adapter->ops->destroy)
610 adapter->ops->destroy(adapter);
611 opae_adapter_shm_free(adapter);
612 opae_adapter_mutex_close(adapter);
617 * opae_adapter_get_acc - find and return accelerator with matched id
618 * @adapter: adapter to find the accelerator.
619 * @acc_id: id (index) of the accelerator.
621 * destroy things allocated during adapter enumeration.
623 struct opae_accelerator *
624 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
626 struct opae_accelerator *acc = NULL;
631 opae_adapter_for_each_acc(adapter, acc)
632 if (acc->index == acc_id)
639 * opae_manager_read_mac_rom - read the content of the MAC ROM
640 * @mgr: opae_manager for MAC ROM
641 * @port: the port number of retimer
642 * @addr: buffer of the MAC address
644 * Return: return the bytes of read successfully
646 int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
647 struct opae_ether_addr *addr)
649 if (!mgr || !mgr->network_ops)
652 if (mgr->network_ops->read_mac_rom)
653 return mgr->network_ops->read_mac_rom(mgr,
654 port * sizeof(struct opae_ether_addr),
655 addr, sizeof(struct opae_ether_addr));
661 * opae_manager_write_mac_rom - write data into MAC ROM
662 * @mgr: opae_manager for MAC ROM
663 * @port: the port number of the retimer
664 * @addr: data of the MAC address
666 * Return: return written bytes
668 int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
669 struct opae_ether_addr *addr)
671 if (!mgr || !mgr->network_ops)
674 if (mgr->network_ops && mgr->network_ops->write_mac_rom)
675 return mgr->network_ops->write_mac_rom(mgr,
676 port * sizeof(struct opae_ether_addr),
677 addr, sizeof(struct opae_ether_addr));
683 * opae_manager_get_eth_group_nums - get eth group numbers
684 * @mgr: opae_manager for eth group
686 * Return: the numbers of eth group
688 int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
690 if (!mgr || !mgr->network_ops)
693 if (mgr->network_ops->get_retimer_info)
694 return mgr->network_ops->get_eth_group_nums(mgr);
700 * opae_manager_get_eth_group_info - get eth group info
701 * @mgr: opae_manager for eth group
702 * @group_id: id for eth group
703 * @info: info return to caller
705 * Return: 0 on success, otherwise error code
707 int opae_manager_get_eth_group_info(struct opae_manager *mgr,
708 u8 group_id, struct opae_eth_group_info *info)
710 if (!mgr || !mgr->network_ops)
713 if (mgr->network_ops->get_retimer_info)
714 return mgr->network_ops->get_eth_group_info(mgr,
721 * opae_manager_get_eth_group_region_info
722 * @mgr: opae_manager for flash.
723 * @info: the memory region info for eth group
725 * Return: 0 on success, otherwise error code.
727 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
728 u8 group_id, struct opae_eth_group_region_info *info)
733 if (group_id >= MAX_ETH_GROUP_DEVICES)
736 info->group_id = group_id;
738 if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
739 return mgr->ops->get_eth_group_region_info(mgr, info);
745 * opae_manager_eth_group_read_reg - read ETH group register
746 * @mgr: opae_manager for ETH Group
747 * @group_id: ETH group id
749 * @index: port index in eth group device
750 * @addr: register address of ETH Group
753 * Return: 0 on success, otherwise error code
755 int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
756 u8 type, u8 index, u16 addr, u32 *data)
758 if (!mgr || !mgr->network_ops)
761 if (mgr->network_ops->eth_group_reg_read)
762 return mgr->network_ops->eth_group_reg_read(mgr, group_id,
763 type, index, addr, data);
769 * opae_manager_eth_group_write_reg - write ETH group register
770 * @mgr: opae_manager for ETH Group
771 * @group_id: ETH group id
773 * @index: port index in eth group device
774 * @addr: register address of ETH Group
775 * @data: data will write to register
777 * Return: 0 on success, otherwise error code
779 int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
780 u8 type, u8 index, u16 addr, u32 data)
782 if (!mgr || !mgr->network_ops)
785 if (mgr->network_ops->eth_group_reg_write)
786 return mgr->network_ops->eth_group_reg_write(mgr, group_id,
787 type, index, addr, data);
793 * opae_manager_get_retimer_info - get retimer info like PKVL chip
794 * @mgr: opae_manager for retimer
795 * @info: info return to caller
797 * Return: 0 on success, otherwise error code
799 int opae_manager_get_retimer_info(struct opae_manager *mgr,
800 struct opae_retimer_info *info)
802 if (!mgr || !mgr->network_ops)
805 if (mgr->network_ops->get_retimer_info)
806 return mgr->network_ops->get_retimer_info(mgr, info);
812 * opae_manager_get_retimer_status - get retimer status
813 * @mgr: opae_manager of retimer
814 * @status: status of retimer
816 * Return: 0 on success, otherwise error code
818 int opae_manager_get_retimer_status(struct opae_manager *mgr,
819 struct opae_retimer_status *status)
821 if (!mgr || !mgr->network_ops)
824 if (mgr->network_ops->get_retimer_status)
825 return mgr->network_ops->get_retimer_status(mgr,
832 * opae_manager_get_sensor_by_id - get sensor device
833 * @id: the id of the sensor
835 * Return: the pointer of the opae_sensor_info
837 struct opae_sensor_info *
838 opae_mgr_get_sensor_by_id(struct opae_manager *mgr,
841 struct opae_sensor_info *sensor;
843 opae_mgr_for_each_sensor(mgr, sensor)
844 if (sensor->id == id)
851 * opae_manager_get_sensor_by_name - get sensor device
852 * @name: the name of the sensor
854 * Return: the pointer of the opae_sensor_info
856 struct opae_sensor_info *
857 opae_mgr_get_sensor_by_name(struct opae_manager *mgr,
860 struct opae_sensor_info *sensor;
862 opae_mgr_for_each_sensor(mgr, sensor)
863 if (!strcmp(sensor->name, name))
870 * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
872 * @mgr: opae_manager for sensor.
873 * @name: the name of the sensor
874 * @value: the readout sensor value
876 * Return: 0 on success, otherwise error code
879 opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
880 const char *name, unsigned int *value)
882 struct opae_sensor_info *sensor;
887 sensor = opae_mgr_get_sensor_by_name(mgr, name);
891 if (mgr->ops && mgr->ops->get_sensor_value)
892 return mgr->ops->get_sensor_value(mgr, sensor, value);
898 * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
900 * @mgr: opae_manager for sensor
901 * @id: the id of the sensor
902 * @value: the readout sensor value
904 * Return: 0 on success, otherwise error code
907 opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
908 unsigned int id, unsigned int *value)
910 struct opae_sensor_info *sensor;
915 sensor = opae_mgr_get_sensor_by_id(mgr, id);
919 if (mgr->ops && mgr->ops->get_sensor_value)
920 return mgr->ops->get_sensor_value(mgr, sensor, value);
926 * opae_manager_get_sensor_value - get the current
928 * @mgr: opae_manager for sensor
929 * @sensor: opae_sensor_info for sensor
930 * @value: the readout sensor value
932 * Return: 0 on success, otherwise error code
935 opae_mgr_get_sensor_value(struct opae_manager *mgr,
936 struct opae_sensor_info *sensor,
942 if (mgr->ops && mgr->ops->get_sensor_value)
943 return mgr->ops->get_sensor_value(mgr, sensor, value);
949 * opae_manager_get_board_info - get board info
951 * @info: opae_board_info for the card
953 * Return: 0 on success, otherwise error code
956 opae_mgr_get_board_info(struct opae_manager *mgr,
957 struct opae_board_info **info)
962 if (mgr->ops && mgr->ops->get_board_info)
963 return mgr->ops->get_board_info(mgr, info);