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