raw/ifpga/base: enhance driver reliability in multi-process
[dpdk.git] / drivers / raw / ifpga / base / opae_hw_api.c
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 }
474
475 static void *opae_adapter_shm_alloc(struct opae_adapter *adapter)
476 {
477         char shm_name[32];
478         opae_share_data *sd;
479         u32 size = sizeof(opae_share_data);
480         int shm_id;
481         int new_shm = 0;
482
483         if (!adapter->data)
484                 return NULL;
485
486         snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
487         adapter->shm.ptr = NULL;
488
489         opae_adapter_lock(adapter, -1);
490         shm_id = opae_shm_open(shm_name, size, &new_shm);
491         if (shm_id == -1) {
492                 dev_err(NULL, "failed to open shared memory %s\n", shm_name);
493         } else {
494                 dev_info(NULL, "shared memory %s id is %d\n",
495                                 shm_name, shm_id);
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) {
501                         dev_info(NULL,
502                                         "shared memory %s address is %p\n",
503                                         shm_name, adapter->shm.ptr);
504                         if (new_shm)
505                                 opae_adapter_shm_init(adapter);
506                         sd = (opae_share_data *)adapter->shm.ptr;
507                         sd->ref_cnt++;
508                 } else {
509                         dev_err(NULL, "failed to map shared memory %s\n",
510                                         shm_name);
511                 }
512         }
513         opae_adapter_unlock(adapter);
514
515         return adapter->shm.ptr;
516 }
517
518 static void opae_adapter_shm_free(struct opae_adapter *adapter)
519 {
520         char shm_name[32];
521         opae_share_data *sd;
522         u32 ref_cnt;
523         int ret;
524
525         if (!adapter->shm.ptr)
526                 return;
527
528         sd = (opae_share_data *)adapter->shm.ptr;
529         snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
530
531         opae_adapter_lock(adapter, -1);
532         ref_cnt = --sd->ref_cnt;
533         ret = munmap(adapter->shm.ptr, adapter->shm.size);
534         if (ret == -1)
535                 dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
536         else
537                 adapter->shm.ptr = NULL;
538
539         if (ref_cnt == 0) {
540                 dev_info(NULL, "unlink shared memory %s\n", shm_name);
541                 ret = shm_unlink(shm_name);
542                 if (ret == -1) {
543                         dev_err(NULL, "failed to unlink shared memory %s\n",
544                                         shm_name);
545                 }
546         }
547         opae_adapter_unlock(adapter);
548 }
549
550 /**
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.
555  *
556  * Return: 0 on success.
557  */
558 int opae_adapter_init(struct opae_adapter *adapter,
559                 const char *name, void *data)
560 {
561         if (!adapter)
562                 return -ENOMEM;
563
564         TAILQ_INIT(&adapter->acc_list);
565         adapter->data = data;
566         adapter->name = name;
567         adapter->ops = match_ops(adapter);
568
569         if (!opae_adapter_mutex_open(adapter))
570                 return -ENOMEM;
571
572         if (!opae_adapter_shm_alloc(adapter))
573                 return -ENOMEM;
574
575         return 0;
576 }
577
578 /**
579  * opae_adapter_enumerate - enumerate this adapter
580  * @adapter: adapter to enumerate.
581  *
582  * Return: 0 on success, otherwise error code.
583  */
584 int opae_adapter_enumerate(struct opae_adapter *adapter)
585 {
586         int ret = -ENOENT;
587
588         if (!adapter)
589                 return -EINVAL;
590
591         if (adapter->ops && adapter->ops->enumerate)
592                 ret = adapter->ops->enumerate(adapter);
593
594         if (!ret)
595                 opae_adapter_dump(adapter, 0);
596
597         return ret;
598 }
599
600 /**
601  * opae_adapter_destroy - destroy this adapter
602  * @adapter: adapter to destroy.
603  *
604  * destroy things allocated during adapter enumeration.
605  */
606 void opae_adapter_destroy(struct opae_adapter *adapter)
607 {
608         if (adapter && adapter->ops && adapter->ops->destroy)
609                 adapter->ops->destroy(adapter);
610         opae_adapter_shm_free(adapter);
611         opae_adapter_mutex_close(adapter);
612 }
613
614 /**
615  * opae_adapter_get_acc - find and return accelerator with matched id
616  * @adapter: adapter to find the accelerator.
617  * @acc_id: id (index) of the accelerator.
618  *
619  * destroy things allocated during adapter enumeration.
620  */
621 struct opae_accelerator *
622 opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
623 {
624         struct opae_accelerator *acc = NULL;
625
626         if (!adapter)
627                 return NULL;
628
629         opae_adapter_for_each_acc(adapter, acc)
630                 if (acc->index == acc_id)
631                         return acc;
632
633         return NULL;
634 }
635
636 /**
637  * opae_manager_read_mac_rom - read the content of the MAC ROM
638  * @mgr: opae_manager for MAC ROM
639  * @port: the port number of retimer
640  * @addr: buffer of the MAC address
641  *
642  * Return: return the bytes of read successfully
643  */
644 int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
645                 struct opae_ether_addr *addr)
646 {
647         if (!mgr || !mgr->network_ops)
648                 return -EINVAL;
649
650         if (mgr->network_ops->read_mac_rom)
651                 return mgr->network_ops->read_mac_rom(mgr,
652                                 port * sizeof(struct opae_ether_addr),
653                                 addr, sizeof(struct opae_ether_addr));
654
655         return -ENOENT;
656 }
657
658 /**
659  * opae_manager_write_mac_rom - write data into MAC ROM
660  * @mgr: opae_manager for MAC ROM
661  * @port: the port number of the retimer
662  * @addr: data of the MAC address
663  *
664  * Return: return written bytes
665  */
666 int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
667                 struct opae_ether_addr *addr)
668 {
669         if (!mgr || !mgr->network_ops)
670                 return -EINVAL;
671
672         if (mgr->network_ops && mgr->network_ops->write_mac_rom)
673                 return mgr->network_ops->write_mac_rom(mgr,
674                                 port * sizeof(struct opae_ether_addr),
675                                 addr, sizeof(struct opae_ether_addr));
676
677         return -ENOENT;
678 }
679
680 /**
681  * opae_manager_get_eth_group_nums - get eth group numbers
682  * @mgr: opae_manager for eth group
683  *
684  * Return: the numbers of eth group
685  */
686 int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
687 {
688         if (!mgr || !mgr->network_ops)
689                 return -EINVAL;
690
691         if (mgr->network_ops->get_retimer_info)
692                 return mgr->network_ops->get_eth_group_nums(mgr);
693
694         return -ENOENT;
695 }
696
697 /**
698  * opae_manager_get_eth_group_info - get eth group info
699  * @mgr: opae_manager for eth group
700  * @group_id: id for eth group
701  * @info: info return to caller
702  *
703  * Return: 0 on success, otherwise error code
704  */
705 int opae_manager_get_eth_group_info(struct opae_manager *mgr,
706                u8 group_id, struct opae_eth_group_info *info)
707 {
708         if (!mgr || !mgr->network_ops)
709                 return -EINVAL;
710
711         if (mgr->network_ops->get_retimer_info)
712                 return mgr->network_ops->get_eth_group_info(mgr,
713                         group_id, info);
714
715         return -ENOENT;
716 }
717
718 /**
719  * opae_manager_get_eth_group_region_info
720  * @mgr: opae_manager for flash.
721  * @info: the memory region info for eth group
722  *
723  * Return: 0 on success, otherwise error code.
724  */
725 int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
726                 u8 group_id, struct opae_eth_group_region_info *info)
727 {
728         if (!mgr)
729                 return -EINVAL;
730
731         if (group_id >= MAX_ETH_GROUP_DEVICES)
732                 return -EINVAL;
733
734         info->group_id = group_id;
735
736         if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
737                 return mgr->ops->get_eth_group_region_info(mgr, info);
738
739         return -ENOENT;
740 }
741
742 /**
743  * opae_manager_eth_group_read_reg - read ETH group register
744  * @mgr: opae_manager for ETH Group
745  * @group_id: ETH group id
746  * @type: eth type
747  * @index: port index in eth group device
748  * @addr: register address of ETH Group
749  * @data: read buffer
750  *
751  * Return: 0 on success, otherwise error code
752  */
753 int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
754                 u8 type, u8 index, u16 addr, u32 *data)
755 {
756         if (!mgr || !mgr->network_ops)
757                 return -EINVAL;
758
759         if (mgr->network_ops->eth_group_reg_read)
760                 return mgr->network_ops->eth_group_reg_read(mgr, group_id,
761                                 type, index, addr, data);
762
763         return -ENOENT;
764 }
765
766 /**
767  * opae_manager_eth_group_write_reg - write ETH group register
768  * @mgr: opae_manager for ETH Group
769  * @group_id: ETH group id
770  * @type: eth type
771  * @index: port index in eth group device
772  * @addr: register address of ETH Group
773  * @data: data will write to register
774  *
775  * Return: 0 on success, otherwise error code
776  */
777 int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
778                 u8 type, u8 index, u16 addr, u32 data)
779 {
780         if (!mgr || !mgr->network_ops)
781                 return -EINVAL;
782
783         if (mgr->network_ops->eth_group_reg_write)
784                 return mgr->network_ops->eth_group_reg_write(mgr, group_id,
785                                 type, index, addr, data);
786
787         return -ENOENT;
788 }
789
790 /**
791  * opae_manager_get_retimer_info - get retimer info like PKVL chip
792  * @mgr: opae_manager for retimer
793  * @info: info return to caller
794  *
795  * Return: 0 on success, otherwise error code
796  */
797 int opae_manager_get_retimer_info(struct opae_manager *mgr,
798                struct opae_retimer_info *info)
799 {
800         if (!mgr || !mgr->network_ops)
801                 return -EINVAL;
802
803         if (mgr->network_ops->get_retimer_info)
804                 return mgr->network_ops->get_retimer_info(mgr, info);
805
806         return -ENOENT;
807 }
808
809 /**
810  * opae_manager_get_retimer_status - get retimer status
811  * @mgr: opae_manager of retimer
812  * @status: status of retimer
813  *
814  * Return: 0 on success, otherwise error code
815  */
816 int opae_manager_get_retimer_status(struct opae_manager *mgr,
817                 struct opae_retimer_status *status)
818 {
819         if (!mgr || !mgr->network_ops)
820                 return -EINVAL;
821
822         if (mgr->network_ops->get_retimer_status)
823                 return mgr->network_ops->get_retimer_status(mgr,
824                                 status);
825
826         return -ENOENT;
827 }
828
829 /**
830  * opae_manager_get_sensor_by_id - get sensor device
831  * @id: the id of the sensor
832  *
833  * Return: the pointer of the opae_sensor_info
834  */
835 struct opae_sensor_info *
836 opae_mgr_get_sensor_by_id(struct opae_manager *mgr,
837                 unsigned int id)
838 {
839         struct opae_sensor_info *sensor;
840
841         opae_mgr_for_each_sensor(mgr, sensor)
842                 if (sensor->id == id)
843                         return sensor;
844
845         return NULL;
846 }
847
848 /**
849  * opae_manager_get_sensor_by_name - get sensor device
850  * @name: the name of the sensor
851  *
852  * Return: the pointer of the opae_sensor_info
853  */
854 struct opae_sensor_info *
855 opae_mgr_get_sensor_by_name(struct opae_manager *mgr,
856                 const char *name)
857 {
858         struct opae_sensor_info *sensor;
859
860         opae_mgr_for_each_sensor(mgr, sensor)
861                 if (!strcmp(sensor->name, name))
862                         return sensor;
863
864         return NULL;
865 }
866
867 /**
868  * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
869  * the value
870  * @mgr: opae_manager for sensor.
871  * @name: the name of the sensor
872  * @value: the readout sensor value
873  *
874  * Return: 0 on success, otherwise error code
875  */
876 int
877 opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
878                 const char *name, unsigned int *value)
879 {
880         struct opae_sensor_info *sensor;
881
882         if (!mgr)
883                 return -EINVAL;
884
885         sensor = opae_mgr_get_sensor_by_name(mgr, name);
886         if (!sensor)
887                 return -ENODEV;
888
889         if (mgr->ops && mgr->ops->get_sensor_value)
890                 return mgr->ops->get_sensor_value(mgr, sensor, value);
891
892         return -ENOENT;
893 }
894
895 /**
896  * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
897  * value
898  * @mgr: opae_manager for sensor
899  * @id: the id of the sensor
900  * @value: the readout sensor value
901  *
902  * Return: 0 on success, otherwise error code
903  */
904 int
905 opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
906                 unsigned int id, unsigned int *value)
907 {
908         struct opae_sensor_info *sensor;
909
910         if (!mgr)
911                 return -EINVAL;
912
913         sensor = opae_mgr_get_sensor_by_id(mgr, id);
914         if (!sensor)
915                 return -ENODEV;
916
917         if (mgr->ops && mgr->ops->get_sensor_value)
918                 return mgr->ops->get_sensor_value(mgr, sensor, value);
919
920         return -ENOENT;
921 }
922
923 /**
924  * opae_manager_get_sensor_value - get the current
925  * sensor value
926  * @mgr: opae_manager for sensor
927  * @sensor: opae_sensor_info for sensor
928  * @value: the readout sensor value
929  *
930  * Return: 0 on success, otherwise error code
931  */
932 int
933 opae_mgr_get_sensor_value(struct opae_manager *mgr,
934                 struct opae_sensor_info *sensor,
935                 unsigned int *value)
936 {
937         if (!mgr || !sensor)
938                 return -EINVAL;
939
940         if (mgr->ops && mgr->ops->get_sensor_value)
941                 return mgr->ops->get_sensor_value(mgr, sensor, value);
942
943         return -ENOENT;
944 }
945
946 /**
947  * opae_manager_get_board_info - get board info
948  * sensor value
949  * @info: opae_board_info for the card
950  *
951  * Return: 0 on success, otherwise error code
952  */
953 int
954 opae_mgr_get_board_info(struct opae_manager *mgr,
955                 struct opae_board_info **info)
956 {
957         if (!mgr || !info)
958                 return -EINVAL;
959
960         if (mgr->ops && mgr->ops->get_board_info)
961                 return mgr->ops->get_board_info(mgr, info);
962
963         return -ENOENT;
964 }