summaryrefslogtreecommitdiff
path: root/drivers/raw/ifpga/base/opae_hw_api.c
blob: 1ccc967ec193671aab1218d7712dc1ee9f423304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2018 Intel Corporation
 */

#include "opae_hw_api.h"
#include "opae_debug.h"
#include "ifpga_api.h"

/* OPAE Bridge Functions */

/**
 * opae_bridge_alloc - alloc opae_bridge data structure
 * @name: bridge name.
 * @ops: ops of this bridge.
 * @data: private data of this bridge.
 *
 * Return opae_bridge on success, otherwise NULL.
 */
struct opae_bridge *
opae_bridge_alloc(const char *name, struct opae_bridge_ops *ops, void *data)
{
	struct opae_bridge *br = opae_zmalloc(sizeof(*br));

	if (!br)
		return NULL;

	br->name = name;
	br->ops = ops;
	br->data = data;

	opae_log("%s %p\n", __func__, br);

	return br;
}

/**
 * opae_bridge_reset -  reset opae_bridge
 * @br: bridge to be reset.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_bridge_reset(struct opae_bridge *br)
{
	if (!br)
		return -EINVAL;

	if (br->ops && br->ops->reset)
		return br->ops->reset(br);

	opae_log("%s no ops\n", __func__);

	return -ENOENT;
}

/* Accelerator Functions */

/**
 * opae_accelerator_alloc - alloc opae_accelerator data structure
 * @name: accelerator name.
 * @ops: ops of this accelerator.
 * @data: private data of this accelerator.
 *
 * Return: opae_accelerator on success, otherwise NULL.
 */
struct opae_accelerator *
opae_accelerator_alloc(const char *name, struct opae_accelerator_ops *ops,
		       void *data)
{
	struct opae_accelerator *acc = opae_zmalloc(sizeof(*acc));

	if (!acc)
		return NULL;

	acc->name = name;
	acc->ops = ops;
	acc->data = data;

	opae_log("%s %p\n", __func__, acc);

	return acc;
}

/**
 * opae_acc_reg_read - read accelerator's register from its reg region.
 * @acc: accelerator to read.
 * @region_idx: reg region index.
 * @offset: reg offset.
 * @byte: read operation width, e.g 4 byte = 32bit read.
 * @data: data to store the value read from the register.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_reg_read(struct opae_accelerator *acc, unsigned int region_idx,
		      u64 offset, unsigned int byte, void *data)
{
	if (!acc || !data)
		return -EINVAL;

	if (acc->ops && acc->ops->read)
		return acc->ops->read(acc, region_idx, offset, byte, data);

	return -ENOENT;
}

/**
 * opae_acc_reg_write - write to accelerator's register from its reg region.
 * @acc: accelerator to write.
 * @region_idx: reg region index.
 * @offset: reg offset.
 * @byte: write operation width, e.g 4 byte = 32bit write.
 * @data: data stored the value to write to the register.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_reg_write(struct opae_accelerator *acc, unsigned int region_idx,
		       u64 offset, unsigned int byte, void *data)
{
	if (!acc || !data)
		return -EINVAL;

	if (acc->ops && acc->ops->write)
		return acc->ops->write(acc, region_idx, offset, byte, data);

	return -ENOENT;
}

/**
 * opae_acc_get_info - get information of an accelerator.
 * @acc: targeted accelerator
 * @info: accelerator info data structure to be filled.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_get_info(struct opae_accelerator *acc, struct opae_acc_info *info)
{
	if (!acc || !info)
		return -EINVAL;

	if (acc->ops && acc->ops->get_info)
		return acc->ops->get_info(acc, info);

	return -ENOENT;
}

/**
 * opae_acc_get_region_info - get information of an accelerator register region.
 * @acc: targeted accelerator
 * @info: accelerator region info data structure to be filled.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_get_region_info(struct opae_accelerator *acc,
			     struct opae_acc_region_info *info)
{
	if (!acc || !info)
		return -EINVAL;

	if (acc->ops && acc->ops->get_region_info)
		return acc->ops->get_region_info(acc, info);

	return -ENOENT;
}

/**
 * opae_acc_set_irq -  set an accelerator's irq.
 * @acc: targeted accelerator
 * @start: start vector number
 * @count: count of vectors to be set from the start vector
 * @evtfds: event fds to be notified when corresponding irqs happens
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_set_irq(struct opae_accelerator *acc,
		     u32 start, u32 count, s32 evtfds[])
{
	if (!acc || !acc->data)
		return -EINVAL;

	if (start + count <= start)
		return -EINVAL;

	if (acc->ops && acc->ops->set_irq)
		return acc->ops->set_irq(acc, start, count, evtfds);

	return -ENOENT;
}

/**
 * opae_acc_get_uuid -  get accelerator's UUID.
 * @acc: targeted accelerator
 * @uuid: a pointer to UUID
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_acc_get_uuid(struct opae_accelerator *acc,
		      struct uuid *uuid)
{
	if (!acc || !uuid)
		return -EINVAL;

	if (acc->ops && acc->ops->get_uuid)
		return acc->ops->get_uuid(acc, uuid);

	return -ENOENT;
}

/* Manager Functions */

/**
 * opae_manager_alloc - alloc opae_manager data structure
 * @name: manager name.
 * @ops: ops of this manager.
 * @network_ops: ops of network management.
 * @data: private data of this manager.
 *
 * Return: opae_manager on success, otherwise NULL.
 */
struct opae_manager *
opae_manager_alloc(const char *name, struct opae_manager_ops *ops,
		struct opae_manager_networking_ops *network_ops, void *data)
{
	struct opae_manager *mgr = opae_zmalloc(sizeof(*mgr));

	if (!mgr)
		return NULL;

	mgr->name = name;
	mgr->ops = ops;
	mgr->network_ops = network_ops;
	mgr->data = data;

	opae_log("%s %p\n", __func__, mgr);

	return mgr;
}

/**
 * opae_manager_flash - flash a reconfiguration image via opae_manager
 * @mgr: opae_manager for flash.
 * @id: id of target region (accelerator).
 * @buf: image data buffer.
 * @size: buffer size.
 * @status: status to store flash result.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_manager_flash(struct opae_manager *mgr, int id, const char *buf,
		u32 size, u64 *status)
{
	if (!mgr)
		return -EINVAL;

	if (mgr && mgr->ops && mgr->ops->flash)
		return mgr->ops->flash(mgr, id, buf, size, status);

	return -ENOENT;
}

/* Adapter Functions */

/**
 * opae_adapter_data_alloc - alloc opae_adapter_data data structure
 * @type: opae_adapter_type.
 *
 * Return: opae_adapter_data on success, otherwise NULL.
 */
void *opae_adapter_data_alloc(enum opae_adapter_type type)
{
	struct opae_adapter_data *data;
	int size;

	switch (type) {
	case OPAE_FPGA_PCI:
		size = sizeof(struct opae_adapter_data_pci);
		break;
	case OPAE_FPGA_NET:
		size = sizeof(struct opae_adapter_data_net);
		break;
	default:
		size = sizeof(struct opae_adapter_data);
		break;
	}

	data = opae_zmalloc(size);
	if (!data)
		return NULL;

	data->type = type;

	return data;
}

static struct opae_adapter_ops *match_ops(struct opae_adapter *adapter)
{
	struct opae_adapter_data *data;

	if (!adapter || !adapter->data)
		return NULL;

	data = adapter->data;

	if (data->type == OPAE_FPGA_PCI)
		return &ifpga_adapter_ops;

	return NULL;
}

/**
 * opae_adapter_init - init opae_adapter data structure
 * @adapter: pointer of opae_adapter data structure
 * @name: adapter name.
 * @data: private data of this adapter.
 *
 * Return: 0 on success.
 */
int opae_adapter_init(struct opae_adapter *adapter,
		const char *name, void *data)
{
	if (!adapter)
		return -ENOMEM;

	TAILQ_INIT(&adapter->acc_list);
	adapter->data = data;
	adapter->name = name;
	adapter->ops = match_ops(adapter);

	return 0;
}

/**
 * opae_adapter_enumerate - enumerate this adapter
 * @adapter: adapter to enumerate.
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_adapter_enumerate(struct opae_adapter *adapter)
{
	int ret = -ENOENT;

	if (!adapter)
		return -EINVAL;

	if (adapter->ops && adapter->ops->enumerate)
		ret = adapter->ops->enumerate(adapter);

	if (!ret)
		opae_adapter_dump(adapter, 0);

	return ret;
}

/**
 * opae_adapter_destroy - destroy this adapter
 * @adapter: adapter to destroy.
 *
 * destroy things allocated during adapter enumeration.
 */
void opae_adapter_destroy(struct opae_adapter *adapter)
{
	if (adapter && adapter->ops && adapter->ops->destroy)
		adapter->ops->destroy(adapter);
}

/**
 * opae_adapter_get_acc - find and return accelerator with matched id
 * @adapter: adapter to find the accelerator.
 * @acc_id: id (index) of the accelerator.
 *
 * destroy things allocated during adapter enumeration.
 */
struct opae_accelerator *
opae_adapter_get_acc(struct opae_adapter *adapter, int acc_id)
{
	struct opae_accelerator *acc = NULL;

	if (!adapter)
		return NULL;

	opae_adapter_for_each_acc(adapter, acc)
		if (acc->index == acc_id)
			return acc;

	return NULL;
}

/**
 * opae_manager_read_mac_rom - read the content of the MAC ROM
 * @mgr: opae_manager for MAC ROM
 * @port: the port number of retimer
 * @addr: buffer of the MAC address
 *
 * Return: return the bytes of read successfully
 */
int opae_manager_read_mac_rom(struct opae_manager *mgr, int port,
		struct opae_ether_addr *addr)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->read_mac_rom)
		return mgr->network_ops->read_mac_rom(mgr,
				port * sizeof(struct opae_ether_addr),
				addr, sizeof(struct opae_ether_addr));

	return -ENOENT;
}

/**
 * opae_manager_write_mac_rom - write data into MAC ROM
 * @mgr: opae_manager for MAC ROM
 * @port: the port number of the retimer
 * @addr: data of the MAC address
 *
 * Return: return written bytes
 */
int opae_manager_write_mac_rom(struct opae_manager *mgr, int port,
		struct opae_ether_addr *addr)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops && mgr->network_ops->write_mac_rom)
		return mgr->network_ops->write_mac_rom(mgr,
				port * sizeof(struct opae_ether_addr),
				addr, sizeof(struct opae_ether_addr));

	return -ENOENT;
}

/**
 * opae_manager_get_eth_group_nums - get eth group numbers
 * @mgr: opae_manager for eth group
 *
 * Return: the numbers of eth group
 */
int opae_manager_get_eth_group_nums(struct opae_manager *mgr)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->get_retimer_info)
		return mgr->network_ops->get_eth_group_nums(mgr);

	return -ENOENT;
}

/**
 * opae_manager_get_eth_group_info - get eth group info
 * @mgr: opae_manager for eth group
 * @group_id: id for eth group
 * @info: info return to caller
 *
 * Return: 0 on success, otherwise error code
 */
int opae_manager_get_eth_group_info(struct opae_manager *mgr,
	       u8 group_id, struct opae_eth_group_info *info)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->get_retimer_info)
		return mgr->network_ops->get_eth_group_info(mgr,
			group_id, info);

	return -ENOENT;
}

/**
 * opae_manager_get_eth_group_region_info
 * @mgr: opae_manager for flash.
 * @info: the memory region info for eth group
 *
 * Return: 0 on success, otherwise error code.
 */
int opae_manager_get_eth_group_region_info(struct opae_manager *mgr,
		u8 group_id, struct opae_eth_group_region_info *info)
{
	if (!mgr)
		return -EINVAL;

	if (group_id >= MAX_ETH_GROUP_DEVICES)
		return -EINVAL;

	info->group_id = group_id;

	if (mgr && mgr->ops && mgr->ops->get_eth_group_region_info)
		return mgr->ops->get_eth_group_region_info(mgr, info);

	return -ENOENT;
}

/**
 * opae_manager_eth_group_read_reg - read ETH group register
 * @mgr: opae_manager for ETH Group
 * @group_id: ETH group id
 * @type: eth type
 * @index: port index in eth group device
 * @addr: register address of ETH Group
 * @data: read buffer
 *
 * Return: 0 on success, otherwise error code
 */
int opae_manager_eth_group_read_reg(struct opae_manager *mgr, u8 group_id,
		u8 type, u8 index, u16 addr, u32 *data)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->eth_group_reg_read)
		return mgr->network_ops->eth_group_reg_read(mgr, group_id,
				type, index, addr, data);

	return -ENOENT;
}

/**
 * opae_manager_eth_group_write_reg - write ETH group register
 * @mgr: opae_manager for ETH Group
 * @group_id: ETH group id
 * @type: eth type
 * @index: port index in eth group device
 * @addr: register address of ETH Group
 * @data: data will write to register
 *
 * Return: 0 on success, otherwise error code
 */
int opae_manager_eth_group_write_reg(struct opae_manager *mgr, u8 group_id,
		u8 type, u8 index, u16 addr, u32 data)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->eth_group_reg_write)
		return mgr->network_ops->eth_group_reg_write(mgr, group_id,
				type, index, addr, data);

	return -ENOENT;
}

/**
 * opae_manager_get_retimer_info - get retimer info like PKVL chip
 * @mgr: opae_manager for retimer
 * @info: info return to caller
 *
 * Return: 0 on success, otherwise error code
 */
int opae_manager_get_retimer_info(struct opae_manager *mgr,
	       struct opae_retimer_info *info)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->get_retimer_info)
		return mgr->network_ops->get_retimer_info(mgr, info);

	return -ENOENT;
}

/**
 * opae_manager_get_retimer_status - get retimer status
 * @mgr: opae_manager of retimer
 * @status: status of retimer
 *
 * Return: 0 on success, otherwise error code
 */
int opae_manager_get_retimer_status(struct opae_manager *mgr,
		struct opae_retimer_status *status)
{
	if (!mgr || !mgr->network_ops)
		return -EINVAL;

	if (mgr->network_ops->get_retimer_status)
		return mgr->network_ops->get_retimer_status(mgr,
				status);

	return -ENOENT;
}

/**
 * opae_manager_get_sensor_by_id - get sensor device
 * @id: the id of the sensor
 *
 * Return: the pointer of the opae_sensor_info
 */
struct opae_sensor_info *
opae_mgr_get_sensor_by_id(unsigned int id)
{
	struct opae_sensor_info *sensor;

	opae_mgr_for_each_sensor(sensor)
		if (sensor->id == id)
			return sensor;

	return NULL;
}

/**
 * opae_manager_get_sensor_by_name - get sensor device
 * @name: the name of the sensor
 *
 * Return: the pointer of the opae_sensor_info
 */
struct opae_sensor_info *
opae_mgr_get_sensor_by_name(const char *name)
{
	struct opae_sensor_info *sensor;

	opae_mgr_for_each_sensor(sensor)
		if (!strcmp(sensor->name, name))
			return sensor;

	return NULL;
}

/**
 * opae_manager_get_sensor_value_by_name - find the sensor by name and read out
 * the value
 * @mgr: opae_manager for sensor.
 * @name: the name of the sensor
 * @value: the readout sensor value
 *
 * Return: 0 on success, otherwise error code
 */
int
opae_mgr_get_sensor_value_by_name(struct opae_manager *mgr,
		const char *name, unsigned int *value)
{
	struct opae_sensor_info *sensor;

	if (!mgr)
		return -EINVAL;

	sensor = opae_mgr_get_sensor_by_name(name);
	if (!sensor)
		return -ENODEV;

	if (mgr->ops && mgr->ops->get_sensor_value)
		return mgr->ops->get_sensor_value(mgr, sensor, value);

	return -ENOENT;
}

/**
 * opae_manager_get_sensor_value_by_id - find the sensor by id and readout the
 * value
 * @mgr: opae_manager for sensor
 * @id: the id of the sensor
 * @value: the readout sensor value
 *
 * Return: 0 on success, otherwise error code
 */
int
opae_mgr_get_sensor_value_by_id(struct opae_manager *mgr,
		unsigned int id, unsigned int *value)
{
	struct opae_sensor_info *sensor;

	if (!mgr)
		return -EINVAL;

	sensor = opae_mgr_get_sensor_by_id(id);
	if (!sensor)
		return -ENODEV;

	if (mgr->ops && mgr->ops->get_sensor_value)
		return mgr->ops->get_sensor_value(mgr, sensor, value);

	return -ENOENT;
}

/**
 * opae_manager_get_sensor_value - get the current
 * sensor value
 * @mgr: opae_manager for sensor
 * @sensor: opae_sensor_info for sensor
 * @value: the readout sensor value
 *
 * Return: 0 on success, otherwise error code
 */
int
opae_mgr_get_sensor_value(struct opae_manager *mgr,
		struct opae_sensor_info *sensor,
		unsigned int *value)
{
	if (!mgr || !sensor)
		return -EINVAL;

	if (mgr->ops && mgr->ops->get_sensor_value)
		return mgr->ops->get_sensor_value(mgr, sensor, value);

	return -ENOENT;
}

/**
 * opae_manager_get_board_info - get board info
 * sensor value
 * @info: opae_board_info for the card
 *
 * Return: 0 on success, otherwise error code
 */
int
opae_mgr_get_board_info(struct opae_manager *mgr,
		struct opae_board_info **info)
{
	if (!mgr || !info)
		return -EINVAL;

	if (mgr->ops && mgr->ops->get_board_info)
		return mgr->ops->get_board_info(mgr, info);

	return -ENOENT;
}