1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #ifndef _RTW_RECV_H_
8 #define _RTW_RECV_H_
9 
10 #define NR_RECVBUFF (8)
11 
12 #define NR_PREALLOC_RECV_SKB (8)
13 
14 #define NR_RECVFRAME 256
15 
16 #define RXFRAME_ALIGN	8
17 #define RXFRAME_ALIGN_SZ	(1<<RXFRAME_ALIGN)
18 
19 #define DRVINFO_SZ	4 /*  unit is 8bytes */
20 
21 #define MAX_RXFRAME_CNT	512
22 #define MAX_RX_NUMBLKS		(32)
23 #define RECVFRAME_HDR_ALIGN 128
24 
25 
26 #define PHY_RSSI_SLID_WIN_MAX				100
27 #define PHY_LINKQUALITY_SLID_WIN_MAX		20
28 
29 
30 #define SNAP_SIZE sizeof(struct ieee80211_snap_hdr)
31 
32 #define RX_MPDU_QUEUE				0
33 #define RX_CMD_QUEUE				1
34 #define RX_MAX_QUEUE				2
35 
36 #define MAX_SUBFRAME_COUNT	64
37 
38 #define LLC_HEADER_LENGTH	6
39 
40 /* for Rx reordering buffer control */
41 struct recv_reorder_ctrl {
42 	struct adapter	*padapter;
43 	u8 enable;
44 	u16 indicate_seq;/* wstart_b, init_value = 0xffff */
45 	u16 wend_b;
46 	u8 wsize_b;
47 	struct __queue pending_recvframe_queue;
48 	struct timer_list reordering_ctrl_timer;
49 };
50 
51 struct	stainfo_rxcache	{
52 	u16 tid_rxseq[16];
53 /*
54 	unsigned short	tid0_rxseq;
55 	unsigned short	tid1_rxseq;
56 	unsigned short	tid2_rxseq;
57 	unsigned short	tid3_rxseq;
58 	unsigned short	tid4_rxseq;
59 	unsigned short	tid5_rxseq;
60 	unsigned short	tid6_rxseq;
61 	unsigned short	tid7_rxseq;
62 	unsigned short	tid8_rxseq;
63 	unsigned short	tid9_rxseq;
64 	unsigned short	tid10_rxseq;
65 	unsigned short	tid11_rxseq;
66 	unsigned short	tid12_rxseq;
67 	unsigned short	tid13_rxseq;
68 	unsigned short	tid14_rxseq;
69 	unsigned short	tid15_rxseq;
70 */
71 };
72 
73 
74 struct signal_stat {
75 	u8 update_req;		/* used to indicate */
76 	u8 avg_val;		/* avg of valid elements */
77 	u32 total_num;		/* num of valid elements */
78 	u32 total_val;		/* sum of valid elements */
79 };
80 
81 struct phy_info {
82 	u8 rx_pwd_ba11;
83 
84 	u8 SignalQuality;	 /*  in 0-100 index. */
85 	s8		rx_mimo_signal_quality[4];	/* per-path's EVM */
86 	u8 RxMIMOEVMdbm[4];		/* per-path's EVM dbm */
87 
88 	u8 rx_mimo_signal_strength[4];/*  in 0~100 index */
89 
90 	u16 	Cfo_short[4];			/*  per-path's Cfo_short */
91 	u16 	Cfo_tail[4];			/*  per-path's Cfo_tail */
92 
93 	s8		RxPower; /*  in dBm Translate from PWdB */
94 	s8		RecvSignalPower;/*  Real power in dBm for this packet, no beautification and aggregation. Keep this raw info to be used for the other procedures. */
95 	u8 bt_rx_rssi_percentage;
96 	u8 SignalStrength; /*  in 0-100 index. */
97 
98 	s8		RxPwr[4];				/* per-path's pwdb */
99 	u8 RxSNR[4];				/* per-path's SNR */
100 	u8 BandWidth;
101 	u8 btCoexPwrAdjust;
102 };
103 
104 #ifdef DBG_RX_SIGNAL_DISPLAY_RAW_DATA
105 struct rx_raw_rssi {
106 	u8 data_rate;
107 	u8 pwdball;
108 	s8 pwr_all;
109 
110 	u8 mimo_signal_strength[4];/*  in 0~100 index */
111 	u8 mimo_signal_quality[4];
112 
113 	s8 ofdm_pwr[4];
114 	u8 ofdm_snr[4];
115 
116 };
117 #endif
118 
119 struct rx_pkt_attrib	{
120 	u16 pkt_len;
121 	u8 physt;
122 	u8 drvinfo_sz;
123 	u8 shift_sz;
124 	u8 hdrlen; /* the WLAN Header Len */
125 	u8 to_fr_ds;
126 	u8 amsdu;
127 	u8 qos;
128 	u8 priority;
129 	u8 pw_save;
130 	u8 mdata;
131 	u16 seq_num;
132 	u8 frag_num;
133 	u8 mfrag;
134 	u8 order;
135 	u8 privacy; /* in frame_ctrl field */
136 	u8 bdecrypted;
137 	u8 encrypt; /* when 0 indicates no encryption; when non-zero, indicates the encryption algorithm */
138 	u8 iv_len;
139 	u8 icv_len;
140 	u8 crc_err;
141 	u8 icv_err;
142 
143 	u16 eth_type;
144 
145 	u8 dst[ETH_ALEN];
146 	u8 src[ETH_ALEN];
147 	u8 ta[ETH_ALEN];
148 	u8 ra[ETH_ALEN];
149 	u8 bssid[ETH_ALEN];
150 
151 	u8 ack_policy;
152 
153 	u8 key_index;
154 
155 	u8 data_rate;
156 	u8 sgi;
157 	u8 pkt_rpt_type;
158 	u32 MacIDValidEntry[2];	/*  64 bits present 64 entry. */
159 
160 /*
161 	u8 signal_qual;
162 	s8	rx_mimo_signal_qual[2];
163 	u8 signal_strength;
164 	u32 rx_pwd_ba11;
165 	s32	RecvSignalPower;
166 */
167 	struct phy_info phy_info;
168 };
169 
170 
171 /* These definition is used for Rx packet reordering. */
172 #define SN_LESS(a, b)		(((a - b) & 0x800) != 0)
173 #define SN_EQUAL(a, b)	(a == b)
174 /* define REORDER_WIN_SIZE	128 */
175 /* define REORDER_ENTRY_NUM	128 */
176 #define REORDER_WAIT_TIME	(50) /*  (ms) */
177 
178 #define RECVBUFF_ALIGN_SZ 8
179 
180 #define RXDESC_SIZE	24
181 #define RXDESC_OFFSET RXDESC_SIZE
182 
183 struct recv_stat {
184 	__le32 rxdw0;
185 	__le32 rxdw1;
186 	__le32 rxdw2;
187 	__le32 rxdw3;
188 #ifndef BUF_DESC_ARCH
189 	__le32 rxdw4;
190 	__le32 rxdw5;
191 #endif /* if BUF_DESC_ARCH is defined, rx_buf_desc occupy 4 double words */
192 };
193 
194 #define EOR BIT(30)
195 
196 /*
197 accesser of recv_priv: rtw_recv_entry(dispatch / passive level); recv_thread(passive) ; returnpkt(dispatch)
198 ; halt(passive) ;
199 
200 using enter_critical section to protect
201 */
202 struct recv_priv {
203 	spinlock_t	lock;
204 	struct __queue	free_recv_queue;
205 	struct __queue	recv_pending_queue;
206 	struct __queue	uc_swdec_pending_queue;
207 	u8 *pallocated_frame_buf;
208 	u8 *precv_frame_buf;
209 	uint free_recvframe_cnt;
210 	struct adapter	*adapter;
211 	u32 bIsAnyNonBEPkts;
212 	u64	rx_bytes;
213 	u64	rx_pkts;
214 	u64	rx_drop;
215 	uint  rx_icv_err;
216 	uint  rx_largepacket_crcerr;
217 	uint  rx_smallpacket_crcerr;
218 	uint  rx_middlepacket_crcerr;
219 
220 	struct tasklet_struct irq_prepare_beacon_tasklet;
221 	struct tasklet_struct recv_tasklet;
222 	struct sk_buff_head free_recv_skb_queue;
223 	struct sk_buff_head rx_skb_queue;
224 
225 	u8 *pallocated_recv_buf;
226 	u8 *precv_buf;    /*  4 alignment */
227 	struct __queue	free_recv_buf_queue;
228 	u32 free_recv_buf_queue_cnt;
229 
230 	struct __queue	recv_buf_pending_queue;
231 
232 	/* For display the phy information */
233 	u8 is_signal_dbg;	/*  for debug */
234 	u8 signal_strength_dbg;	/*  for debug */
235 
236 	u8 signal_strength;
237 	u8 signal_qual;
238 	s8 rssi;	/* translate_percentage_to_dbm(ptarget_wlan->network.PhyInfo.SignalStrength); */
239 	#ifdef DBG_RX_SIGNAL_DISPLAY_RAW_DATA
240 	struct rx_raw_rssi raw_rssi_info;
241 	#endif
242 	/* s8 rxpwdb; */
243 	s16 noise;
244 	/* int RxSNRdB[2]; */
245 	/* s8 RxRssi[2]; */
246 	/* int FalseAlmCnt_all; */
247 
248 
249 	struct timer_list signal_stat_timer;
250 	u32 signal_stat_sampling_interval;
251 	/* u32 signal_stat_converging_constant; */
252 	struct signal_stat signal_qual_data;
253 	struct signal_stat signal_strength_data;
254 };
255 
256 #define rtw_set_signal_stat_timer(recvpriv) _set_timer(&(recvpriv)->signal_stat_timer, (recvpriv)->signal_stat_sampling_interval)
257 
258 struct sta_recv_priv {
259 
260 	spinlock_t	lock;
261 	signed int	option;
262 
263 	/* struct __queue	blk_strms[MAX_RX_NUMBLKS]; */
264 	struct __queue defrag_q;	 /* keeping the fragment frame until defrag */
265 
266 	struct	stainfo_rxcache rxcache;
267 
268 	/* uint	sta_rx_bytes; */
269 	/* uint	sta_rx_pkts; */
270 	/* uint	sta_rx_fail; */
271 
272 };
273 
274 
275 struct recv_buf {
276 	struct list_head list;
277 
278 	spinlock_t recvbuf_lock;
279 
280 	u32 ref_cnt;
281 
282 	struct adapter *adapter;
283 
284 	u8 *pbuf;
285 	u8 *pallocated_buf;
286 
287 	u32 len;
288 	u8 *phead;
289 	u8 *pdata;
290 	u8 *ptail;
291 	u8 *pend;
292 
293 	struct sk_buff	*pskb;
294 	u8 reuse;
295 };
296 
297 
298 /*
299 	head  ----->
300 
301 		data  ----->
302 
303 			payload
304 
305 		tail  ----->
306 
307 
308 	end   ----->
309 
310 	len = (unsigned int)(tail - data);
311 
312 */
313 struct recv_frame_hdr {
314 	struct list_head	list;
315 	struct sk_buff	 *pkt;
316 	struct sk_buff	 *pkt_newalloc;
317 
318 	struct adapter  *adapter;
319 
320 	u8 fragcnt;
321 
322 	int frame_tag;
323 
324 	struct rx_pkt_attrib attrib;
325 
326 	uint  len;
327 	u8 *rx_head;
328 	u8 *rx_data;
329 	u8 *rx_tail;
330 	u8 *rx_end;
331 
332 	void *precvbuf;
333 
334 
335 	/*  */
336 	struct sta_info *psta;
337 
338 	/* for A-MPDU Rx reordering buffer control */
339 	struct recv_reorder_ctrl *preorder_ctrl;
340 };
341 
342 
343 union recv_frame {
344 	union{
345 		struct list_head list;
346 		struct recv_frame_hdr hdr;
347 		uint mem[RECVFRAME_HDR_ALIGN>>2];
348 	} u;
349 
350 	/* uint mem[MAX_RXSZ>>2]; */
351 
352 };
353 
354 enum {
355 	NORMAL_RX,/* Normal rx packet */
356 	TX_REPORT1,/* CCX */
357 	TX_REPORT2,/* TX RPT */
358 	HIS_REPORT,/*  USB HISR RPT */
359 	C2H_PACKET
360 };
361 
362 extern union recv_frame *_rtw_alloc_recvframe(struct __queue *pfree_recv_queue);  /* get a free recv_frame from pfree_recv_queue */
363 extern union recv_frame *rtw_alloc_recvframe(struct __queue *pfree_recv_queue);  /* get a free recv_frame from pfree_recv_queue */
364 extern int	 rtw_free_recvframe(union recv_frame *precvframe, struct __queue *pfree_recv_queue);
365 
366 #define rtw_dequeue_recvframe(queue) rtw_alloc_recvframe(queue)
367 extern int _rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue);
368 extern int rtw_enqueue_recvframe(union recv_frame *precvframe, struct __queue *queue);
369 
370 extern void rtw_free_recvframe_queue(struct __queue *pframequeue,  struct __queue *pfree_recv_queue);
371 u32 rtw_free_uc_swdec_pending_queue(struct adapter *adapter);
372 
373 signed int rtw_enqueue_recvbuf_to_head(struct recv_buf *precvbuf, struct __queue *queue);
374 signed int rtw_enqueue_recvbuf(struct recv_buf *precvbuf, struct __queue *queue);
375 struct recv_buf *rtw_dequeue_recvbuf(struct __queue *queue);
376 
377 void rtw_reordering_ctrl_timeout_handler(struct timer_list *t);
378 
get_rxmem(union recv_frame * precvframe)379 static inline u8 *get_rxmem(union recv_frame *precvframe)
380 {
381 	/* always return rx_head... */
382 	if (precvframe == NULL)
383 		return NULL;
384 
385 	return precvframe->u.hdr.rx_head;
386 }
387 
recvframe_pull(union recv_frame * precvframe,signed int sz)388 static inline u8 *recvframe_pull(union recv_frame *precvframe, signed int sz)
389 {
390 	/*  rx_data += sz; move rx_data sz bytes  hereafter */
391 
392 	/* used for extract sz bytes from rx_data, update rx_data and return the updated rx_data to the caller */
393 
394 
395 	if (precvframe == NULL)
396 		return NULL;
397 
398 
399 	precvframe->u.hdr.rx_data += sz;
400 
401 	if (precvframe->u.hdr.rx_data > precvframe->u.hdr.rx_tail)
402 	{
403 		precvframe->u.hdr.rx_data -= sz;
404 		return NULL;
405 	}
406 
407 	precvframe->u.hdr.len -= sz;
408 
409 	return precvframe->u.hdr.rx_data;
410 
411 }
412 
recvframe_put(union recv_frame * precvframe,signed int sz)413 static inline u8 *recvframe_put(union recv_frame *precvframe, signed int sz)
414 {
415 	/*  rx_tai += sz; move rx_tail sz bytes  hereafter */
416 
417 	/* used for append sz bytes from ptr to rx_tail, update rx_tail and return the updated rx_tail to the caller */
418 	/* after putting, rx_tail must be still larger than rx_end. */
419 	unsigned char *prev_rx_tail;
420 
421 	if (precvframe == NULL)
422 		return NULL;
423 
424 	prev_rx_tail = precvframe->u.hdr.rx_tail;
425 
426 	precvframe->u.hdr.rx_tail += sz;
427 
428 	if (precvframe->u.hdr.rx_tail > precvframe->u.hdr.rx_end)
429 	{
430 		precvframe->u.hdr.rx_tail = prev_rx_tail;
431 		return NULL;
432 	}
433 
434 	precvframe->u.hdr.len += sz;
435 
436 	return precvframe->u.hdr.rx_tail;
437 
438 }
439 
440 
441 
recvframe_pull_tail(union recv_frame * precvframe,signed int sz)442 static inline u8 *recvframe_pull_tail(union recv_frame *precvframe, signed int sz)
443 {
444 	/*  rmv data from rx_tail (by yitsen) */
445 
446 	/* used for extract sz bytes from rx_end, update rx_end and return the updated rx_end to the caller */
447 	/* after pulling, rx_end must be still larger than rx_data. */
448 
449 	if (precvframe == NULL)
450 		return NULL;
451 
452 	precvframe->u.hdr.rx_tail -= sz;
453 
454 	if (precvframe->u.hdr.rx_tail < precvframe->u.hdr.rx_data)
455 	{
456 		precvframe->u.hdr.rx_tail += sz;
457 		return NULL;
458 	}
459 
460 	precvframe->u.hdr.len -= sz;
461 
462 	return precvframe->u.hdr.rx_tail;
463 
464 }
465 
rxmem_to_recvframe(u8 * rxmem)466 static inline union recv_frame *rxmem_to_recvframe(u8 *rxmem)
467 {
468 	/* due to the design of 2048 bytes alignment of recv_frame, we can reference the union recv_frame */
469 	/* from any given member of recv_frame. */
470 	/*  rxmem indicates the any member/address in recv_frame */
471 
472 	return (union recv_frame *)(((SIZE_PTR)rxmem >> RXFRAME_ALIGN) << RXFRAME_ALIGN);
473 
474 }
475 
get_recvframe_len(union recv_frame * precvframe)476 static inline signed int get_recvframe_len(union recv_frame *precvframe)
477 {
478 	return precvframe->u.hdr.len;
479 }
480 
481 
translate_percentage_to_dbm(u32 SignalStrengthIndex)482 static inline s32 translate_percentage_to_dbm(u32 SignalStrengthIndex)
483 {
484 	s32	SignalPower; /*  in dBm. */
485 
486 	/*  Translate to dBm (x = 0.5y-95). */
487 	SignalPower = (s32)((SignalStrengthIndex + 1) >> 1);
488 	SignalPower -= 95;
489 
490 	return SignalPower;
491 }
492 
493 
494 struct sta_info;
495 
496 extern void _rtw_init_sta_recv_priv(struct sta_recv_priv *psta_recvpriv);
497 
498 extern void  mgt_dispatcher(struct adapter *padapter, union recv_frame *precv_frame);
499 
500 #endif
501