-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge_frer.c
2990 lines (2524 loc) · 73.9 KB
/
edge_frer.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
// SPDX-License-Identifier: GPL-2.0
/* TTTech EDGE/DE-IP Linux driver
* Copyright(c) 2018 TTTech Computertechnik AG.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <http://www.gnu.org/licenses/>.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*
* Contact Information:
* TTTech Computertechnik AG, Schoenbrunnerstrasse 7, 1040 Vienna, Austria
*/
#include <linux/version.h>
#include <linux/sysfs.h>
#include "edge_frer.h"
#include "edge_util.h"
#include "edge_br_fdb.h"
#include "edge_port.h"
#include "edge_br_sid.h"
#include "edge_ac.h"
#define STREAM_CMD 0x0400
#define STREAM_TABLE0 0x0410
#define STREAM_TABLE1 0x0412
#define STREAM_TABLE2 0x0414
#define STREAM_TABLE3 0x0416
#define FRER_MAX_NO_STREAMS 1024U /*max FRER_ENTRIES = max FRER_STREAMS*/
#define FRER_TBL0 0
#define FRER_TBL1 1
#define FRER_TBL2 2 //Sequence Generation
#define FRER_TBL3 3 //Sequence Recovery
#define FRER_DEL 1U
#define FRER_U16_INVALID 0xFFFF
#define FRER_BOOL_INVALID 0XFF
#define FRER_WRITE_BUSY(ctrl) ((ctrl) & BIT(15))
#define _SEQ_REC_CMD 0x0300
#define _SEQ_REC_TBL 0x0318
#define _SEQ_GEN_CMD 0x0380 /* Reset sequence number */
#define _STRTBL_MASK 0xFFF /* bits 11-0 - used as pointer to row of tbl*/
#define _SEQREC_MAX_HISTL 0xF
#define _SEQREC_MIN_HISTL 0x2
#define _SEQREC_NO_SEQ 0x1
#define _SEQREC_IND_REC 0x2
#define _SEQREC_LAT_ERR 0x3
#define _SEQREC_SHIFT_ALG 5U
#define _SEQREC_SHIFT_INDREC 6U
#define _SEQREC_SHIFT_TNS 7U
#define _SEQREC_SHIFT_BRATE 12U
#define _SEQREC_SHIFT_ARATE 14U
#define _SEQREC_INVSEQ 65536U //RecovSeqSpace
#define _SEQREC_RCVRST_MAXRES 16U
#define _CNT_SEQREC_TOUT_L 0x200
#define _CNT_SEQREC_TOUT_H 0x202
#define _CNT_VECT_JUMP_L 0x0204
#define _CNT_VECT_JUMP_H 0x0206
#define _CNT_SEQREC_OOO_L 0x0208
#define _CNT_SEQREC_OOO_H 0x020A
#define _CNT_SEQREC_OOO2_L 0x0308
#define _CNT_SEQREC_OOO2_H 0x030A
#define _CNT_VECT_ROG_L 0x030C
#define _CNT_VECT_ROG_H 0x030E
#define _CNT_SEQREC_TAGLESS_L 0x0310
#define _CNT_SEQREC_TAGLESS_H 0x0312
#define _CNT_SEQREC_SB_L 0x0314
#define _CNT_SEQREC_SB_H 0x0316
#define _CNT_CAPT0 0x0
#define _CNT_CAPT1 0x2
#define _CNT_CAPT2 0x4
#define _CNT_CAPT3 0x6
#define AGING_BASE_TIME_LO 0x0022
#define AGING_BASE_TIME_HI 0x0024
#define FRER_SCNT_OFFS 0xF000
enum frer_dir {
DIR_IN_FAC = 0,
DIR_OUT_FAC,
DIR_UNKNOWN,
};
enum _seqenc_type {
TYPE_UNKNOWN = 0,
TYPE_R_TAG = 1, //Only R-tag used
TYPE_HSR_TAG = 2,
TYPE_PRP_TRAILER = 3,
};
enum _seqenc_active {
ENC_PASSIVE = 0,
ENC_ACTIVE = 1,
ENC_UNKNOWN = 2,//used when entry is added but active not set yet
ENC_UNDEFINED = 3,//used at init, entry doesn't exist
};
enum _seqrec_alg {
ALG_VECT = 0,
ALG_MATCH = 1,
ALG_UNKNOWN = 2,
};
struct _se_strlist {
struct list_head entry;
u16 strhdl;
};
struct _se_ports {
struct list_head se_list;
struct _se_strlist *l;
u8 active;
};
struct _se_streams_rbt {
struct rb_node rbnode;
u16 strhdl; //key
u16 port_mask;
};
struct strlist {
struct list_head entry;
u16 strhdl;
};
struct _seqgen_rbt {
struct rb_node rbnode;
struct list_head sg_list;
struct strlist *l; /*multiple str handles per 1 seqgenfn possible*/
u8 dir;
u16 fnidx; /* RBT key */
};
struct _seqrec_rbt {
struct rb_node rbnode;
struct list_head sr_list; //list of stream handles
struct strlist *l; /*list of strhdls for 1 seqrec fn*/
enum _seqrec_alg alg;
u16 fnidx; //key
u16 rcv_rst;
u16 ptmask;
u8 dir;
u8 hist_len;//4 bits assigned by the IP
u8 take_no_seq;
u8 ind_rec;
u8 lat_err;
bool seqrec_cfg_applied;
};
/*sq - sequence generation, se - sequence encode, sr - sequence recovery*/
struct edgx_frer {
struct edgx_br *parent;
edgx_io_t *iobase;
struct rb_root sg_root;
struct rb_root se_root;
struct rb_root sr_root;
struct _se_ports se_ports[EDGX_BR_MAX_PORTS];
u16 seqrec_max;
u16 streams_max;
u16 seqgencnt;
u16 seqreccnt;
u16 seqrec_rst[_SEQREC_RCVRST_MAXRES];//FLEXDE-3139
struct mutex lock; /*Protects FRER data; access to STREAM_TABLE0/1/2/3*/
};
//FLEXDE-3139
static u32 calc_rcv_rst(struct edgx_frer **f)
{
u8 i, j;
u32 aging_bt, denom;
u64 num;
unsigned int tclk = edgx_br_get_cycle_ns((*f)->parent);
aging_bt = edgx_rd16((*f)->iobase, AGING_BASE_TIME_LO) |
(((u32)edgx_rd16((*f)->iobase, AGING_BASE_TIME_HI)) << 16);
//rem = do_div(num, sec_denom);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
//i = aging rate scale, j = aging basic rate
num = tclk * aging_bt * 124;
//multiply with 1000000 to convert ns to ms
denom = 1000000 * (1 << i) * (j + 5);
do_div(num, denom);
(*f)->seqrec_rst[(i * 4) + j] = num;
}
}
return 0;
}
static u8 first_bigger(struct edgx_frer *f, u16 val)
{
s8 i = 15;
//The array is sorted, first member is the biggest one
while (f->seqrec_rst[(u8)i] < val && i >= 0)
i--;
if (i < 0)
i = 0;
return (u8)i;
}
/*per entity = function counter*/
static u32 edgx_frer_ip_perfn_cnt(edgx_io_t *base_addr, u16 fnidx, int cnt)
{
u16 capture = fnidx;
edgx_io_t *cnt_base = base_addr + FRER_SCNT_OFFS;
edgx_wr16(cnt_base, _CNT_CAPT1, capture);
do {
capture = edgx_rd16(cnt_base, _CNT_CAPT0);
} while (capture & BIT(0));
switch (cnt) {
case _CNT_SEQREC_TOUT_L:
case _CNT_SEQREC_TOUT_H:
return (edgx_rd16(cnt_base, _CNT_SEQREC_TOUT_L) |
(((u32)edgx_rd16(cnt_base, _CNT_SEQREC_TOUT_H)) << 16));
case _CNT_VECT_JUMP_L:
case _CNT_VECT_JUMP_H:
return (edgx_rd16(cnt_base, _CNT_VECT_JUMP_L) |
(((u32)edgx_rd16(cnt_base, _CNT_VECT_JUMP_H)) << 16));
case _CNT_SEQREC_OOO_L:
case _CNT_SEQREC_OOO_H:
return (edgx_rd16(cnt_base, _CNT_SEQREC_OOO_L) |
(((u32)edgx_rd16(cnt_base, _CNT_SEQREC_OOO_H)) << 16));
}
return 0;
}
/*per port per stream frer counters*/
static u32 edgx_frer_ip_ppps_cnt(edgx_io_t *base_addr, u16 str_hdl, u8 port,
int cnt)
{
u16 capture = port | (str_hdl << 4);
edgx_io_t *cnt_base = base_addr + FRER_SCNT_OFFS;
edgx_wr16(cnt_base, _CNT_CAPT3, capture);
do {
capture = edgx_rd16(cnt_base, _CNT_CAPT0);
} while (capture & BIT(5));
switch (cnt) {
case _CNT_SEQREC_OOO2_L:
case _CNT_SEQREC_OOO2_H:
return (edgx_rd16(cnt_base, _CNT_SEQREC_OOO2_L) |
(((u32)edgx_rd16(cnt_base, _CNT_SEQREC_OOO2_H)) << 16));
case _CNT_VECT_ROG_L:
case _CNT_VECT_ROG_H:
return (edgx_rd16(cnt_base, _CNT_VECT_ROG_L) |
(((u32)edgx_rd16(cnt_base, _CNT_VECT_ROG_H)) << 16));
case _CNT_SEQREC_TAGLESS_L:
case _CNT_SEQREC_TAGLESS_H:
return (edgx_rd16(cnt_base, _CNT_SEQREC_TAGLESS_L) |
(((u32)edgx_rd16(cnt_base,
_CNT_SEQREC_TAGLESS_H)) << 16));
case _CNT_SEQREC_SB_L:
case _CNT_SEQREC_SB_H:
return (edgx_rd16(cnt_base, _CNT_SEQREC_SB_L) |
(((u32)edgx_rd16(cnt_base, _CNT_SEQREC_SB_H)) << 16));
}
return 0;
}
static u16 edgx_frer_ip_rd_strtbl(struct edgx_frer *f, u16 strhdl, int tbl_num)
{
u16 val;
edgx_wr16(f->iobase, STREAM_CMD, 0x8000 | strhdl);
do {
val = edgx_rd16(f->iobase, STREAM_CMD);
} while (FRER_WRITE_BUSY(val));
switch (tbl_num) {
case FRER_TBL0:
return edgx_rd16(f->iobase, STREAM_TABLE0);
case FRER_TBL1:
return edgx_rd16(f->iobase, STREAM_TABLE1);
case FRER_TBL2:
return edgx_rd16(f->iobase, STREAM_TABLE2);
case FRER_TBL3:
return edgx_rd16(f->iobase, STREAM_TABLE3);
}
return 0;
}
static void edgx_frer_ip_wr_strtbl(struct edgx_frer *f, u16 strhdl, u16 val,
int tbl_num, u8 del)
{
/*
* Read before writing in order to refresh values STREAM_TABLE0-3
* Third parameter is not important, it can be any table
*/
edgx_frer_ip_rd_strtbl(f, strhdl, FRER_TBL0);
switch (tbl_num) {
case FRER_TBL0:
/*r-tag modify port vector*/
edgx_wr16(f->iobase, STREAM_TABLE0, val);
break;
case FRER_TBL1:
/*r-tag output port vector*/
edgx_wr16(f->iobase, STREAM_TABLE1, val);
break;
case FRER_TBL2:
/*val = sequence generation table row*/
if (!del)
edgx_wr16(f->iobase, STREAM_TABLE2, val | BIT(15));
else
edgx_wr16(f->iobase, STREAM_TABLE2, val & (~BIT(15)));
break;
case FRER_TBL3:
/*val = sequence recovery table row*/
if (!del)
edgx_wr16(f->iobase, STREAM_TABLE3, val | BIT(15));
else
edgx_wr16(f->iobase, STREAM_TABLE3, val & (~BIT(15)));
break;
}
/* write to row = strhdl of the Stream Table*/
edgx_wr16(f->iobase, STREAM_CMD, 0xC000 | strhdl);
do {
val = edgx_rd16(f->iobase, STREAM_CMD);
} while (FRER_WRITE_BUSY(val));
}
static void edgx_frer_ip_wr_seqrectbl(struct edgx_frer *f, u16 fnidx, u16 val)
{
/*All parameters are updated, write directly to register*/
edgx_wr16(f->iobase, _SEQ_REC_TBL, val);
/* write to row = strhdl of the SeqRecTable*/
edgx_wr16(f->iobase, _SEQ_REC_CMD, 0xC000 | fnidx);
do {
val = edgx_rd16(f->iobase, _SEQ_REC_CMD);
} while (FRER_WRITE_BUSY(val));
}
static void edgx_frer_ip_rd_seqrectbl(struct edgx_frer *f, u16 fnidx, u16 val)
{
/* write to row = strhdl of the SeqRecTable*/
edgx_wr16(f->iobase, _SEQ_REC_CMD, 0x8000 | fnidx);
do {
val = edgx_rd16(f->iobase, _SEQ_REC_CMD);
} while (FRER_WRITE_BUSY(val));
val = edgx_rd16(f->iobase, _SEQ_REC_TBL);
}
static int edgx_frer_check_fnidx(u16 fnidx, struct edgx_frer *f)
{
if (fnidx > f->seqrec_max) {
edgx_br_err(f->parent,
"SeqGen fn index out of boundaries, max = %d\n",
f->seqrec_max);
return -EINVAL;
}
return 0;
}
static int edgx_frer_check_strhdl(u16 strhdl, struct edgx_frer *f)
{
if (strhdl > f->streams_max - 1) {
edgx_br_err(f->parent, "Stream handle out of boundaries\n");
return -EINVAL;
}
return 0;
}
int edgx_frer_check_port(u16 ptid, struct edgx_frer *f)
{
if (ptid > edgx_br_get_num_ports(f->parent)) {
edgx_br_err(f->parent, "SeqEnc: Port number invalid\n");
return -EINVAL;
} else {
return 0;
}
}
/*check if stream handle has sequence or recovery generation function enabled*/
static int edgx_frer_seqfn_enabled(struct edgx_frer *f, u16 strhdl, int tbl)
{
u16 reg = edgx_frer_ip_rd_strtbl(f, strhdl, tbl);
return (reg & BIT(15)) ? 1 : 0;
}
static void _seqgen_ip_reset(struct edgx_frer *f, u16 fnidx)
{
edgx_wr16(f->iobase, _SEQ_GEN_CMD, fnidx | BIT(13));
}
static int _seqgen_check_direction(enum frer_dir dir, struct edgx_frer *f)
{
if (dir != (u8)DIR_IN_FAC) {
edgx_br_err(f->parent, "Only in-facing(0) direction allowed\n");
return -EINVAL;
}
return 0;
}
static inline int _seqgen_list_add(u16 strhdl, struct _seqgen_rbt *gen)
{
gen->l = kzalloc(sizeof(*gen->l), GFP_KERNEL);
if (!gen->l)
return -ENOMEM;
gen->l->strhdl = strhdl;
list_add_tail(&gen->l->entry, &gen->sg_list);
return 0;
}
static int seqgen_list_entry_exists(u16 strhdl, struct _seqgen_rbt *gen)
{
struct list_head *pos;
struct strlist *l;
list_for_each(pos, &gen->sg_list)
{
l = list_entry(pos, struct strlist, entry);
if (l->strhdl == strhdl)
return 1;
}
return 0;
}
static struct _seqgen_rbt *_seqgen_rbt_search(struct rb_root *root, u16 key)
{
struct rb_node *node = root->rb_node;
while (node) {
struct _seqgen_rbt *sgen = rb_entry(node, struct _seqgen_rbt,
rbnode);
if (key < sgen->fnidx)
node = node->rb_left;
else if (key > sgen->fnidx)
node = node->rb_right;
else
return sgen;
}
return NULL;
}
/*
* Function used for removing entry from the list within RBT. In case this is
* the last entry in the RBT, entire rbt node is removed
*/
static int _seqgen_list_del_entry(struct edgx_frer *f, u16 str)
{
struct strlist *l;
struct list_head *pos;
struct list_head *tmp;
struct _seqgen_rbt *n;
u16 fn;
/*
* xxx Erroneous use case: SeqGen function added, direction not set,
* streams added to the list; delete will not work since nothing is
* written to the IP until direction is set. One way to solve this
* is to go through entire RBT and through every list in every node and
* erase stream handle. Worst case scenario:1024 nodes in the RBT
* up to 1024 streams added Other way is to pass fnidx as parameter
* in the sysfs call. Third: leave as is :)
*/
if (!edgx_frer_seqfn_enabled(f, str, FRER_TBL2))
return -EINVAL;
fn = edgx_frer_ip_rd_strtbl(f, str, FRER_TBL2) & _STRTBL_MASK;
n = _seqgen_rbt_search(&f->sg_root, fn);
if (!n) {
edgx_br_err(f->parent, "Seqgen function not found in SW dtb\n");
return -EINVAL;
}
list_for_each_safe(pos, tmp, &n->sg_list) {
l = list_entry(pos, struct strlist, entry);
if (l->strhdl == str) {
list_del(pos);
kfree(l);
edgx_frer_ip_wr_strtbl(f, str, fn, FRER_TBL2, FRER_DEL);
if (list_empty(&n->sg_list)) {
/*If the list is empty, delete entry from rbt*/
rb_erase(&n->rbnode, &f->sg_root);
RB_CLEAR_NODE(&n->rbnode);
kfree(n);
f->seqgencnt--;
}
return 0;
}
}
/*couldn't find match*/
return -EINVAL;
}
/* Remove entire RBT entry */
static int _seqgen_rbt_del_entry(struct edgx_frer *f, u16 key)
{
struct strlist *l;
struct list_head *tmp;
struct list_head *pos;
struct _seqgen_rbt *n = _seqgen_rbt_search(&f->sg_root, key);
if (!n)
return -EINVAL;
/*Delete all stream handles from the list assigned to rbtree*/
if (!list_empty(&n->sg_list)) {
list_for_each_safe(pos, tmp, &n->sg_list) {
l = list_entry(pos, struct strlist, entry);
list_del(pos);
kfree(l);
edgx_frer_ip_wr_strtbl(f, l->strhdl, 0, FRER_TBL2,
FRER_DEL);
}
}
/*Delete entry from rb-tree*/
rb_erase(&n->rbnode, &f->sg_root);
RB_CLEAR_NODE(&n->rbnode);
kfree(n);
f->seqgencnt--;
return 0;
}
/* Remove single stream from the list without checking RBT or registers */
static int _seqgen_list_rm_single(u16 strhdl, struct _seqgen_rbt *n)
{
struct list_head *pos;
struct list_head *tmp;
struct strlist *l;
list_for_each_safe(pos, tmp, &n->sg_list) {
l = list_entry(pos, struct strlist, entry);
if (strhdl == l->strhdl) {
list_del(pos);
kfree(l);
return 0;
}
}
return -ENOENT;
}
static int _seqgen_rbt_insert_node(struct _seqgen_rbt *new,
struct edgx_frer *f)
{
struct _seqgen_rbt *sgen;
struct rb_node **pp, *p;
pp = &f->sg_root.rb_node;
p = NULL;
while (*pp) {
p = *pp;
sgen = rb_entry(p, struct _seqgen_rbt, rbnode);
if (new->fnidx < sgen->fnidx)
pp = &(*pp)->rb_left;
else if (new->fnidx > sgen->fnidx)
pp = &(*pp)->rb_right;
else
return -EINVAL;
}
rb_link_node(&new->rbnode, p, pp);
rb_insert_color(&new->rbnode, &f->sg_root);
return 0;
}
static int edgx_frer_seqgen_apply(struct _seqgen_rbt *n, struct edgx_frer *f)
{
struct list_head *pos;
struct strlist *l;
u16 reg;
if (n->dir == (u8)DIR_UNKNOWN || list_empty(&n->sg_list))
return 0;
list_for_each(pos, &n->sg_list)
{
l = list_entry(pos, struct strlist, entry);
/* entry has Seq-Gen-Fn enabled already*/
if (edgx_frer_seqfn_enabled(f, l->strhdl, FRER_TBL2)) {
reg = edgx_frer_ip_rd_strtbl(f, l->strhdl,
FRER_TBL2);
if ((reg & 0xFFF) != n->fnidx) {
edgx_br_err(f->parent,
"Stream handle %d assigned to seqGenFn, %d\n",
l->strhdl, reg & 0xFFF);
return -EBUSY;
}
} else {
/*SeqGenFn data not written to the table yet*/
edgx_frer_ip_wr_strtbl(f, l->strhdl, n->fnidx,
FRER_TBL2, 0);
_seqgen_ip_reset(f, n->fnidx);
}
}
return 0;
}
static int edgx_frer_seqgen_addstr(struct edgx_frer *f, u16 str, u16 fnidx)
{
struct _seqgen_rbt *seqgen;
if (edgx_frer_check_strhdl(str, f))
return -EINVAL;
seqgen = _seqgen_rbt_search(&f->sg_root, fnidx);
if (!seqgen) {
edgx_br_err(f->parent, "AddStr:SeqGenfn doesn't exist\n");
return -ENOENT;
}
if (!seqgen_list_entry_exists(str, seqgen))
if (_seqgen_list_add(str, seqgen))
return -ENOMEM;
if (edgx_frer_seqgen_apply(seqgen, f)) {
_seqgen_list_rm_single(str, seqgen);
return -EBUSY;
}
return 0;
}
static int edgx_frer_seqgen_adddir(struct edgx_frer *f, u8 dir, u16 fnidx)
{
struct _seqgen_rbt *seqgen;
if (_seqgen_check_direction(dir, f))
return -EINVAL;
seqgen = _seqgen_rbt_search(&f->sg_root, fnidx);
if (!seqgen) {
edgx_br_err(f->parent, "AddDir:SeqGenFn doesn't exist\n");
return -ENOENT;
}
seqgen->dir = dir;
if (edgx_frer_seqgen_apply(seqgen, f)) {
seqgen->dir = (u8)DIR_UNKNOWN;
return -EBUSY;
}
return 0;
}
static int edgx_frer_seqgen_create(struct edgx_frer *f, u16 fnidx)
{
struct _seqgen_rbt *rbt_gen;
if (edgx_frer_check_fnidx(fnidx, f))
return -EINVAL;
rbt_gen = _seqgen_rbt_search(&f->sg_root, fnidx);
if (rbt_gen) {
edgx_br_err(f->parent, "SeqGen entry %d already exists\n",
fnidx);
return -EBUSY;
}
rbt_gen = kzalloc(sizeof(*rbt_gen), GFP_KERNEL);
if (!rbt_gen)
return -ENOMEM;
rbt_gen->dir = (u8)DIR_UNKNOWN;
rbt_gen->fnidx = fnidx;
f->seqgencnt++;
INIT_LIST_HEAD(&rbt_gen->sg_list);
if (_seqgen_rbt_insert_node(rbt_gen, f))
return -EINVAL;
return 0;
}
int _seqenc_check_dir(u8 dir, struct edgx_frer *f)
{
if (dir != (u8)DIR_OUT_FAC) {
edgx_br_err(f->parent,
"SeqEnc:Only out-facing(1) direction supported\n");
return -EINVAL;
} else {
return 0;
}
}
int _seqenc_check_type(u8 type, struct edgx_frer *f)
{
if (type != (u8)TYPE_R_TAG) {
edgx_br_err(f->parent, "Supporting only R-tag encapsulation\n");
return -EINVAL;
} else {
return 0;
}
}
int _seqenc_check_active(u16 active, struct edgx_frer *f)
{
if ((active != ENC_ACTIVE) && (active != ENC_PASSIVE)){
edgx_br_err(f->parent, "SeqEnc: invalid activation value\n");
return -EINVAL;
} else {
return 0;
}
}
static int _seqenc_list_entry_exists(u16 strhdl, struct edgx_frer *f, u16 idx)
{
struct list_head *pos;
struct _se_strlist *l;
list_for_each(pos, &f->se_ports[idx].se_list)
{
l = list_entry(pos, struct _se_strlist, entry);
if (l->strhdl == strhdl)
return -EEXIST;
}
return 0;
}
static inline int _seqenc_list_add(u16 strhdl, struct edgx_frer *f, u16 idx)
{
f->se_ports[idx].l = kzalloc(sizeof(*f->se_ports[idx].l), GFP_KERNEL);
if (!f->se_ports[idx].l)
return -ENOMEM;
f->se_ports[idx].l->strhdl = strhdl;
list_add_tail(&f->se_ports[idx].l->entry, &f->se_ports[idx].se_list);
return 0;
}
static struct _se_streams_rbt *_seqenc_rbt_search(struct rb_root *root, u16 key)
{
struct rb_node *node = root->rb_node;
while (node) {
struct _se_streams_rbt *senc = rb_entry(node,
struct _se_streams_rbt,
rbnode);
if (key < senc->strhdl)
node = node->rb_left;
else if (key > senc->strhdl)
node = node->rb_right;
else
return senc;
}
return NULL;
}
static int _seqenc_rbt_insert_node(struct _se_streams_rbt *new,
struct edgx_frer *f)
{
struct _se_streams_rbt *senc;
struct rb_node **pp, *p;
pp = &f->se_root.rb_node;
p = NULL;
while (*pp) {
p = *pp;
senc = rb_entry(p, struct _se_streams_rbt, rbnode);
if (new->strhdl < senc->strhdl)
pp = &(*pp)->rb_left;
else if (new->strhdl > senc->strhdl)
pp = &(*pp)->rb_right;
else
return -EINVAL;
}
rb_link_node(&new->rbnode, p, pp);
rb_insert_color(&new->rbnode, &f->se_root);
return 0;
}
static void _seqenc_apply_active(struct edgx_frer *f, u16 pt, u16 str)
{
u16 reg;
//ACTIVE: write 1 to STREAM_TABLE0 and 1 for the chosen port and stream
reg = edgx_frer_ip_rd_strtbl(f, str, FRER_TBL0);
reg |= BIT(pt);
edgx_frer_ip_wr_strtbl(f, str, reg, FRER_TBL0, 0);
reg = edgx_frer_ip_rd_strtbl(f, str, FRER_TBL1);
reg |= BIT(pt);
edgx_frer_ip_wr_strtbl(f, str, reg, FRER_TBL1, 0);
}
static void _seqenc_apply_passive(struct edgx_frer *f, u16 pt, u16 str)
{
u16 reg;
//PASSIVE: set STR_TABLE0 to 1 and STR_TABLE1 to 1 for chosen pt-str
reg = edgx_frer_ip_rd_strtbl(f, str, FRER_TBL0);
reg |= BIT(pt);
edgx_frer_ip_wr_strtbl(f, str, reg, FRER_TBL0, 0);
reg = edgx_frer_ip_rd_strtbl(f, str, FRER_TBL1);
reg &= (~BIT(pt));
edgx_frer_ip_wr_strtbl(f, str, reg, FRER_TBL1, 0);
}
static int edgx_frer_seqenc_apply(struct edgx_frer *f, u16 pt)
{
struct list_head *pos;
struct strlist *l;
/*
* Configuration is written to the board only if list of streams for
* the port is not empty and type of encoding is chosen
*/
if (f->se_ports[pt].active > ENC_ACTIVE ||
list_empty(&f->se_ports[pt].se_list))
return -ENOENT;
/*
* Possible improvement: save pointer to the lastly written (applied)
* value from list and continue from there
*/
list_for_each(pos, &f->se_ports[pt].se_list) {
l = list_entry(pos, struct strlist, entry);
if (f->se_ports[pt].active)
_seqenc_apply_active(f, pt, l->strhdl);
else
_seqenc_apply_passive(f, pt, l->strhdl);
}
return 0;
}
static void edgx_frer_seqenc_applydel(struct edgx_frer *f, u16 port,
u16 strhdl, u16 portmask)
{
u16 reg;
if (portmask == 0) {
/*Stream doesn't have any seq-enc settings, reset IP settings*/
edgx_frer_ip_wr_strtbl(f, strhdl, 0, FRER_TBL0, 0);
edgx_frer_ip_wr_strtbl(f, strhdl, 0, FRER_TBL1, 0);
} else {
/*Only reset table1 for the corresponding port*/
reg = edgx_frer_ip_rd_strtbl(f, strhdl, FRER_TBL0);
reg &= ~BIT(port);
edgx_frer_ip_wr_strtbl(f, strhdl, reg, FRER_TBL0, 0);
reg = edgx_frer_ip_rd_strtbl(f, strhdl, FRER_TBL1);
reg &= ~BIT(port);
edgx_frer_ip_wr_strtbl(f, strhdl, reg, FRER_TBL1, 0);
}
}
static int edgx_frer_seqenc_create_entry(u16 port, u8 dir, struct edgx_frer *f)
{
if (_seqenc_check_dir(dir, f) ||
edgx_frer_check_port(port, f))
return -EINVAL;
/*
* ENC_UNKNOWN denotes that entry was added, but parameters are still
* not completely set (stream handles to be added, act/passive to be set
* Driver doesn't save data about position since only out-fac is allowed
*/
f->se_ports[port].active = ENC_UNKNOWN;
return 0;
}
static int edgx_frer_seqenc_add_str(u16 str, u8 type, u16 port,
struct edgx_frer *f)
{
struct _se_streams_rbt *s;
/* If entry doesn't exist */
if (f->se_ports[port].active == ENC_UNDEFINED) {
edgx_br_err(f->parent, "AddStr: SeqEnc entry doesn't exist\n");
return -ENOENT;
}
if (edgx_frer_check_strhdl(str, f) || _seqenc_check_type(type, f))
return -EINVAL;
/* If the string is already added to the list nothing to be done */
if (!_seqenc_list_entry_exists(str, f, port)) {
if (_seqenc_list_add(str, f, port))
return -ENOMEM;
}
s = _seqenc_rbt_search(&f->se_root, str);
if (s) {
//if stream already exists in rbtree, just update port mask
s->port_mask |= BIT(port);
} else {
s = kzalloc(sizeof(*s), GFP_KERNEL);
if (!s)
return -ENOMEM;
s->port_mask = BIT(port);
s->strhdl = str;
if (_seqenc_rbt_insert_node(s, f)) {
kfree(s);
return -EINVAL;
}
}
edgx_frer_seqenc_apply(f, port);
return 0;
}
static int edgx_frer_seqenc_set_active(u8 active, u16 port, struct edgx_frer *f)
{
/* If entry doesn't exist */
if (f->se_ports[port].active == ENC_UNDEFINED) {
edgx_br_err(f->parent,
"SetActive: SeqEnc entry doesn't exist\n");
return -ENOENT;
} else if (_seqenc_check_active(active, f)) {
edgx_br_err(f->parent,
"SetActive value incorrect, set to either Passive(0), Active(1)\n");
return -EINVAL;
}
f->se_ports[port].active = active ? 1 : 0;
edgx_frer_seqenc_apply(f, port);
return 0;
}
static int _seqenc_del_str_from_rbt(u16 strhdl, struct edgx_frer *f)
{
struct _se_streams_rbt *se_rbt;
se_rbt = _seqenc_rbt_search(&f->se_root, strhdl);
if (se_rbt) {
rb_erase(&se_rbt->rbnode, &f->se_root);
RB_CLEAR_NODE(&se_rbt->rbnode);
kfree(se_rbt);
} else {
return -ENOENT;
}
return 0;
}
static int edgx_frer_seqenc_delportfn(u16 port, struct edgx_frer *f)
{
struct list_head *tmp;
struct list_head *pos;
struct _se_strlist *l_se;
struct _se_streams_rbt *se_rbt;
if (f->se_ports[port].active == ENC_UNDEFINED)
return -ENOENT;
if (!list_empty(&f->se_ports[port].se_list)) {
list_for_each_safe(pos, tmp, &f->se_ports[port].se_list) {
l_se = list_entry(pos, struct _se_strlist, entry);
se_rbt = _seqenc_rbt_search(&f->se_root, l_se->strhdl);
if (se_rbt) {
se_rbt->port_mask &= ~BIT(port);
if (!se_rbt->port_mask)
_seqenc_del_str_from_rbt(l_se->strhdl,
f);
} else {
edgx_br_err(f->parent,
"Undefined behavior: Stream handle %d might not be added properly\n",
l_se->strhdl);
return -EINVAL;
}
list_del(pos);
edgx_frer_seqenc_applydel(f, port, l_se->strhdl,
se_rbt->port_mask);
}