-
Notifications
You must be signed in to change notification settings - Fork 79
/
ksz_mrp.c
8394 lines (7476 loc) · 204 KB
/
ksz_mrp.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
/**
* Microchip MRP driver code
*
* Copyright (c) 2015-2018 Microchip Technology Inc.
* Tristram Ha <Tristram.Ha@microchip.com>
*
* Copyright (c) 2014-2015 Micrel, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that 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.
*/
#if 0
#define DEBUG_MRP_MEM
#endif
#if 1
#define DEBUG_MRP_OPER
#endif
#if 1
#define DEBUG_MVRP
static int dbg_mrp_vlan = 1;
#endif
#if 1
#define DEBUG_MSRP
#endif
#if 1
static int mrp_10_1_2f_hack;
static int mrp_10_1_8a_hack;
static int mrp_10_5_1_hack;
static int mrp_10_5_1c_hack;
static int mrp_10_5_1d_hack;
static int msrp_35_1_14g_hack;
static int fqtss_hack;
static int fqtss_34_2_3_hack;
static int fqtss_34_2_1b_hack;
static int fqtss_34_2_5b_hack;
static int fqtss_34_2_9b_hack;
static int regeneration_hack;
#endif
static const u8 bcast_addr[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
static const u8 maap_addr[][6] = {
/* Used by MAAP. */
{ 0x91, 0xE0, 0xF0, 0x00, 0xFF, 0x00 },
/* Used by AVB Discovery Protocol. */
{ 0x91, 0xE0, 0xF0, 0x01, 0x00, 0x00 },
/* Used by AVB AECP Test Status Message. */
{ 0x01, 0x1B, 0xC5, 0x0A, 0xC0, 0x00 },
/* This is sent by Mac and contains only a repeat of these bytes!? */
{ 0xAB, 0xBA, 0xCA, 0xFE, 0xBE, 0xEF },
};
#define BCAST_DA_ACL_ENTRY 10
#define AVB_BOUNDARY_ACL_ENTRY 11
struct avtpdu {
u8 subtype;
#ifdef __BIG_ENDIAN_BITFIELD
u8 sv:1;
u8 version:3;
u8 mr:1;
u8 fsd:2;
u8 tv:2;
#else
u8 tv:2;
u8 fsd:2;
u8 mr:1;
u8 version:3;
u8 sv:1;
#endif
u8 seq_num;
#ifdef __BIG_ENDIAN_BITFIELD
u8 format_spec_data_1:7;
u8 tu:1;
#else
u8 tu:1;
u8 format_spec_data_1:7;
#endif
u8 stream_id[8];
u32 avtp_timestamp;
u32 format_spec_data_2;
u16 stream_data_len;
u16 format_spec_data_3;
} __packed;
struct avtpdu_ctrl {
#ifdef __BIG_ENDIAN_BITFIELD
u32 subtype:8;
u32 sv:1;
u32 version:3;
u32 format_spec_data:9;
u32 ctrl_data_len:11;
#else
u32 ctrl_data_len:11;
u32 format_spec_data:9;
u8 version:3;
u8 sv:1;
u32 subtype:8;
#endif
u8 stream_id[8];
} __packed;
struct maap_pdu {
u8 subtype;
#ifdef __BIG_ENDIAN_BITFIELD
u8 sv:1;
u8 version:3;
u8 message_type:4;
u16 maap_version:5;
u16 ctrl_data_len:11;
#else
u8 message_type:4;
u8 version:3;
u8 sv:1;
union {
struct {
u16 ctrl_data_len:11;
u16 maap_version:5;
} __packed data_len;
u16 data;
} __packed len;
#endif
u8 stream_id[8];
u8 req_start_addr[6];
u16 req_cnt;
u8 conflict_start_addr[6];
u16 conflict_cnt;
} __packed;
#define AVTP_SUBTYPE_AAF 0x02
#define AVTP_SUBTYPE_CVF 0x03
#define AVTP_SUBTYPE_CRF 0x04
#define AVTP_SUBTYPE_TSCF 0x05
#define AVTP_SUBTYPE_SVF 0x06
#define AVTP_SUBTYPE_RVF 0x07
#define AVTP_SUBTYPE_VSF_STREAM 0x6F
#define AVTP_SUBTYPE_EF_STREAM 0x7F
#define AVTP_SUBTYPE_NTSCF 0x82
#define AVTP_SUBTYPE_ADP 0xFA
#define AVTP_SUBTYPE_AECP 0xFB
#define AVTP_SUBTYPE_ACMP 0xFC
#define AVTP_SUBTYPE_MAAP 0xFE
#define AVTP_SUBTYPE_EF_CONTROL 0xFF
static struct mrp_port_info *mrp_get_port_info(struct mrp_info *mrp, u8 port)
{
return &mrp->port_info[port];
} /* mrp_get_port_info */
static void setup_acl_drop(struct mrp_info *mrp, uint port)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
struct ksz_port_cfg *cfg = get_port_cfg(sw, port);
struct ksz_acl_table *acl;
int i = BCAST_DA_ACL_ENTRY;
mutex_lock(&sw->acllock);
acl = &cfg->acl_info[i];
acl->mode = ACL_MODE_LAYER_2;
acl->enable = ACL_ENABLE_2_BOTH;
acl->equal = 1;
acl->src = 0;
memcpy(acl->mac, bcast_addr, ETH_ALEN);
acl->eth_type = 0x22F0;
acl->first_rule = i;
acl->ruleset = (1 << i);
acl->ruleset = 0;
acl->map_mode = ACL_MAP_MODE_REPLACE;
acl->ports = 0;
sw_w_acl_table(sw, port, i, acl);
mutex_unlock(&sw->acllock);
} /* setup_acl_drop */
static void setup_acl_remap(struct mrp_info *mrp, uint port)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
struct ksz_port_cfg *cfg = get_port_cfg(sw, port);
struct mrp_port_info *info = mrp_get_port_info(mrp, port);
struct ksz_acl_table *acl;
int i = AVB_BOUNDARY_ACL_ENTRY;
mutex_lock(&sw->acllock);
acl = &cfg->acl_info[i];
acl->mode = ACL_MODE_LAYER_2;
acl->enable = ACL_ENABLE_2_TYPE;
acl->equal = 1;
acl->eth_type = 0x22F0;
if (regeneration_hack) {
acl->eth_type = 0;
acl->equal = 0;
}
acl->first_rule = i;
acl->ruleset = (1 << i);
acl->map_mode = ACL_MAP_MODE_DISABLE;
acl->ports = 0;
acl->prio_mode = ACL_PRIO_MODE_REPLACE;
acl->prio = info->priority[SR_CLASS_B].regenerated_priority;
acl->vlan_prio_replace = 1;
acl->vlan_prio = info->priority[SR_CLASS_B].regenerated_priority;
sw_w_acl_table(sw, port, i, acl);
mutex_unlock(&sw->acllock);
} /* setup_acl_remap */
static void enable_acl_remap(struct mrp_info *mrp, uint port, bool remap)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
struct ksz_port_cfg *cfg = get_port_cfg(sw, port);
struct ksz_acl_table *acl;
int i = AVB_BOUNDARY_ACL_ENTRY;
mutex_lock(&sw->acllock);
acl = &cfg->acl_info[i];
dbg_msg(" remap: %d=%d %d:%d\n", port, remap, cfg->avb_a, cfg->avb_b);
if (remap)
acl->ruleset = (1 << i);
else
acl->ruleset = 0;
sw_w_acl_ruleset(sw, port, i, acl);
i = BCAST_DA_ACL_ENTRY;
acl = &cfg->acl_info[i];
if (remap)
acl->ruleset = 0;
else
acl->ruleset = (1 << i);
sw_w_acl_ruleset(sw, port, i, acl);
mutex_unlock(&sw->acllock);
sw->ops->acquire(sw);
if (!remap) {
if (!(mrp->mcast_ports & (1 << port))) {
mrp->mcast_ports |= (1 << port);
if (!mrp->mcast_port_cnt)
sw->ops->fwd_unk_mcast(sw, true);
mrp->mcast_port_cnt++;
}
} else {
if (mrp->mcast_ports & (1 << port)) {
mrp->mcast_ports &= ~(1 << port);
mrp->mcast_port_cnt--;
if (!mrp->mcast_port_cnt)
sw->ops->fwd_unk_mcast(sw, false);
}
}
sw->ops->release(sw);
} /* enable_acl_remap */
static int is_host_port(struct mrp_info *mrp, u8 port)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
return (port == sw->HOST_PORT);
} /* is_host_port */
#ifdef CONFIG_KSZ_MSRP
#define SW_CREDIT_SHAPING_SCALE 0x10000
#define SW_CREDIT_SHAPING_S 16
#define CREDIT_PERCENTAGE_S (16 + 9)
#define NETWORK_SPEED_IN_MBIT 1000000
static char *format_num(char *str, u32 num)
{
u32 num0;
u32 num1;
u32 num2;
u32 num3;
num0 = num % 1000;
num1 = (num / 1000) % 1000;
num2 = (num / 1000000) % 1000;
num3 = (num / 1000000000);
if (num3)
sprintf(str, "%u,%03u,%03u,%03u", num3, num2, num1, num0);
else if (num2)
sprintf(str, "%u,%03u,%03u", num2, num1, num0);
else if (num1)
sprintf(str, "%u,%3u", num1, num0);
else
sprintf(str, "%u", num0);
return str;
} /* format_num */
static char *format_per(char *str, u32 per)
{
u64 val;
u32 num;
u32 num0;
u32 num1;
val = per;
val *= 1000;
val += 1 << (CREDIT_PERCENTAGE_S - 1);
val >>= CREDIT_PERCENTAGE_S;
num = (u32) val;
num0 = num % 1000;
num1 = num / 1000;
if (num1)
sprintf(str, "%u.%03u%%", num1, num0);
else
sprintf(str, "0.%03u%%", num0);
return str;
} /* format_per */
static u16 get_credit_increment(u32 speed, u32 bandwidth)
{
u64 val;
u32 rem;
speed *= NETWORK_SPEED_IN_MBIT;
/* Cannot get higher than the running speed. */
if (bandwidth > speed)
return 0;
val = bandwidth;
val <<= SW_CREDIT_SHAPING_S;
val += speed / 2;
val = div_u64_rem(val, speed, &rem);
/* Cannot become zero. */
if (!val)
val = 1;
return (u16) val;
} /* get_credit_increment */
static u16 get_credit_watermark(u16 size)
{
/* Preamable + vlan_ethhdr + CRC + IFG = 42 */
size += 8 + 14 + 4 + 4 + 12;
return size;
} /* get_credit_watermark */
static u32 get_idle_slope(u32 speed, u32 bandwidth)
{
u64 val;
u32 rem;
speed *= NETWORK_SPEED_IN_MBIT;
/* Cannot get higher than the running speed. */
if (bandwidth > speed)
return 0;
val = bandwidth;
val *= 100;
val <<= CREDIT_PERCENTAGE_S;
val = div_u64_rem(val, speed, &rem);
return (u32) val;
} /* get_idle_slope */
static u32 get_send_slope(u32 idle_slope)
{
u32 slope;
slope = 100;
slope <<= CREDIT_PERCENTAGE_S;
slope -= idle_slope;
return slope;
} /* get_send_slope */
static u8 get_queue_priority(struct mrp_info *mrp, int tc)
{
return mrp->queue[tc];
} /* get_queue_priority */
static void srp_cfg_credit_shaper(struct mrp_info *mrp, uint port,
struct mrp_port_info *info, struct mrp_traffic_info *traffic)
{
u16 credit;
u32 idle;
u32 send;
u8 queue;
#ifdef DEBUG_MRP_OPER
char bw_str[20];
char idle_str[20];
char send_str[20];
#endif
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
if (!(sw->features & AVB_SUPPORT))
return;
credit = get_credit_increment(info->speed, traffic->bandwidth_used);
idle = get_idle_slope(info->speed, traffic->bandwidth_used);
send = get_send_slope(idle);
queue = traffic->queue;
info->bandwidth[traffic->queue].operIdleSlope = idle;
#ifdef DEBUG_MRP_OPER
format_num(bw_str, traffic->bandwidth_used);
format_per(idle_str, idle);
format_per(send_str, send);
dbg_msg(" %s %d:%d=%u; %s %s %s\n", __func__, port,
queue, credit,
bw_str, idle_str, send_str);
#endif
#if defined(CONFIG_HAVE_KSZ9897) || defined(CONFIG_HAVE_LAN937X)
do {
if (credit < 2)
credit = info->credit[queue];
sw->reg->lock(sw);
if (credit > 1) {
port_set_increment(sw, port, queue, credit);
#if 0
port_set_schedule_mode(sw, port, queue,
MTI_SCHEDULE_STRICT_PRIO);
port_set_shaping(sw, port, queue, MTI_SHAPING_SRP);
#endif
} else {
#if 0
port_set_schedule_mode(sw, port, queue,
MTI_SCHEDULE_WRR);
port_set_shaping(sw, port, queue, MTI_SHAPING_OFF);
#endif
}
sw->reg->unlock(sw);
} while (0);
#endif
} /* srp_cfg_credit_shaper */
static void srp_cfg_idle_slope(struct mrp_info *mrp, uint port, uint queue,
struct mrp_port_info *info, u32 idle)
{
u16 credit;
u16 credit_lo;
u16 credit_hi;
u32 send;
u32 rem;
u64 idle_slope;
#if 0
char bw_str[20];
char idle_str[20];
char send_str[20];
#endif
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
idle_slope = info->speed;
idle_slope *= NETWORK_SPEED_IN_MBIT;
idle_slope *= idle;
send = 100;
send <<= CREDIT_PERCENTAGE_S;
idle_slope = div_u64_rem(idle_slope, send, &rem);
credit = get_credit_increment(info->speed, idle_slope);
info->credit[queue] = credit;
send = get_send_slope(idle);
credit_hi = get_credit_watermark(mrp->max_interference_size);
credit_lo = get_credit_watermark(1500);
#if 0
format_num(bw_str, idle_slope);
format_per(idle_str, idle);
format_per(send_str, send);
dbg_msg(" %s %d:%d=%u %u %u; %s %s %s\n", __func__, port,
queue, credit, credit_hi, credit_lo,
bw_str, idle_str, send_str);
#endif
#if defined(CONFIG_HAVE_KSZ9897) || defined(CONFIG_HAVE_LAN937X)
sw->reg->lock(sw);
if (credit > 1) {
port_set_hi_water_mark(sw, port, queue, credit_hi);
port_set_lo_water_mark(sw, port, queue, credit_lo);
port_set_increment(sw, port, queue, credit);
port_set_schedule_mode(sw, port, queue,
MTI_SCHEDULE_STRICT_PRIO);
port_set_shaping(sw, port, queue, MTI_SHAPING_SRP);
} else {
port_set_schedule_mode(sw, port, queue,
MTI_SCHEDULE_WRR);
port_set_shaping(sw, port, queue, MTI_SHAPING_OFF);
}
sw->reg->unlock(sw);
#endif
} /* srp_cfg_idle_slope */
#endif
static void mrp_cfg_dest_addr(struct mrp_info *mrp, u8 index, u8 *dest,
u32 ports, u16 fid)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
#ifdef DEBUG_MRP_OPER
dbg_msg(" %s %d=%02x:%02x:%02x:%02x:%02x:%02x %04x %x\n", __func__, index,
dest[0], dest[1], dest[2], dest[3], dest[4],dest[5],
ports, fid);
#endif
ports &= SRP_PORT_AVAIL | SRP_PORT_READY;
sw->ops->cfg_mac(sw, index, dest, ports, false, fid != 0, fid);
} /* mrp_cfg_dest_addr */
static void mrp_cfg_vlan(struct mrp_info *mrp, u8 index, u16 vid, u16 fid,
u32 ports)
{
struct ksz_sw *sw = container_of(mrp, struct ksz_sw, mrp);
#ifdef DEBUG_MVRP
if (dbg_mrp_vlan || vid == 2 || vid > 4090)
dbg_msg(" %s %d=%x %x %04x\n", __func__, index, vid, fid, ports);
#endif
ports &= SRP_PORT_AVAIL | SRP_PORT_READY;
#if 0
/* Needed to catch unwanted multicast traffic. */
if (ports)
ports |= sw->HOST_MASK;
#endif
sw->ops->cfg_vlan(sw, index, vid, fid, ports);
} /* mrp_cfg_vlan */
#if 0
static int mrp_req_cfg_vlan(struct mrp_info *mrp, u8 index, u16 vid, u16 fid,
u32 ports)
{
struct sk_buff *skb;
u16 *data;
u32 *dword;
skb = alloc_skb(64, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
data = (u16 *)skb->data;
*data++ = index;
*data++ = vid;
*data++ = fid;
dword = (u32 *)data;
*dword = ports;
skb_queue_tail(&mrp->vlanq, skb);
if (!mrp->vlanq_sched) {
mrp->vlanq_sched = 1;
schedule_work(&mrp->cfg_vlan);
}
return 0;
} /* mrp_req_cfg_vlan */
#endif
static void mrp_cfg_vlan_work(struct work_struct *work)
{
#ifdef DEBUG_MVRP
int save_dbg_vlan;
#endif
u16 *data;
u32 *dword;
u8 index;
u16 vid;
u16 fid;
u32 ports;
bool last;
struct sk_buff *skb;
struct mrp_info *mrp = container_of(work, struct mrp_info, cfg_vlan);
last = skb_queue_empty(&mrp->vlanq);
while (!last) {
skb = skb_dequeue(&mrp->vlanq);
last = skb_queue_empty(&mrp->vlanq);
if (!skb)
continue;
data = (u16 *)skb->data;
index = (u8) *data++;
vid = *data++;
fid = *data++;
dword = (u32 *)data;
ports = *dword;
#ifdef DEBUG_MVRP
save_dbg_vlan = dbg_mrp_vlan;
dbg_mrp_vlan = 0;
#endif
mrp_cfg_vlan(mrp, index, vid, fid, ports);
#ifdef DEBUG_MVRP
dbg_mrp_vlan = save_dbg_vlan;
#endif
kfree_skb(skb);
}
mrp->vlanq_sched = 0;
} /* mrp_cfg_vlan_work */
#ifdef CONFIG_KSZ_MSRP
static int get_traffic_index(int tc)
{
if (SR_CLASS_A == tc)
return 1;
return 0;
}
static int get_traffic_class(struct mrp_info *mrp, u8 prio)
{
return mrp->tc[prio];
} /* get_traffic_class */
static struct mrp_traffic_info *get_traffic_info(struct mrp_port_info *port,
int tc)
{
if (SR_CLASS_A == tc)
return &port->traffic[1];
else
return &port->traffic[0];
} /* get_traffic_info */
static int frames_per_sec(int traffic_class)
{
int frames;
switch (traffic_class) {
case SR_CLASS_A:
frames = 8000;
break;
case SR_CLASS_B:
default:
frames = 4000;
}
return frames;
} /* frames_per_sec */
static u64 calculate_bandwidth(u32 size, u32 interval, u32 frames)
{
u64 bandwidth;
/* Mininum frame size is 68. */
#if 1
if (size < 46 && fqtss_34_2_5b_hack)
size = 46 - 4;
else
#endif
if (size < 46)
size = 46;
#if 1
if (size < 100 && fqtss_34_2_9b_hack)
size = 100;
#endif
bandwidth = size;
/* Preamable + vlan_ethhdr + CRC + IFG */
bandwidth += 8 + 14 + 4 + 4 + 12;
/* AVnu MSRP test says to add one more byte for bridge. */
bandwidth += 1;
bandwidth *= interval;
bandwidth *= frames;
bandwidth *= 8;
return bandwidth;
} /* calculate_bandwidth */
#endif
static int cmp_mac(void *first, void *second)
{
int cmp;
struct mrp_mac_info *a = first;
struct mrp_mac_info *b = second;
#if 0
dbg_msg("%s %02x:%02x:%02x:%02x:%02x:%02x=%x %02x:%02x:%02x:%02x:%02x:%02x=%x\n", __func__,
a->addr[0], a->addr[1], a->addr[2], a->addr[3], a->addr[4], a->addr[5], a->fid,
b->addr[0], b->addr[1], b->addr[2], b->addr[3], b->addr[4], b->addr[5], b->fid);
#endif
cmp = a->fid - b->fid;
if (!cmp)
cmp = memcmp(a->addr, b->addr, ETH_ALEN);
return cmp;
} /* cmp_mac */
static void show_mac_info(void *this)
{
struct mrp_mac_info *info = this;
dbg_msg(
"%02x:%02x:%02x:%02x:%02x:%02x %d=%03x r:%04x s:%04x t:%04x\n",
info->addr[0], info->addr[1], info->addr[2],
info->addr[3], info->addr[4], info->addr[5],
info->index, info->fid,
info->mrp_ports, info->srp_ports, info->tx_ports);
} /* show_mac_info */
static int cmp_vlan(void *first, void *second)
{
int cmp;
struct mrp_vlan_info *a = first;
struct mrp_vlan_info *b = second;
cmp = a->vid - b->vid;
if (!cmp)
cmp = memcmp(a->addr, b->addr, ETH_ALEN);
return cmp;
} /* cmp_vlan */
static void show_vlan_info(void *this)
{
struct mrp_vlan_info *info = this;
if (info->addr[0] != 0xff)
dbg_msg(
"[%02x:%02x:%02x:%02x:%02x:%02x] ",
info->addr[0], info->addr[1], info->addr[2],
info->addr[3], info->addr[4], info->addr[5]);
dbg_msg(
"%d=%03x.%03x r:%04x t:%04x\n", info->index,
info->vid, info->fid, info->ports, info->tx_ports);
} /* show_vlan_info */
#ifdef CONFIG_KSZ_MSRP
static u64 get_stream_age(struct mrp_port_info *info)
{
struct timespec ts;
u64 age;
ts = ktime_to_timespec(ktime_get_real());
age = ts.tv_sec;
age *= 1000000000;
age += ts.tv_nsec;
if (age == info->age)
age++;
info->age = age;
return age;
}
static void prepare_stream_info(struct SRP_reserv *reserv,
struct srp_stream_info *x)
{
x->reserv = reserv;
x->id = reserv->stream->id;
x->age = reserv->streamAge;
x->rank = reserv->stream->rank;
} /* prepare_stream_info */
static int cmp_stream(struct srp_stream_info *a, struct srp_stream_info *b)
{
int cmp;
/* Only rank and stream age are compared. */
cmp = a->rank - b->rank;
if (cmp)
return cmp;
if (a->age > b->age)
return 1;
else if (a->age < b->age)
return -1;
cmp = memcmp(a->id, b->id, 8);
return cmp;
} /* cmp_stream */
static int cmp_lower_stream(void *first, void *second)
{
return cmp_stream(second, first);
} /* cmp_lower_stream */
static int cmp_higher_stream(void *first, void *second)
{
return cmp_stream(first, second);
} /* cmp_higher_stream */
static void show_stream_info(void *this)
{
struct srp_stream_info *info = this;
dbg_msg(
"r:%u t:%08llx %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
info->rank, info->age,
info->id[0], info->id[1], info->id[2], info->id[3],
info->id[4], info->id[5], info->id[6], info->id[7]);
} /* show_stream_info */
#endif
#ifdef DEBUG_MRP_BASIC
static void *get_show(int (*cmp)(void *a, void *b))
{
if (cmp == cmp_mac)
return show_mac_info;
else if (cmp == cmp_vlan)
return show_vlan_info;
#ifdef CONFIG_KSZ_MSRP
else
return show_stream_info;
#else
else
return NULL;
#endif
} /* get_show */
#endif
static void mrp_init_list(struct mrp_node_anchor *list)
{
list->last = &list->anchor;
list->anchor.next = NULL;
} /* mrp_init_list */
static void *mrp_find_node(struct mrp_node_anchor *list,
int (*cmp)(void *a, void *b), void *data)
{
int c;
struct mrp_node *prev;
struct mrp_node *next;
prev = &list->anchor;
next = prev->next;
while (next) {
c = cmp(next->data, data);
/* Exact match. */
if (!c)
goto mrp_find_node_done;
/* Will not be found as list is sorted. */
if (c > 0) {
#ifdef DEBUG_MRP_BASIC
void (*show)(void *this) = get_show(cmp);
dbg_msg(" %s ", __func__);
show(next->data);
#endif
next = NULL;
break;
}
prev = next;
next = prev->next;
}
mrp_find_node_done:
list->last = prev;
return next;
} /* mrp_find_node */
static void mrp_insert_node(struct mrp_node_anchor *list,
int (*cmp)(void *a, void *b), struct mrp_node *this)
{
int c;
struct mrp_node *prev;
struct mrp_node *next;
if (list->last != &list->anchor) {
prev = list->last;
next = prev->next;
#ifdef DEBUG_MRP_BASIC
dbg_msg(" %s ", __func__);
if (next) {
void (*show)(void *this) = get_show(cmp);
show(next->data);
} else
dbg_msg("last one\n");
#endif
c = 1;
if (next) {
c = cmp(next->data, this->data);
}
if (c > 0) {
this->next = prev->next;
prev->next = this;
list->last = &list->anchor;
list->cnt++;
return;
}
}
prev = &list->anchor;
next = prev->next;
while (next) {
c = cmp(next->data, this->data);
/* Stop if next one is higher in the list. */
if (c > 0)
break;
prev = next;
next = prev->next;
}
this->next = prev->next;
prev->next = this;
list->last = &list->anchor;
list->cnt++;
} /* mrp_insert_node */
static struct mrp_node *mrp_delete_this_node(struct mrp_node_anchor *list,
int (*cmp)(void *a, void *b), struct mrp_node *this, int delete)
{
int c;
struct mrp_node *prev;
struct mrp_node *next;
if (list->last != &list->anchor) {
prev = list->last;
next = prev->next;
#ifdef DEBUG_MRP_BASIC
dbg_msg(" %s ", __func__);
if (next) {
void (*show)(void *this) = get_show(cmp);
show(next->data);
} else
dbg_msg("last one\n");
#endif
c = 1;
if (next == this)
c = 0;
else if (next)
c = cmp(next->data, this->data);
/* Exact match. */
if (!c)
goto mrp_delete_this_node_done;
}
prev = &list->anchor;
next = prev->next;
while (next) {
c = cmp(next->data, this->data);
/* Exact match. */
if (!c)
goto mrp_delete_this_node_done;
/* Stop if next one is higher in the list. */
if (c > 0)
break;
prev = next;
next = prev->next;
}
/* Nothing is removed. */
return NULL;
mrp_delete_this_node_done:
prev->next = this->next;
list->last = &list->anchor;
list->cnt--;
/* Just remove the node. */
if (!delete)
return next;
/* Free the node data. */
#ifdef DEBUG_MRP_MEM
if (delete > 1)
dbg_msg(" %s %p\n", __func__, next->data);
#endif
if (delete > 1)
kfree(next->data);
#ifdef DEBUG_MRP_MEM
dbg_msg(" %s %p\n", __func__, next);
#endif
/* Free the node. */
kfree(next);
return NULL;
} /* mrp_delete_this_node */
static void mrp_delete_node(struct mrp_node_anchor *list,
int (*cmp)(void *a, void *b), struct mrp_node *this)
{
mrp_delete_this_node(list, cmp, this, 2);
} /* mrp_delete_node */
static struct mrp_node *mrp_remove_node(struct mrp_node_anchor *list,
int (*cmp)(void *a, void *b), struct mrp_node *this)
{
return mrp_delete_this_node(list, cmp, this, 0);
} /* mrp_remove_node */
static void mrp_show_node(struct mrp_node_anchor *list,
void (*show)(void *a))
{
struct mrp_node *next;
next = list->anchor.next;
while (next) {
show(next->data);
next = next->next;
}
} /* mrp_show_node */
#ifdef CONFIG_KSZ_MSRP
static struct SRP_stream *srp_create_stream(u8 *id, u8 *dest, u16 vlan_id,
u16 size, u16 interval, u8 prio, u8 rank, u8 reserved, u32 latency)
{
struct SRP_stream *stream;
stream = kzalloc(sizeof(struct SRP_stream), GFP_KERNEL);
#ifdef DEBUG_MRP_MEM
dbg_msg(" %s %p\n", __func__, stream);
#endif
if (stream) {