-
Notifications
You must be signed in to change notification settings - Fork 115
/
esp_sip.c
executable file
·2374 lines (2004 loc) · 77 KB
/
esp_sip.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 (c) 2009 - 2014 Espressif System.
*
* Serial Interconnctor Protocol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#include <linux/ieee80211.h>
#include <net/mac80211.h>
#include <net/cfg80211.h>
#include <linux/skbuff.h>
#include <linux/bitops.h>
#include <linux/version.h>
#include <linux/mmc/card.h>
#include <linux/mmc/mmc.h>
#include <linux/mmc/host.h>
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>
#include <linux/mmc/sdio.h>
#include <linux/mmc/sd.h>
#include <linux/completion.h>
#include "esp_mac80211.h"
#include "esp_pub.h"
#include "esp_sip.h"
#include "esp_ctrl.h"
#include "esp_sif.h"
#include "esp_debug.h"
#include "slc_host_register.h"
#include "esp_wmac.h"
#include "esp_utils.h"
#ifdef TEST_MODE
#include "testmode.h"
#endif
#ifdef USE_EXT_GPIO
#include "esp_ext.h"
#endif /* USE_EXT_GPIO */
extern struct completion *gl_bootup_cplx;
static int old_signal = -35;
static int avg_signal = 0;
static int signal_loop = 0;
struct esp_mac_prefix esp_mac_prefix_table[] = {
{0,{0x18,0xfe,0x34}},
{1,{0xac,0xd0,0x74}},
{255,{0x18,0xfe,0x34}},
};
#define SIGNAL_COUNT 300
#define TID_TO_AC(_tid) ((_tid)== 0||((_tid)==3)?WME_AC_BE:((_tid)<3)?WME_AC_BK:((_tid)<6)?WME_AC_VI:WME_AC_VO)
#ifdef SIP_DEBUG
#define esp_sip_dbg esp_dbg
struct sip_trace {
u32 tx_data;
u32 tx_cmd;
u32 rx_data;
u32 rx_evt;
u32 rx_tx_status;
u32 tx_out_of_credit;
u32 tx_one_shot_overflow;
};
static struct sip_trace str;
#define STRACE_TX_DATA_INC() (str.tx_data++)
#define STRACE_TX_CMD_INC() (str.tx_cmd++)
#define STRACE_RX_DATA_INC() (str.rx_data++)
#define STRACE_RX_EVENT_INC() (str.rx_evt++)
#define STRACE_RX_TXSTATUS_INC() (str.rx_tx_status++)
#define STRACE_TX_OUT_OF_CREDIT_INC() (str.tx_out_of_credit++)
#define STRACE_TX_ONE_SHOT_INC() (str.tx_one_shot_overflow++)
#define STRACE_SHOW(sip)
#else
#define esp_sip_dbg(...)
#define STRACE_TX_DATA_INC()
#define STRACE_TX_CMD_INC()
#define STRACE_RX_DATA_INC()
#define STRACE_RX_EVENT_INC()
#define STRACE_RX_TXSTATUS_INC()
#define STRACE_TX_OUT_OF_CREDIT_INC()
#define STRACE_TX_ONE_SHOT_INC()
#define STRACE_SHOW(sip)
#endif /* SIP_DEBUG */
#define SIP_STOP_QUEUE_THRESHOLD 48
#define SIP_RESUME_QUEUE_THRESHOLD 12
#ifndef FAST_TX_STATUS
#define SIP_PENDING_STOP_TX_THRESHOLD 6
#define SIP_PENDING_RESUME_TX_THRESHOLD 6
#endif /* !FAST_TX_STATUS */
#define SIP_MIN_DATA_PKT_LEN (sizeof(struct esp_mac_rx_ctrl) + 24) //24 is min 80211hdr
#ifdef ESP_PREALLOC
extern struct sk_buff *esp_get_sip_skb(int size);
extern void esp_put_sip_skb(struct sk_buff **skb);
extern u8 *esp_get_tx_aggr_buf(void);
extern void esp_put_tx_aggr_buf(u8 **p);
#endif
static void sip_recalc_credit_init(struct esp_sip *sip);
static int sip_recalc_credit_claim(struct esp_sip *sip, int force);
static void sip_recalc_credit_release(struct esp_sip *sip);
static struct sip_pkt *sip_get_ctrl_buf(struct esp_sip *sip, SIP_BUF_TYPE bftype);
static void sip_reclaim_ctrl_buf(struct esp_sip *sip, struct sip_pkt *pkt, SIP_BUF_TYPE bftype);
static void sip_free_init_ctrl_buf(struct esp_sip *sip);
static int sip_pack_pkt(struct esp_sip *sip, struct sk_buff *skb, int *pm_state);
static struct esp_mac_rx_ctrl *sip_parse_normal_mac_ctrl(struct sk_buff *skb, int * pkt_len_enc, int *buf_len, int *pulled_len);
static struct sk_buff * sip_parse_data_rx_info(struct esp_sip *sip, struct sk_buff *skb, int pkt_len_enc, int buf_len, struct esp_mac_rx_ctrl *mac_ctrl, int *pulled_len);
static inline void sip_rx_pkt_enqueue(struct esp_sip *sip, struct sk_buff *skb);
#ifndef FAST_TX_STATUS
static void sip_after_tx_status_update(struct esp_sip *sip);
#endif /* !FAST_TX_STATUS */
static void sip_after_write_pkts(struct esp_sip *sip);
static void sip_update_tx_credits(struct esp_sip *sip, u16 recycled_credits);
//static void sip_trigger_txq_process(struct esp_sip *sip);
static bool sip_rx_pkt_process(struct esp_sip * sip, struct sk_buff *skb);
#ifdef FAST_TX_STATUS
static void sip_tx_status_report(struct esp_sip *sip, struct sk_buff *skb, struct ieee80211_tx_info* tx_info, bool success);
#endif /* FAST_TX_STATUS */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
static void sip_check_skb_alignment(struct sk_buff *skb);
#endif /* NEW_KERNEL */
#ifdef FPGA_TXDATA
int sip_send_tx_data(struct esp_sip *sip);
#endif/* FPGA_TXDATA */
#ifdef FPGA_LOOPBACK
int sip_send_loopback_cmd_mblk(struct esp_sip *sip);
#endif /* FPGA_LOOPBACK */
static bool check_ac_tid(u8 *pkt, u8 ac, u8 tid)
{
struct ieee80211_hdr * wh = (struct ieee80211_hdr *)pkt;
#ifdef TID_DEBUG
u16 real_tid = 0;
#endif //TID_DEBUG
if (ieee80211_is_data_qos(wh->frame_control)) {
#ifdef TID_DEBUG
real_tid = *ieee80211_get_qos_ctl(wh) & IEEE80211_QOS_CTL_TID_MASK;
esp_sip_dbg(ESP_SHOW, "ac:%u, tid:%u, tid in pkt:%u\n", ac, tid, real_tid);
if (tid != real_tid) {
esp_sip_dbg(ESP_DBG_ERROR, "111 ac:%u, tid:%u, tid in pkt:%u\n", ac, tid, real_tid);
}
if (TID_TO_AC(tid) != ac) {
esp_sip_dbg(ESP_DBG_ERROR, "222 ac:%u, tid:%u, tid in pkt:%u\n", ac, tid, real_tid);
}
#endif /* TID_DEBUG*/
} else if (ieee80211_is_mgmt(wh->frame_control)) {
#ifdef TID_DEBUG
esp_sip_dbg(ESP_SHOW, "ac:%u, tid:%u\n", ac, tid);
if (tid != 7 || ac != WME_AC_VO) {
esp_sip_dbg(ESP_DBG_ERROR, "333 ac:%u, tid:%u\n", ac, tid);
}
#endif /* TID_DEBUG*/
} else {
if (ieee80211_is_ctl(wh->frame_control)) {
#ifdef TID_DEBUG
esp_sip_dbg(ESP_SHOW, "%s is ctrl pkt fc 0x%04x ac:%u, tid:%u, tid in pkt:%u\n", __func__, wh->frame_control, ac, tid, real_tid);
#endif /* TID_DEBUG*/
} else {
if (tid != 0 || ac != WME_AC_BE) {
//show_buf(pkt, 24);
esp_sip_dbg(ESP_DBG_LOG, "444 ac:%u, tid:%u \n", ac, tid);
if (tid == 7 && ac == WME_AC_VO)
return false;
}
return true; //hack to modify non-qos null data.
}
}
return false;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
static void sip_recalc_credit_timeout(unsigned long data)
#else
struct esp_sip *_gsip = NULL;
static void sip_recalc_credit_timeout(struct timer_list *list)
#endif
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
struct esp_sip *sip = (struct esp_sip *)data;
#else
struct esp_sip *sip = (struct esp_sip *)list->flags;
#endif
esp_dbg(ESP_DBG_ERROR, "rct");
sip_recalc_credit_claim(sip, 1); /* recalc again */
}
static void sip_recalc_credit_init(struct esp_sip *sip)
{
atomic_set(&sip->credit_status, RECALC_CREDIT_DISABLE); //set it disable
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
init_timer(&sip->credit_timer);
sip->credit_timer.data = (unsigned long)sip;
sip->credit_timer.function = sip_recalc_credit_timeout;
#else
_gsip = sip;
timer_setup(&sip->credit_timer, sip_recalc_credit_timeout, 0);
#endif
}
static int sip_recalc_credit_claim(struct esp_sip *sip, int force)
{
int ret;
if (atomic_read(&sip->credit_status) == RECALC_CREDIT_ENABLE && force == 0)
return 1;
atomic_set(&sip->credit_status, RECALC_CREDIT_ENABLE);
ret = sip_send_recalc_credit(sip->epub);
if (ret) {
esp_dbg(ESP_DBG_ERROR, "%s error %d", __func__, ret);
return ret;
}
/*setup a timer for handle the abs_credit not receive */
mod_timer(&sip->credit_timer, jiffies + msecs_to_jiffies(2000));
esp_dbg(ESP_SHOW, "rcc");
return ret;
}
static void sip_recalc_credit_release(struct esp_sip *sip)
{
esp_dbg(ESP_SHOW, "rcr");
if (atomic_read(&sip->credit_status) == RECALC_CREDIT_ENABLE) {
atomic_set(&sip->credit_status, RECALC_CREDIT_DISABLE);
del_timer_sync(&sip->credit_timer);
} else
esp_dbg(ESP_SHOW, "maybe bogus credit");
}
static void sip_update_tx_credits(struct esp_sip *sip, u16 recycled_credits)
{
esp_sip_dbg(ESP_DBG_TRACE, "%s:before add, credits is %d\n", __func__, atomic_read(&sip->tx_credits));
if (recycled_credits & 0x800) {
atomic_set(&sip->tx_credits, (recycled_credits & 0x7ff));
sip_recalc_credit_release(sip);
} else
atomic_add(recycled_credits, &sip->tx_credits);
esp_sip_dbg(ESP_DBG_TRACE, "%s:after add %d, credits is %d\n", __func__, recycled_credits, atomic_read(&sip->tx_credits));
}
void sip_trigger_txq_process(struct esp_sip *sip)
{
if (atomic_read(&sip->tx_credits) <= sip->credit_to_reserve + SIP_CTRL_CREDIT_RESERVE //no credits, do nothing
|| atomic_read(&sip->credit_status) == RECALC_CREDIT_ENABLE)
return;
if (sip_queue_may_resume(sip)) {
/* wakeup upper queue only if we have sufficient credits */
esp_sip_dbg(ESP_DBG_TRACE, "%s wakeup ieee80211 txq \n", __func__);
atomic_set(&sip->epub->txq_stopped, false);
ieee80211_wake_queues(sip->epub->hw);
} else if (atomic_read(&sip->epub->txq_stopped) ) {
esp_sip_dbg(ESP_DBG_TRACE, "%s can't wake txq, credits: %d \n", __func__, atomic_read(&sip->tx_credits) );
}
if (!skb_queue_empty(&sip->epub->txq)) {
/* try to send out pkt already in sip queue once we have credits */
esp_sip_dbg(ESP_DBG_TRACE, "%s resume sip txq \n", __func__);
#if !defined(FPGA_TXDATA) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
if(sif_get_ate_config() == 0){
ieee80211_queue_work(sip->epub->hw, &sip->epub->tx_work);
} else {
queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
}
#else
queue_work(sip->epub->esp_wkq, &sip->epub->tx_work);
#endif
}
}
static bool sip_ampdu_occupy_buf(struct esp_sip *sip, struct esp_rx_ampdu_len * ampdu_len)
{
return (ampdu_len->substate == 0 || esp_wmac_rxsec_error(ampdu_len->substate) || (sip->dump_rpbm_err && ampdu_len->substate == RX_RPBM_ERR));
}
static bool sip_rx_pkt_process(struct esp_sip * sip, struct sk_buff *skb)
{
#define DO_NOT_COPY false
#define DO_COPY true
struct sip_hdr * hdr = NULL;
struct sk_buff * rskb = NULL;
int remains_len = 0;
int first_pkt_len = 0;
u8 *bufptr = NULL;
int ret = 0;
bool trigger_rxq = false;
if (skb == NULL) {
esp_sip_dbg(ESP_DBG_ERROR, "%s NULL SKB!!!!!!!! \n", __func__);
return trigger_rxq;
}
hdr = (struct sip_hdr *)skb->data;
bufptr = skb->data;
esp_sip_dbg(ESP_DBG_TRACE, "%s Hcredits 0x%08x, realCredits %d\n", __func__, hdr->h_credits, hdr->h_credits & SIP_CREDITS_MASK);
if (hdr->h_credits & SIP_CREDITS_MASK) {
sip_update_tx_credits(sip, hdr->h_credits & SIP_CREDITS_MASK);
}
hdr->h_credits &= ~SIP_CREDITS_MASK; /* clean credits in sip_hdr, prevent over-add */
esp_sip_dbg(ESP_DBG_TRACE, "%s credits %d\n", __func__, hdr->h_credits);
/*
* first pkt's length is stored in recycled_credits first 20 bits
* config w3 [31:12]
* repair hdr->len of first pkt
*/
remains_len = hdr->len;
first_pkt_len = hdr->h_credits >> 12;
hdr->len = first_pkt_len;
esp_dbg(ESP_DBG_TRACE, "%s first_pkt_len %d, whole pkt len %d \n", __func__, first_pkt_len, remains_len);
if (first_pkt_len > remains_len) {
sip_recalc_credit_claim(sip, 0);
esp_dbg(ESP_DBG_ERROR, "first_pkt_len %d, whole pkt len %d\n", first_pkt_len, remains_len);
show_buf((u8 *)hdr, first_pkt_len);
ESSERT(0);
goto _exit;
}
/*
* pkts handling, including the first pkt, should alloc new skb for each data pkt.
* free the original whole skb after parsing is done.
*/
while (remains_len) {
if (remains_len < sizeof(struct sip_hdr)) {
sip_recalc_credit_claim(sip, 0);
ESSERT(0);
show_buf((u8 *)hdr, 512);
goto _exit;
}
hdr = (struct sip_hdr *)bufptr;
if (hdr->len <= 0) {
sip_recalc_credit_claim(sip, 0);
show_buf((u8 *)hdr, 512);
ESSERT(0);
goto _exit;
}
if((hdr->len & 3) != 0) {
sip_recalc_credit_claim(sip, 0);
show_buf((u8 *)hdr, 512);
ESSERT(0);
goto _exit;
}
if (unlikely(hdr->seq != sip->rxseq++)) {
sip_recalc_credit_claim(sip, 0);
esp_dbg(ESP_DBG_ERROR, "%s seq mismatch! got %u, expect %u\n", __func__, hdr->seq, sip->rxseq-1);
sip->rxseq = hdr->seq + 1;
show_buf(bufptr, 32);
ESSERT(0);
}
if (SIP_HDR_IS_CTRL(hdr)) {
STRACE_RX_EVENT_INC();
esp_sip_dbg(ESP_DBG_TRACE, "seq %u \n", hdr->seq);
ret = sip_parse_events(sip, bufptr);
skb_pull(skb, hdr->len);
} else if (SIP_HDR_IS_DATA(hdr)) {
struct esp_mac_rx_ctrl * mac_ctrl = NULL;
int pkt_len_enc = 0, buf_len = 0, pulled_len = 0;
STRACE_RX_DATA_INC();
esp_sip_dbg(ESP_DBG_TRACE, "seq %u \n", hdr->seq);
mac_ctrl = sip_parse_normal_mac_ctrl(skb, &pkt_len_enc, &buf_len, &pulled_len);
rskb = sip_parse_data_rx_info(sip, skb, pkt_len_enc, buf_len, mac_ctrl, &pulled_len);
if(rskb == NULL)
goto _move_on;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
sip_check_skb_alignment(rskb);
#endif /* !NEW_KERNEL */
if (likely(atomic_read(&sip->epub->wl.off) == 0)) {
#ifndef RX_SENDUP_SYNC
skb_queue_tail(&sip->epub->rxq, rskb);
trigger_rxq = true;
#else
#ifdef RX_CHECKSUM_TEST
esp_rx_checksum_test(rskb);
#endif
local_bh_disable();
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
ieee80211_rx(sip->epub->hw, rskb);
#else
//simulate IEEE80211_SKB_RXCB in 2.6.32
ieee80211_rx(sip->epub->hw, rskb ,(struct ieee80211_rx_status *)rskb->cb);
#endif
local_bh_enable();
#endif /* RX_SENDUP_SYNC */
} else {
/* still need go thro parsing as skb_pull should invoke */
kfree_skb(rskb);
}
} else if (SIP_HDR_IS_AMPDU(hdr)) {
struct esp_mac_rx_ctrl * mac_ctrl = NULL;
struct esp_mac_rx_ctrl new_mac_ctrl;
struct esp_rx_ampdu_len *ampdu_len;
int pkt_num;
int pulled_len = 0;
static int pkt_dropped = 0;
static int pkt_total = 0;
bool have_rxabort = false;
bool have_goodpkt = false;
static u8 frame_head[16];
static u8 frame_buf_ttl = 0;
ampdu_len = (struct esp_rx_ampdu_len *)(skb->data + hdr->len/sip->rx_blksz * sip->rx_blksz);
esp_sip_dbg(ESP_DBG_TRACE, "%s rx ampdu total len %u\n", __func__, hdr->len);
if(skb->data != (u8 *)hdr) {
printk("%p %p\n", skb->data, hdr);
show_buf(skb->data, 512);
show_buf((u8 *)hdr, 512);
ESSERT(0);
goto _exit;
}
mac_ctrl = sip_parse_normal_mac_ctrl(skb, NULL, NULL, &pulled_len);
memcpy(&new_mac_ctrl, mac_ctrl, sizeof(struct esp_mac_rx_ctrl));
mac_ctrl = &new_mac_ctrl;
pkt_num = mac_ctrl->ampdu_cnt;
esp_sip_dbg(ESP_DBG_TRACE, "%s %d rx ampdu %u pkts, %d pkts dumped, first len %u\n",__func__,
__LINE__, (unsigned int)((hdr->len % sip->rx_blksz) / sizeof(struct esp_rx_ampdu_len)),
pkt_num, (unsigned int)ampdu_len->sublen);
pkt_total += mac_ctrl->ampdu_cnt;
//esp_sip_dbg(ESP_DBG_ERROR, "%s ampdu dropped %d/%d\n", __func__, pkt_dropped, pkt_total);
while (pkt_num > 0) {
esp_sip_dbg(ESP_DBG_TRACE, "%s %d ampdu sub state %02x,\n", __func__, __LINE__,
ampdu_len->substate);
if (sip_ampdu_occupy_buf(sip, ampdu_len)) { //pkt is dumped
rskb = sip_parse_data_rx_info(sip, skb, ampdu_len->sublen - FCS_LEN, 0, mac_ctrl, &pulled_len);
if (!rskb) {
ESSERT(0);
goto _exit;
}
if (likely(atomic_read(&sip->epub->wl.off) == 0) &&
(ampdu_len->substate == 0 || ampdu_len->substate == RX_TKIPMIC_ERR ||
(sip->sendup_rpbm_pkt && ampdu_len->substate == RX_RPBM_ERR)) &&
(sip->rxabort_fixed || !have_rxabort) )
{
if(!have_goodpkt) {
have_goodpkt = true;
memcpy(frame_head, rskb->data, 16);
frame_head[1] &= ~0x80;
frame_buf_ttl = 3;
}
#ifndef RX_SENDUP_SYNC
skb_queue_tail(&sip->epub->rxq, rskb);
trigger_rxq = true;
#else
#ifdef RX_CHECKSUM_TEST
esp_rx_checksum_test(rskb);
#endif
local_bh_disable();
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
ieee80211_rx(sip->epub->hw, rskb);
#else
//simulate IEEE80211_SKB_RXCB in 2.6.32
ieee80211_rx(sip->epub->hw, rskb ,(struct ieee80211_rx_status *)rskb->cb);
#endif
local_bh_enable();
#endif /* RX_SENDUP_SYNC */
} else {
kfree_skb(rskb);
}
} else {
if (ampdu_len->substate == RX_ABORT) {
u8 * a;
have_rxabort = true;
esp_sip_dbg(ESP_DBG_TRACE, "rx abort %d %d\n", frame_buf_ttl, pkt_num);
if(frame_buf_ttl && !sip->rxabort_fixed) {
struct esp_rx_ampdu_len * next_good_ampdu_len = ampdu_len + 1;
a = frame_head;
esp_sip_dbg(ESP_DBG_TRACE, "frame:%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
while(!sip_ampdu_occupy_buf(sip, next_good_ampdu_len)) {
if(next_good_ampdu_len > ampdu_len + pkt_num - 1)
break;
next_good_ampdu_len++;
}
if(next_good_ampdu_len <= ampdu_len + pkt_num -1) {
bool b0, b10, b11;
a = skb->data;
esp_sip_dbg(ESP_DBG_TRACE, "buf:%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
b0 = memcmp(frame_head + 4, skb->data + 4, 12) == 0;
b10 = memcmp(frame_head + 10, skb->data, 6) == 0;
b11 = memcpy(frame_head + 11, skb->data, 5) == 0;
esp_sip_dbg(ESP_DBG_TRACE, "com %d %d %d\n", b0, b10, b11);
if(b0 && !b10 && !b11) {
have_rxabort = false;
esp_sip_dbg(ESP_DBG_TRACE, "repair 0\n");
} else if(!b0 && b10 && !b11) {
skb_push(skb, 10);
memcpy(skb->data, frame_head, 10);
have_rxabort = false;
pulled_len -= 10;
esp_sip_dbg(ESP_DBG_TRACE, "repair 10\n");
} else if(!b0 && !b10 && b11) {
skb_push(skb, 11);
memcpy(skb->data, frame_head, 11);
have_rxabort = false;
pulled_len -= 11;
esp_sip_dbg(ESP_DBG_TRACE, "repair 11\n");
}
}
}
}
pkt_dropped++;
esp_sip_dbg(ESP_DBG_LOG, "%s ampdu dropped %d/%d\n", __func__, pkt_dropped, pkt_total);
}
pkt_num--;
ampdu_len++;
}
if(frame_buf_ttl)
frame_buf_ttl--;
skb_pull(skb, hdr->len - pulled_len);
} else {
esp_sip_dbg(ESP_DBG_ERROR, "%s %d unknown type\n", __func__, __LINE__);
}
_move_on:
if (hdr->len < remains_len) {
remains_len -= hdr->len;
} else {
break;
}
bufptr += hdr->len;
}
_exit:
#ifdef ESP_PREALLOC
esp_put_sip_skb(&skb);
#else
kfree_skb(skb);
#endif
return trigger_rxq;
#undef DO_NOT_COPY
#undef DO_COPY
}
static void _sip_rxq_process(struct esp_sip *sip)
{
struct sk_buff *skb = NULL;
bool sendup = false;
while ((skb = skb_dequeue(&sip->rxq))) {
if (sip_rx_pkt_process(sip, skb))
sendup = true;
}
#ifndef RX_SENDUP_SYNC
if (sendup) {
queue_work(sip->epub->esp_wkq, &sip->epub->sendup_work);
}
#endif /* !RX_SENDUP_SYNC */
/* probably tx_credit is updated, try txq */
sip_trigger_txq_process(sip);
}
void sip_rxq_process(struct work_struct *work)
{
struct esp_sip *sip = container_of(work, struct esp_sip, rx_process_work);
if (sip == NULL) {
ESSERT(0);
return;
}
if (unlikely(atomic_read(&sip->state) == SIP_SEND_INIT)) {
sip_send_chip_init(sip);
atomic_set(&sip->state, SIP_WAIT_BOOTUP);
return;
}
mutex_lock(&sip->rx_mtx);
_sip_rxq_process(sip);
mutex_unlock(&sip->rx_mtx);
}
static inline void sip_rx_pkt_enqueue(struct esp_sip *sip, struct sk_buff *skb)
{
skb_queue_tail(&sip->rxq, skb);
}
static inline struct sk_buff * sip_rx_pkt_dequeue(struct esp_sip *sip) {
return skb_dequeue(&sip->rxq);
}
static u32 sip_rx_count = 0;
void sip_debug_show(struct esp_sip *sip)
{
esp_sip_dbg(ESP_DBG_ERROR, "txq left %d %d\n", skb_queue_len(&sip->epub->txq), atomic_read(&sip->tx_data_pkt_queued));
esp_sip_dbg(ESP_DBG_ERROR, "tx queues stop ? %d\n", atomic_read(&sip->epub->txq_stopped));
esp_sip_dbg(ESP_DBG_ERROR, "txq stop? %d\n", test_bit(ESP_WL_FLAG_STOP_TXQ, &sip->epub->wl.flags));
esp_sip_dbg(ESP_DBG_ERROR, "tx credit %d\n", atomic_read(&sip->tx_credits));
esp_sip_dbg(ESP_DBG_ERROR, "rx collect %d\n", sip_rx_count);
sip_rx_count = 0;
}
int sip_rx(struct esp_pub *epub)
{
struct sip_hdr *shdr = NULL;
struct esp_sip *sip = epub->sip;
int err = 0;
struct sk_buff *first_skb = NULL;
u8 *rx_buf = NULL;
u32 rx_blksz;
struct sk_buff *rx_skb = NULL;
u32 first_sz;
first_sz = sif_get_regs(epub)->config_w0;
if (likely(sif_get_ate_config() != 1)) {
do {
u8 raw_seq = sif_get_regs(epub)->intr_raw & 0xff;
if (raw_seq != sip->to_host_seq) {
if (raw_seq == sip->to_host_seq + 1) { /* when last read pkt crc err, this situation may occur, but raw_seq mustn't < to_host_Seq */
sip->to_host_seq = raw_seq;
esp_dbg(ESP_DBG_TRACE, "warn: to_host_seq reg 0x%02x, seq 0x%02x", raw_seq, sip->to_host_seq);
break;
}
esp_dbg(ESP_DBG_ERROR, "err: to_host_seq reg 0x%02x, seq 0x%02x", raw_seq, sip->to_host_seq);
goto _err;
}
} while (0);
}
esp_sip_dbg(ESP_DBG_LOG, "%s enter\n", __func__);
/* first read one block out, if we luck enough, that's it
*
* To make design as simple as possible, we allocate skb(s)
* separately for each sif read operation to avoid global
* read_buf_pointe access. It coule be optimized late.
*/
rx_blksz = sif_get_blksz(epub);
#ifdef ESP_PREALLOC
first_skb = esp_get_sip_skb(roundup(first_sz, rx_blksz));
#else
first_skb = __dev_alloc_skb(roundup(first_sz, rx_blksz), GFP_KERNEL);
#endif /* ESP_PREALLOC */
if (first_skb == NULL) {
sif_unlock_bus(epub);
esp_sip_dbg(ESP_DBG_ERROR, "%s first no memory \n", __func__);
goto _err;
}
rx_buf = skb_put(first_skb, first_sz);
esp_sip_dbg(ESP_DBG_LOG, "%s rx_buf ptr %p, first_sz %d\n", __func__, rx_buf, first_sz);
#ifdef USE_EXT_GPIO
do{
int err2 = 0;
u16 value = 0;
u16 intr_mask = ext_gpio_get_int_mask_reg();
if(!intr_mask)
break;
value = sif_get_regs(epub)->config_w3 & intr_mask;
if(value)
{
err2 = sif_interrupt_target(epub, 6);
esp_sip_dbg(ESP_DBG, "write gpio\n");
}
if(!err2 && value) {
esp_sip_dbg(ESP_DBG_TRACE, "%s intr_mask[0x%04x] value[0x%04x]\n", __func__, intr_mask, value);
ext_gpio_int_process(value);
}
}while(0);
#endif
#ifdef ESP_ACK_INTERRUPT
#ifdef ESP_ACK_LATER
err = esp_common_read(epub, rx_buf, first_sz, ESP_SIF_NOSYNC, false);
sif_platform_ack_interrupt(epub);
#else
sif_platform_ack_interrupt(epub);
err = esp_common_read(epub, rx_buf, first_sz, ESP_SIF_NOSYNC, false);
#endif /* ESP_ACK_LATER */
#else
err = esp_common_read(epub, rx_buf, first_sz, ESP_SIF_NOSYNC, false);
#endif //ESP_ACK_INTERRUPT
sip_rx_count++;
if (unlikely(err)) {
esp_dbg(ESP_DBG_ERROR, " %s first read err %d %d\n", __func__, err, sif_get_regs(epub)->config_w0);
#ifdef ESP_PREALLOC
esp_put_sip_skb(&first_skb);
#else
kfree_skb(first_skb);
#endif /* ESP_PREALLOC */
sif_unlock_bus(epub);
goto _err;
}
shdr = (struct sip_hdr *)rx_buf;
if(SIP_HDR_IS_CTRL(shdr) && (shdr->c_evtid == SIP_EVT_SLEEP)) {
atomic_set(&sip->epub->ps.state, ESP_PM_ON);
esp_dbg(ESP_DBG_TRACE, "s\n");
}
if (likely(sif_get_ate_config() != 1)) {
sip->to_host_seq++;
}
if ((shdr->len & 3) != 0){
esp_sip_dbg(ESP_DBG_ERROR, "%s shdr->len[%d] error\n", __func__, shdr->len);
#ifdef ESP_PREALLOC
esp_put_sip_skb(&first_skb);
#else
kfree_skb(first_skb);
#endif /* ESP_PREALLOC */
sif_unlock_bus(epub);
err = -EIO;
goto _err;
}
if (shdr->len != first_sz){
esp_sip_dbg(ESP_DBG_ERROR, "%s shdr->len[%d] first_size[%d] error\n", __func__, shdr->len, first_sz);
#ifdef ESP_PREALLOC
esp_put_sip_skb(&first_skb);
#else
kfree_skb(first_skb);
#endif /* ESP_PREALLOC */
sif_unlock_bus(epub);
err = -EIO;
goto _err;
} else {
sif_unlock_bus(epub);
skb_trim(first_skb, shdr->len);
esp_dbg(ESP_DBG_TRACE, " %s first_skb only\n", __func__);
rx_skb = first_skb;
}
if (atomic_read(&sip->state) == SIP_STOP) {
#ifdef ESP_PREALLOC
esp_put_sip_skb(&rx_skb);
#else
kfree_skb(rx_skb);
#endif /* ESP_PREALLOC */
esp_sip_dbg(ESP_DBG_ERROR, "%s when sip stopped\n", __func__);
return 0;
}
sip_rx_pkt_enqueue(sip, rx_skb);
queue_work(sip->epub->esp_wkq, &sip->rx_process_work);
_err:
return err;
}
int sip_post_init(struct esp_sip *sip, struct sip_evt_bootup2 *bevt)
{
struct esp_pub *epub;
u8 mac_id = bevt->mac_addr[0];
int mac_index = 0;
int i = 0;
if (sip == NULL) {
ESSERT(0);
return -EINVAL;
}
epub = sip->epub;
sip->tx_aggr_write_ptr = sip->tx_aggr_buf;
sip->tx_blksz = bevt->tx_blksz;
sip->rx_blksz = bevt->rx_blksz;
sip->credit_to_reserve = bevt->credit_to_reserve;
sip->dump_rpbm_err = (bevt->options & SIP_DUMP_RPBM_ERR);
sip->rxabort_fixed = (bevt->options & SIP_RXABORT_FIXED);
sip->support_bgscan = (bevt->options & SIP_SUPPORT_BGSCAN);
sip->sendup_rpbm_pkt = sip->dump_rpbm_err && false;
/* print out MAC addr... */
memcpy(epub->mac_addr, bevt->mac_addr, ETH_ALEN);
for(i = 0;i < sizeof(esp_mac_prefix_table)/sizeof(struct esp_mac_prefix);i++) {
if(esp_mac_prefix_table[i].mac_index == mac_id) {
mac_index = i;
break;
}
}
epub->mac_addr[0] = esp_mac_prefix_table[mac_index].mac_addr_prefix[0];
epub->mac_addr[1] = esp_mac_prefix_table[mac_index].mac_addr_prefix[1];
epub->mac_addr[2] = esp_mac_prefix_table[mac_index].mac_addr_prefix[2];
#ifdef SELF_MAC
epub->mac_addr[0] = 0xff;
epub->mac_addr[1] = 0xff;
epub->mac_addr[2] = 0xff;
#endif
atomic_set(&sip->noise_floor, bevt->noise_floor);
sip_recalc_credit_init(sip);
esp_sip_dbg(ESP_DBG_TRACE, "%s tx_blksz %d rx_blksz %d mac addr %pM\n", __func__, sip->tx_blksz, sip->rx_blksz, epub->mac_addr);
return 0;
}
/* write pkts in aggr buf to target memory */
static void sip_write_pkts(struct esp_sip *sip, int pm_state)
{
int tx_aggr_len = 0;
struct sip_hdr *first_shdr = NULL;
int err = 0;
tx_aggr_len = sip->tx_aggr_write_ptr - sip->tx_aggr_buf;
if (tx_aggr_len < sizeof(struct sip_hdr)) {
printk("%s tx_aggr_len %d \n", __func__, tx_aggr_len);
ESSERT(0);
return;
}
if ((tx_aggr_len & 0x3) != 0) {
ESSERT(0);
return;
}
first_shdr = (struct sip_hdr *)sip->tx_aggr_buf;
if (atomic_read(&sip->tx_credits) <= SIP_CREDITS_LOW_THRESHOLD) {
first_shdr->fc[1] |= SIP_HDR_F_NEED_CRDT_RPT;
}
/* still use lock bus instead of sif_lldesc_write_sync since we want to protect several global varibles assignments */
sif_lock_bus(sip->epub);
err = esp_common_write(sip->epub, sip->tx_aggr_buf, tx_aggr_len, ESP_SIF_NOSYNC);
sip->tx_aggr_write_ptr = sip->tx_aggr_buf;
sip->tx_tot_len = 0;
sif_unlock_bus(sip->epub);
if (err)
esp_sip_dbg(ESP_DBG_ERROR, "func %s err!!!!!!!!!: %d\n", __func__, err);
}
/* setup sip header and tx info, copy pkt into aggr buf */
static int sip_pack_pkt(struct esp_sip *sip, struct sk_buff *skb, int *pm_state)
{
struct ieee80211_tx_info *itx_info;
struct sip_hdr *shdr;
u32 tx_len = 0, offset = 0;
bool is_data = true;
itx_info = IEEE80211_SKB_CB(skb);
if (itx_info->flags == 0xffffffff) {
shdr = (struct sip_hdr *)skb->data;
is_data = false;
tx_len = skb->len;
} else {
struct ieee80211_hdr * wh = (struct ieee80211_hdr *)skb->data;
struct esp_vif *evif = (struct esp_vif *)itx_info->control.vif->drv_priv;
u8 sta_index;
struct esp_node *node;
/* update sip header */
shdr = (struct sip_hdr *)sip->tx_aggr_write_ptr;
shdr->fc[0] = 0;
shdr->fc[1] = 0;
if ((itx_info->flags & IEEE80211_TX_CTL_AMPDU) && (true || esp_is_ip_pkt(skb)))
SIP_HDR_SET_TYPE(shdr->fc[0], SIP_DATA_AMPDU);
else
SIP_HDR_SET_TYPE(shdr->fc[0], SIP_DATA);
if(evif->epub == NULL){
#ifndef FAST_TX_STATUS
/* TBD */
#else
sip_tx_status_report(sip, skb, itx_info, false);
atomic_dec(&sip->tx_data_pkt_queued);
return -EINVAL;
#endif /* FAST_TX_STATUS */
}
/* make room for encrypted pkt */
if (itx_info->control.hw_key) {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
shdr->d_enc_flag= itx_info->control.hw_key->alg+1;
#else
int alg = esp_cipher2alg(itx_info->control.hw_key->cipher);
if (unlikely(alg == -1)) {
#ifndef FAST_TX_STATUS
/* TBD */
#else
sip_tx_status_report(sip, skb, itx_info, false);
atomic_dec(&sip->tx_data_pkt_queued);
return -1;
#endif /* FAST_TX_STATUS */
} else {
shdr->d_enc_flag = alg + 1;
}
#endif /* NEW_KERNEL */
shdr->d_hw_kid = itx_info->control.hw_key->hw_key_idx | (evif->index<<7);
} else {
shdr->d_enc_flag=0;
shdr->d_hw_kid = (evif->index << 7 | evif->index);
}
/* update sip tx info */
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
node = esp_get_node_by_addr(sip->epub, wh->addr1);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
if(itx_info->control.sta == NULL){
node = NULL;
} else {
node = esp_get_node_by_addr(sip->epub, itx_info->control.sta->addr);
}
#else
node = esp_get_node_by_addr(sip->epub, wh->addr1);
#endif
if(node != NULL)
sta_index = node->index;
else
sta_index = ESP_PUB_MAX_STA + 1;
SIP_HDR_SET_IFIDX(shdr->fc[0], evif->index << 3 | sta_index);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
shdr->d_p2p = itx_info->control.vif->p2p;
if(evif->index == 1)
shdr->d_p2p = 1;
#endif
shdr->d_ac = skb_get_queue_mapping(skb);
shdr->d_tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
wh = (struct ieee80211_hdr *)skb->data;