-
Notifications
You must be signed in to change notification settings - Fork 2
/
dprc_commands.c
2466 lines (2115 loc) · 60.1 KB
/
dprc_commands.c
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2014-2016 Freescale Semiconductor Inc.
* Copyright 2017-2022 NXP
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the above-listed copyright holders nor the
* names of any contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* ALTERNATIVELY, this software may be distributed under the terms of the
* GNU General Public License ("GPL") as published by the Free Software
* Foundation, either version 2 of that License or (at your option) any
* later version.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <getopt.h>
#include <math.h>
#include <sys/ioctl.h>
#include "restool.h"
#include "utils.h"
#include "dprc_commands_generate_dpl.h"
#define ALL_DPRC_OPTS ( \
DPRC_CFG_OPT_SPAWN_ALLOWED | \
DPRC_CFG_OPT_ALLOC_ALLOWED | \
DPRC_CFG_OPT_OBJ_CREATE_ALLOWED | \
DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED | \
DPRC_CFG_OPT_AIOP | \
DPRC_CFG_OPT_IRQ_CFG_ALLOWED | \
DPRC_CFG_OPT_PL_ALLOWED)
static enum mc_cmd_status mc_status;
/**
* dprc sync command options
*/
enum dprc_sync_options {
SYNC_OPT_HELP = 0,
};
static struct option dprc_sync_options[] = {
[SYNC_OPT_HELP] = {
.name = "help",
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_sync_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc list command options
*/
enum dprc_list_options {
LIST_OPT_HELP = 0,
LIST_OPT_FULL_PATH,
};
static struct option dprc_list_options[] = {
[LIST_OPT_HELP] = {
.name = "help",
},
[LIST_OPT_FULL_PATH] = {
.name = "full-path",
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_list_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc show command options
*/
enum dprc_show_options {
SHOW_OPT_HELP = 0,
SHOW_OPT_RESOURCES,
SHOW_OPT_RES_TYPE,
};
static struct option dprc_show_options[] = {
[SHOW_OPT_HELP] = {
.name = "help",
},
[SHOW_OPT_RESOURCES] = {
.name = "resources",
},
[SHOW_OPT_RES_TYPE] = {
.name = "resource-type",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_show_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc info command options
*/
enum dprc_info_options {
INFO_OPT_HELP = 0,
INFO_OPT_VERBOSE,
};
static struct option dprc_info_options[] = {
[INFO_OPT_HELP] = {
.name = "help",
},
[INFO_OPT_VERBOSE] = {
.name = "verbose",
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_info_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc create command options
*/
enum dprc_create_child_options {
CREATE_OPT_HELP = 0,
CREATE_OPT_OPTIONS,
CREATE_OPT_LABEL,
};
static struct option dprc_create_child_options[] = {
[CREATE_OPT_HELP] = {
.name = "help",
},
[CREATE_OPT_OPTIONS] = {
.name = "options",
.has_arg = 1,
},
[CREATE_OPT_LABEL] = {
.name = "label",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_create_child_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc destroy command options
*/
enum dprc_destroy_options {
DESTROY_OPT_HELP = 0,
};
static struct option dprc_destroy_options[] = {
[DESTROY_OPT_HELP] = {
.name = "help",
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_destroy_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc assign command options
*/
enum dprc_assign_options {
ASSIGN_OPT_HELP = 0,
ASSIGN_OPT_OBJECT,
ASSIGN_OPT_CHILD,
ASSIGN_OPT_RES_TYPE,
ASSIGN_OPT_COUNT,
ASSIGN_OPT_PLUGGED,
};
static struct option dprc_assign_options[] = {
[ASSIGN_OPT_HELP] = {
.name = "help",
},
[ASSIGN_OPT_OBJECT] = {
.name = "object",
.has_arg = 1,
},
[ASSIGN_OPT_CHILD] = {
.name = "child",
.has_arg = 1,
},
[ASSIGN_OPT_RES_TYPE] = {
.name = "resource-type",
.has_arg = 1,
},
[ASSIGN_OPT_COUNT] = {
.name = "count",
.has_arg = 1,
},
[ASSIGN_OPT_PLUGGED] = {
.name = "plugged",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_assign_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc set-label command options
*/
enum dprc_set_label_options {
SET_LABEL_OPT_HELP = 0,
SET_LABEL_OPT_LABEL,
};
static struct option dprc_set_label_options[] = {
[SET_LABEL_OPT_HELP] = {
.name = "help",
},
[SET_LABEL_OPT_LABEL] = {
.name = "label",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_set_label_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc set-locked command options
*/
enum dprc_set_locked_options {
SET_LOCKED_OPT_HELP = 0,
SET_LOCKED_OPT_LOCKED,
};
static struct option dprc_set_locked_options[] = {
[SET_LOCKED_OPT_HELP] = {
.name = "help",
},
[SET_LOCKED_OPT_LOCKED] = {
.name = "locked",
.has_arg = 1,
},
{ 0 },
};
enum dprc_dump_mem_options {
DUMP_MEM_OPT_HELP = 0,
DUMP_MEM_OPT_PART,
};
static struct option dprc_dump_mem_options[] = {
[DUMP_MEM_OPT_HELP] = {
.name = "help",
},
[DUMP_MEM_OPT_PART] = {
.name = "partition_id",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_set_locked_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc connect command options
*/
enum dprc_connect_options {
CONNECT_OPT_HELP = 0,
CONNECT_OPT_ENDPOINT1,
CONNECT_OPT_ENDPOINT2,
CONNECT_OPT_COMMITTED_RATE,
CONNECT_OPT_MAX_RATE,
};
static struct option dprc_connect_options[] = {
[CONNECT_OPT_HELP] = {
.name = "help",
},
[CONNECT_OPT_ENDPOINT1] = {
.name = "endpoint1",
.has_arg = 1,
},
[CONNECT_OPT_ENDPOINT2] = {
.name = "endpoint2",
.has_arg = 1,
},
[CONNECT_OPT_COMMITTED_RATE] = {
.name = "committed-rate",
.has_arg = 1,
.flag = NULL,
.val = 0,
},
[CONNECT_OPT_MAX_RATE] = {
.name = "max-rate",
.has_arg = 1,
.flag = NULL,
.val = 0,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_connect_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dprc disconnect command options
*/
enum dprc_disconnect_options {
DISCONNECT_OPT_HELP = 0,
DISCONNECT_OPT_ENDPOINT,
};
static struct option dprc_disconnect_options[] = {
[DISCONNECT_OPT_HELP] = {
.name = "help",
},
[DISCONNECT_OPT_ENDPOINT] = {
.name = "endpoint",
.has_arg = 1,
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dprc_disconnect_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
/**
* dpl generate command options
*/
enum dpl_generate_options {
GENERATE_OPT_HELP = 0,
};
struct option dpl_generate_options[] = {
[GENERATE_OPT_HELP] = {
.name = "help",
},
{ 0 },
};
C_ASSERT(ARRAY_SIZE(dpl_generate_options) <= MAX_NUM_CMD_LINE_OPTIONS + 1);
const struct flib_ops dprc_ops = {
.obj_open = dprc_open,
.obj_close = dprc_close,
.obj_get_irq_mask = dprc_get_irq_mask,
.obj_get_irq_status = dprc_get_irq_status,
};
static struct option_entry options_map[] = {
OPTION_MAP_ENTRY(DPRC_CFG_OPT_SPAWN_ALLOWED),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_ALLOC_ALLOWED),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_OBJ_CREATE_ALLOWED),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_AIOP),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_IRQ_CFG_ALLOWED),
OPTION_MAP_ENTRY(DPRC_CFG_OPT_PL_ALLOWED),
};
static unsigned int options_num = ARRAY_SIZE(options_map);
static int cmd_dprc_help(void)
{
static const char help_msg[] =
"\n"
"Usage: restool dprc <command> [--help] [ARGS...]\n"
"Where <command> can be:\n"
" sync - synchronize the objects in MC with MC bus.\n"
" list - lists all containers (DPRC objects) in the system.\n"
" show - displays the object contents of a DPRC object.\n"
" info - displays detailed information about a DPRC object.\n"
" create - creates a new child DPRC under the specified parent.\n"
" destroy - destroys a child DPRC under the specified parent.\n"
" assign - moves an object from a parent container to a child container.\n"
" change an object's plugged state\n"
" unassign - moves an object from a child container to a parent container.\n"
" set-label - sets label/alias for any objects except root container.\n"
" set-locked - lock/unlock a child container.\n"
" connect - connects 2 objects, creating a link between them.\n"
" disconnect - removes the link between two objects. Either endpoint can\n"
" be specified as the target of the operation.\n"
" generate-dpl - generate DPL syntax for the specified container\n"
" dump-mem - dump the free memory blocks of a partition\n"
"\n"
"For command-specific help, use the --help option of each command.\n"
"\n";
printf(help_msg);
return 0;
}
static int cmd_dprc_sync(void)
{
int error;
static const char usage_msg[] =
"\n"
"Usage: restool dprc sync\n"
"\n";
if (restool.cmd_option_mask & ONE_BIT_MASK(SYNC_OPT_HELP)) {
puts(usage_msg);
restool.cmd_option_mask &= ~ONE_BIT_MASK(SYNC_OPT_HELP);
return 0;
}
if (restool.obj_name != NULL) {
ERROR_PRINTF(
"Unexpected argument: \'%s\'\n\n", restool.obj_name);
puts(usage_msg);
return -EINVAL;
}
DEBUG_PRINTF("calling sytem()\n");
error = system("echo 1 > /sys/bus/fsl-mc/rescan");
if (error == -1) {
error = -errno;
DEBUG_PRINTF(
"system() failed with error %d\n",
error);
}
return error;
}
/**
* Lists nested DPRCs inside a given DPRC, recursively
*/
static int list_dprc(uint32_t dprc_id, uint16_t dprc_handle,
int nesting_level, bool show_non_dprc_objects,
char *full_path)
{
char *updated_full_path = NULL;
int num_child_devices;
int error = 0;
int full_path_len;
assert(nesting_level <= MAX_DPRC_NESTING);
if (full_path) {
full_path_len = strlen(full_path);
updated_full_path = malloc(full_path_len + 10);
if (!updated_full_path) {
ERROR_PRINTF("Could not alloc memory for full-path!\n");
return -ENOMEM;
}
if (full_path_len != 0)
sprintf(updated_full_path, "%s/dprc.%d", full_path, dprc_id);
else
sprintf(updated_full_path, "dprc.%d", dprc_id);
printf("%s\n", updated_full_path);
} else {
for (int i = 0; i < nesting_level; i++)
printf(" ");
printf("dprc.%u\n", dprc_id);
}
error = dprc_get_obj_count(&restool.mc_io, 0,
dprc_handle,
&num_child_devices);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
for (int i = 0; i < num_child_devices; i++) {
struct dprc_obj_desc obj_desc = {0};
uint16_t child_dprc_handle;
int error2;
error = dprc_get_obj(
&restool.mc_io, 0,
dprc_handle,
i,
&obj_desc);
if (error < 0) {
DEBUG_PRINTF(
"dprc_get_object(%u) failed with error %d\n",
i, error);
goto out;
}
if (strcmp(obj_desc.type, "dprc") != 0) {
if (show_non_dprc_objects) {
for (int i = 0; i < nesting_level + 1; i++)
printf(" ");
printf("%s.%u\n", obj_desc.type, obj_desc.id);
}
continue;
}
error = open_dprc(obj_desc.id, &child_dprc_handle);
if (error < 0)
goto out;
error = list_dprc(obj_desc.id,
child_dprc_handle,
nesting_level + 1,
show_non_dprc_objects,
updated_full_path);
error2 = dprc_close(&restool.mc_io, 0, child_dprc_handle);
if (error2 < 0) {
mc_status = flib_error_to_mc_status(error2);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
if (error == 0)
error = error2;
goto out;
}
}
out:
if (full_path)
free(updated_full_path);
return error;
}
static int cmd_dprc_list(void)
{
static const char usage_msg[] =
"\n"
"Usage: restool dprc list [OPTIONS]\n"
"\n"
"OPTIONS:\n"
"--full-path\n"
" prints the dprc list in a full-path\n"
" format like: dprc.1/dprc.2\n"
"\n";
bool full_path = false;
if (restool.cmd_option_mask & ONE_BIT_MASK(LIST_OPT_HELP)) {
puts(usage_msg);
restool.cmd_option_mask &= ~ONE_BIT_MASK(LIST_OPT_HELP);
return 0;
}
if (restool.cmd_option_mask & ONE_BIT_MASK(LIST_OPT_FULL_PATH)) {
restool.cmd_option_mask &= ~ONE_BIT_MASK(LIST_OPT_FULL_PATH);
full_path = true;
}
if (restool.obj_name != NULL) {
ERROR_PRINTF(
"Unexpected argument: \'%s\'\n\n", restool.obj_name);
puts(usage_msg);
return -EINVAL;
}
return list_dprc(restool.root_dprc_id,
restool.root_dprc_handle,
0, false,
full_path ? "" : NULL);
}
static int show_one_resource_type(uint16_t dprc_handle,
const char *mc_res_type)
{
int res_count;
int res_discovered_count;
struct dprc_res_ids_range_desc range_desc;
int error;
error = dprc_get_res_count(&restool.mc_io, 0, dprc_handle,
(char *)mc_res_type, &res_count);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
if (res_count == 0) {
printf("Don't have any %s resource\n", mc_res_type);
goto out;
}
memset(&range_desc, 0, sizeof(struct dprc_res_ids_range_desc));
res_discovered_count = 0;
do {
int id;
error = dprc_get_res_ids(&restool.mc_io, 0, dprc_handle,
(char *)mc_res_type, &range_desc);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
if (range_desc.base_id == range_desc.last_id)
printf("%s.%d\n", mc_res_type, range_desc.base_id);
else
printf("%s.%d - %s.%d\n",
mc_res_type, range_desc.base_id,
mc_res_type, range_desc.last_id);
for (id = range_desc.base_id; id <= range_desc.last_id; id++)
res_discovered_count++;
} while (res_discovered_count < res_count &&
range_desc.iter_status != DPRC_ITER_STATUS_LAST);
out:
return error;
}
static int show_one_resource_type_count(uint16_t dprc_handle,
const char *mc_res_type)
{
int res_count = -1;
int error;
error = dprc_get_res_count(&restool.mc_io, 0, dprc_handle,
(char *)mc_res_type, &res_count);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
assert(res_count >= 0);
printf("%s: %d\n", mc_res_type, res_count);
out:
return error;
}
/**
* List resources of all types found in the container specified by dprc_handle
*/
static int show_mc_resources(uint16_t dprc_handle)
{
int pool_count;
char res_type[RES_TYPE_MAX_LENGTH + 1];
int error;
int ret_error = 0;
error = dprc_get_pool_count(&restool.mc_io, 0, dprc_handle,
&pool_count);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
assert(pool_count >= 0);
if (0 == pool_count) {
printf("Don't have any resource in current dprc container.\n");
return 0;
}
for (int i = 0; i < pool_count; i++) {
memset(res_type, 0, sizeof(res_type));
error = dprc_get_pool(&restool.mc_io, 0, dprc_handle,
i, res_type);
/* check for buffer overrun: */
assert(res_type[sizeof(res_type) - 1] == '\0');
if (error < 0) {
DEBUG_PRINTF(
"dprc_get_pool() failed for pool index %d (error: %d)\n",
i, error);
if (ret_error == 0)
ret_error = error;
continue;
}
DEBUG_PRINTF("pool index %d: ", i);
error = show_one_resource_type_count(dprc_handle, res_type);
if (error < 0) {
if (ret_error == 0)
ret_error = error;
continue;
}
}
out:
return ret_error;
}
static int show_mc_objects(uint16_t dprc_handle, const char *dprc_name)
{
int num_child_devices;
int error;
int width;
int labelen;
char plug_stat[10] = {'\0'};
struct dprc_obj_desc obj_desc;
error = dprc_get_obj_count(&restool.mc_io, 0,
dprc_handle,
&num_child_devices);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
printf("%s contains %u objects%c\n", dprc_name, num_child_devices,
num_child_devices == 0 ? '.' : ':');
printf("object\t\tlabel\t\tplugged-state\n");
for (int i = 0; i < num_child_devices; i++) {
plug_stat[0] = '\0';
memset(&obj_desc, 0, sizeof(obj_desc));
error = dprc_get_obj(&restool.mc_io, 0,
dprc_handle,
i,
&obj_desc);
if (error < 0) {
DEBUG_PRINTF(
"dprc_get_object(%u) failed with error %d\n",
i, error);
goto out;
}
assert(strlen(obj_desc.label) <= MC_OBJ_LABEL_MAX_LENGTH);
if (obj_desc.id < 0)
width = strlen(obj_desc.type) + 1 +
(2 + (int)log10(0 - obj_desc.id));
else if (obj_desc.id == 0)
width = strlen(obj_desc.type) + 1 + 1;
else
width = strlen(obj_desc.type) + 1 +
(1 + (int)log10(obj_desc.id));
labelen = strlen(obj_desc.label);
DEBUG_PRINTF("%s.%d name length=%d\n",
obj_desc.type, obj_desc.id, width);
DEBUG_PRINTF("label \"%s\" length=%d\n",
obj_desc.label, labelen);
if (strcmp(obj_desc.label, "dprc") == 0)
plug_stat[0] = '\0';
else if (obj_desc.state & DPRC_OBJ_STATE_PLUGGED)
strncpy(plug_stat, "plugged", 9);
else
strncpy(plug_stat, "unplugged", 10);
plug_stat[9] = '\0';
if (width < 8 && labelen < 8)
printf("%s.%d\t\t%s\t\t%s\n",
obj_desc.type, obj_desc.id, obj_desc.label, plug_stat);
else if (width < 8 && labelen >= 8)
printf("%s.%d\t\t%s\t%s\n",
obj_desc.type, obj_desc.id, obj_desc.label, plug_stat);
else if (width >= 8 && labelen < 8)
printf("%s.%d\t%s\t\t%s\n",
obj_desc.type, obj_desc.id, obj_desc.label, plug_stat);
else
printf("%s.%d\t%s\t%s\n",
obj_desc.type, obj_desc.id, obj_desc.label, plug_stat);
}
error = 0;
out:
return error;
}
static int cmd_dprc_show(void)
{
static const char usage_msg[] =
"\n"
"Usage: restool dprc show <container>\n"
"\n";
uint32_t dprc_id;
uint16_t dprc_handle;
const char *dprc_name;
int error;
bool dprc_opened = false;
const char *res_type;
if (restool.cmd_option_mask & ONE_BIT_MASK(SHOW_OPT_HELP)) {
puts(usage_msg);
restool.cmd_option_mask &= ~ONE_BIT_MASK(SHOW_OPT_HELP);
error = 0;
goto out;
}
if (restool.obj_name == NULL) {
ERROR_PRINTF("<object> argument missing\n");
puts(usage_msg);
error = -EINVAL;
goto out;
}
dprc_name = restool.obj_name;
if (strcmp(dprc_name, "mc.global") == 0)
dprc_name = "dprc.0";
error = parse_object_name(dprc_name, "dprc", &dprc_id);
if (error < 0)
goto out;
if (dprc_id != restool.root_dprc_id) {
error = open_dprc(dprc_id, &dprc_handle);
if (error < 0)
goto out;
dprc_opened = true;
} else {
dprc_handle = restool.root_dprc_handle;
}
if (restool.cmd_option_mask & ONE_BIT_MASK(SHOW_OPT_RESOURCES)) {
restool.cmd_option_mask &= ~ONE_BIT_MASK(SHOW_OPT_RESOURCES);
error = show_mc_resources(dprc_handle);
} else if (restool.cmd_option_mask & ONE_BIT_MASK(SHOW_OPT_RES_TYPE)) {
assert(restool.cmd_option_args[SHOW_OPT_RES_TYPE] != NULL);
error = check_resource_type(
restool.cmd_option_args[SHOW_OPT_RES_TYPE]);
if (error < 0) {
puts(usage_msg);
goto out;
}
res_type = restool.cmd_option_args[SHOW_OPT_RES_TYPE];
restool.cmd_option_mask &= ~ONE_BIT_MASK(SHOW_OPT_RES_TYPE);
error = show_one_resource_type(dprc_handle, res_type);
} else {
error = show_mc_objects(dprc_handle, dprc_name);
}
out:
if (dprc_opened) {
int error2;
error2 = dprc_close(&restool.mc_io, 0, dprc_handle);
if (error2 < 0) {
mc_status = flib_error_to_mc_status(error2);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
if (error == 0)
error = error2;
}
}
return error;
}
static void print_dprc_options(uint64_t options)
{
if ((options & ~ALL_DPRC_OPTS) != 0) {
printf("\tUnrecognized options found...\n");
return;
}
if (options & DPRC_CFG_OPT_SPAWN_ALLOWED)
printf("\tDPRC_CFG_OPT_SPAWN_ALLOWED\n");
if (options & DPRC_CFG_OPT_ALLOC_ALLOWED)
printf("\tDPRC_CFG_OPT_ALLOC_ALLOWED\n");
if (options & DPRC_CFG_OPT_OBJ_CREATE_ALLOWED)
printf("\tDPRC_CFG_OPT_OBJ_CREATE_ALLOWED\n");
if (options & DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED)
printf("\tDPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED\n");
if (options & DPRC_CFG_OPT_AIOP)
printf("\tDPRC_CFG_OPT_AIOP\n");
if (options & DPRC_CFG_OPT_IRQ_CFG_ALLOWED)
printf("\tDPRC_CFG_OPT_IRQ_CFG_ALLOWED\n");
if (options & DPRC_CFG_OPT_PL_ALLOWED)
printf("\tDPRC_CFG_OPT_PL_ALLOWED\n");
}
static int print_dprc_attr(uint32_t dprc_id,
struct dprc_obj_desc *target_obj_desc)
{
uint16_t dprc_handle;
int error;
struct dprc_attributes dprc_attr;
bool dprc_opened = false;
if (dprc_id != restool.root_dprc_id) {
error = open_dprc(dprc_id, &dprc_handle);
if (error < 0)
goto out;
dprc_opened = true;
} else {
dprc_handle = restool.root_dprc_handle;
}
memset(&dprc_attr, 0, sizeof(dprc_attr));
error = dprc_get_attributes(&restool.mc_io, 0, dprc_handle, &dprc_attr);
if (error < 0) {
mc_status = flib_error_to_mc_status(error);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
goto out;
}
assert(dprc_id == (uint32_t)dprc_attr.container_id);
printf(
"container id: %d\n"
"icid: %u\n"
"portal id: %d\n"
"dprc options: %#llx\n",
dprc_attr.container_id,
dprc_attr.icid,
dprc_attr.portal_id,
(unsigned long long)dprc_attr.options);
print_dprc_options(dprc_attr.options);
print_obj_label(target_obj_desc);
error = 0;
out:
if (dprc_opened) {
int error2;
error2 = dprc_close(&restool.mc_io, 0, dprc_handle);
if (error2 < 0) {
mc_status = flib_error_to_mc_status(error2);
ERROR_PRINTF("MC error: %s (status %#x)\n",
mc_status_to_string(mc_status), mc_status);
if (error == 0)
error = error2;
}
}
return error;
}
static int print_dprc_info(uint32_t dprc_id)
{
int error;
struct dprc_obj_desc target_obj_desc;
uint32_t target_parent_dprc_id;
bool found = false;
memset(&target_obj_desc, 0, sizeof(struct dprc_obj_desc));
error = find_target_obj_desc(restool.root_dprc_id,
restool.root_dprc_handle, 0, dprc_id,
"dprc", &target_obj_desc,
&target_parent_dprc_id, &found);
if (error < 0)
goto out;
if (strcmp(target_obj_desc.type, "dprc")) {
printf("dprc.%d does not exist\n", dprc_id);
return -EINVAL;
}