xref: /dpdk/drivers/raw/ifpga/base/opae_hw_api.c (revision e6a2804b77c5fbfd97d0fe05ec7f959a0404a380)
1473c88f9SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2473c88f9SBruce Richardson  * Copyright(c) 2010-2018 Intel Corporation
3473c88f9SBruce Richardson  */
4473c88f9SBruce Richardson 
5e41856b5SWei Huang #include <sys/mman.h>
6e41856b5SWei Huang #include <sys/stat.h>
7e41856b5SWei Huang #include <fcntl.h>
8e41856b5SWei Huang #include <unistd.h>
9473c88f9SBruce Richardson #include "opae_hw_api.h"
10473c88f9SBruce Richardson #include "opae_debug.h"
11473c88f9SBruce Richardson #include "ifpga_api.h"
12473c88f9SBruce Richardson 
13473c88f9SBruce Richardson /* OPAE Bridge Functions */
14473c88f9SBruce Richardson 
15473c88f9SBruce Richardson /**
16473c88f9SBruce Richardson  * opae_bridge_alloc - alloc opae_bridge data structure
17473c88f9SBruce Richardson  * @name: bridge name.
18473c88f9SBruce Richardson  * @ops: ops of this bridge.
19473c88f9SBruce Richardson  * @data: private data of this bridge.
20473c88f9SBruce Richardson  *
21473c88f9SBruce Richardson  * Return opae_bridge on success, otherwise NULL.
22473c88f9SBruce Richardson  */
23473c88f9SBruce Richardson struct opae_bridge *
opae_bridge_alloc(const char * name,struct opae_bridge_ops * ops,void * data)24473c88f9SBruce Richardson opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
25473c88f9SBruce Richardson {
26473c88f9SBruce Richardson 	struct opae_bridge *br = opae_zmalloc(sizeof(*br));
27473c88f9SBruce Richardson 
28473c88f9SBruce Richardson 	if (!br)
29473c88f9SBruce Richardson 		return NULL;
30473c88f9SBruce Richardson 
31473c88f9SBruce Richardson 	br->name = name;
32473c88f9SBruce Richardson 	br->ops = ops;
33473c88f9SBruce Richardson 	br->data = data;
34473c88f9SBruce Richardson 
35473c88f9SBruce Richardson 	opae_log("%s %p\n", __func__, br);
36473c88f9SBruce Richardson 
37473c88f9SBruce Richardson 	return br;
38473c88f9SBruce Richardson }
39473c88f9SBruce Richardson 
40473c88f9SBruce Richardson /**
41473c88f9SBruce Richardson  * opae_bridge_reset -  reset opae_bridge
42473c88f9SBruce Richardson  * @br: bridge to be reset.
43473c88f9SBruce Richardson  *
44473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
45473c88f9SBruce Richardson  */
opae_bridge_reset(struct opae_bridge * br)46473c88f9SBruce Richardson int opae_bridge_reset(struct opae_bridge *br)
47473c88f9SBruce Richardson {
48473c88f9SBruce Richardson 	if (!br)
49473c88f9SBruce Richardson 		return -EINVAL;
50473c88f9SBruce Richardson 
51473c88f9SBruce Richardson 	if (br->ops && br->ops->reset)
52473c88f9SBruce Richardson 		return br->ops->reset(br);
53473c88f9SBruce Richardson 
54473c88f9SBruce Richardson 	opae_log("%s no ops\n", __func__);
55473c88f9SBruce Richardson 
56473c88f9SBruce Richardson 	return -ENOENT;
57473c88f9SBruce Richardson }
58473c88f9SBruce Richardson 
59473c88f9SBruce Richardson /* Accelerator Functions */
60473c88f9SBruce Richardson 
61473c88f9SBruce Richardson /**
62473c88f9SBruce Richardson  * opae_accelerator_alloc - alloc opae_accelerator data structure
63473c88f9SBruce Richardson  * @name: accelerator name.
64473c88f9SBruce Richardson  * @ops: ops of this accelerator.
65473c88f9SBruce Richardson  * @data: private data of this accelerator.
66473c88f9SBruce Richardson  *
67473c88f9SBruce Richardson  * Return: opae_accelerator on success, otherwise NULL.
68473c88f9SBruce Richardson  */
69473c88f9SBruce Richardson struct opae_accelerator *
opae_accelerator_alloc(const char * name,struct opae_accelerator_ops * ops,void * data)70473c88f9SBruce Richardson opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
71473c88f9SBruce Richardson 		       void *data)
72473c88f9SBruce Richardson {
73473c88f9SBruce Richardson 	struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));
74473c88f9SBruce Richardson 
75473c88f9SBruce Richardson 	if (!acc)
76473c88f9SBruce Richardson 		return NULL;
77473c88f9SBruce Richardson 
78473c88f9SBruce Richardson 	acc->name = name;
79473c88f9SBruce Richardson 	acc->ops = ops;
80473c88f9SBruce Richardson 	acc->data = data;
81473c88f9SBruce Richardson 
82473c88f9SBruce Richardson 	opae_log("%s %p\n", __func__, acc);
83473c88f9SBruce Richardson 
84473c88f9SBruce Richardson 	return acc;
85473c88f9SBruce Richardson }
86473c88f9SBruce Richardson 
87473c88f9SBruce Richardson /**
88473c88f9SBruce Richardson  * opae_acc_reg_read - read accelerator's register from its reg region.
89473c88f9SBruce Richardson  * @acc: accelerator to read.
90473c88f9SBruce Richardson  * @region_idx: reg region index.
91473c88f9SBruce Richardson  * @offset: reg offset.
92473c88f9SBruce Richardson  * @byte: read operation width, e.g 4 byte = 32bit read.
93473c88f9SBruce Richardson  * @data: data to store the value read from the register.
94473c88f9SBruce Richardson  *
95473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
96473c88f9SBruce Richardson  */
opae_acc_reg_read(struct opae_accelerator * acc,unsigned int region_idx,u64 offset,unsigned int byte,void * data)97473c88f9SBruce Richardson int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
98473c88f9SBruce Richardson 		      u64 offset, unsigned int byte, void *data)
99473c88f9SBruce Richardson {
100473c88f9SBruce Richardson 	if (!acc || !data)
101473c88f9SBruce Richardson 		return -EINVAL;
102473c88f9SBruce Richardson 
103473c88f9SBruce Richardson 	if (acc->ops && acc->ops->read)
104473c88f9SBruce Richardson 		return acc->ops->read(acc, region_idx, offset, byte, data);
105473c88f9SBruce Richardson 
106473c88f9SBruce Richardson 	return -ENOENT;
107473c88f9SBruce Richardson }
108473c88f9SBruce Richardson 
109473c88f9SBruce Richardson /**
110473c88f9SBruce Richardson  * opae_acc_reg_write - write to accelerator's register from its reg region.
111473c88f9SBruce Richardson  * @acc: accelerator to write.
112473c88f9SBruce Richardson  * @region_idx: reg region index.
113473c88f9SBruce Richardson  * @offset: reg offset.
114473c88f9SBruce Richardson  * @byte: write operation width, e.g 4 byte = 32bit write.
115473c88f9SBruce Richardson  * @data: data stored the value to write to the register.
116473c88f9SBruce Richardson  *
117473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
118473c88f9SBruce Richardson  */
opae_acc_reg_write(struct opae_accelerator * acc,unsigned int region_idx,u64 offset,unsigned int byte,void * data)119473c88f9SBruce Richardson int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
120473c88f9SBruce Richardson 		       u64 offset, unsigned int byte, void *data)
121473c88f9SBruce Richardson {
122473c88f9SBruce Richardson 	if (!acc || !data)
123473c88f9SBruce Richardson 		return -EINVAL;
124473c88f9SBruce Richardson 
125473c88f9SBruce Richardson 	if (acc->ops && acc->ops->write)
126473c88f9SBruce Richardson 		return acc->ops->write(acc, region_idx, offset, byte, data);
127473c88f9SBruce Richardson 
128473c88f9SBruce Richardson 	return -ENOENT;
129473c88f9SBruce Richardson }
130473c88f9SBruce Richardson 
131473c88f9SBruce Richardson /**
132473c88f9SBruce Richardson  * opae_acc_get_info - get information of an accelerator.
133473c88f9SBruce Richardson  * @acc: targeted accelerator
134473c88f9SBruce Richardson  * @info: accelerator info data structure to be filled.
135473c88f9SBruce Richardson  *
136473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
137473c88f9SBruce Richardson  */
opae_acc_get_info(struct opae_accelerator * acc,struct opae_acc_info * info)138473c88f9SBruce Richardson int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
139473c88f9SBruce Richardson {
140473c88f9SBruce Richardson 	if (!acc || !info)
141473c88f9SBruce Richardson 		return -EINVAL;
142473c88f9SBruce Richardson 
143473c88f9SBruce Richardson 	if (acc->ops && acc->ops->get_info)
144473c88f9SBruce Richardson 		return acc->ops->get_info(acc, info);
145473c88f9SBruce Richardson 
146473c88f9SBruce Richardson 	return -ENOENT;
147473c88f9SBruce Richardson }
148473c88f9SBruce Richardson 
149473c88f9SBruce Richardson /**
150473c88f9SBruce Richardson  * opae_acc_get_region_info - get information of an accelerator register region.
151473c88f9SBruce Richardson  * @acc: targeted accelerator
152473c88f9SBruce Richardson  * @info: accelerator region info data structure to be filled.
153473c88f9SBruce Richardson  *
154473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
155473c88f9SBruce Richardson  */
opae_acc_get_region_info(struct opae_accelerator * acc,struct opae_acc_region_info * info)156473c88f9SBruce Richardson int opae_acc_get_region_info(struct opae_accelerator *acc,
157473c88f9SBruce Richardson 			     struct opae_acc_region_info *info)
158473c88f9SBruce Richardson {
159473c88f9SBruce Richardson 	if (!acc || !info)
160473c88f9SBruce Richardson 		return -EINVAL;
161473c88f9SBruce Richardson 
162473c88f9SBruce Richardson 	if (acc->ops && acc->ops->get_region_info)
163473c88f9SBruce Richardson 		return acc->ops->get_region_info(acc, info);
164473c88f9SBruce Richardson 
165473c88f9SBruce Richardson 	return -ENOENT;
166473c88f9SBruce Richardson }
167473c88f9SBruce Richardson 
168473c88f9SBruce Richardson /**
169473c88f9SBruce Richardson  * opae_acc_set_irq -  set an accelerator's irq.
170473c88f9SBruce Richardson  * @acc: targeted accelerator
171473c88f9SBruce Richardson  * @start: start vector number
172473c88f9SBruce Richardson  * @count: count of vectors to be set from the start vector
173473c88f9SBruce Richardson  * @evtfds: event fds to be notified when corresponding irqs happens
174473c88f9SBruce Richardson  *
175473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
176473c88f9SBruce Richardson  */
opae_acc_set_irq(struct opae_accelerator * acc,u32 start,u32 count,s32 evtfds[])177473c88f9SBruce Richardson int opae_acc_set_irq(struct opae_accelerator *acc,
178473c88f9SBruce Richardson 		     u32 start, u32 count, s32 evtfds[])
179473c88f9SBruce Richardson {
180673c897fSWei Huang 	if (!acc)
181473c88f9SBruce Richardson 		return -EINVAL;
182473c88f9SBruce Richardson 
183473c88f9SBruce Richardson 	if (start + count <= start)
184473c88f9SBruce Richardson 		return -EINVAL;
185473c88f9SBruce Richardson 
186473c88f9SBruce Richardson 	if (acc->ops && acc->ops->set_irq)
187473c88f9SBruce Richardson 		return acc->ops->set_irq(acc, start, count, evtfds);
188473c88f9SBruce Richardson 
189473c88f9SBruce Richardson 	return -ENOENT;
190473c88f9SBruce Richardson }
191473c88f9SBruce Richardson 
192473c88f9SBruce Richardson /**
193473c88f9SBruce Richardson  * opae_acc_get_uuid -  get accelerator's UUID.
194473c88f9SBruce Richardson  * @acc: targeted accelerator
195473c88f9SBruce Richardson  * @uuid: a pointer to UUID
196473c88f9SBruce Richardson  *
197473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
198473c88f9SBruce Richardson  */
opae_acc_get_uuid(struct opae_accelerator * acc,struct uuid * uuid)199473c88f9SBruce Richardson int opae_acc_get_uuid(struct opae_accelerator *acc,
200473c88f9SBruce Richardson 		      struct uuid *uuid)
201473c88f9SBruce Richardson {
202473c88f9SBruce Richardson 	if (!acc || !uuid)
203473c88f9SBruce Richardson 		return -EINVAL;
204473c88f9SBruce Richardson 
205473c88f9SBruce Richardson 	if (acc->ops && acc->ops->get_uuid)
206473c88f9SBruce Richardson 		return acc->ops->get_uuid(acc, uuid);
207473c88f9SBruce Richardson 
208473c88f9SBruce Richardson 	return -ENOENT;
209473c88f9SBruce Richardson }
210473c88f9SBruce Richardson 
211473c88f9SBruce Richardson /* Manager Functions */
212473c88f9SBruce Richardson 
213473c88f9SBruce Richardson /**
214473c88f9SBruce Richardson  * opae_manager_alloc - alloc opae_manager data structure
215473c88f9SBruce Richardson  * @name: manager name.
216473c88f9SBruce Richardson  * @ops: ops of this manager.
217473c88f9SBruce Richardson  * @network_ops: ops of network management.
218473c88f9SBruce Richardson  * @data: private data of this manager.
219473c88f9SBruce Richardson  *
220473c88f9SBruce Richardson  * Return: opae_manager on success, otherwise NULL.
221473c88f9SBruce Richardson  */
222473c88f9SBruce Richardson struct opae_manager *
opae_manager_alloc(const char * name,struct opae_manager_ops * ops,struct opae_manager_networking_ops * network_ops,void * data)223473c88f9SBruce Richardson opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
224473c88f9SBruce Richardson 		struct opae_manager_networking_ops *network_ops, void *data)
225473c88f9SBruce Richardson {
226473c88f9SBruce Richardson 	struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));
227473c88f9SBruce Richardson 
228473c88f9SBruce Richardson 	if (!mgr)
229473c88f9SBruce Richardson 		return NULL;
230473c88f9SBruce Richardson 
231473c88f9SBruce Richardson 	mgr->name = name;
232473c88f9SBruce Richardson 	mgr->ops = ops;
233473c88f9SBruce Richardson 	mgr->network_ops = network_ops;
234473c88f9SBruce Richardson 	mgr->data = data;
235473c88f9SBruce Richardson 
236473c88f9SBruce Richardson 	opae_log("%s %p\n", __func__, mgr);
237473c88f9SBruce Richardson 
238473c88f9SBruce Richardson 	return mgr;
239473c88f9SBruce Richardson }
240473c88f9SBruce Richardson 
241473c88f9SBruce Richardson /**
242473c88f9SBruce Richardson  * opae_manager_flash - flash a reconfiguration image via opae_manager
243473c88f9SBruce Richardson  * @mgr: opae_manager for flash.
244473c88f9SBruce Richardson  * @id: id of target region (accelerator).
245473c88f9SBruce Richardson  * @buf: image data buffer.
246473c88f9SBruce Richardson  * @size: buffer size.
247473c88f9SBruce Richardson  * @status: status to store flash result.
248473c88f9SBruce Richardson  *
249473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
250473c88f9SBruce Richardson  */
opae_manager_flash(struct opae_manager * mgr,int id,const char * buf,u32 size,u64 * status)251473c88f9SBruce Richardson int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
252473c88f9SBruce Richardson 		u32 size, u64 *status)
253473c88f9SBruce Richardson {
254473c88f9SBruce Richardson 	if (!mgr)
255473c88f9SBruce Richardson 		return -EINVAL;
256473c88f9SBruce Richardson 
257473c88f9SBruce Richardson 	if (mgr && mgr->ops && mgr->ops->flash)
258473c88f9SBruce Richardson 		return mgr->ops->flash(mgr, id, buf, size, status);
259473c88f9SBruce Richardson 
260473c88f9SBruce Richardson 	return -ENOENT;
261473c88f9SBruce Richardson }
262473c88f9SBruce Richardson 
263473c88f9SBruce Richardson /* Adapter Functions */
264473c88f9SBruce Richardson 
265473c88f9SBruce Richardson /**
266473c88f9SBruce Richardson  * opae_adapter_data_alloc - alloc opae_adapter_data data structure
267473c88f9SBruce Richardson  * @type: opae_adapter_type.
268473c88f9SBruce Richardson  *
269473c88f9SBruce Richardson  * Return: opae_adapter_data on success, otherwise NULL.
270473c88f9SBruce Richardson  */
opae_adapter_data_alloc(enum opae_adapter_type type)271473c88f9SBruce Richardson void *opae_adapter_data_alloc(enum opae_adapter_type type)
272473c88f9SBruce Richardson {
273473c88f9SBruce Richardson 	struct opae_adapter_data *data;
274473c88f9SBruce Richardson 	int size;
275473c88f9SBruce Richardson 
276473c88f9SBruce Richardson 	switch (type) {
277473c88f9SBruce Richardson 	case OPAE_FPGA_PCI:
278473c88f9SBruce Richardson 		size = sizeof(struct opae_adapter_data_pci);
279473c88f9SBruce Richardson 		break;
280473c88f9SBruce Richardson 	case OPAE_FPGA_NET:
281473c88f9SBruce Richardson 		size = sizeof(struct opae_adapter_data_net);
282473c88f9SBruce Richardson 		break;
283473c88f9SBruce Richardson 	default:
284473c88f9SBruce Richardson 		size = sizeof(struct opae_adapter_data);
285473c88f9SBruce Richardson 		break;
286473c88f9SBruce Richardson 	}
287473c88f9SBruce Richardson 
288473c88f9SBruce Richardson 	data = opae_zmalloc(size);
289473c88f9SBruce Richardson 	if (!data)
290473c88f9SBruce Richardson 		return NULL;
291473c88f9SBruce Richardson 
292473c88f9SBruce Richardson 	data->type = type;
293473c88f9SBruce Richardson 
294473c88f9SBruce Richardson 	return data;
295473c88f9SBruce Richardson }
296473c88f9SBruce Richardson 
match_ops(struct opae_adapter * adapter)297473c88f9SBruce Richardson static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
298473c88f9SBruce Richardson {
299473c88f9SBruce Richardson 	struct opae_adapter_data *data;
300473c88f9SBruce Richardson 
301473c88f9SBruce Richardson 	if (!adapter || !adapter->data)
302473c88f9SBruce Richardson 		return NULL;
303473c88f9SBruce Richardson 
304473c88f9SBruce Richardson 	data = adapter->data;
305473c88f9SBruce Richardson 
306473c88f9SBruce Richardson 	if (data->type == OPAE_FPGA_PCI)
307473c88f9SBruce Richardson 		return &ifpga_adapter_ops;
308473c88f9SBruce Richardson 
309473c88f9SBruce Richardson 	return NULL;
310473c88f9SBruce Richardson }
311473c88f9SBruce Richardson 
opae_mutex_init(pthread_mutex_t * mutex)312e41856b5SWei Huang static void opae_mutex_init(pthread_mutex_t *mutex)
313e41856b5SWei Huang {
314e41856b5SWei Huang 	pthread_mutexattr_t mattr;
315e41856b5SWei Huang 
316e41856b5SWei Huang 	pthread_mutexattr_init(&mattr);
317e41856b5SWei Huang 	pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
318e41856b5SWei Huang 	pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
319e41856b5SWei Huang 	pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
320e41856b5SWei Huang 	pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
321e41856b5SWei Huang 	pthread_mutex_init(mutex, &mattr);
322e41856b5SWei Huang 	pthread_mutexattr_destroy(&mattr);
323e41856b5SWei Huang }
324e41856b5SWei Huang 
opae_shm_open(char * shm_name,u32 size,int * new_shm)325e41856b5SWei Huang static int opae_shm_open(char *shm_name, u32 size, int *new_shm)
326e41856b5SWei Huang {
327e41856b5SWei Huang 	int shm_id;
328e41856b5SWei Huang 	int ret;
329e41856b5SWei Huang 
330e41856b5SWei Huang 	shm_id = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0666);
331e41856b5SWei Huang 	if (shm_id == -1) {
332e41856b5SWei Huang 		if (errno == EEXIST) {
333e41856b5SWei Huang 			dev_info(NULL, "shared memory %s already exist\n",
334e41856b5SWei Huang 					shm_name);
335e41856b5SWei Huang 			shm_id = shm_open(shm_name, O_RDWR, 0666);
336e41856b5SWei Huang 		} else {
337e41856b5SWei Huang 			dev_err(NULL, "failed to create shared memory %s\n",
338e41856b5SWei Huang 					shm_name);
339e41856b5SWei Huang 			return -1;
340e41856b5SWei Huang 		}
341e41856b5SWei Huang 	} else {
342e41856b5SWei Huang 		*new_shm = 1;
343e41856b5SWei Huang 		ret = ftruncate(shm_id, size);
344e41856b5SWei Huang 		if (ret == -1) {
345e41856b5SWei Huang 			dev_err(NULL,
346e41856b5SWei Huang 					"failed to set shared memory size to %u\n",
347e41856b5SWei Huang 					size);
348e41856b5SWei Huang 			ret = shm_unlink(shm_name);
349e41856b5SWei Huang 			if (ret == -1) {
350e41856b5SWei Huang 				dev_err(NULL,
351e41856b5SWei Huang 						"failed to unlink shared memory %s\n",
352e41856b5SWei Huang 						shm_name);
353e41856b5SWei Huang 			}
354e41856b5SWei Huang 			return -1;
355e41856b5SWei Huang 		}
356e41856b5SWei Huang 	}
357e41856b5SWei Huang 
358e41856b5SWei Huang 	return shm_id;
359e41856b5SWei Huang }
360e41856b5SWei Huang 
opae_adapter_mutex_open(struct opae_adapter * adapter)361e41856b5SWei Huang static pthread_mutex_t *opae_adapter_mutex_open(struct opae_adapter *adapter)
362e41856b5SWei Huang {
363e41856b5SWei Huang 	char shm_name[32];
364e41856b5SWei Huang 	void *ptr;
365e41856b5SWei Huang 	int shm_id;
366e41856b5SWei Huang 	int new_shm = 0;
367e41856b5SWei Huang 
368e41856b5SWei Huang 	if (!adapter->data)
369e41856b5SWei Huang 		return NULL;
370e41856b5SWei Huang 	adapter->lock = NULL;
371e41856b5SWei Huang 
372e41856b5SWei Huang 	snprintf(shm_name, sizeof(shm_name), "/mutex.IFPGA:%s", adapter->name);
373e41856b5SWei Huang 	shm_id = opae_shm_open(shm_name, sizeof(pthread_mutex_t), &new_shm);
374e41856b5SWei Huang 	if (shm_id == -1) {
375e41856b5SWei Huang 		dev_err(NULL, "failed to open shared memory %s\n", shm_name);
376e41856b5SWei Huang 	} else {
377e41856b5SWei Huang 		dev_info(NULL, "shared memory %s id is %d\n",
378e41856b5SWei Huang 				shm_name, shm_id);
379e41856b5SWei Huang 		ptr = mmap(NULL, sizeof(pthread_mutex_t),
380e41856b5SWei Huang 				PROT_READ | PROT_WRITE, MAP_SHARED,
381e41856b5SWei Huang 				shm_id, 0);
382e41856b5SWei Huang 		adapter->lock = (pthread_mutex_t *)ptr;
383*e6a2804bSChengwen Feng 		if (ptr != MAP_FAILED) {
384e41856b5SWei Huang 			dev_info(NULL,
385e41856b5SWei Huang 					"shared memory %s address is %p\n",
386e41856b5SWei Huang 					shm_name, ptr);
387e41856b5SWei Huang 			if (new_shm)
388e41856b5SWei Huang 				opae_mutex_init(adapter->lock);
389e41856b5SWei Huang 		} else {
390e41856b5SWei Huang 			dev_err(NULL, "failed to map shared memory %s\n",
391e41856b5SWei Huang 					shm_name);
392e41856b5SWei Huang 		}
393e41856b5SWei Huang 	}
394e41856b5SWei Huang 
395e41856b5SWei Huang 	return adapter->lock;
396e41856b5SWei Huang }
397e41856b5SWei Huang 
opae_adapter_mutex_close(struct opae_adapter * adapter)398e41856b5SWei Huang static void opae_adapter_mutex_close(struct opae_adapter *adapter)
399e41856b5SWei Huang {
400e41856b5SWei Huang 	char shm_name[32];
401e41856b5SWei Huang 	int ret;
402e41856b5SWei Huang 
403e41856b5SWei Huang 	if (!adapter->lock)
404e41856b5SWei Huang 		return;
405e41856b5SWei Huang 
406e41856b5SWei Huang 	snprintf(shm_name, sizeof(shm_name), "/mutex.IFPGA:%s", adapter->name);
407e41856b5SWei Huang 
408e41856b5SWei Huang 	ret = munmap(adapter->lock, sizeof(pthread_mutex_t));
409e41856b5SWei Huang 	if (ret == -1)
410e41856b5SWei Huang 		dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
411e41856b5SWei Huang 	else
412e41856b5SWei Huang 		adapter->lock = NULL;
413e41856b5SWei Huang }
414e41856b5SWei Huang 
415e41856b5SWei Huang /**
416e41856b5SWei Huang  * opae_adapter_lock - lock this adapter
417e41856b5SWei Huang  * @adapter: adapter to lock.
418e41856b5SWei Huang  * @timeout: maximum time to wait for lock done
419e41856b5SWei Huang  *           -1  wait until the lock is available
420e41856b5SWei Huang  *           0   do not wait and return immediately
421e41856b5SWei Huang  *           t   positive time in second to wait
422e41856b5SWei Huang  *
423e41856b5SWei Huang  * Return: 0 on success, otherwise error code.
424e41856b5SWei Huang  */
opae_adapter_lock(struct opae_adapter * adapter,int timeout)425e41856b5SWei Huang int opae_adapter_lock(struct opae_adapter *adapter, int timeout)
426e41856b5SWei Huang {
427e41856b5SWei Huang 	struct timespec t;
428e41856b5SWei Huang 	int ret = -EINVAL;
429e41856b5SWei Huang 
430e41856b5SWei Huang 	if (adapter && adapter->lock) {
431e41856b5SWei Huang 		if (timeout < 0) {
432e41856b5SWei Huang 			ret = pthread_mutex_lock(adapter->lock);
433e41856b5SWei Huang 		} else if (timeout == 0) {
434e41856b5SWei Huang 			ret = pthread_mutex_trylock(adapter->lock);
435e41856b5SWei Huang 		} else {
436e41856b5SWei Huang 			clock_gettime(CLOCK_REALTIME, &t);
437e41856b5SWei Huang 			t.tv_sec += timeout;
438e41856b5SWei Huang 			ret = pthread_mutex_timedlock(adapter->lock, &t);
439e41856b5SWei Huang 		}
440e41856b5SWei Huang 	}
441e41856b5SWei Huang 	return ret;
442e41856b5SWei Huang }
443e41856b5SWei Huang 
444e41856b5SWei Huang /**
445e41856b5SWei Huang  * opae_adapter_unlock - unlock this adapter
446e41856b5SWei Huang  * @adapter: adapter to unlock.
447e41856b5SWei Huang  *
448e41856b5SWei Huang  * Return: 0 on success, otherwise error code.
449e41856b5SWei Huang  */
opae_adapter_unlock(struct opae_adapter * adapter)450e41856b5SWei Huang int opae_adapter_unlock(struct opae_adapter *adapter)
451e41856b5SWei Huang {
452e41856b5SWei Huang 	int ret = -EINVAL;
453e41856b5SWei Huang 
454e41856b5SWei Huang 	if (adapter && adapter->lock)
455e41856b5SWei Huang 		ret = pthread_mutex_unlock(adapter->lock);
456e41856b5SWei Huang 
457e41856b5SWei Huang 	return ret;
458e41856b5SWei Huang }
459e41856b5SWei Huang 
opae_adapter_shm_init(struct opae_adapter * adapter)460e41856b5SWei Huang static void opae_adapter_shm_init(struct opae_adapter *adapter)
461e41856b5SWei Huang {
462e41856b5SWei Huang 	opae_share_data *sd;
463e41856b5SWei Huang 
464e41856b5SWei Huang 	if (!adapter->shm.ptr)
465e41856b5SWei Huang 		return;
466e41856b5SWei Huang 
467e41856b5SWei Huang 	sd = (opae_share_data *)adapter->shm.ptr;
468e41856b5SWei Huang 	dev_info(NULL, "initialize shared memory\n");
469e41856b5SWei Huang 	opae_mutex_init(&sd->spi_mutex);
470e41856b5SWei Huang 	opae_mutex_init(&sd->i2c_mutex);
471e41856b5SWei Huang 	sd->ref_cnt = 0;
472e41856b5SWei Huang 	sd->dtb_size = SHM_BLK_SIZE;
473a05bd1b4SWei Huang 	sd->rsu_ctrl = 0;
474a05bd1b4SWei Huang 	sd->rsu_stat = 0;
475e41856b5SWei Huang }
476e41856b5SWei Huang 
opae_adapter_shm_alloc(struct opae_adapter * adapter)477e41856b5SWei Huang static void *opae_adapter_shm_alloc(struct opae_adapter *adapter)
478e41856b5SWei Huang {
479e41856b5SWei Huang 	char shm_name[32];
480e41856b5SWei Huang 	opae_share_data *sd;
481e41856b5SWei Huang 	u32 size = sizeof(opae_share_data);
482e41856b5SWei Huang 	int shm_id;
483e41856b5SWei Huang 	int new_shm = 0;
484e41856b5SWei Huang 
485e41856b5SWei Huang 	if (!adapter->data)
486e41856b5SWei Huang 		return NULL;
487e41856b5SWei Huang 
488e41856b5SWei Huang 	snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
489e41856b5SWei Huang 	adapter->shm.ptr = NULL;
490e41856b5SWei Huang 
491e41856b5SWei Huang 	opae_adapter_lock(adapter, -1);
492e41856b5SWei Huang 	shm_id = opae_shm_open(shm_name, size, &new_shm);
493e41856b5SWei Huang 	if (shm_id == -1) {
494e41856b5SWei Huang 		dev_err(NULL, "failed to open shared memory %s\n", shm_name);
495e41856b5SWei Huang 	} else {
496e41856b5SWei Huang 		dev_info(NULL, "shared memory %s id is %d\n",
497e41856b5SWei Huang 				shm_name, shm_id);
498e41856b5SWei Huang 		adapter->shm.id = shm_id;
499e41856b5SWei Huang 		adapter->shm.size = size;
500e41856b5SWei Huang 		adapter->shm.ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
501e41856b5SWei Huang 							MAP_SHARED, shm_id, 0);
502*e6a2804bSChengwen Feng 		if (adapter->shm.ptr != MAP_FAILED) {
503e41856b5SWei Huang 			dev_info(NULL,
504e41856b5SWei Huang 					"shared memory %s address is %p\n",
505e41856b5SWei Huang 					shm_name, adapter->shm.ptr);
506e41856b5SWei Huang 			if (new_shm)
507e41856b5SWei Huang 				opae_adapter_shm_init(adapter);
508e41856b5SWei Huang 			sd = (opae_share_data *)adapter->shm.ptr;
509e41856b5SWei Huang 			sd->ref_cnt++;
510e41856b5SWei Huang 		} else {
511e41856b5SWei Huang 			dev_err(NULL, "failed to map shared memory %s\n",
512e41856b5SWei Huang 					shm_name);
513e41856b5SWei Huang 		}
514e41856b5SWei Huang 	}
515e41856b5SWei Huang 	opae_adapter_unlock(adapter);
516e41856b5SWei Huang 
517e41856b5SWei Huang 	return adapter->shm.ptr;
518e41856b5SWei Huang }
519e41856b5SWei Huang 
opae_adapter_shm_free(struct opae_adapter * adapter)520e41856b5SWei Huang static void opae_adapter_shm_free(struct opae_adapter *adapter)
521e41856b5SWei Huang {
522e41856b5SWei Huang 	char shm_name[32];
523e41856b5SWei Huang 	opae_share_data *sd;
524e41856b5SWei Huang 	u32 ref_cnt;
525e41856b5SWei Huang 	int ret;
526e41856b5SWei Huang 
527e41856b5SWei Huang 	if (!adapter->shm.ptr)
528e41856b5SWei Huang 		return;
529e41856b5SWei Huang 
530e41856b5SWei Huang 	sd = (opae_share_data *)adapter->shm.ptr;
531e41856b5SWei Huang 	snprintf(shm_name, sizeof(shm_name), "/IFPGA:%s", adapter->name);
532e41856b5SWei Huang 
533e41856b5SWei Huang 	opae_adapter_lock(adapter, -1);
534e41856b5SWei Huang 	ref_cnt = --sd->ref_cnt;
535e41856b5SWei Huang 	ret = munmap(adapter->shm.ptr, adapter->shm.size);
536e41856b5SWei Huang 	if (ret == -1)
537e41856b5SWei Huang 		dev_err(NULL, "failed to unmap shared memory %s\n", shm_name);
538e41856b5SWei Huang 	else
539e41856b5SWei Huang 		adapter->shm.ptr = NULL;
540e41856b5SWei Huang 
541e41856b5SWei Huang 	if (ref_cnt == 0) {
542e41856b5SWei Huang 		dev_info(NULL, "unlink shared memory %s\n", shm_name);
543e41856b5SWei Huang 		ret = shm_unlink(shm_name);
544e41856b5SWei Huang 		if (ret == -1) {
545e41856b5SWei Huang 			dev_err(NULL, "failed to unlink shared memory %s\n",
546e41856b5SWei Huang 					shm_name);
547e41856b5SWei Huang 		}
548e41856b5SWei Huang 	}
549e41856b5SWei Huang 	opae_adapter_unlock(adapter);
550e41856b5SWei Huang }
551e41856b5SWei Huang 
552473c88f9SBruce Richardson /**
553473c88f9SBruce Richardson  * opae_adapter_init - init opae_adapter data structure
554473c88f9SBruce Richardson  * @adapter: pointer of opae_adapter data structure
555473c88f9SBruce Richardson  * @name: adapter name.
556473c88f9SBruce Richardson  * @data: private data of this adapter.
557473c88f9SBruce Richardson  *
558473c88f9SBruce Richardson  * Return: 0 on success.
559473c88f9SBruce Richardson  */
opae_adapter_init(struct opae_adapter * adapter,const char * name,void * data)560473c88f9SBruce Richardson int opae_adapter_init(struct opae_adapter *adapter,
561473c88f9SBruce Richardson 		const char *name, void *data)
562473c88f9SBruce Richardson {
563473c88f9SBruce Richardson 	if (!adapter)
564473c88f9SBruce Richardson 		return -ENOMEM;
565473c88f9SBruce Richardson 
566473c88f9SBruce Richardson 	TAILQ_INIT(&adapter->acc_list);
567473c88f9SBruce Richardson 	adapter->data = data;
568473c88f9SBruce Richardson 	adapter->name = name;
569473c88f9SBruce Richardson 	adapter->ops = match_ops(adapter);
570473c88f9SBruce Richardson 
571e41856b5SWei Huang 	if (!opae_adapter_mutex_open(adapter))
572e41856b5SWei Huang 		return -ENOMEM;
573e41856b5SWei Huang 
574e41856b5SWei Huang 	if (!opae_adapter_shm_alloc(adapter))
575e41856b5SWei Huang 		return -ENOMEM;
576e41856b5SWei Huang 
577473c88f9SBruce Richardson 	return 0;
578473c88f9SBruce Richardson }
579473c88f9SBruce Richardson 
580473c88f9SBruce Richardson /**
581473c88f9SBruce Richardson  * opae_adapter_enumerate - enumerate this adapter
582473c88f9SBruce Richardson  * @adapter: adapter to enumerate.
583473c88f9SBruce Richardson  *
584473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
585473c88f9SBruce Richardson  */
opae_adapter_enumerate(struct opae_adapter * adapter)586473c88f9SBruce Richardson int opae_adapter_enumerate(struct opae_adapter *adapter)
587473c88f9SBruce Richardson {
588473c88f9SBruce Richardson 	int ret = -ENOENT;
589473c88f9SBruce Richardson 
590473c88f9SBruce Richardson 	if (!adapter)
591473c88f9SBruce Richardson 		return -EINVAL;
592473c88f9SBruce Richardson 
593473c88f9SBruce Richardson 	if (adapter->ops && adapter->ops->enumerate)
594473c88f9SBruce Richardson 		ret = adapter->ops->enumerate(adapter);
595473c88f9SBruce Richardson 
596473c88f9SBruce Richardson 	if (!ret)
597473c88f9SBruce Richardson 		opae_adapter_dump(adapter, 0);
598473c88f9SBruce Richardson 
599473c88f9SBruce Richardson 	return ret;
600473c88f9SBruce Richardson }
601473c88f9SBruce Richardson 
602473c88f9SBruce Richardson /**
603473c88f9SBruce Richardson  * opae_adapter_destroy - destroy this adapter
604473c88f9SBruce Richardson  * @adapter: adapter to destroy.
605473c88f9SBruce Richardson  *
606473c88f9SBruce Richardson  * destroy things allocated during adapter enumeration.
607473c88f9SBruce Richardson  */
opae_adapter_destroy(struct opae_adapter * adapter)608473c88f9SBruce Richardson void opae_adapter_destroy(struct opae_adapter *adapter)
609473c88f9SBruce Richardson {
610e6dc14c8SWei Huang 	if (adapter) {
611e6dc14c8SWei Huang 		if (adapter->ops && adapter->ops->destroy)
612473c88f9SBruce Richardson 			adapter->ops->destroy(adapter);
613e41856b5SWei Huang 		opae_adapter_shm_free(adapter);
614e41856b5SWei Huang 		opae_adapter_mutex_close(adapter);
615473c88f9SBruce Richardson 	}
616e6dc14c8SWei Huang }
617473c88f9SBruce Richardson 
618473c88f9SBruce Richardson /**
619473c88f9SBruce Richardson  * opae_adapter_get_acc - find and return accelerator with matched id
620473c88f9SBruce Richardson  * @adapter: adapter to find the accelerator.
621473c88f9SBruce Richardson  * @acc_id: id (index) of the accelerator.
622473c88f9SBruce Richardson  *
623473c88f9SBruce Richardson  * destroy things allocated during adapter enumeration.
624473c88f9SBruce Richardson  */
625473c88f9SBruce Richardson struct opae_accelerator *
opae_adapter_get_acc(struct opae_adapter * adapter,int acc_id)626473c88f9SBruce Richardson opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
627473c88f9SBruce Richardson {
628473c88f9SBruce Richardson 	struct opae_accelerator *acc = NULL;
629473c88f9SBruce Richardson 
630473c88f9SBruce Richardson 	if (!adapter)
631473c88f9SBruce Richardson 		return NULL;
632473c88f9SBruce Richardson 
633473c88f9SBruce Richardson 	opae_adapter_for_each_acc(adapter, acc)
634473c88f9SBruce Richardson 		if (acc->index == acc_id)
635473c88f9SBruce Richardson 			return acc;
636473c88f9SBruce Richardson 
637473c88f9SBruce Richardson 	return NULL;
638473c88f9SBruce Richardson }
639473c88f9SBruce Richardson 
640473c88f9SBruce Richardson /**
641473c88f9SBruce Richardson  * opae_manager_read_mac_rom - read the content of the MAC ROM
642473c88f9SBruce Richardson  * @mgr: opae_manager for MAC ROM
643473c88f9SBruce Richardson  * @port: the port number of retimer
644473c88f9SBruce Richardson  * @addr: buffer of the MAC address
645473c88f9SBruce Richardson  *
646473c88f9SBruce Richardson  * Return: return the bytes of read successfully
647473c88f9SBruce Richardson  */
opae_manager_read_mac_rom(struct opae_manager * mgr,int port,struct opae_ether_addr * addr)648473c88f9SBruce Richardson int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
649473c88f9SBruce Richardson 		struct opae_ether_addr *addr)
650473c88f9SBruce Richardson {
651473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
652473c88f9SBruce Richardson 		return -EINVAL;
653473c88f9SBruce Richardson 
654473c88f9SBruce Richardson 	if (mgr->network_ops->read_mac_rom)
655473c88f9SBruce Richardson 		return mgr->network_ops->read_mac_rom(mgr,
656473c88f9SBruce Richardson 				port * sizeof(struct opae_ether_addr),
657473c88f9SBruce Richardson 				addr, sizeof(struct opae_ether_addr));
658473c88f9SBruce Richardson 
659473c88f9SBruce Richardson 	return -ENOENT;
660473c88f9SBruce Richardson }
661473c88f9SBruce Richardson 
662473c88f9SBruce Richardson /**
663473c88f9SBruce Richardson  * opae_manager_write_mac_rom - write data into MAC ROM
664473c88f9SBruce Richardson  * @mgr: opae_manager for MAC ROM
665473c88f9SBruce Richardson  * @port: the port number of the retimer
666473c88f9SBruce Richardson  * @addr: data of the MAC address
667473c88f9SBruce Richardson  *
668473c88f9SBruce Richardson  * Return: return written bytes
669473c88f9SBruce Richardson  */
opae_manager_write_mac_rom(struct opae_manager * mgr,int port,struct opae_ether_addr * addr)670473c88f9SBruce Richardson int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
671473c88f9SBruce Richardson 		struct opae_ether_addr *addr)
672473c88f9SBruce Richardson {
673473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
674473c88f9SBruce Richardson 		return -EINVAL;
675473c88f9SBruce Richardson 
676473c88f9SBruce Richardson 	if (mgr->network_ops && mgr->network_ops->write_mac_rom)
677473c88f9SBruce Richardson 		return mgr->network_ops->write_mac_rom(mgr,
678473c88f9SBruce Richardson 				port * sizeof(struct opae_ether_addr),
679473c88f9SBruce Richardson 				addr, sizeof(struct opae_ether_addr));
680473c88f9SBruce Richardson 
681473c88f9SBruce Richardson 	return -ENOENT;
682473c88f9SBruce Richardson }
683473c88f9SBruce Richardson 
684473c88f9SBruce Richardson /**
685473c88f9SBruce Richardson  * opae_manager_get_eth_group_nums - get eth group numbers
686473c88f9SBruce Richardson  * @mgr: opae_manager for eth group
687473c88f9SBruce Richardson  *
688473c88f9SBruce Richardson  * Return: the numbers of eth group
689473c88f9SBruce Richardson  */
opae_manager_get_eth_group_nums(struct opae_manager * mgr)690473c88f9SBruce Richardson int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
691473c88f9SBruce Richardson {
692473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
693473c88f9SBruce Richardson 		return -EINVAL;
694473c88f9SBruce Richardson 
695473c88f9SBruce Richardson 	if (mgr->network_ops->get_retimer_info)
696473c88f9SBruce Richardson 		return mgr->network_ops->get_eth_group_nums(mgr);
697473c88f9SBruce Richardson 
698473c88f9SBruce Richardson 	return -ENOENT;
699473c88f9SBruce Richardson }
700473c88f9SBruce Richardson 
701473c88f9SBruce Richardson /**
702473c88f9SBruce Richardson  * opae_manager_get_eth_group_info - get eth group info
703473c88f9SBruce Richardson  * @mgr: opae_manager for eth group
704473c88f9SBruce Richardson  * @group_id: id for eth group
705473c88f9SBruce Richardson  * @info: info return to caller
706473c88f9SBruce Richardson  *
707473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code
708473c88f9SBruce Richardson  */
opae_manager_get_eth_group_info(struct opae_manager * mgr,u8 group_id,struct opae_eth_group_info * info)709473c88f9SBruce Richardson int opae_manager_get_eth_group_info(struct opae_manager *mgr,
710473c88f9SBruce Richardson 	       u8 group_id, struct opae_eth_group_info *info)
711473c88f9SBruce Richardson {
712473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
713473c88f9SBruce Richardson 		return -EINVAL;
714473c88f9SBruce Richardson 
715473c88f9SBruce Richardson 	if (mgr->network_ops->get_retimer_info)
716473c88f9SBruce Richardson 		return mgr->network_ops->get_eth_group_info(mgr,
717473c88f9SBruce Richardson 			group_id, info);
718473c88f9SBruce Richardson 
719473c88f9SBruce Richardson 	return -ENOENT;
720473c88f9SBruce Richardson }
721473c88f9SBruce Richardson 
722473c88f9SBruce Richardson /**
723473c88f9SBruce Richardson  * opae_manager_get_eth_group_region_info
724473c88f9SBruce Richardson  * @mgr: opae_manager for flash.
725473c88f9SBruce Richardson  * @info: the memory region info for eth group
726473c88f9SBruce Richardson  *
727473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code.
728473c88f9SBruce Richardson  */
opae_manager_get_eth_group_region_info(struct opae_manager * mgr,u8 group_id,struct opae_eth_group_region_info * info)729473c88f9SBruce Richardson int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
730473c88f9SBruce Richardson 		u8 group_id, struct opae_eth_group_region_info *info)
731473c88f9SBruce Richardson {
732473c88f9SBruce Richardson 	if (!mgr)
733473c88f9SBruce Richardson 		return -EINVAL;
734473c88f9SBruce Richardson 
735473c88f9SBruce Richardson 	if (group_id >= MAX_ETH_GROUP_DEVICES)
736473c88f9SBruce Richardson 		return -EINVAL;
737473c88f9SBruce Richardson 
738473c88f9SBruce Richardson 	info->group_id = group_id;
739473c88f9SBruce Richardson 
740473c88f9SBruce Richardson 	if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
741473c88f9SBruce Richardson 		return mgr->ops->get_eth_group_region_info(mgr, info);
742473c88f9SBruce Richardson 
743473c88f9SBruce Richardson 	return -ENOENT;
744473c88f9SBruce Richardson }
745473c88f9SBruce Richardson 
746473c88f9SBruce Richardson /**
747473c88f9SBruce Richardson  * opae_manager_eth_group_read_reg - read ETH group register
748473c88f9SBruce Richardson  * @mgr: opae_manager for ETH Group
749473c88f9SBruce Richardson  * @group_id: ETH group id
750473c88f9SBruce Richardson  * @type: eth type
751473c88f9SBruce Richardson  * @index: port index in eth group device
752473c88f9SBruce Richardson  * @addr: register address of ETH Group
753473c88f9SBruce Richardson  * @data: read buffer
754473c88f9SBruce Richardson  *
755473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code
756473c88f9SBruce Richardson  */
opae_manager_eth_group_read_reg(struct opae_manager * mgr,u8 group_id,u8 type,u8 index,u16 addr,u32 * data)757473c88f9SBruce Richardson int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
758473c88f9SBruce Richardson 		u8 type, u8 index, u16 addr, u32 *data)
759473c88f9SBruce Richardson {
760473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
761473c88f9SBruce Richardson 		return -EINVAL;
762473c88f9SBruce Richardson 
763473c88f9SBruce Richardson 	if (mgr->network_ops->eth_group_reg_read)
764473c88f9SBruce Richardson 		return mgr->network_ops->eth_group_reg_read(mgr, group_id,
765473c88f9SBruce Richardson 				type, index, addr, data);
766473c88f9SBruce Richardson 
767473c88f9SBruce Richardson 	return -ENOENT;
768473c88f9SBruce Richardson }
769473c88f9SBruce Richardson 
770473c88f9SBruce Richardson /**
771473c88f9SBruce Richardson  * opae_manager_eth_group_write_reg - write ETH group register
772473c88f9SBruce Richardson  * @mgr: opae_manager for ETH Group
773473c88f9SBruce Richardson  * @group_id: ETH group id
774473c88f9SBruce Richardson  * @type: eth type
775473c88f9SBruce Richardson  * @index: port index in eth group device
776473c88f9SBruce Richardson  * @addr: register address of ETH Group
777473c88f9SBruce Richardson  * @data: data will write to register
778473c88f9SBruce Richardson  *
779473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code
780473c88f9SBruce Richardson  */
opae_manager_eth_group_write_reg(struct opae_manager * mgr,u8 group_id,u8 type,u8 index,u16 addr,u32 data)781473c88f9SBruce Richardson int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
782473c88f9SBruce Richardson 		u8 type, u8 index, u16 addr, u32 data)
783473c88f9SBruce Richardson {
784473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
785473c88f9SBruce Richardson 		return -EINVAL;
786473c88f9SBruce Richardson 
787473c88f9SBruce Richardson 	if (mgr->network_ops->eth_group_reg_write)
788473c88f9SBruce Richardson 		return mgr->network_ops->eth_group_reg_write(mgr, group_id,
789473c88f9SBruce Richardson 				type, index, addr, data);
790473c88f9SBruce Richardson 
791473c88f9SBruce Richardson 	return -ENOENT;
792473c88f9SBruce Richardson }
793473c88f9SBruce Richardson 
794473c88f9SBruce Richardson /**
795473c88f9SBruce Richardson  * opae_manager_get_retimer_info - get retimer info like PKVL chip
796473c88f9SBruce Richardson  * @mgr: opae_manager for retimer
797473c88f9SBruce Richardson  * @info: info return to caller
798473c88f9SBruce Richardson  *
799473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code
800473c88f9SBruce Richardson  */
opae_manager_get_retimer_info(struct opae_manager * mgr,struct opae_retimer_info * info)801473c88f9SBruce Richardson int opae_manager_get_retimer_info(struct opae_manager *mgr,
802473c88f9SBruce Richardson 	       struct opae_retimer_info *info)
803473c88f9SBruce Richardson {
804473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
805473c88f9SBruce Richardson 		return -EINVAL;
806473c88f9SBruce Richardson 
807473c88f9SBruce Richardson 	if (mgr->network_ops->get_retimer_info)
808473c88f9SBruce Richardson 		return mgr->network_ops->get_retimer_info(mgr, info);
809473c88f9SBruce Richardson 
810473c88f9SBruce Richardson 	return -ENOENT;
811473c88f9SBruce Richardson }
812473c88f9SBruce Richardson 
813473c88f9SBruce Richardson /**
814473c88f9SBruce Richardson  * opae_manager_get_retimer_status - get retimer status
815473c88f9SBruce Richardson  * @mgr: opae_manager of retimer
816473c88f9SBruce Richardson  * @status: status of retimer
817473c88f9SBruce Richardson  *
818473c88f9SBruce Richardson  * Return: 0 on success, otherwise error code
819473c88f9SBruce Richardson  */
opae_manager_get_retimer_status(struct opae_manager * mgr,struct opae_retimer_status * status)820473c88f9SBruce Richardson int opae_manager_get_retimer_status(struct opae_manager *mgr,
821473c88f9SBruce Richardson 		struct opae_retimer_status *status)
822473c88f9SBruce Richardson {
823473c88f9SBruce Richardson 	if (!mgr || !mgr->network_ops)
824473c88f9SBruce Richardson 		return -EINVAL;
825473c88f9SBruce Richardson 
826473c88f9SBruce Richardson 	if (mgr->network_ops->get_retimer_status)
827473c88f9SBruce Richardson 		return mgr->network_ops->get_retimer_status(mgr,
828473c88f9SBruce Richardson 				status);
829473c88f9SBruce Richardson 
830473c88f9SBruce Richardson 	return -ENOENT;
831473c88f9SBruce Richardson }
83219118989STianfei Zhang 
83319118989STianfei Zhang /**
834528a9fc2SWei Huang  * opae_manager_get_sensor_list - get sensor name list
835528a9fc2SWei Huang  * @mgr: opae_manager of sensors
836528a9fc2SWei Huang  * @buf: buffer to accommodate name list separated by semicolon
837528a9fc2SWei Huang  * @size: size of buffer
838528a9fc2SWei Huang  *
839528a9fc2SWei Huang  * Return: the pointer of the opae_sensor_info
840528a9fc2SWei Huang  */
841528a9fc2SWei Huang int
opae_mgr_get_sensor_list(struct opae_manager * mgr,char * buf,size_t size)842528a9fc2SWei Huang opae_mgr_get_sensor_list(struct opae_manager *mgr, char *buf, size_t size)
843528a9fc2SWei Huang {
844528a9fc2SWei Huang 	struct opae_sensor_info *sensor;
845528a9fc2SWei Huang 	uint32_t offset = 0;
846528a9fc2SWei Huang 
847528a9fc2SWei Huang 	opae_mgr_for_each_sensor(mgr, sensor) {
848528a9fc2SWei Huang 		if (sensor->name) {
849528a9fc2SWei Huang 			if (buf && (offset < size))
850528a9fc2SWei Huang 				snprintf(buf + offset, size - offset, "%s;",
851528a9fc2SWei Huang 					sensor->name);
852528a9fc2SWei Huang 			offset += strlen(sensor->name) + 1;
853528a9fc2SWei Huang 		}
854528a9fc2SWei Huang 	}
855528a9fc2SWei Huang 
856528a9fc2SWei Huang 	if (buf && (offset > 0) && (offset <= size))
857528a9fc2SWei Huang 		buf[offset-1] = 0;
858528a9fc2SWei Huang 
859528a9fc2SWei Huang 	return offset;
860528a9fc2SWei Huang }
861528a9fc2SWei Huang 
862528a9fc2SWei Huang /**
86319118989STianfei Zhang  * opae_manager_get_sensor_by_id - get sensor device
86419118989STianfei Zhang  * @id: the id of the sensor
86519118989STianfei Zhang  *
86619118989STianfei Zhang  * Return: the pointer of the opae_sensor_info
86719118989STianfei Zhang  */
86819118989STianfei Zhang struct opae_sensor_info *
opae_mgr_get_sensor_by_id(struct opae_manager * mgr,unsigned int id)8694a19f891STianfei Zhang opae_mgr_get_sensor_by_id(struct opae_manager *mgr,
8704a19f891STianfei Zhang 		unsigned int id)
87119118989STianfei Zhang {
87219118989STianfei Zhang 	struct opae_sensor_info *sensor;
87319118989STianfei Zhang 
8744a19f891STianfei Zhang 	opae_mgr_for_each_sensor(mgr, sensor)
87519118989STianfei Zhang 		if (sensor->id == id)
87619118989STianfei Zhang 			return sensor;
87719118989STianfei Zhang 
87819118989STianfei Zhang 	return NULL;
87919118989STianfei Zhang }
88019118989STianfei Zhang 
88119118989STianfei Zhang /**
88219118989STianfei Zhang  * opae_manager_get_sensor_by_name - get sensor device
88319118989STianfei Zhang  * @name: the name of the sensor
88419118989STianfei Zhang  *
88519118989STianfei Zhang  * Return: the pointer of the opae_sensor_info
88619118989STianfei Zhang  */
88719118989STianfei Zhang struct opae_sensor_info *
opae_mgr_get_sensor_by_name(struct opae_manager * mgr,const char * name)8884a19f891STianfei Zhang opae_mgr_get_sensor_by_name(struct opae_manager *mgr,
8894a19f891STianfei Zhang 		const char *name)
89019118989STianfei Zhang {
89119118989STianfei Zhang 	struct opae_sensor_info *sensor;
89219118989STianfei Zhang 
8934a19f891STianfei Zhang 	opae_mgr_for_each_sensor(mgr, sensor)
89419118989STianfei Zhang 		if (!strcmp(sensor->name, name))
89519118989STianfei Zhang 			return sensor;
89619118989STianfei Zhang 
89719118989STianfei Zhang 	return NULL;
89819118989STianfei Zhang }
89919118989STianfei Zhang 
90019118989STianfei Zhang /**
90119118989STianfei Zhang  * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
90219118989STianfei Zhang  * the value
90319118989STianfei Zhang  * @mgr: opae_manager for sensor.
90419118989STianfei Zhang  * @name: the name of the sensor
90519118989STianfei Zhang  * @value: the readout sensor value
90619118989STianfei Zhang  *
90719118989STianfei Zhang  * Return: 0 on success, otherwise error code
90819118989STianfei Zhang  */
90919118989STianfei Zhang int
opae_mgr_get_sensor_value_by_name(struct opae_manager * mgr,const char * name,unsigned int * value)91019118989STianfei Zhang opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
91119118989STianfei Zhang 		const char *name, unsigned int *value)
91219118989STianfei Zhang {
91319118989STianfei Zhang 	struct opae_sensor_info *sensor;
91419118989STianfei Zhang 
91519118989STianfei Zhang 	if (!mgr)
91619118989STianfei Zhang 		return -EINVAL;
91719118989STianfei Zhang 
9184a19f891STianfei Zhang 	sensor = opae_mgr_get_sensor_by_name(mgr, name);
91919118989STianfei Zhang 	if (!sensor)
92019118989STianfei Zhang 		return -ENODEV;
92119118989STianfei Zhang 
92219118989STianfei Zhang 	if (mgr->ops && mgr->ops->get_sensor_value)
92319118989STianfei Zhang 		return mgr->ops->get_sensor_value(mgr, sensor, value);
92419118989STianfei Zhang 
92519118989STianfei Zhang 	return -ENOENT;
92619118989STianfei Zhang }
92719118989STianfei Zhang 
92819118989STianfei Zhang /**
92919118989STianfei Zhang  * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
93019118989STianfei Zhang  * value
93119118989STianfei Zhang  * @mgr: opae_manager for sensor
93219118989STianfei Zhang  * @id: the id of the sensor
93319118989STianfei Zhang  * @value: the readout sensor value
93419118989STianfei Zhang  *
93519118989STianfei Zhang  * Return: 0 on success, otherwise error code
93619118989STianfei Zhang  */
93719118989STianfei Zhang int
opae_mgr_get_sensor_value_by_id(struct opae_manager * mgr,unsigned int id,unsigned int * value)93819118989STianfei Zhang opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
93919118989STianfei Zhang 		unsigned int id, unsigned int *value)
94019118989STianfei Zhang {
94119118989STianfei Zhang 	struct opae_sensor_info *sensor;
94219118989STianfei Zhang 
94319118989STianfei Zhang 	if (!mgr)
94419118989STianfei Zhang 		return -EINVAL;
94519118989STianfei Zhang 
9464a19f891STianfei Zhang 	sensor = opae_mgr_get_sensor_by_id(mgr, id);
94719118989STianfei Zhang 	if (!sensor)
94819118989STianfei Zhang 		return -ENODEV;
94919118989STianfei Zhang 
95019118989STianfei Zhang 	if (mgr->ops && mgr->ops->get_sensor_value)
95119118989STianfei Zhang 		return mgr->ops->get_sensor_value(mgr, sensor, value);
95219118989STianfei Zhang 
95319118989STianfei Zhang 	return -ENOENT;
95419118989STianfei Zhang }
95519118989STianfei Zhang 
95619118989STianfei Zhang /**
95719118989STianfei Zhang  * opae_manager_get_sensor_value - get the current
95819118989STianfei Zhang  * sensor value
95919118989STianfei Zhang  * @mgr: opae_manager for sensor
96019118989STianfei Zhang  * @sensor: opae_sensor_info for sensor
96119118989STianfei Zhang  * @value: the readout sensor value
96219118989STianfei Zhang  *
96319118989STianfei Zhang  * Return: 0 on success, otherwise error code
96419118989STianfei Zhang  */
96519118989STianfei Zhang int
opae_mgr_get_sensor_value(struct opae_manager * mgr,struct opae_sensor_info * sensor,unsigned int * value)96619118989STianfei Zhang opae_mgr_get_sensor_value(struct opae_manager *mgr,
96719118989STianfei Zhang 		struct opae_sensor_info *sensor,
96819118989STianfei Zhang 		unsigned int *value)
96919118989STianfei Zhang {
97019118989STianfei Zhang 	if (!mgr || !sensor)
97119118989STianfei Zhang 		return -EINVAL;
97219118989STianfei Zhang 
97319118989STianfei Zhang 	if (mgr->ops && mgr->ops->get_sensor_value)
97419118989STianfei Zhang 		return mgr->ops->get_sensor_value(mgr, sensor, value);
97519118989STianfei Zhang 
97619118989STianfei Zhang 	return -ENOENT;
97719118989STianfei Zhang }
978c127953fSTianfei Zhang 
979c127953fSTianfei Zhang /**
980c127953fSTianfei Zhang  * opae_manager_get_board_info - get board info
981c127953fSTianfei Zhang  * sensor value
982c127953fSTianfei Zhang  * @info: opae_board_info for the card
983c127953fSTianfei Zhang  *
984c127953fSTianfei Zhang  * Return: 0 on success, otherwise error code
985c127953fSTianfei Zhang  */
986c127953fSTianfei Zhang int
opae_mgr_get_board_info(struct opae_manager * mgr,struct opae_board_info ** info)987c127953fSTianfei Zhang opae_mgr_get_board_info(struct opae_manager *mgr,
988c127953fSTianfei Zhang 		struct opae_board_info **info)
989c127953fSTianfei Zhang {
990c127953fSTianfei Zhang 	if (!mgr || !info)
991c127953fSTianfei Zhang 		return -EINVAL;
992c127953fSTianfei Zhang 
993c127953fSTianfei Zhang 	if (mgr->ops && mgr->ops->get_board_info)
994c127953fSTianfei Zhang 		return mgr->ops->get_board_info(mgr, info);
995c127953fSTianfei Zhang 
996c127953fSTianfei Zhang 	return -ENOENT;
997c127953fSTianfei Zhang }
998a05bd1b4SWei Huang 
999a05bd1b4SWei Huang /**
1000cf38bcd7SWei Huang  * opae_mgr_get_uuid -  get manager's UUID.
1001cf38bcd7SWei Huang  * @mgr: targeted manager
1002cf38bcd7SWei Huang  * @uuid: a pointer to UUID
1003cf38bcd7SWei Huang  *
1004cf38bcd7SWei Huang  * Return: 0 on success, otherwise error code.
1005cf38bcd7SWei Huang  */
opae_mgr_get_uuid(struct opae_manager * mgr,struct uuid * uuid)1006cf38bcd7SWei Huang int opae_mgr_get_uuid(struct opae_manager *mgr, struct uuid *uuid)
1007cf38bcd7SWei Huang {
1008cf38bcd7SWei Huang 	if (!mgr || !uuid)
1009cf38bcd7SWei Huang 		return -EINVAL;
1010cf38bcd7SWei Huang 
1011cf38bcd7SWei Huang 	if (mgr->ops && mgr->ops->get_uuid)
1012cf38bcd7SWei Huang 		return mgr->ops->get_uuid(mgr, uuid);
1013cf38bcd7SWei Huang 
1014cf38bcd7SWei Huang 	return -ENOENT;
1015cf38bcd7SWei Huang }
1016cf38bcd7SWei Huang 
1017cf38bcd7SWei Huang /**
1018a05bd1b4SWei Huang  * opae_mgr_update_flash -  update image in flash.
1019a05bd1b4SWei Huang  * @mgr: targeted manager
1020a05bd1b4SWei Huang  * @image: name of image file
1021a05bd1b4SWei Huang  * @status: status of update
1022a05bd1b4SWei Huang  *
1023a05bd1b4SWei Huang  * Return: 0 on success, otherwise error code.
1024a05bd1b4SWei Huang  */
opae_mgr_update_flash(struct opae_manager * mgr,const char * image,uint64_t * status)1025a05bd1b4SWei Huang int opae_mgr_update_flash(struct opae_manager *mgr, const char *image,
1026a05bd1b4SWei Huang 	uint64_t *status)
1027a05bd1b4SWei Huang {
1028a05bd1b4SWei Huang 	if (!mgr)
1029a05bd1b4SWei Huang 		return -EINVAL;
1030a05bd1b4SWei Huang 
1031a05bd1b4SWei Huang 	if (mgr->ops && mgr->ops->update_flash)
1032a05bd1b4SWei Huang 		return mgr->ops->update_flash(mgr, image, status);
1033a05bd1b4SWei Huang 
1034a05bd1b4SWei Huang 	return -ENOENT;
1035a05bd1b4SWei Huang }
1036a05bd1b4SWei Huang 
1037a05bd1b4SWei Huang /**
1038a05bd1b4SWei Huang  * opae_stop_flash_update -  stop flash update.
1039a05bd1b4SWei Huang  * @mgr: targeted manager
1040a05bd1b4SWei Huang  * @force: make sure the update process is stopped
1041a05bd1b4SWei Huang  *
1042a05bd1b4SWei Huang  * Return: 0 on success, otherwise error code.
1043a05bd1b4SWei Huang  */
opae_mgr_stop_flash_update(struct opae_manager * mgr,int force)1044a05bd1b4SWei Huang int opae_mgr_stop_flash_update(struct opae_manager *mgr, int force)
1045a05bd1b4SWei Huang {
1046a05bd1b4SWei Huang 	if (!mgr)
1047a05bd1b4SWei Huang 		return -EINVAL;
1048a05bd1b4SWei Huang 
1049a05bd1b4SWei Huang 	if (mgr->ops && mgr->ops->stop_flash_update)
1050a05bd1b4SWei Huang 		return mgr->ops->stop_flash_update(mgr, force);
1051a05bd1b4SWei Huang 
1052a05bd1b4SWei Huang 	return -ENOENT;
1053a05bd1b4SWei Huang }
1054a05bd1b4SWei Huang 
1055a05bd1b4SWei Huang /**
1056a05bd1b4SWei Huang  * opae_mgr_reload -  reload FPGA.
1057a05bd1b4SWei Huang  * @mgr: targeted manager
1058a05bd1b4SWei Huang  * @type: FPGA type
1059a05bd1b4SWei Huang  * @page: reload from which page
1060a05bd1b4SWei Huang  *
1061a05bd1b4SWei Huang  * Return: 0 on success, otherwise error code.
1062a05bd1b4SWei Huang  */
opae_mgr_reload(struct opae_manager * mgr,int type,int page)1063a05bd1b4SWei Huang int opae_mgr_reload(struct opae_manager *mgr, int type, int page)
1064a05bd1b4SWei Huang {
1065a05bd1b4SWei Huang 	if (!mgr)
1066a05bd1b4SWei Huang 		return -EINVAL;
1067a05bd1b4SWei Huang 
1068a05bd1b4SWei Huang 	if (mgr->ops && mgr->ops->reload)
1069a05bd1b4SWei Huang 		return mgr->ops->reload(mgr, type, page);
1070a05bd1b4SWei Huang 
1071a05bd1b4SWei Huang 	return -ENOENT;
1072a05bd1b4SWei Huang }
1073b74ee6c8SWei Huang /**
1074b74ee6c8SWei Huang  * opae_mgr_read_flash -  read flash content
1075b74ee6c8SWei Huang  * @mgr: targeted manager
1076b74ee6c8SWei Huang  * @address: the start address of flash
1077b74ee6c8SWei Huang  * @size: the size of flash
1078b74ee6c8SWei Huang  * @buf: the read buffer
1079b74ee6c8SWei Huang  *
1080b74ee6c8SWei Huang  * Return: 0 on success, otherwise error code.
1081b74ee6c8SWei Huang  */
opae_mgr_read_flash(struct opae_manager * mgr,u32 address,u32 size,void * buf)1082b74ee6c8SWei Huang int opae_mgr_read_flash(struct opae_manager *mgr, u32 address,
1083b74ee6c8SWei Huang 		u32 size, void *buf)
1084b74ee6c8SWei Huang {
1085b74ee6c8SWei Huang 	if (!mgr)
1086b74ee6c8SWei Huang 		return -EINVAL;
1087b74ee6c8SWei Huang 
1088b74ee6c8SWei Huang 	if (mgr->ops && mgr->ops->read_flash)
1089b74ee6c8SWei Huang 		return mgr->ops->read_flash(mgr, address, size, buf);
1090b74ee6c8SWei Huang 
1091b74ee6c8SWei Huang 	return -ENOENT;
1092b74ee6c8SWei Huang }
1093