1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
5 * Contact Information: wlanfae <wlanfae@realtek.com>
8 #include <linux/etherdevice.h>
9 #include "rtl819x_TS.h"
11 static void TsSetupTimeOut(struct timer_list *unused)
15 static void TsInactTimeout(struct timer_list *unused)
19 static void RxPktPendingTimeout(struct timer_list *t)
21 struct rx_ts_record *pRxTs = from_timer(pRxTs, t,
22 rx_pkt_pending_timer);
23 struct rtllib_device *ieee = container_of(pRxTs, struct rtllib_device,
24 RxTsRecord[pRxTs->num]);
26 struct rx_reorder_entry *pReorderEntry = NULL;
28 unsigned long flags = 0;
30 bool bPktInBuf = false;
32 spin_lock_irqsave(&(ieee->reorder_spinlock), flags);
33 if (pRxTs->rx_timeout_indicate_seq != 0xffff) {
34 while (!list_empty(&pRxTs->rx_pending_pkt_list)) {
35 pReorderEntry = (struct rx_reorder_entry *)
36 list_entry(pRxTs->rx_pending_pkt_list.prev,
37 struct rx_reorder_entry, List);
39 pRxTs->rx_indicate_seq = pReorderEntry->SeqNum;
41 if (SN_LESS(pReorderEntry->SeqNum,
42 pRxTs->rx_indicate_seq) ||
43 SN_EQUAL(pReorderEntry->SeqNum,
44 pRxTs->rx_indicate_seq)) {
45 list_del_init(&pReorderEntry->List);
47 if (SN_EQUAL(pReorderEntry->SeqNum,
48 pRxTs->rx_indicate_seq))
49 pRxTs->rx_indicate_seq =
50 (pRxTs->rx_indicate_seq + 1) % 4096;
53 "%s(): Indicate SeqNum: %d\n",
54 __func__, pReorderEntry->SeqNum);
55 ieee->stats_IndicateArray[index] =
59 list_add_tail(&pReorderEntry->List,
60 &ieee->RxReorder_Unused_List);
69 pRxTs->rx_timeout_indicate_seq = 0xffff;
71 if (index > REORDER_WIN_SIZE) {
72 netdev_warn(ieee->dev,
73 "%s(): Rx Reorder struct buffer full\n",
75 spin_unlock_irqrestore(&(ieee->reorder_spinlock),
79 rtllib_indicate_packets(ieee, ieee->stats_IndicateArray, index);
83 if (bPktInBuf && (pRxTs->rx_timeout_indicate_seq == 0xffff)) {
84 pRxTs->rx_timeout_indicate_seq = pRxTs->rx_indicate_seq;
85 mod_timer(&pRxTs->rx_pkt_pending_timer, jiffies +
86 msecs_to_jiffies(ieee->pHTInfo->RxReorderPendingTime)
89 spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
92 static void TsAddBaProcess(struct timer_list *t)
94 struct tx_ts_record *pTxTs = from_timer(pTxTs, t, TsAddBaTimer);
96 struct rtllib_device *ieee = container_of(pTxTs, struct rtllib_device,
99 TsInitAddBA(ieee, pTxTs, BA_POLICY_IMMEDIATE, false);
100 netdev_dbg(ieee->dev, "%s(): ADDBA Req is started\n", __func__);
103 static void ResetTsCommonInfo(struct ts_common_info *pTsCommonInfo)
105 eth_zero_addr(pTsCommonInfo->Addr);
106 memset(&pTsCommonInfo->TSpec, 0, sizeof(union tspec_body));
107 memset(&pTsCommonInfo->TClass, 0, sizeof(union qos_tclas) * TCLAS_NUM);
108 pTsCommonInfo->TClasProc = 0;
109 pTsCommonInfo->TClasNum = 0;
112 static void ResetTxTsEntry(struct tx_ts_record *pTS)
114 ResetTsCommonInfo(&pTS->TsCommonInfo);
116 pTS->bAddBaReqInProgress = false;
117 pTS->bAddBaReqDelayed = false;
118 pTS->bUsingBa = false;
119 pTS->bDisable_AddBa = false;
120 ResetBaEntry(&pTS->TxAdmittedBARecord);
121 ResetBaEntry(&pTS->TxPendingBARecord);
124 static void ResetRxTsEntry(struct rx_ts_record *pTS)
126 ResetTsCommonInfo(&pTS->ts_common_info);
127 pTS->rx_indicate_seq = 0xffff;
128 pTS->rx_timeout_indicate_seq = 0xffff;
129 ResetBaEntry(&pTS->rx_admitted_ba_record);
132 void TSInitialize(struct rtllib_device *ieee)
134 struct tx_ts_record *pTxTS = ieee->TxTsRecord;
135 struct rx_ts_record *pRxTS = ieee->RxTsRecord;
136 struct rx_reorder_entry *pRxReorderEntry = ieee->RxReorderEntry;
139 INIT_LIST_HEAD(&ieee->Tx_TS_Admit_List);
140 INIT_LIST_HEAD(&ieee->Tx_TS_Pending_List);
141 INIT_LIST_HEAD(&ieee->Tx_TS_Unused_List);
143 for (count = 0; count < TOTAL_TS_NUM; count++) {
145 timer_setup(&pTxTS->TsCommonInfo.SetupTimer, TsSetupTimeOut,
148 timer_setup(&pTxTS->TsCommonInfo.InactTimer, TsInactTimeout,
151 timer_setup(&pTxTS->TsAddBaTimer, TsAddBaProcess, 0);
153 timer_setup(&pTxTS->TxPendingBARecord.timer, BaSetupTimeOut,
155 timer_setup(&pTxTS->TxAdmittedBARecord.timer,
156 TxBaInactTimeout, 0);
158 ResetTxTsEntry(pTxTS);
159 list_add_tail(&pTxTS->TsCommonInfo.List,
160 &ieee->Tx_TS_Unused_List);
164 INIT_LIST_HEAD(&ieee->Rx_TS_Admit_List);
165 INIT_LIST_HEAD(&ieee->Rx_TS_Pending_List);
166 INIT_LIST_HEAD(&ieee->Rx_TS_Unused_List);
167 for (count = 0; count < TOTAL_TS_NUM; count++) {
169 INIT_LIST_HEAD(&pRxTS->rx_pending_pkt_list);
171 timer_setup(&pRxTS->ts_common_info.SetupTimer, TsSetupTimeOut,
174 timer_setup(&pRxTS->ts_common_info.InactTimer, TsInactTimeout,
177 timer_setup(&pRxTS->rx_admitted_ba_record.timer,
178 RxBaInactTimeout, 0);
180 timer_setup(&pRxTS->rx_pkt_pending_timer, RxPktPendingTimeout, 0);
182 ResetRxTsEntry(pRxTS);
183 list_add_tail(&pRxTS->ts_common_info.List,
184 &ieee->Rx_TS_Unused_List);
187 INIT_LIST_HEAD(&ieee->RxReorder_Unused_List);
188 for (count = 0; count < REORDER_ENTRY_NUM; count++) {
189 list_add_tail(&pRxReorderEntry->List,
190 &ieee->RxReorder_Unused_List);
191 if (count == (REORDER_ENTRY_NUM - 1))
193 pRxReorderEntry = &ieee->RxReorderEntry[count + 1];
197 static void AdmitTS(struct rtllib_device *ieee,
198 struct ts_common_info *pTsCommonInfo, u32 InactTime)
200 del_timer_sync(&pTsCommonInfo->SetupTimer);
201 del_timer_sync(&pTsCommonInfo->InactTimer);
204 mod_timer(&pTsCommonInfo->InactTimer, jiffies +
205 msecs_to_jiffies(InactTime));
208 static struct ts_common_info *SearchAdmitTRStream(struct rtllib_device *ieee,
210 enum tr_select TxRxSelect)
213 bool search_dir[4] = {0};
214 struct list_head *psearch_list;
215 struct ts_common_info *pRet = NULL;
217 if (ieee->iw_mode == IW_MODE_MASTER) {
218 if (TxRxSelect == TX_DIR) {
219 search_dir[DIR_DOWN] = true;
220 search_dir[DIR_BI_DIR] = true;
222 search_dir[DIR_UP] = true;
223 search_dir[DIR_BI_DIR] = true;
225 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
226 if (TxRxSelect == TX_DIR)
227 search_dir[DIR_UP] = true;
229 search_dir[DIR_DOWN] = true;
231 if (TxRxSelect == TX_DIR) {
232 search_dir[DIR_UP] = true;
233 search_dir[DIR_BI_DIR] = true;
234 search_dir[DIR_DIRECT] = true;
236 search_dir[DIR_DOWN] = true;
237 search_dir[DIR_BI_DIR] = true;
238 search_dir[DIR_DIRECT] = true;
242 if (TxRxSelect == TX_DIR)
243 psearch_list = &ieee->Tx_TS_Admit_List;
245 psearch_list = &ieee->Rx_TS_Admit_List;
247 for (dir = 0; dir <= DIR_BI_DIR; dir++) {
248 if (!search_dir[dir])
250 list_for_each_entry(pRet, psearch_list, List) {
251 if (memcmp(pRet->Addr, Addr, 6) == 0 &&
252 pRet->TSpec.f.TSInfo.field.ucTSID == TID &&
253 pRet->TSpec.f.TSInfo.field.ucDirection == dir)
256 if (&pRet->List != psearch_list)
260 if (pRet && &pRet->List != psearch_list)
265 static void MakeTSEntry(struct ts_common_info *pTsCommonInfo, u8 *Addr,
266 union tspec_body *pTSPEC, union qos_tclas *pTCLAS,
267 u8 TCLAS_Num, u8 TCLAS_Proc)
274 memcpy(pTsCommonInfo->Addr, Addr, 6);
277 memcpy((u8 *)(&(pTsCommonInfo->TSpec)), (u8 *)pTSPEC,
278 sizeof(union tspec_body));
280 for (count = 0; count < TCLAS_Num; count++)
281 memcpy((u8 *)(&(pTsCommonInfo->TClass[count])),
282 (u8 *)pTCLAS, sizeof(union qos_tclas));
284 pTsCommonInfo->TClasProc = TCLAS_Proc;
285 pTsCommonInfo->TClasNum = TCLAS_Num;
288 bool GetTs(struct rtllib_device *ieee, struct ts_common_info **ppTS,
289 u8 *Addr, u8 TID, enum tr_select TxRxSelect, bool bAddNewTs)
292 union tspec_body TSpec;
293 union qos_tsinfo *pTSInfo = &TSpec.f.TSInfo;
294 struct list_head *pUnusedList;
295 struct list_head *pAddmitList;
296 enum direction_value Dir;
298 if (is_multicast_ether_addr(Addr)) {
299 netdev_warn(ieee->dev, "Get TS for Broadcast or Multicast\n");
302 if (ieee->current_network.qos_data.supported == 0) {
323 netdev_warn(ieee->dev, "%s(): TID(%d) is not valid\n",
329 *ppTS = SearchAdmitTRStream(ieee, Addr, UP, TxRxSelect);
334 netdev_dbg(ieee->dev, "add new TS failed(tid:%d)\n", UP);
338 pUnusedList = (TxRxSelect == TX_DIR) ?
339 (&ieee->Tx_TS_Unused_List) :
340 (&ieee->Rx_TS_Unused_List);
342 pAddmitList = (TxRxSelect == TX_DIR) ?
343 (&ieee->Tx_TS_Admit_List) :
344 (&ieee->Rx_TS_Admit_List);
346 Dir = (ieee->iw_mode == IW_MODE_MASTER) ?
347 ((TxRxSelect == TX_DIR) ? DIR_DOWN : DIR_UP) :
348 ((TxRxSelect == TX_DIR) ? DIR_UP : DIR_DOWN);
350 if (!list_empty(pUnusedList)) {
351 (*ppTS) = list_entry(pUnusedList->next,
352 struct ts_common_info, List);
353 list_del_init(&(*ppTS)->List);
354 if (TxRxSelect == TX_DIR) {
355 struct tx_ts_record *tmp =
361 struct rx_ts_record *tmp =
368 netdev_dbg(ieee->dev,
369 "to init current TS, UP:%d, Dir:%d, addr: %pM ppTs=%p\n",
370 UP, Dir, Addr, *ppTS);
371 pTSInfo->field.ucTrafficType = 0;
372 pTSInfo->field.ucTSID = UP;
373 pTSInfo->field.ucDirection = Dir;
374 pTSInfo->field.ucAccessPolicy = 1;
375 pTSInfo->field.ucAggregation = 0;
376 pTSInfo->field.ucPSB = 0;
377 pTSInfo->field.ucUP = UP;
378 pTSInfo->field.ucTSInfoAckPolicy = 0;
379 pTSInfo->field.ucSchedule = 0;
381 MakeTSEntry(*ppTS, Addr, &TSpec, NULL, 0, 0);
382 AdmitTS(ieee, *ppTS, 0);
383 list_add_tail(&((*ppTS)->List), pAddmitList);
388 netdev_warn(ieee->dev,
389 "There is not enough dir=%d(0=up down=1) TS record to be used!",
394 static void RemoveTsEntry(struct rtllib_device *ieee,
395 struct ts_common_info *pTs, enum tr_select TxRxSelect)
397 del_timer_sync(&pTs->SetupTimer);
398 del_timer_sync(&pTs->InactTimer);
399 TsInitDelBA(ieee, pTs, TxRxSelect);
401 if (TxRxSelect == RX_DIR) {
402 struct rx_reorder_entry *pRxReorderEntry;
403 struct rx_ts_record *pRxTS = (struct rx_ts_record *)pTs;
405 if (timer_pending(&pRxTS->rx_pkt_pending_timer))
406 del_timer_sync(&pRxTS->rx_pkt_pending_timer);
408 while (!list_empty(&pRxTS->rx_pending_pkt_list)) {
409 pRxReorderEntry = (struct rx_reorder_entry *)
410 list_entry(pRxTS->rx_pending_pkt_list.prev,
411 struct rx_reorder_entry, List);
412 netdev_dbg(ieee->dev, "%s(): Delete SeqNum %d!\n",
413 __func__, pRxReorderEntry->SeqNum);
414 list_del_init(&pRxReorderEntry->List);
417 struct rtllib_rxb *prxb = pRxReorderEntry->prxb;
421 for (i = 0; i < prxb->nr_subframes; i++)
422 dev_kfree_skb(prxb->subframes[i]);
426 list_add_tail(&pRxReorderEntry->List,
427 &ieee->RxReorder_Unused_List);
430 struct tx_ts_record *pTxTS = (struct tx_ts_record *)pTs;
432 del_timer_sync(&pTxTS->TsAddBaTimer);
436 void RemovePeerTS(struct rtllib_device *ieee, u8 *Addr)
438 struct ts_common_info *pTS, *pTmpTS;
440 netdev_info(ieee->dev, "===========>%s, %pM\n", __func__, Addr);
442 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Pending_List, List) {
443 if (memcmp(pTS->Addr, Addr, 6) == 0) {
444 RemoveTsEntry(ieee, pTS, TX_DIR);
445 list_del_init(&pTS->List);
446 list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
450 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Admit_List, List) {
451 if (memcmp(pTS->Addr, Addr, 6) == 0) {
452 netdev_info(ieee->dev,
453 "====>remove Tx_TS_admin_list\n");
454 RemoveTsEntry(ieee, pTS, TX_DIR);
455 list_del_init(&pTS->List);
456 list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
460 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Pending_List, List) {
461 if (memcmp(pTS->Addr, Addr, 6) == 0) {
462 RemoveTsEntry(ieee, pTS, RX_DIR);
463 list_del_init(&pTS->List);
464 list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
468 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Admit_List, List) {
469 if (memcmp(pTS->Addr, Addr, 6) == 0) {
470 RemoveTsEntry(ieee, pTS, RX_DIR);
471 list_del_init(&pTS->List);
472 list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
476 EXPORT_SYMBOL(RemovePeerTS);
478 void RemoveAllTS(struct rtllib_device *ieee)
480 struct ts_common_info *pTS, *pTmpTS;
482 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Pending_List, List) {
483 RemoveTsEntry(ieee, pTS, TX_DIR);
484 list_del_init(&pTS->List);
485 list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
488 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Tx_TS_Admit_List, List) {
489 RemoveTsEntry(ieee, pTS, TX_DIR);
490 list_del_init(&pTS->List);
491 list_add_tail(&pTS->List, &ieee->Tx_TS_Unused_List);
494 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Pending_List, List) {
495 RemoveTsEntry(ieee, pTS, RX_DIR);
496 list_del_init(&pTS->List);
497 list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
500 list_for_each_entry_safe(pTS, pTmpTS, &ieee->Rx_TS_Admit_List, List) {
501 RemoveTsEntry(ieee, pTS, RX_DIR);
502 list_del_init(&pTS->List);
503 list_add_tail(&pTS->List, &ieee->Rx_TS_Unused_List);
507 void TsStartAddBaProcess(struct rtllib_device *ieee, struct tx_ts_record *pTxTS)
509 if (pTxTS->bAddBaReqInProgress == false) {
510 pTxTS->bAddBaReqInProgress = true;
512 if (pTxTS->bAddBaReqDelayed) {
513 netdev_dbg(ieee->dev, "Start ADDBA after 60 sec!!\n");
514 mod_timer(&pTxTS->TsAddBaTimer, jiffies +
515 msecs_to_jiffies(TS_ADDBA_DELAY));
517 netdev_dbg(ieee->dev, "Immediately Start ADDBA\n");
518 mod_timer(&pTxTS->TsAddBaTimer, jiffies + 10);
521 netdev_dbg(ieee->dev, "BA timer is already added\n");