-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathswitchtec.c
More file actions
1499 lines (1284 loc) · 38.6 KB
/
switchtec.c
File metadata and controls
1499 lines (1284 loc) · 38.6 KB
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
/*
* Microsemi Switchtec(tm) PCIe Management Library
* Copyright (c) 2017, Microsemi Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* @file
* @brief Switchtec core library functions for basic device operations
*/
#define SWITCHTEC_LIB_CORE
#include "switchtec_priv.h"
#include "switchtec/switchtec.h"
#include "switchtec/mrpc.h"
#include "switchtec/errors.h"
#include "switchtec/log.h"
#include "switchtec/endian.h"
#include "switchtec/utils.h"
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
/**
* @defgroup Device Switchtec Management
* @brief Functions to list, open and perform basic operations on Switchtec devices
*
* switchtec_list() may be used to list all the devices in the system. The
* devices may then be opened using switchtec_open(). There are a number of
* other functions to open devices by more specific information but
* switchtec_open() is prefered and covers all cases.
*
* MRPC commands may be submitted to an open switch handle with switchtec_cmd() and
* port status information may be retrieved with switchtec_status().
* @{
*/
/**
* @brief Module-specific log definitions
*/
struct module_log_defs {
char *mod_name; //!< module name
char **entries; //!< log entry array
int num_entries; //!< number of log entries
};
/**
* @brief Log definitions for all modules
*/
struct log_defs {
struct module_log_defs *module_defs; //!< per-module log definitions
int num_alloc; //!< number of modules allocated
};
/**
* @brief Switchtec device id to generation/variant mapping
*/
struct switchtec_device_id {
unsigned short device_id;
enum switchtec_gen gen;
enum switchtec_variant var;
};
/**
* @brief Supported Switchtec device id table
*/
static const struct switchtec_device_id switchtec_device_id_tbl[] = {
{0x8531, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 24xG3
{0x8532, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 32xG3
{0x8533, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 48xG3
{0x8534, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 64xG3
{0x8535, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 80xG3
{0x8536, SWITCHTEC_GEN3, SWITCHTEC_PFX}, //PFX 96xG3
{0x8541, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 24xG3
{0x8542, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 32xG3
{0x8543, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 48xG3
{0x8544, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 64xG3
{0x8545, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 80xG3
{0x8546, SWITCHTEC_GEN3, SWITCHTEC_PSX}, //PSX 96xG3
{0x8551, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 24XG3
{0x8552, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 32XG3
{0x8553, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 48XG3
{0x8554, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 64XG3
{0x8555, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 80XG3
{0x8556, SWITCHTEC_GEN3, SWITCHTEC_PAX}, //PAX 96XG3
{0x8561, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 24XG3
{0x8562, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 32XG3
{0x8563, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 48XG3
{0x8564, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 64XG3
{0x8565, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 80XG3
{0x8566, SWITCHTEC_GEN3, SWITCHTEC_PFXL}, //PFXL 96XG3
{0x8571, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 24XG3
{0x8572, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 32XG3
{0x8573, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 48XG3
{0x8574, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 64XG3
{0x8575, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 80XG3
{0x8576, SWITCHTEC_GEN3, SWITCHTEC_PFXI}, //PFXI 96XG3
{0x4000, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 100XG4
{0x4084, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 84XG4
{0x4068, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 68XG4
{0x4052, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 52XG4
{0x4036, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 36XG4
{0x4028, SWITCHTEC_GEN4, SWITCHTEC_PFX}, //PFX 28XG4
{0x4100, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 100XG4
{0x4184, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 84XG4
{0x4168, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 68XG4
{0x4152, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 52XG4
{0x4136, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 36XG4
{0x4128, SWITCHTEC_GEN4, SWITCHTEC_PSX}, //PSX 28XG4
{0x4200, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 100XG4
{0x4284, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 84XG4
{0x4268, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 68XG4
{0x4252, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 52XG4
{0x4236, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 36XG4
{0x4352, SWITCHTEC_GEN4, SWITCHTEC_PFXA}, //PFXA 52XG4
{0x4336, SWITCHTEC_GEN4, SWITCHTEC_PFXA}, //PFXA 36XG4
{0x4328, SWITCHTEC_GEN4, SWITCHTEC_PFXA}, //PFXA 28XG4
{0x4452, SWITCHTEC_GEN4, SWITCHTEC_PSXA}, //PSXA 52XG4
{0x4436, SWITCHTEC_GEN4, SWITCHTEC_PSXA}, //PSXA 36XG4
{0x4428, SWITCHTEC_GEN4, SWITCHTEC_PSXA}, //PSXA 28XG4
{0x4552, SWITCHTEC_GEN4, SWITCHTEC_PAXA}, //PAXA 52XG4
{0x4536, SWITCHTEC_GEN4, SWITCHTEC_PAXA}, //PAXA 36XG4
{0x4528, SWITCHTEC_GEN4, SWITCHTEC_PAXA}, //PAXA 28XG4
{0x4228, SWITCHTEC_GEN4, SWITCHTEC_PAX}, //PAX 28XG4
{0},
};
static int find_gen_variant_from_list(struct switchtec_dev *dev)
{
const struct switchtec_device_id *id = switchtec_device_id_tbl;
dev->boot_phase = SWITCHTEC_BOOT_PHASE_FW;
dev->gen = SWITCHTEC_GEN_UNKNOWN;
dev->var = SWITCHTEC_VAR_UNKNOWN;
dev->device_id = dev->ops->get_device_id(dev);
while (id->device_id) {
if (id->device_id == dev->device_id) {
dev->gen = id->gen;
dev->var = id->var;
return 0;
}
id++;
}
return -1;
}
static int set_gen_variant(struct switchtec_dev * dev)
{
int ret;
ret = find_gen_variant_from_list(dev);
if (!ret)
return 0;
ret = switchtec_get_device_info(dev, &dev->boot_phase, &dev->gen, NULL);
if (ret)
return -1;
return 0;
}
static int set_local_pax_id(struct switchtec_dev *dev)
{
unsigned char local_pax_id;
int ret;
dev->local_pax_id = -1;
if (!switchtec_is_pax_all(dev))
return 0;
ret = switchtec_cmd(dev, MRPC_GET_PAX_ID, NULL, 0,
&local_pax_id, sizeof(local_pax_id));
if (ret)
return -1;
dev->local_pax_id = local_pax_id;
return 0;
}
static struct switchtec_dev *open_dev_by_name(const char *device)
{
int idx;
int domain = 0;
int bus, dev, func;
char path[PATH_MAX];
int inst;
char *endptr;
struct switchtec_dev *ret;
if (sscanf(device, "%i@%i", &bus, &dev) == 2) {
ret = switchtec_open_i2c_by_adapter(bus, dev);
goto found;
}
if (sscanf(device, "%2049[^@]@%i", path, &dev) == 2) {
ret = switchtec_open_i2c(path, dev);
goto found;
}
if (device[0] == '/' &&
sscanf(device, "%2049[^:]:%i", path, &dev) == 2) {
ret = switchtec_open_i2c(path, dev);
goto found;
}
if (strchr(device, '/') || strchr(device, '\\')) {
ret = switchtec_open_by_path(device);
goto found;
}
if (sscanf(device, "%x:%x.%x", &bus, &dev, &func) == 3) {
ret = switchtec_open_by_pci_addr(domain, bus, dev, func);
goto found;
}
if (sscanf(device, "%x:%x:%x.%x", &domain, &bus, &dev, &func) == 4) {
ret = switchtec_open_by_pci_addr(domain, bus, dev, func);
goto found;
}
if (sscanf(device, "%2049[^:]:%i", path, &inst) == 2) {
ret = switchtec_open_eth(path, inst);
goto found;
}
errno = 0;
idx = strtol(device, &endptr, 0);
if (!errno && endptr != device) {
ret = switchtec_open_by_index(idx);
goto found;
}
if (sscanf(device, "switchtec%d", &idx) == 1) {
ret = switchtec_open_by_index(idx);
goto found;
}
ret = NULL;
found:
if (!ret)
errno = ENODEV;
return ret;
}
/**
* @brief Open a Switchtec device by string
* @param[in] device A string representing the device to open
* @return A switchtec_dev structure for use in other library functions
* or NULL if an error occurred.
*
* The string can be specified as:
* * A path to the device (/dev/switchtec0)
* * An index (0, 1, etc)
* * An index with a 'switchtec' prefix (switchtec0)
* * A BDF (bus, device function) string (3:00.1)
* * An I2C device with slave number (/dev/i2c-1@0x20)
* * An I2C adapter number and slave number (0@0x20)
* * An I2C device delimited with a colon (/dev/i2c-1:0x20)
* (must start with a / so that it is distinguishable from a BDF)
* * A UART device (/dev/ttyUSB0)
*/
struct switchtec_dev *switchtec_open(const char *device)
{
struct switchtec_dev *ret;
ret = open_dev_by_name(device);
if (!ret)
return NULL;
snprintf(ret->name, sizeof(ret->name), "%s", device);
if (set_gen_variant(ret))
return NULL;
if (set_local_pax_id(ret))
return NULL;
return ret;
}
/**
* @brief Open a Switchtec device by string, without sending MRPC command
* @param[in] device A string representing the device to open
* @return A switchtec_dev structure for use in other library functions
* or NULL if an error occurred.
*
* The string can be specified as:
* * A path to the device (/dev/switchtec0)
* * An index (0, 1, etc)
* * An index with a 'switchtec' prefix (switchtec0)
* * A BDF (bus, device function) string (3:00.1)
* * An I2C device with slave number (/dev/i2c-1@0x20)
* * An I2C adapter number and slave number (0@0x20)
* * An I2C device delimited with a colon (/dev/i2c-1:0x20)
* (must start with a / so that it is distinguishable from a BDF)
* * A UART device (/dev/ttyUSB0)
*/
struct switchtec_dev *switchtec_open_no_mrpc(const char *device)
{
struct switchtec_dev *ret;
int not_found;
ret = open_dev_by_name(device);
if (!ret)
return NULL;
snprintf(ret->name, sizeof(ret->name), "%s", device);
not_found = find_gen_variant_from_list(ret);
if (not_found)
return NULL;
ret->local_pax_id = -1;
return ret;
}
/**
* @brief Get the device id of the device
* @param[in] dev Switchtec device handle
* @return The device id of the device
*
* This is only valid if the device was opend with switchtec_open().
*/
_PURE int switchtec_device_id(struct switchtec_dev *dev)
{
return dev->device_id;
}
/**
* @brief Get the generation of the device
* @param[in] dev Switchtec device handle
* @return The generation of the device
*
* This is only valid if the device was opend with switchtec_open().
*/
_PURE enum switchtec_gen switchtec_gen(struct switchtec_dev *dev)
{
return dev->gen;
}
/**
* @brief Get the variant type of the device
* @param[in] dev Switchtec device handle
* @return The variant type of the device
*
* This is only valid if the device was opend with switchtec_open().
*/
_PURE enum switchtec_variant switchtec_variant(struct switchtec_dev *dev)
{
return dev->var;
}
/**
* @brief Get boot phase of the device
* @param[in] dev Switchtec device handle
* @return The boot phase of the device
*
* This is only valid if the device was opend with switchtec_open().
*/
_PURE enum switchtec_boot_phase switchtec_boot_phase(struct switchtec_dev *dev)
{
return dev->boot_phase;
}
/**
* @brief Get the string that was used to open the deviec
* @param[in] dev Switchtec device handle
* @return The name of the device as a string
*
* This is only valid if the device was opend with switchtec_open().
*/
_PURE const char *switchtec_name(struct switchtec_dev *dev)
{
return dev->name;
}
/**
* @brief Get the partiton number of the device that was opened
* @param[in] dev Switchtec device handle
* @return The partition number
*/
_PURE int switchtec_partition(struct switchtec_dev *dev)
{
return dev->partition;
}
int switchtec_set_pax_id(struct switchtec_dev *dev, int pax_id)
{
if (!(switchtec_is_gen4(dev) && switchtec_is_pax_all(dev)) &&
(pax_id != SWITCHTEC_PAX_ID_LOCAL))
return -1;
if (pax_id == SWITCHTEC_PAX_ID_LOCAL)
dev->pax_id = dev->local_pax_id;
else
dev->pax_id = pax_id;
return 0;
}
static int compare_port_id(const void *aa, const void *bb)
{
const struct switchtec_port_id *a = aa, *b = bb;
if (a->partition != b->partition)
return a->partition - b->partition;
if (a->upstream != b->upstream)
return b->upstream - a->upstream;
return a->log_id - b->log_id;
}
static int compare_status(const void *aa, const void *bb)
{
const struct switchtec_status *a = aa, *b = bb;
return compare_port_id(&a->port, &b->port);
}
static const char *lane_reversal_str(int link_up,
int lane_reversal)
{
if (!link_up)
return "N/A";
switch(lane_reversal) {
case 0: return "Normal Lane Ordering";
case 1: return "x16 (Full) Lane Reversal";
case 2: return "x2 Lane Reversal";
case 4: return "x4 Lane Reversal";
case 8: return "x8 Lane Reversal";
default: return "Unknown Lane Ordering";
}
}
/**
* @brief Get the status of all the ports on a switchtec device
* @param[in] dev Switchtec device handle
* @param[out] status A pointer to an allocated list of port statuses
* @return The number of ports in the status list or a negative value
* on failure
*
* This function a allocates memory for the number of ports in the
* system. The returned \p status structure should be freed with the
* switchtec_status_free() function.
*/
int switchtec_status(struct switchtec_dev *dev,
struct switchtec_status **status)
{
uint64_t port_bitmap = 0;
int ret;
int i, p;
int nr_ports = 0;
struct switchtec_status *s;
if (!status) {
errno = EINVAL;
return -errno;
}
struct {
uint8_t phys_port_id;
uint8_t par_id;
uint8_t log_port_id;
uint8_t stk_id;
uint8_t cfg_lnk_width;
uint8_t neg_lnk_width;
uint8_t usp_flag;
uint8_t linkup_linkrate;
uint16_t LTSSM;
uint8_t lane_reversal;
uint8_t first_act_lane;
} ports[SWITCHTEC_MAX_PORTS];
ret = switchtec_cmd(dev, MRPC_LNKSTAT, &port_bitmap, sizeof(port_bitmap),
ports, sizeof(ports));
if (ret)
return ret;
for (i = 0; i < SWITCHTEC_MAX_PORTS; i++) {
if ((ports[i].stk_id >> 4) > SWITCHTEC_MAX_STACKS)
continue;
nr_ports++;
}
s = *status = calloc(nr_ports, sizeof(*s));
if (!s)
return -ENOMEM;
for (i = 0, p = 0; i < SWITCHTEC_MAX_PORTS && p < nr_ports; i++) {
if ((ports[i].stk_id >> 4) > SWITCHTEC_MAX_STACKS)
continue;
s[p].port.partition = ports[i].par_id;
s[p].port.stack = ports[i].stk_id >> 4;
s[p].port.upstream = ports[i].usp_flag;
s[p].port.stk_id = ports[i].stk_id & 0xF;
s[p].port.phys_id = ports[i].phys_port_id;
s[p].port.log_id = ports[i].log_port_id;
s[p].cfg_lnk_width = ports[i].cfg_lnk_width;
s[p].neg_lnk_width = ports[i].neg_lnk_width;
s[p].link_up = ports[i].linkup_linkrate >> 7;
s[p].link_rate = ports[i].linkup_linkrate & 0x7F;
s[p].ltssm = le16toh(ports[i].LTSSM);
s[p].ltssm_str = switchtec_ltssm_str(s[i].ltssm, 1);
s[p].lane_reversal = ports[i].lane_reversal;
s[p].lane_reversal_str = lane_reversal_str(s[p].link_up,
s[p].lane_reversal);
s[p].first_act_lane = ports[i].first_act_lane & 0xF;
s[p].acs_ctrl = -1;
p++;
}
qsort(s, nr_ports, sizeof(*s), compare_status);
return nr_ports;
}
/**
* @brief Free a list of status structures allocated by switchtec_status()
* @param[in] status Status structure list
* @param[in] ports Number of ports in the list (as returned by
* switchtec_status())
*/
void switchtec_status_free(struct switchtec_status *status, int ports)
{
int i;
for (i = 0; i < ports; i++) {
if (status[i].pci_bdf)
free(status[i].pci_bdf);
if (status[i].pci_bdf_path)
free(status[i].pci_bdf_path);
if (status[i].pci_dev)
free(status[i].pci_dev);
if (status[i].class_devices)
free(status[i].class_devices);
}
free(status);
}
/**
* @brief The MRPC command ID when errno is set.
*
* If errno is for MRPC (with the SWITCHTEC_ERRNO_MRPC_FLAG_BIT set), this
* variable will be set to the corresponding MRPC command ID.
*/
int mrpc_error_cmd;
/**
* @brief Return a message coresponding to the last error
*
* This can be called after another switchtec function returned an error
* to find out what caused the problem.
*
* For MRPC errors (mrpc_error_cmd is not -1) that are unknown to this function,
* the string "Unknown MRPC error" are returned. Otherwise, either proper
* system error string or MRPC error string is returned.
*/
const char *switchtec_strerror(void)
{
const char *msg = "Unknown MRPC error";
int err;
if ((errno & (SWITCHTEC_ERRNO_MRPC_FLAG_BIT |
SWITCHTEC_ERRNO_GENERAL_FLAG_BIT)) == 0) {
if (errno)
return strerror(errno);
else
return platform_strerror();
}
if (errno & SWITCHTEC_ERRNO_GENERAL_FLAG_BIT) {
switch (errno) {
case SWITCHTEC_ERR_LOG_DEF_READ_ERROR:
msg = "Error reading log definition file"; break;
case SWITCHTEC_ERR_BIN_LOG_READ_ERROR:
msg = "Error reading binary log file"; break;
case SWITCHTEC_ERR_PARSED_LOG_WRITE_ERROR:
msg = "Error writing parsed log file"; break;
case SWITCHTEC_ERR_LOG_DEF_DATA_INVAL:
msg = "Invalid log definition data"; break;
default:
msg = "Unknown Switchtec error"; break;
}
return msg;
}
err = errno & ~SWITCHTEC_ERRNO_MRPC_FLAG_BIT;
switch (err) {
case ERR_NO_AVAIL_MRPC_THREAD:
msg = "No available MRPC handler thread"; break;
case ERR_HANDLER_THREAD_NOT_IDLE:
msg = "The handler thread is not idle"; break;
case ERR_NO_BG_THREAD:
msg = "No background thread run for the command"; break;
case ERR_SUBCMD_INVALID: msg = "Invalid subcommand"; break;
case ERR_CMD_INVALID: msg = "Invalid command"; break;
case ERR_PARAM_INVALID: msg = "Invalid parameter"; break;
case ERR_BAD_FW_STATE: msg = "Bad firmware state"; break;
case ERR_MRPC_DENIED: msg = "MRPC request denied"; break;
case ERR_STACK_INVALID: msg = "Invalid Stack"; break;
case ERR_PORT_INVALID: msg = "Invalid Port"; break;
case ERR_EVENT_INVALID: msg = "Invalid Event"; break;
case ERR_RST_RULE_FAILED: msg = "Reset rule search failed"; break;
case ERR_UART_NOT_SUPPORTED:
msg = "UART interface not supported for this command"; break;
case ERR_XML_VERSION_MISMATCH:
msg = "XML version mismatch between MAIN and CFG partition";
break;
case ERR_ACCESS_REFUSED: msg = "Access Refused"; break;
default: break;
}
switch (mrpc_error_cmd) {
case MRPC_PORTPARTP2P:
switch (err) {
case ERR_PHYC_PORT_ARDY_BIND:
msg = "Physical port already bound"; break;
case ERR_LOGC_PORT_ARDY_BIND:
msg = "Logical bridge instance already bound"; break;
case ERR_BIND_PRTT_NOT_EXIST:
msg = "Partition does not exist"; break;
case ERR_PHYC_PORT_NOT_EXIST:
msg = "Physical port does not exist"; break;
case ERR_PHYC_PORT_DIS:
msg = "Physical port disabled"; break;
case ERR_NO_LOGC_PORT:
msg = "No logical bridge instance"; break;
case ERR_BIND_IN_PROGRESS:
msg = "Bind/unbind in progress"; break;
case ERR_BIND_TGT_IS_USP:
msg = "Bind/unbind target is USP"; break;
case ERR_BIND_SUBCMD_INVALID:
msg = "Sub-command does not exist"; break;
case ERR_PHYC_PORT_LINK_ACT:
msg = "Physical port link active"; break;
case ERR_LOGC_PORT_NOT_BIND_PHYC_PORT:
msg = "Logical bridge not bind to physical port"; break;
case ERR_UNBIND_OPT_INVALID:
msg = "Invalid unbind option"; break;
case ERR_BIND_CHECK_FAIL:
msg = "Port bind checking failed"; break;
default: break;
}
break;
default: break;
}
return msg;
}
/**
* @brief Print an error string to stdout
* @param[in] str String that will be prefixed to the error message
*
* This can be called after another switchtec function returned an error
* to find out what caused the problem.
*/
void switchtec_perror(const char *str)
{
const char *msg = switchtec_strerror();
int is_mrpc = errno & SWITCHTEC_ERRNO_MRPC_FLAG_BIT;
int err = errno & ~SWITCHTEC_ERRNO_MRPC_FLAG_BIT;
if (is_mrpc)
fprintf(stderr, "%s: %s (MRPC: 0x%x, error: 0x%x)\n",
str, msg, mrpc_error_cmd, err);
else
fprintf(stderr, "%s: %s\n", str, msg);
}
/**@}*/
/**
* @defgroup Misc Miscellaneous Commands
* @brief Various functions that fit don't fit into other categories
* @{
*/
/**
* @brief Perform an MRPC echo command
* @param[in] dev Switchtec device handle
* @param[in] input The input data for the echo command
* @param[out] output The result of the echo command
* @return 0 on success, error code on failure
*
* The echo command takes 4 bytes and returns the bitwise-not of those
* bytes.
*/
int switchtec_echo(struct switchtec_dev *dev, uint32_t input,
uint32_t *output)
{
return switchtec_cmd(dev, MRPC_ECHO, &input, sizeof(input),
output, sizeof(*output));
}
/**
* @brief Perform an MRPC hard reset command
* @param[in] dev Switchtec device handle
* @return 0 on success, error code on failure
*
* Note: if your system does not support hotplug this may leave
* the Switchtec device in an unusable state. A reboot would be
* required to fix this.
*/
int switchtec_hard_reset(struct switchtec_dev *dev)
{
uint32_t subcmd = 0;
return switchtec_cmd(dev, MRPC_RESET, &subcmd, sizeof(subcmd),
NULL, 0);
}
/**
* @brief Free log definition data
* @param[in] defs - log definition data to free
*/
static void free_log_defs(struct log_defs *defs)
{
int i, j;
if (!defs->module_defs)
return;
for (i = 0; i < defs->num_alloc; i++) {
free(defs->module_defs[i].mod_name);
for (j = 0; j < defs->module_defs[i].num_entries; j++)
free(defs->module_defs[i].entries[j]);
free(defs->module_defs[i].entries);
}
free(defs->module_defs);
}
/**
* @brief Allocate / reallocate log definition data
* @param[in] defs - log definition data
* @param[in] num_modules - number of modules to allocate for
* @return 0 on success, negative value on failure
*/
static int realloc_log_defs(struct log_defs *defs, int num_modules)
{
int i;
defs->module_defs = realloc(defs->module_defs,
(num_modules *
sizeof(struct module_log_defs)));
if (!defs->module_defs) {
free_log_defs(defs);
return -1;
}
for (i = defs->num_alloc; i < num_modules; i++)
memset(&defs->module_defs[i], 0,
sizeof(struct module_log_defs));
defs->num_alloc = num_modules;
return 0;
}
/**
* @brief Parse an integer from a string
* @param[in] str - string to parse
* @param[out] val - integer
* @return true on success, false on failure
*/
static bool parse_int(char *str, int *val)
{
char *endptr;
errno = 0;
*val = strtol(str, &endptr, 0);
if ((endptr == str) || (*endptr != '\0') || (errno != 0))
return false;
return true;
}
/**
* @brief Read an app log definition file and store the definitions
* @param[in] log_def_file - log definition file
* @param[out] defs - log definitions
* @return 0 on success, negative value on failure
*/
static int read_app_log_defs(FILE *log_def_file, struct log_defs *defs)
{
int ret;
char line[512];
char *tok;
int mod_id;
struct module_log_defs *mod_defs;
int num_entries;
int i;
/* allocate some log definition entries */
ret = realloc_log_defs(defs, 200);
if (ret < 0)
return ret;
while (fgets(line, sizeof(line), log_def_file)) {
/* ignore comments */
if (line[0] == '#')
continue;
/* strip any newline characters */
line[strcspn(line, "\r\n")] = '\0';
/*
* Tokenize and parse the line. Module headings are of the form:
* mod_name mod_id num_entries
*/
tok = strtok(line, " \t");
if (!tok)
continue;
tok = strtok(NULL, " \t");
if (!tok)
continue;
if (!parse_int(tok, &mod_id)) {
errno = SWITCHTEC_ERR_LOG_DEF_DATA_INVAL;
goto err_free_log_defs;
}
/* reallocate more log definition entries if needed */
if (mod_id > defs->num_alloc) {
ret = realloc_log_defs(defs, mod_id * 2);
if (ret < 0)
return ret;
}
mod_defs = &defs->module_defs[mod_id];
tok = strtok(NULL, " \t");
if (!tok)
continue;
if (!parse_int(tok, &num_entries)) {
errno = SWITCHTEC_ERR_LOG_DEF_DATA_INVAL;
goto err_free_log_defs;
}
/*
* Skip this module if it has already been done. This can happen
* if the module is duplicated in the log definition file.
*/
if (mod_defs->mod_name != NULL) {
for (i = 0; i < num_entries; i++) {
if (!fgets(line, sizeof(line),
log_def_file))
break;
}
continue;
}
mod_defs->mod_name = strdup(line);
mod_defs->num_entries = num_entries;
mod_defs->entries = calloc(mod_defs->num_entries,
sizeof(*mod_defs->entries));
if (!mod_defs->entries)
goto err_free_log_defs;
for (i = 0; i < mod_defs->num_entries; i++) {
if (fgets(line, sizeof(line), log_def_file) == NULL) {
errno = SWITCHTEC_ERR_LOG_DEF_READ_ERROR;
goto err_free_log_defs;
}
mod_defs->entries[i] = strdup(line);
if (!mod_defs->entries[i])
goto err_free_log_defs;
}
}
if (ferror(log_def_file)) {
errno = SWITCHTEC_ERR_LOG_DEF_READ_ERROR;
goto err_free_log_defs;
}
return 0;
err_free_log_defs:
free_log_defs(defs);
return -1;
}
/**
* @brief Read a mailbox log definition file and store the definitions
* @param[in] log_def_file - log definition file
* @param[out] defs - log definitions
* @return 0 on success, negative value on failure
*/
static int read_mailbox_log_defs(FILE *log_def_file, struct log_defs *defs)
{
int ret;
char line[512];
struct module_log_defs *mod_defs;
int num_entries_alloc;
/*
* The mailbox log definitions don't keep track of modules. Allocate a
* single log definition entry for all definitions.
*/
ret = realloc_log_defs(defs, 1);
if (ret < 0)
return ret;
mod_defs = &defs->module_defs[0];
mod_defs->num_entries = 0;
/* allocate some entries */
num_entries_alloc = 100;
mod_defs->entries = calloc(num_entries_alloc,
sizeof(*mod_defs->entries));
if (!mod_defs->entries)
goto err_free_log_defs;
while (fgets(line, sizeof(line), log_def_file)) {
if (mod_defs->num_entries >= num_entries_alloc) {
/* allocate more entries */
num_entries_alloc *= 2;
mod_defs->entries = realloc(mod_defs->entries,
(num_entries_alloc *
sizeof(*mod_defs->entries)));
if (!mod_defs->entries)
goto err_free_log_defs;
}
mod_defs->entries[mod_defs->num_entries] = strdup(line);
if (!mod_defs->entries[mod_defs->num_entries])
goto err_free_log_defs;
mod_defs->num_entries++;
}
if (ferror(log_def_file)) {
errno = SWITCHTEC_ERR_LOG_DEF_READ_ERROR;
goto err_free_log_defs;
}
return 0;
err_free_log_defs:
free_log_defs(defs);
return -1;
}
/**
* @brief Parse an app log or mailbox log and write the results to a file
* @param[in] log_data - logging data
* @param[in] count - number of entries
* @param[in] init_entry_idx - index of the initial entry
* @param[in] defs - log definitions
* @param[in] log_type - log type
* @param[in] log_file - log output file