1/******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3 *
4 * This program is distributed in the hope that it will be useful, but WITHOUT
5 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
7 * more details.
8 *
9 * You should have received a copy of the GNU General Public License along with
10 * this program; if not, write to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
12 *
13 * The full GNU General Public License is included in this distribution in the
14 * file called LICENSE.
15 *
16 * Contact Information:
17 * wlanfae <wlanfae@realtek.com>
18******************************************************************************/
19#include "rtllib.h"
20#include <linux/etherdevice.h>
21#include "rtl819x_TS.h"
22
23static void TsSetupTimeOut(unsigned long data)
24{
25}
26
27static void TsInactTimeout(unsigned long data)
28{
29}
30
31static void RxPktPendingTimeout(unsigned long data)
32{
33	struct rx_ts_record *pRxTs = (struct rx_ts_record *)data;
34	struct rtllib_device *ieee = container_of(pRxTs, struct rtllib_device,
35						  RxTsRecord[pRxTs->num]);
36
37	struct rx_reorder_entry *pReorderEntry = NULL;
38
39	unsigned long flags = 0;
40	u8 index = 0;
41	bool bPktInBuf = false;
42
43	spin_lock_irqsave(&(ieee->reorder_spinlock), flags);
44	if (pRxTs->RxTimeoutIndicateSeq != 0xffff) {
45		while (!list_empty(&pRxTs->RxPendingPktList)) {
46			pReorderEntry = (struct rx_reorder_entry *)
47					list_entry(pRxTs->RxPendingPktList.prev,
48					struct rx_reorder_entry, List);
49			if (index == 0)
50				pRxTs->RxIndicateSeq = pReorderEntry->SeqNum;
51
52			if (SN_LESS(pReorderEntry->SeqNum, pRxTs->RxIndicateSeq) ||
53				SN_EQUAL(pReorderEntry->SeqNum, pRxTs->RxIndicateSeq)) {
54				list_del_init(&pReorderEntry->List);
55
56				if (SN_EQUAL(pReorderEntry->SeqNum,
57				    pRxTs->RxIndicateSeq))
58					pRxTs->RxIndicateSeq =
59					      (pRxTs->RxIndicateSeq + 1) % 4096;
60
61				RTLLIB_DEBUG(RTLLIB_DL_REORDER,
62					     "%s(): Indicate SeqNum: %d\n",
63					     __func__, pReorderEntry->SeqNum);
64				ieee->stats_IndicateArray[index] =
65							 pReorderEntry->prxb;
66				index++;
67
68				list_add_tail(&pReorderEntry->List,
69					      &ieee->RxReorder_Unused_List);
70			} else {
71				bPktInBuf = true;
72				break;
73			}
74		}
75	}
76
77	if (index > 0) {
78		pRxTs->RxTimeoutIndicateSeq = 0xffff;
79
80		if (index > REORDER_WIN_SIZE) {
81			RTLLIB_DEBUG(RTLLIB_DL_ERR,
82				     "RxReorderIndicatePacket(): Rx Reorder struct buffer full!!\n");
83			spin_unlock_irqrestore(&(ieee->reorder_spinlock),
84					       flags);
85			return;
86		}
87		rtllib_indicate_packets(ieee, ieee->stats_IndicateArray, index);
88		bPktInBuf = false;
89	}
90
91	if (bPktInBuf && (pRxTs->RxTimeoutIndicateSeq == 0xffff)) {
92		pRxTs->RxTimeoutIndicateSeq = pRxTs->RxIndicateSeq;
93		mod_timer(&pRxTs->RxPktPendingTimer,  jiffies +
94			  msecs_to_jiffies(ieee->pHTInfo->RxReorderPendingTime));
95	}
96	spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
97}
98
99static void TsAddBaProcess(unsigned long data)
100{
101	struct tx_ts_record *pTxTs = (struct tx_ts_record *)data;
102	u8 num = pTxTs->num;
103	struct rtllib_device *ieee = container_of(pTxTs, struct rtllib_device,
104				     TxTsRecord[num]);
105
106	TsInitAddBA(ieee, pTxTs, BA_POLICY_IMMEDIATE, false);
107	RTLLIB_DEBUG(RTLLIB_DL_BA,
108		     "TsAddBaProcess(): ADDBA Req is started!!\n");
109}
110
111static void ResetTsCommonInfo(struct ts_common_info *pTsCommonInfo)
112{
113	memset(pTsCommonInfo->Addr, 0, 6);
114	memset(&pTsCommonInfo->TSpec, 0, sizeof(union tspec_body));
115	memset(&pTsCommonInfo->TClass, 0, sizeof(union qos_tclas)*TCLAS_NUM);
116	pTsCommonInfo->TClasProc = 0;
117	pTsCommonInfo->TClasNum = 0;
118}
119
120static void ResetTxTsEntry(struct tx_ts_record *pTS)
121{
122	ResetTsCommonInfo(&pTS->TsCommonInfo);
123	pTS->TxCurSeq = 0;
124	pTS->bAddBaReqInProgress = false;
125	pTS->bAddBaReqDelayed = false;
126	pTS->bUsingBa = false;
127	pTS->bDisable_AddBa = false;
128	ResetBaEntry(&pTS->TxAdmittedBARecord);
129	ResetBaEntry(&pTS->TxPendingBARecord);
130}
131
132static void ResetRxTsEntry(struct rx_ts_record *pTS)
133{
134	ResetTsCommonInfo(&pTS->TsCommonInfo);
135	pTS->RxIndicateSeq = 0xffff;
136	pTS->RxTimeoutIndicateSeq = 0xffff;
137	ResetBaEntry(&pTS->RxAdmittedBARecord);
138}
139
140void TSInitialize(struct rtllib_device *ieee)
141{
142	struct tx_ts_record *pTxTS  = ieee->TxTsRecord;
143	struct rx_ts_record *pRxTS  = ieee->RxTsRecord;
144	struct rx_reorder_entry *pRxReorderEntry = ieee->RxReorderEntry;
145	u8				count = 0;
146
147	RTLLIB_DEBUG(RTLLIB_DL_TS, "==========>%s()\n", __func__);
148	INIT_LIST_HEAD(&ieee->Tx_TS_Admit_List);
149	INIT_LIST_HEAD(&ieee->Tx_TS_Pending_List);
150	INIT_LIST_HEAD(&ieee->Tx_TS_Unused_List);
151
152	for (count = 0; count < TOTAL_TS_NUM; count++) {
153		pTxTS->num = count;
154		_setup_timer(&pTxTS->TsCommonInfo.SetupTimer,
155			    TsSetupTimeOut,
156			    (unsigned long) pTxTS);
157
158		_setup_timer(&pTxTS->TsCommonInfo.InactTimer,
159			    TsInactTimeout,
160			    (unsigned long) pTxTS);
161
162		_setup_timer(&pTxTS->TsAddBaTimer,
163			    TsAddBaProcess,
164			    (unsigned long) pTxTS);
165
166		_setup_timer(&pTxTS->TxPendingBARecord.Timer,
167			    BaSetupTimeOut,
168			    (unsigned long) pTxTS);
169		_setup_timer(&pTxTS->TxAdmittedBARecord.Timer,
170			    TxBaInactTimeout,
171			    (unsigned long) pTxTS);
172
173		ResetTxTsEntry(pTxTS);
174		list_add_tail(&pTxTS->TsCommonInfo.List,
175				&ieee->Tx_TS_Unused_List);
176		pTxTS++;
177	}
178
179	INIT_LIST_HEAD(&ieee->Rx_TS_Admit_List);
180	INIT_LIST_HEAD(&ieee->Rx_TS_Pending_List);
181	INIT_LIST_HEAD(&ieee->Rx_TS_Unused_List);
182	for (count = 0; count < TOTAL_TS_NUM; count++) {
183		pRxTS->num = count;
184		INIT_LIST_HEAD(&pRxTS->RxPendingPktList);
185
186		_setup_timer(&pRxTS->TsCommonInfo.SetupTimer,
187			    TsSetupTimeOut,
188			    (unsigned long) pRxTS);
189
190		_setup_timer(&pRxTS->TsCommonInfo.InactTimer,
191			    TsInactTimeout,
192			    (unsigned long) pRxTS);
193
194		_setup_timer(&pRxTS->RxAdmittedBARecord.Timer,
195			    RxBaInactTimeout,
196			    (unsigned long) pRxTS);
197
198		_setup_timer(&pRxTS->RxPktPendingTimer,
199			    RxPktPendingTimeout,
200			    (unsigned long) pRxTS);
201
202		ResetRxTsEntry(pRxTS);
203		list_add_tail(&pRxTS->TsCommonInfo.List,
204			      &ieee->Rx_TS_Unused_List);
205		pRxTS++;
206	}
207	INIT_LIST_HEAD(&ieee->RxReorder_Unused_List);
208	for (count = 0; count < REORDER_ENTRY_NUM; count++) {
209		list_add_tail(&pRxReorderEntry->List,
210			      &ieee->RxReorder_Unused_List);
211		if (count == (REORDER_ENTRY_NUM-1))
212			break;
213		pRxReorderEntry = &ieee->RxReorderEntry[count+1];
214	}
215
216}
217
218static void AdmitTS(struct rtllib_device *ieee,
219		    struct ts_common_info *pTsCommonInfo, u32 InactTime)
220{
221	del_timer_sync(&pTsCommonInfo->SetupTimer);
222	del_timer_sync(&pTsCommonInfo->InactTimer);
223
224	if (InactTime != 0)
225		mod_timer(&pTsCommonInfo->InactTimer, jiffies +
226			  msecs_to_jiffies(InactTime));
227}
228
229static struct ts_common_info *SearchAdmitTRStream(struct rtllib_device *ieee,
230						  u8 *Addr, u8 TID,
231						  enum tr_select TxRxSelect)
232{
233	u8	dir;
234	bool	search_dir[4] = {0};
235	struct list_head *psearch_list;
236	struct ts_common_info *pRet = NULL;
237
238	if (ieee->iw_mode == IW_MODE_MASTER) {
239		if (TxRxSelect == TX_DIR) {
240			search_dir[DIR_DOWN] = true;
241			search_dir[DIR_BI_DIR] = true;
242		} else {
243			search_dir[DIR_UP] = true;
244			search_dir[DIR_BI_DIR] = true;
245		}
246	} else if (ieee->iw_mode == IW_MODE_ADHOC) {
247		if (TxRxSelect == TX_DIR)
248			search_dir[DIR_UP] = true;
249		else
250			search_dir[DIR_DOWN] = true;
251	} else {
252		if (TxRxSelect == TX_DIR) {
253			search_dir[DIR_UP] = true;
254			search_dir[DIR_BI_DIR] = true;
255			search_dir[DIR_DIRECT] = true;
256		} else {
257			search_dir[DIR_DOWN] = true;
258			search_dir[DIR_BI_DIR] = true;
259			search_dir[DIR_DIRECT] = true;
260		}
261	}
262
263	if (TxRxSelect == TX_DIR)
264		psearch_list = &ieee->Tx_TS_Admit_List;
265	else
266		psearch_list = &ieee->Rx_TS_Admit_List;
267
268	for (dir = 0; dir <= DIR_BI_DIR; dir++) {
269		if (!search_dir[dir])
270			continue;
271		list_for_each_entry(pRet, psearch_list, List) {
272			if (memcmp(pRet->Addr, Addr, 6) == 0)
273				if (pRet->TSpec.f.TSInfo.field.ucTSID == TID)
274					if (pRet->TSpec.f.TSInfo.field.ucDirection == dir)
275						break;
276
277		}
278		if (&pRet->List  != psearch_list)
279			break;
280	}
281
282	if (pRet && &pRet->List  != psearch_list)
283		return pRet;
284	return NULL;
285}
286
287static void MakeTSEntry(struct ts_common_info *pTsCommonInfo, u8 *Addr,
288			union tspec_body *pTSPEC, union qos_tclas *pTCLAS,
289			u8 TCLAS_Num, u8 TCLAS_Proc)
290{
291	u8	count;
292
293	if (pTsCommonInfo == NULL)
294		return;
295
296	memcpy(pTsCommonInfo->Addr, Addr, 6);
297
298	if (pTSPEC != NULL)
299		memcpy((u8 *)(&(pTsCommonInfo->TSpec)), (u8 *)pTSPEC,
300			sizeof(union tspec_body));
301
302	for (count = 0; count < TCLAS_Num; count++)
303		memcpy((u8 *)(&(pTsCommonInfo->TClass[count])),
304		       (u8 *)pTCLAS, sizeof(union qos_tclas));
305
306	pTsCommonInfo->TClasProc = TCLAS_Proc;
307	pTsCommonInfo->TClasNum = TCLAS_Num;
308}
309
310bool GetTs(struct rtllib_device *ieee, struct ts_common_info **ppTS,
311	   u8 *Addr, u8 TID, enum tr_select TxRxSelect, bool bAddNewTs)
312{
313	u8	UP = 0;
314	union tspec_body TSpec;
315	union qos_tsinfo *pTSInfo = &TSpec.f.TSInfo;
316	struct list_head *pUnusedList;
317	struct list_head *pAddmitList;
318	enum direction_value Dir;
319
320	if (is_multicast_ether_addr(Addr)) {
321		RTLLIB_DEBUG(RTLLIB_DL_ERR,
322			     "ERR! get TS for Broadcast or Multicast\n");
323		return false;
324	}
325	if (ieee->current_network.qos_data.supported == 0) {
326		UP = 0;
327	} else {
328		if (!IsACValid(TID)) {
329			RTLLIB_DEBUG(RTLLIB_DL_ERR,
330				     "ERR! in %s(), TID(%d) is not valid\n",
331				     __func__, TID);
332			return false;
333		}
334
335		switch (TID) {
336		case 0:
337		case 3:
338			UP = 0;
339			break;
340		case 1:
341		case 2:
342			UP = 2;
343			break;
344		case 4:
345		case 5:
346			UP = 5;
347			break;
348		case 6:
349		case 7:
350			UP = 7;
351			break;
352		}
353	}
354
355	*ppTS = SearchAdmitTRStream(ieee, Addr, UP, TxRxSelect);
356	if (*ppTS != NULL)
357		return true;
358
359	if (!bAddNewTs) {
360		RTLLIB_DEBUG(RTLLIB_DL_TS,
361			     "add new TS failed(tid:%d)\n", UP);
362		return false;
363	}
364
365	pUnusedList = (TxRxSelect == TX_DIR) ?
366				(&ieee->Tx_TS_Unused_List) :
367				(&ieee->Rx_TS_Unused_List);
368
369	pAddmitList = (TxRxSelect == TX_DIR) ?
370				(&ieee->Tx_TS_Admit_List) :
371				(&ieee->Rx_TS_Admit_List);
372
373	Dir = (ieee->iw_mode == IW_MODE_MASTER) ?
374				((TxRxSelect == TX_DIR) ? DIR_DOWN : DIR_UP) :
375				((TxRxSelect == TX_DIR) ? DIR_UP : DIR_DOWN);
376
377	RTLLIB_DEBUG(RTLLIB_DL_TS, "to add Ts\n");
378	if (!list_empty(pUnusedList)) {
379		(*ppTS) = list_entry(pUnusedList->next,
380			  struct ts_common_info, List);
381		list_del_init(&(*ppTS)->List);
382		if (TxRxSelect == TX_DIR) {
383			struct tx_ts_record *tmp =
384				container_of(*ppTS,
385				struct tx_ts_record,
386				TsCommonInfo);
387			ResetTxTsEntry(tmp);
388		} else {
389			struct rx_ts_record *tmp =
390				 container_of(*ppTS,
391				 struct rx_ts_record,
392				 TsCommonInfo);
393			ResetRxTsEntry(tmp);
394		}
395
396		RTLLIB_DEBUG(RTLLIB_DL_TS,
397			     "to init current TS, UP:%d, Dir:%d, addr: %pM ppTs=%p\n",
398			     UP, Dir, Addr, *ppTS);
399		pTSInfo->field.ucTrafficType = 0;
400		pTSInfo->field.ucTSID = UP;
401		pTSInfo->field.ucDirection = Dir;
402		pTSInfo->field.ucAccessPolicy = 1;
403		pTSInfo->field.ucAggregation = 0;
404		pTSInfo->field.ucPSB = 0;
405		pTSInfo->field.ucUP = UP;
406		pTSInfo->field.ucTSInfoAckPolicy = 0;
407		pTSInfo->field.ucSchedule = 0;
408
409		MakeTSEntry(*ppTS, Addr, &TSpec, NULL, 0, 0);
410		AdmitTS(ieee, *ppTS, 0);
411		list_add_tail(&((*ppTS)->List), pAddmitList);
412
413		return true;
414	}
415
416	RTLLIB_DEBUG(RTLLIB_DL_ERR,
417		     "ERR!!in function %s() There is not enough dir=%d(0=up down=1) TS record to be used!!",
418		     __func__, Dir);
419	return false;
420}
421
422static void RemoveTsEntry(struct rtllib_device *ieee, struct ts_common_info *pTs,
423			  enum tr_select TxRxSelect)
424{
425	del_timer_sync(&pTs->SetupTimer);
426	del_timer_sync(&pTs->InactTimer);
427	TsInitDelBA(ieee, pTs, TxRxSelect);
428
429	if (TxRxSelect == RX_DIR) {
430		struct rx_reorder_entry *pRxReorderEntry;
431		struct rx_ts_record *pRxTS = (struct rx_ts_record *)pTs;
432
433		if (timer_pending(&pRxTS->RxPktPendingTimer))
434			del_timer_sync(&pRxTS->RxPktPendingTimer);
435
436		while (!list_empty(&pRxTS->RxPendingPktList)) {
437			pRxReorderEntry = (struct rx_reorder_entry *)
438					list_entry(pRxTS->RxPendingPktList.prev,
439					struct rx_reorder_entry, List);
440			RTLLIB_DEBUG(RTLLIB_DL_REORDER,
441				     "%s(): Delete SeqNum %d!\n", __func__,
442				     pRxReorderEntry->SeqNum);
443			list_del_init(&pRxReorderEntry->List);
444			{
445				int i = 0;
446				struct rtllib_rxb *prxb = pRxReorderEntry->prxb;
447
448				if (unlikely(!prxb))
449					return;
450				for (i = 0; i < prxb->nr_subframes; i++)
451					dev_kfree_skb(prxb->subframes[i]);
452				kfree(prxb);
453				prxb = NULL;
454			}
455			list_add_tail(&pRxReorderEntry->List,
456				      &ieee->RxReorder_Unused_List);
457		}
458	} else {
459		struct tx_ts_record *pTxTS = (struct tx_ts_record *)pTs;
460
461		del_timer_sync(&pTxTS->TsAddBaTimer);
462	}
463}
464
465void RemovePeerTS(struct rtllib_device *ieee, u8 *Addr)
466{
467	struct ts_common_info *pTS, *pTmpTS;
468
469	netdev_info(ieee->dev, "===========>RemovePeerTS, %pM\n", Addr);
470
471	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Pending_List, List) {
472		if (memcmp(pTS->Addr, Addr, 6) == 0) {
473			RemoveTsEntry(ieee, pTS, TX_DIR);
474			list_del_init(&pTS->List);
475			list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
476		}
477	}
478
479	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Admit_List, List) {
480		if (memcmp(pTS->Addr, Addr, 6) == 0) {
481			netdev_info(ieee->dev,
482				    "====>remove Tx_TS_admin_list\n");
483			RemoveTsEntry(ieee, pTS, TX_DIR);
484			list_del_init(&pTS->List);
485			list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
486		}
487	}
488
489	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Pending_List, List) {
490		if (memcmp(pTS->Addr, Addr, 6) == 0) {
491			RemoveTsEntry(ieee, pTS, RX_DIR);
492			list_del_init(&pTS->List);
493			list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
494		}
495	}
496
497	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Admit_List, List) {
498		if (memcmp(pTS->Addr, Addr, 6) == 0) {
499			RemoveTsEntry(ieee, pTS, RX_DIR);
500			list_del_init(&pTS->List);
501			list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
502		}
503	}
504}
505EXPORT_SYMBOL(RemovePeerTS);
506
507void RemoveAllTS(struct rtllib_device *ieee)
508{
509	struct ts_common_info *pTS, *pTmpTS;
510
511	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Pending_List, List) {
512		RemoveTsEntry(ieee, pTS, TX_DIR);
513		list_del_init(&pTS->List);
514		list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
515	}
516
517	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Admit_List, List) {
518		RemoveTsEntry(ieee, pTS, TX_DIR);
519		list_del_init(&pTS->List);
520		list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
521	}
522
523	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Pending_List, List) {
524		RemoveTsEntry(ieee, pTS, RX_DIR);
525		list_del_init(&pTS->List);
526		list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
527	}
528
529	list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Admit_List, List) {
530		RemoveTsEntry(ieee, pTS, RX_DIR);
531		list_del_init(&pTS->List);
532		list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
533	}
534}
535
536void TsStartAddBaProcess(struct rtllib_device *ieee, struct tx_ts_record *pTxTS)
537{
538	if (pTxTS->bAddBaReqInProgress == false) {
539		pTxTS->bAddBaReqInProgress = true;
540
541		if (pTxTS->bAddBaReqDelayed) {
542			RTLLIB_DEBUG(RTLLIB_DL_BA,
543				     "TsStartAddBaProcess(): Delayed Start ADDBA after 60 sec!!\n");
544			mod_timer(&pTxTS->TsAddBaTimer, jiffies +
545				  msecs_to_jiffies(TS_ADDBA_DELAY));
546		} else {
547			RTLLIB_DEBUG(RTLLIB_DL_BA,
548				     "TsStartAddBaProcess(): Immediately Start ADDBA now!!\n");
549			mod_timer(&pTxTS->TsAddBaTimer, jiffies+10);
550		}
551	} else
552		RTLLIB_DEBUG(RTLLIB_DL_BA, "%s()==>BA timer is already added\n",
553			     __func__);
554}
555