xref: /dpdk/drivers/raw/ifpga/base/opae_hw_api.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <sys/mman.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include "opae_hw_api.h"
10 #include "opae_debug.h"
11 #include "ifpga_api.h"
12 
13 /* OPAE Bridge Functions */
14 
15 /**
16  * opae_bridge_alloc - alloc opae_bridge data structure
17  * @name: bridge name.
18  * @ops: ops of this bridge.
19  * @data: private data of this bridge.
20  *
21  * Return opae_bridge on success, otherwise NULL.
22  */
23 struct opae_bridge *
24 opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
25 {
26 	struct opae_bridge *br = opae_zmalloc(sizeof(*br));
27 
28 	if (!br)
29 		return NULL;
30 
31 	br->name = name;
32 	br->ops = ops;
33 	br->data = data;
34 
35 	opae_log("%s %p\n", __func__, br);
36 
37 	return br;
38 }
39 
40 /**
41  * opae_bridge_reset -  reset opae_bridge
42  * @br: bridge to be reset.
43  *
44  * Return: 0 on success, otherwise error code.
45  */
46 int opae_bridge_reset(struct opae_bridge *br)
47 {
48 	if (!br)
49 		return -EINVAL;
50 
51 	if (br->ops && br->ops->reset)
52 		return br->ops->reset(br);
53 
54 	opae_log("%s no ops\n", __func__);
55 
56 	return -ENOENT;
57 }
58 
59 /* Accelerator Functions */
60 
61 /**
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.
66  *
67  * Return: opae_accelerator on success, otherwise NULL.
68  */
69 struct opae_accelerator *
70 opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
71 		       void *data)
72 {
73 	struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));
74 
75 	if (!acc)
76 		return NULL;
77 
78 	acc->name = name;
79 	acc->ops = ops;
80 	acc->data = data;
81 
82 	opae_log("%s %p\n", __func__, acc);
83 
84 	return acc;
85 }
86 
87 /**
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.
94  *
95  * Return: 0 on success, otherwise error code.
96  */
97 int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
98 		      u64 offset, unsigned int byte, void *data)
99 {
100 	if (!acc || !data)
101 		return -EINVAL;
102 
103 	if (acc->ops && acc->ops->read)
104 		return acc->ops->read(acc, region_idx, offset, byte, data);
105 
106 	return -ENOENT;
107 }
108 
109 /**
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.
116  *
117  * Return: 0 on success, otherwise error code.
118  */
119 int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
120 		       u64 offset, unsigned int byte, void *data)
121 {
122 	if (!acc || !data)
123 		return -EINVAL;
124 
125 	if (acc->ops && acc->ops->write)
126 		return acc->ops->write(acc, region_idx, offset, byte, data);
127 
128 	return -ENOENT;
129 }
130 
131 /**
132  * opae_acc_get_info - get information of an accelerator.
133  * @acc: targeted accelerator
134  * @info: accelerator info data structure to be filled.
135  *
136  * Return: 0 on success, otherwise error code.
137  */
138 int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
139 {
140 	if (!acc || !info)
141 		return -EINVAL;
142 
143 	if (acc->ops && acc->ops->get_info)
144 		return acc->ops->get_info(acc, info);
145 
146 	return -ENOENT;
147 }
148 
149 /**
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.
153  *
154  * Return: 0 on success, otherwise error code.
155  */
156 int opae_acc_get_region_info(struct opae_accelerator *acc,
157 			     struct opae_acc_region_info *info)
158 {
159 	if (!acc || !info)
160 		return -EINVAL;
161 
162 	if (acc->ops && acc->ops->get_region_info)
163 		return acc->ops->get_region_info(acc, info);
164 
165 	return -ENOENT;
166 }
167 
168 /**
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
174  *
175  * Return: 0 on success, otherwise error code.
176  */
177 int opae_acc_set_irq(struct opae_accelerator *acc,
178 		     u32 start, u32 count, s32 evtfds[])
179 {
180 	if (!acc || !acc->data)
181 		return -EINVAL;
182 
183 	if (start + count <= start)
184 		return -EINVAL;
185 
186 	if (acc->ops && acc->ops->set_irq)
187 		return acc->ops->set_irq(acc, start, count, evtfds);
188 
189 	return -ENOENT;
190 }
191 
192 /**
193  * opae_acc_get_uuid -  get accelerator's UUID.
194  * @acc: targeted accelerator
195  * @uuid: a pointer to UUID
196  *
197  * Return: 0 on success, otherwise error code.
198  */
199 int opae_acc_get_uuid(struct opae_accelerator *acc,
200 		      struct uuid *uuid)
201 {
202 	if (!acc || !uuid)
203 		return -EINVAL;
204 
205 	if (acc->ops && acc->ops->get_uuid)
206 		return acc->ops->get_uuid(acc, uuid);
207 
208 	return -ENOENT;
209 }
210 
211 /* Manager Functions */
212 
213 /**
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.
219  *
220  * Return: opae_manager on success, otherwise NULL.
221  */
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)
225 {
226 	struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
227 
228 	if (!mgr)
229 		return NULL;
230 
231 	mgr->name = name;
232 	mgr->ops = ops;
233 	mgr->network_ops = network_ops;
234 	mgr->data = data;
235 
236 	opae_log("%s %p\n", __func__, mgr);
237 
238 	return mgr;
239 }
240 
241 /**
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.
248  *
249  * Return: 0 on success, otherwise error code.
250  */
251 int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
252 		u32 size, u64 *status)
253 {
254 	if (!mgr)
255 		return -EINVAL;
256 
257 	if (mgr && mgr->ops && mgr->ops->flash)
258 		return mgr->ops->flash(mgr, id, buf, size, status);
259 
260 	return -ENOENT;
261 }
262 
263 /* Adapter Functions */
264 
265 /**
266  * opae_adapter_data_alloc - alloc opae_adapter_data data structure
267  * @type: opae_adapter_type.
268  *
269  * Return: opae_adapter_data on success, otherwise NULL.
270  */
271 void *opae_adapter_data_alloc(enum opae_adapter_type type)
272 {
273 	struct opae_adapter_data *data;
274 	int size;
275 
276 	switch (type) {
277 	case OPAE_FPGA_PCI:
278 		size = sizeof(struct opae_adapter_data_pci);
279 		break;
280 	case OPAE_FPGA_NET:
281 		size = sizeof(struct opae_adapter_data_net);
282 		break;
283 	default:
284 		size = sizeof(struct opae_adapter_data);
285 		break;
286 	}
287 
288 	data = opae_zmalloc(size);
289 	if (!data)
290 		return NULL;
291 
292 	data->type = type;
293 
294 	return data;
295 }
296 
297 static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
298 {
299 	struct opae_adapter_data *data;
300 
301 	if (!adapter || !adapter->data)
302 		return NULL;
303 
304 	data = adapter->data;
305 
306 	if (data->type == OPAE_FPGA_PCI)
307 		return &ifpga_adapter_ops;
308 
309 	return NULL;
310 }
311 
312 static void opae_mutex_init(pthread_mutex_t *mutex)
313 {
314 	pthread_mutexattr_t mattr;
315 
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);
323 }
324 
325 static int opae_shm_open(char *shm_name, u32 size, int *new_shm)
326 {
327 	int shm_id;
328 	int ret;
329 
330 	shm_id = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0666);
331 	if (shm_id == -1) {
332 		if (errno == EEXIST) {
333 			dev_info(NULL, "shared memory %s already exist\n",
334 					shm_name);
335 			shm_id = shm_open(shm_name, O_RDWR, 0666);
336 		} else {
337 			dev_err(NULL, "failed to create shared memory %s\n",
338 					shm_name);
339 			return -1;
340 		}
341 	} else {
342 		*new_shm = 1;
343 		ret = ftruncate(shm_id, size);
344 		if (ret == -1) {
345 			dev_err(NULL,
346 					"failed to set shared memory size to %u\n",
347 					size);
348 			ret = shm_unlink(shm_name);
349 			if (ret == -1) {
350 				dev_err(NULL,
351 						"failed to unlink shared memory %s\n",
352 						shm_name);
353 			}
354 			return -1;
355 		}
356 	}
357 
358 	return shm_id;
359 }
360 
361 static pthread_mutex_t *opae_adapter_mutex_open(struct opae_adapter *adapter)
362 {
363 	char shm_name[32];
364 	void *ptr;
365 	int shm_id;
366 	int new_shm = 0;
367 
368 	if (!adapter->data)
369 		return NULL;
370 	adapter->lock = NULL;
371 
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);
374 	if (shm_id == -1) {
375 		dev_err(NULL, "failed to open shared memory %s\n", shm_name);
376 	} else {
377 		dev_info(NULL, "shared memory %s id is %d\n",
378 				shm_name, shm_id);
379 		ptr = mmap(NULL, sizeof(pthread_mutex_t),
380 				PROT_READ | PROT_WRITE, MAP_SHARED,
381 				shm_id, 0);
382 		adapter->lock = (pthread_mutex_t *)ptr;
383 		if (ptr) {
384 			dev_info(NULL,
385 					"shared memory %s address is %p\n",
386 					shm_name, ptr);
387 			if (new_shm)
388 				opae_mutex_init(adapter->lock);
389 		} else {
390 			dev_err(NULL, "failed to map shared memory %s\n",
391 					shm_name);
392 		}
393 	}
394 
395 	return adapter->lock;
396 }
397 
398 static void opae_adapter_mutex_close(struct opae_adapter *adapter)
399 {
400 	char shm_name[32];
401 	int ret;
402 
403 	if (!adapter->lock)
404 		return;
405 
406 	snprintf(shm_name, sizeof(shm_name), "/mutex.IFPGA:%s", adapter->name);
407 
408 	ret = munmap(adapter->lock, sizeof(pthread_mutex_t));
409 	if (ret == -1)
410 		dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
411 	else
412 		adapter->lock = NULL;
413 }
414 
415 /**
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
422  *
423  * Return: 0 on success, otherwise error code.
424  */
425 int opae_adapter_lock(struct opae_adapter *adapter, int timeout)
426 {
427 	struct timespec t;
428 	int ret = -EINVAL;
429 
430 	if (adapter && adapter->lock) {
431 		if (timeout < 0) {
432 			ret = pthread_mutex_lock(adapter->lock);
433 		} else if (timeout == 0) {
434 			ret = pthread_mutex_trylock(adapter->lock);
435 		} else {
436 			clock_gettime(CLOCK_REALTIME, &t);
437 			t.tv_sec += timeout;
438 			ret = pthread_mutex_timedlock(adapter->lock, &t);
439 		}
440 	}
441 	return ret;
442 }
443 
444 /**
445  * opae_adapter_unlock - unlock this adapter
446  * @adapter: adapter to unlock.
447  *
448  * Return: 0 on success, otherwise error code.
449  */
450 int opae_adapter_unlock(struct opae_adapter *adapter)
451 {
452 	int ret = -EINVAL;
453 
454 	if (adapter && adapter->lock)
455 		ret = pthread_mutex_unlock(adapter->lock);
456 
457 	return ret;
458 }
459 
460 static void opae_adapter_shm_init(struct opae_adapter *adapter)
461 {
462 	opae_share_data *sd;
463 
464 	if (!adapter->shm.ptr)
465 		return;
466 
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);
471 	sd->ref_cnt = 0;
472 	sd->dtb_size = SHM_BLK_SIZE;
473 	sd->rsu_ctrl = 0;
474 	sd->rsu_stat = 0;
475 }
476 
477 static void *opae_adapter_shm_alloc(struct opae_adapter *adapter)
478 {
479 	char shm_name[32];
480 	opae_share_data *sd;
481 	u32 size = sizeof(opae_share_data);
482 	int shm_id;
483 	int new_shm = 0;
484 
485 	if (!adapter->data)
486 		return NULL;
487 
488 	snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
489 	adapter->shm.ptr = NULL;
490 
491 	opae_adapter_lock(adapter, -1);
492 	shm_id = opae_shm_open(shm_name, size, &new_shm);
493 	if (shm_id == -1) {
494 		dev_err(NULL, "failed to open shared memory %s\n", shm_name);
495 	} else {
496 		dev_info(NULL, "shared memory %s id is %d\n",
497 				shm_name, shm_id);
498 		adapter->shm.id = shm_id;
499 		adapter->shm.size = size;
500 		adapter->shm.ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
501 							MAP_SHARED, shm_id, 0);
502 		if (adapter->shm.ptr) {
503 			dev_info(NULL,
504 					"shared memory %s address is %p\n",
505 					shm_name, adapter->shm.ptr);
506 			if (new_shm)
507 				opae_adapter_shm_init(adapter);
508 			sd = (opae_share_data *)adapter->shm.ptr;
509 			sd->ref_cnt++;
510 		} else {
511 			dev_err(NULL, "failed to map shared memory %s\n",
512 					shm_name);
513 		}
514 	}
515 	opae_adapter_unlock(adapter);
516 
517 	return adapter->shm.ptr;
518 }
519 
520 static void opae_adapter_shm_free(struct opae_adapter *adapter)
521 {
522 	char shm_name[32];
523 	opae_share_data *sd;
524 	u32 ref_cnt;
525 	int ret;
526 
527 	if (!adapter->shm.ptr)
528 		return;
529 
530 	sd = (opae_share_data *)adapter->shm.ptr;
531 	snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
532 
533 	opae_adapter_lock(adapter, -1);
534 	ref_cnt = --sd->ref_cnt;
535 	ret = munmap(adapter->shm.ptr, adapter->shm.size);
536 	if (ret == -1)
537 		dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
538 	else
539 		adapter->shm.ptr = NULL;
540 
541 	if (ref_cnt == 0) {
542 		dev_info(NULL, "unlink shared memory %s\n", shm_name);
543 		ret = shm_unlink(shm_name);
544 		if (ret == -1) {
545 			dev_err(NULL, "failed to unlink shared memory %s\n",
546 					shm_name);
547 		}
548 	}
549 	opae_adapter_unlock(adapter);
550 }
551 
552 /**
553  * opae_adapter_init - init opae_adapter data structure
554  * @adapter: pointer of opae_adapter data structure
555  * @name: adapter name.
556  * @data: private data of this adapter.
557  *
558  * Return: 0 on success.
559  */
560 int opae_adapter_init(struct opae_adapter *adapter,
561 		const char *name, void *data)
562 {
563 	if (!adapter)
564 		return -ENOMEM;
565 
566 	TAILQ_INIT(&adapter->acc_list);
567 	adapter->data = data;
568 	adapter->name = name;
569 	adapter->ops = match_ops(adapter);
570 
571 	if (!opae_adapter_mutex_open(adapter))
572 		return -ENOMEM;
573 
574 	if (!opae_adapter_shm_alloc(adapter))
575 		return -ENOMEM;
576 
577 	return 0;
578 }
579 
580 /**
581  * opae_adapter_enumerate - enumerate this adapter
582  * @adapter: adapter to enumerate.
583  *
584  * Return: 0 on success, otherwise error code.
585  */
586 int opae_adapter_enumerate(struct opae_adapter *adapter)
587 {
588 	int ret = -ENOENT;
589 
590 	if (!adapter)
591 		return -EINVAL;
592 
593 	if (adapter->ops && adapter->ops->enumerate)
594 		ret = adapter->ops->enumerate(adapter);
595 
596 	if (!ret)
597 		opae_adapter_dump(adapter, 0);
598 
599 	return ret;
600 }
601 
602 /**
603  * opae_adapter_destroy - destroy this adapter
604  * @adapter: adapter to destroy.
605  *
606  * destroy things allocated during adapter enumeration.
607  */
608 void opae_adapter_destroy(struct opae_adapter *adapter)
609 {
610 	if (adapter) {
611 		if (adapter->ops && adapter->ops->destroy)
612 			adapter->ops->destroy(adapter);
613 		opae_adapter_shm_free(adapter);
614 		opae_adapter_mutex_close(adapter);
615 	}
616 }
617 
618 /**
619  * opae_adapter_get_acc - find and return accelerator with matched id
620  * @adapter: adapter to find the accelerator.
621  * @acc_id: id (index) of the accelerator.
622  *
623  * destroy things allocated during adapter enumeration.
624  */
625 struct opae_accelerator *
626 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
627 {
628 	struct opae_accelerator *acc = NULL;
629 
630 	if (!adapter)
631 		return NULL;
632 
633 	opae_adapter_for_each_acc(adapter, acc)
634 		if (acc->index == acc_id)
635 			return acc;
636 
637 	return NULL;
638 }
639 
640 /**
641  * opae_manager_read_mac_rom - read the content of the MAC ROM
642  * @mgr: opae_manager for MAC ROM
643  * @port: the port number of retimer
644  * @addr: buffer of the MAC address
645  *
646  * Return: return the bytes of read successfully
647  */
648 int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
649 		struct opae_ether_addr *addr)
650 {
651 	if (!mgr || !mgr->network_ops)
652 		return -EINVAL;
653 
654 	if (mgr->network_ops->read_mac_rom)
655 		return mgr->network_ops->read_mac_rom(mgr,
656 				port * sizeof(struct opae_ether_addr),
657 				addr, sizeof(struct opae_ether_addr));
658 
659 	return -ENOENT;
660 }
661 
662 /**
663  * opae_manager_write_mac_rom - write data into MAC ROM
664  * @mgr: opae_manager for MAC ROM
665  * @port: the port number of the retimer
666  * @addr: data of the MAC address
667  *
668  * Return: return written bytes
669  */
670 int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
671 		struct opae_ether_addr *addr)
672 {
673 	if (!mgr || !mgr->network_ops)
674 		return -EINVAL;
675 
676 	if (mgr->network_ops && mgr->network_ops->write_mac_rom)
677 		return mgr->network_ops->write_mac_rom(mgr,
678 				port * sizeof(struct opae_ether_addr),
679 				addr, sizeof(struct opae_ether_addr));
680 
681 	return -ENOENT;
682 }
683 
684 /**
685  * opae_manager_get_eth_group_nums - get eth group numbers
686  * @mgr: opae_manager for eth group
687  *
688  * Return: the numbers of eth group
689  */
690 int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
691 {
692 	if (!mgr || !mgr->network_ops)
693 		return -EINVAL;
694 
695 	if (mgr->network_ops->get_retimer_info)
696 		return mgr->network_ops->get_eth_group_nums(mgr);
697 
698 	return -ENOENT;
699 }
700 
701 /**
702  * opae_manager_get_eth_group_info - get eth group info
703  * @mgr: opae_manager for eth group
704  * @group_id: id for eth group
705  * @info: info return to caller
706  *
707  * Return: 0 on success, otherwise error code
708  */
709 int opae_manager_get_eth_group_info(struct opae_manager *mgr,
710 	       u8 group_id, struct opae_eth_group_info *info)
711 {
712 	if (!mgr || !mgr->network_ops)
713 		return -EINVAL;
714 
715 	if (mgr->network_ops->get_retimer_info)
716 		return mgr->network_ops->get_eth_group_info(mgr,
717 			group_id, info);
718 
719 	return -ENOENT;
720 }
721 
722 /**
723  * opae_manager_get_eth_group_region_info
724  * @mgr: opae_manager for flash.
725  * @info: the memory region info for eth group
726  *
727  * Return: 0 on success, otherwise error code.
728  */
729 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
730 		u8 group_id, struct opae_eth_group_region_info *info)
731 {
732 	if (!mgr)
733 		return -EINVAL;
734 
735 	if (group_id >= MAX_ETH_GROUP_DEVICES)
736 		return -EINVAL;
737 
738 	info->group_id = group_id;
739 
740 	if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
741 		return mgr->ops->get_eth_group_region_info(mgr, info);
742 
743 	return -ENOENT;
744 }
745 
746 /**
747  * opae_manager_eth_group_read_reg - read ETH group register
748  * @mgr: opae_manager for ETH Group
749  * @group_id: ETH group id
750  * @type: eth type
751  * @index: port index in eth group device
752  * @addr: register address of ETH Group
753  * @data: read buffer
754  *
755  * Return: 0 on success, otherwise error code
756  */
757 int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
758 		u8 type, u8 index, u16 addr, u32 *data)
759 {
760 	if (!mgr || !mgr->network_ops)
761 		return -EINVAL;
762 
763 	if (mgr->network_ops->eth_group_reg_read)
764 		return mgr->network_ops->eth_group_reg_read(mgr, group_id,
765 				type, index, addr, data);
766 
767 	return -ENOENT;
768 }
769 
770 /**
771  * opae_manager_eth_group_write_reg - write ETH group register
772  * @mgr: opae_manager for ETH Group
773  * @group_id: ETH group id
774  * @type: eth type
775  * @index: port index in eth group device
776  * @addr: register address of ETH Group
777  * @data: data will write to register
778  *
779  * Return: 0 on success, otherwise error code
780  */
781 int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
782 		u8 type, u8 index, u16 addr, u32 data)
783 {
784 	if (!mgr || !mgr->network_ops)
785 		return -EINVAL;
786 
787 	if (mgr->network_ops->eth_group_reg_write)
788 		return mgr->network_ops->eth_group_reg_write(mgr, group_id,
789 				type, index, addr, data);
790 
791 	return -ENOENT;
792 }
793 
794 /**
795  * opae_manager_get_retimer_info - get retimer info like PKVL chip
796  * @mgr: opae_manager for retimer
797  * @info: info return to caller
798  *
799  * Return: 0 on success, otherwise error code
800  */
801 int opae_manager_get_retimer_info(struct opae_manager *mgr,
802 	       struct opae_retimer_info *info)
803 {
804 	if (!mgr || !mgr->network_ops)
805 		return -EINVAL;
806 
807 	if (mgr->network_ops->get_retimer_info)
808 		return mgr->network_ops->get_retimer_info(mgr, info);
809 
810 	return -ENOENT;
811 }
812 
813 /**
814  * opae_manager_get_retimer_status - get retimer status
815  * @mgr: opae_manager of retimer
816  * @status: status of retimer
817  *
818  * Return: 0 on success, otherwise error code
819  */
820 int opae_manager_get_retimer_status(struct opae_manager *mgr,
821 		struct opae_retimer_status *status)
822 {
823 	if (!mgr || !mgr->network_ops)
824 		return -EINVAL;
825 
826 	if (mgr->network_ops->get_retimer_status)
827 		return mgr->network_ops->get_retimer_status(mgr,
828 				status);
829 
830 	return -ENOENT;
831 }
832 
833 /**
834  * opae_manager_get_sensor_by_id - get sensor device
835  * @id: the id of the sensor
836  *
837  * Return: the pointer of the opae_sensor_info
838  */
839 struct opae_sensor_info *
840 opae_mgr_get_sensor_by_id(struct opae_manager *mgr,
841 		unsigned int id)
842 {
843 	struct opae_sensor_info *sensor;
844 
845 	opae_mgr_for_each_sensor(mgr, sensor)
846 		if (sensor->id == id)
847 			return sensor;
848 
849 	return NULL;
850 }
851 
852 /**
853  * opae_manager_get_sensor_by_name - get sensor device
854  * @name: the name of the sensor
855  *
856  * Return: the pointer of the opae_sensor_info
857  */
858 struct opae_sensor_info *
859 opae_mgr_get_sensor_by_name(struct opae_manager *mgr,
860 		const char *name)
861 {
862 	struct opae_sensor_info *sensor;
863 
864 	opae_mgr_for_each_sensor(mgr, sensor)
865 		if (!strcmp(sensor->name, name))
866 			return sensor;
867 
868 	return NULL;
869 }
870 
871 /**
872  * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
873  * the value
874  * @mgr: opae_manager for sensor.
875  * @name: the name of the sensor
876  * @value: the readout sensor value
877  *
878  * Return: 0 on success, otherwise error code
879  */
880 int
881 opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
882 		const char *name, unsigned int *value)
883 {
884 	struct opae_sensor_info *sensor;
885 
886 	if (!mgr)
887 		return -EINVAL;
888 
889 	sensor = opae_mgr_get_sensor_by_name(mgr, name);
890 	if (!sensor)
891 		return -ENODEV;
892 
893 	if (mgr->ops && mgr->ops->get_sensor_value)
894 		return mgr->ops->get_sensor_value(mgr, sensor, value);
895 
896 	return -ENOENT;
897 }
898 
899 /**
900  * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
901  * value
902  * @mgr: opae_manager for sensor
903  * @id: the id of the sensor
904  * @value: the readout sensor value
905  *
906  * Return: 0 on success, otherwise error code
907  */
908 int
909 opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
910 		unsigned int id, unsigned int *value)
911 {
912 	struct opae_sensor_info *sensor;
913 
914 	if (!mgr)
915 		return -EINVAL;
916 
917 	sensor = opae_mgr_get_sensor_by_id(mgr, id);
918 	if (!sensor)
919 		return -ENODEV;
920 
921 	if (mgr->ops && mgr->ops->get_sensor_value)
922 		return mgr->ops->get_sensor_value(mgr, sensor, value);
923 
924 	return -ENOENT;
925 }
926 
927 /**
928  * opae_manager_get_sensor_value - get the current
929  * sensor value
930  * @mgr: opae_manager for sensor
931  * @sensor: opae_sensor_info for sensor
932  * @value: the readout sensor value
933  *
934  * Return: 0 on success, otherwise error code
935  */
936 int
937 opae_mgr_get_sensor_value(struct opae_manager *mgr,
938 		struct opae_sensor_info *sensor,
939 		unsigned int *value)
940 {
941 	if (!mgr || !sensor)
942 		return -EINVAL;
943 
944 	if (mgr->ops && mgr->ops->get_sensor_value)
945 		return mgr->ops->get_sensor_value(mgr, sensor, value);
946 
947 	return -ENOENT;
948 }
949 
950 /**
951  * opae_manager_get_board_info - get board info
952  * sensor value
953  * @info: opae_board_info for the card
954  *
955  * Return: 0 on success, otherwise error code
956  */
957 int
958 opae_mgr_get_board_info(struct opae_manager *mgr,
959 		struct opae_board_info **info)
960 {
961 	if (!mgr || !info)
962 		return -EINVAL;
963 
964 	if (mgr->ops && mgr->ops->get_board_info)
965 		return mgr->ops->get_board_info(mgr, info);
966 
967 	return -ENOENT;
968 }
969 
970 /**
971  * opae_mgr_get_uuid -  get manager's UUID.
972  * @mgr: targeted manager
973  * @uuid: a pointer to UUID
974  *
975  * Return: 0 on success, otherwise error code.
976  */
977 int opae_mgr_get_uuid(struct opae_manager *mgr, struct uuid *uuid)
978 {
979 	if (!mgr || !uuid)
980 		return -EINVAL;
981 
982 	if (mgr->ops && mgr->ops->get_uuid)
983 		return mgr->ops->get_uuid(mgr, uuid);
984 
985 	return -ENOENT;
986 }
987 
988 /**
989  * opae_mgr_update_flash -  update image in flash.
990  * @mgr: targeted manager
991  * @image: name of image file
992  * @status: status of update
993  *
994  * Return: 0 on success, otherwise error code.
995  */
996 int opae_mgr_update_flash(struct opae_manager *mgr, const char *image,
997 	uint64_t *status)
998 {
999 	if (!mgr)
1000 		return -EINVAL;
1001 
1002 	if (mgr->ops && mgr->ops->update_flash)
1003 		return mgr->ops->update_flash(mgr, image, status);
1004 
1005 	return -ENOENT;
1006 }
1007 
1008 /**
1009  * opae_stop_flash_update -  stop flash update.
1010  * @mgr: targeted manager
1011  * @force: make sure the update process is stopped
1012  *
1013  * Return: 0 on success, otherwise error code.
1014  */
1015 int opae_mgr_stop_flash_update(struct opae_manager *mgr, int force)
1016 {
1017 	if (!mgr)
1018 		return -EINVAL;
1019 
1020 	if (mgr->ops && mgr->ops->stop_flash_update)
1021 		return mgr->ops->stop_flash_update(mgr, force);
1022 
1023 	return -ENOENT;
1024 }
1025 
1026 /**
1027  * opae_mgr_reload -  reload FPGA.
1028  * @mgr: targeted manager
1029  * @type: FPGA type
1030  * @page: reload from which page
1031  *
1032  * Return: 0 on success, otherwise error code.
1033  */
1034 int opae_mgr_reload(struct opae_manager *mgr, int type, int page)
1035 {
1036 	if (!mgr)
1037 		return -EINVAL;
1038 
1039 	if (mgr->ops && mgr->ops->reload)
1040 		return mgr->ops->reload(mgr, type, page);
1041 
1042 	return -ENOENT;
1043 }
1044