x86/PCI: use host bridge _CRS info on ASUS M2V-MX SE
[linux-btrfs-devel.git] / drivers / staging / bcm / LeakyBucket.c
bloba55d4228e8e056d886d56934cd45ed50487e4f0d
1 /**********************************************************************
2 * LEAKYBUCKET.C
3 * This file contains the routines related to Leaky Bucket Algorithm.
4 ***********************************************************************/
5 #include "headers.h"
7 /*********************************************************************
8 * Function - UpdateTokenCount()
10 * Description - This function calculates the token count for each
11 * channel and updates the same in Adapter strucuture.
13 * Parameters - Adapter: Pointer to the Adapter structure.
15 * Returns - None
16 **********************************************************************/
18 static VOID UpdateTokenCount(register PMINI_ADAPTER Adapter)
20 ULONG liCurrentTime;
21 INT i = 0;
22 struct timeval tv;
24 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "=====>\n");
25 if(NULL == Adapter)
27 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "Adapter found NULL!\n");
28 return;
31 do_gettimeofday(&tv);
32 for(i = 0; i < NO_OF_QUEUES; i++)
34 if(TRUE == Adapter->PackInfo[i].bValid &&
35 (1 == Adapter->PackInfo[i].ucDirection))
37 liCurrentTime = ((tv.tv_sec-
38 Adapter->PackInfo[i].stLastUpdateTokenAt.tv_sec)*1000 +
39 (tv.tv_usec-Adapter->PackInfo[i].stLastUpdateTokenAt.tv_usec)/
40 1000);
41 if(0!=liCurrentTime)
43 Adapter->PackInfo[i].uiCurrentTokenCount += (ULONG)
44 ((Adapter->PackInfo[i].uiMaxAllowedRate) *
45 ((ULONG)((liCurrentTime)))/1000);
46 memcpy(&Adapter->PackInfo[i].stLastUpdateTokenAt,
47 &tv, sizeof(struct timeval));
48 Adapter->PackInfo[i].liLastUpdateTokenAt = liCurrentTime;
49 if((Adapter->PackInfo[i].uiCurrentTokenCount) >=
50 Adapter->PackInfo[i].uiMaxBucketSize)
52 Adapter->PackInfo[i].uiCurrentTokenCount =
53 Adapter->PackInfo[i].uiMaxBucketSize;
58 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "<=====\n");
59 return;
64 /*********************************************************************
65 * Function - IsPacketAllowedForFlow()
67 * Description - This function checks whether the given packet from the
68 * specified queue can be allowed for transmission by
69 * checking the token count.
71 * Parameters - Adapter : Pointer to the Adpater structure.
72 * - iQIndex : The queue Identifier.
73 * - ulPacketLength: Number of bytes to be transmitted.
75 * Returns - The number of bytes allowed for transmission.
77 ***********************************************************************/
78 static ULONG GetSFTokenCount(PMINI_ADAPTER Adapter, PacketInfo *psSF)
80 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "IsPacketAllowedForFlow ===>");
81 /* Validate the parameters */
82 if(NULL == Adapter || (psSF < Adapter->PackInfo &&
83 (uintptr_t)psSF > (uintptr_t) &Adapter->PackInfo[HiPriority]))
85 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "IPAFF: Got wrong Parameters:Adapter: %p, QIndex: %zd\n", Adapter, (psSF-Adapter->PackInfo));
86 return 0;
89 if(FALSE != psSF->bValid && psSF->ucDirection)
91 if(0 != psSF->uiCurrentTokenCount)
93 return psSF->uiCurrentTokenCount;
95 else
97 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "Not enough tokens in queue %zd Available %u\n",
98 psSF-Adapter->PackInfo, psSF->uiCurrentTokenCount);
99 psSF->uiPendedLast = 1;
102 else
104 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "IPAFF: Queue %zd not valid\n", psSF-Adapter->PackInfo);
106 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TOKEN_COUNTS, DBG_LVL_ALL, "IsPacketAllowedForFlow <===");
107 return 0;
111 @ingroup tx_functions
112 This function despatches packet from the specified queue.
113 @return Zero(success) or Negative value(failure)
115 static INT SendPacketFromQueue(PMINI_ADAPTER Adapter,/**<Logical Adapter*/
116 PacketInfo *psSF, /**<Queue identifier*/
117 struct sk_buff* Packet) /**<Pointer to the packet to be sent*/
119 INT Status=STATUS_FAILURE;
120 UINT uiIndex =0,PktLen = 0;
122 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, SEND_QUEUE, DBG_LVL_ALL, "=====>");
123 if(!Adapter || !Packet || !psSF)
125 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, SEND_QUEUE, DBG_LVL_ALL, "Got NULL Adapter or Packet");
126 return -EINVAL;
129 if(psSF->liDrainCalculated==0)
131 psSF->liDrainCalculated = jiffies;
133 ///send the packet to the fifo..
134 PktLen = Packet->len;
135 Status = SetupNextSend(Adapter, Packet, psSF->usVCID_Value);
136 if(Status == 0)
138 for(uiIndex = 0 ; uiIndex < MIBS_MAX_HIST_ENTRIES ; uiIndex++)
139 { if((PktLen <= MIBS_PKTSIZEHIST_RANGE*(uiIndex+1)) && (PktLen > MIBS_PKTSIZEHIST_RANGE*(uiIndex)))
140 Adapter->aTxPktSizeHist[uiIndex]++;
143 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, SEND_QUEUE, DBG_LVL_ALL, "<=====");
144 return Status;
147 /************************************************************************
148 * Function - CheckAndSendPacketFromIndex()
150 * Description - This function dequeues the data/control packet from the
151 * specified queue for transmission.
153 * Parameters - Adapter : Pointer to the driver control structure.
154 * - iQIndex : The queue Identifier.
156 * Returns - None.
158 ****************************************************************************/
159 static VOID CheckAndSendPacketFromIndex(PMINI_ADAPTER Adapter, PacketInfo *psSF)
161 struct sk_buff *QueuePacket=NULL;
162 char *pControlPacket = NULL;
163 INT Status=0;
164 int iPacketLen=0;
167 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "%zd ====>", (psSF-Adapter->PackInfo));
168 if((psSF != &Adapter->PackInfo[HiPriority]) && Adapter->LinkUpStatus && atomic_read(&psSF->uiPerSFTxResourceCount))//Get data packet
170 if(!psSF->ucDirection )
171 return;
173 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "UpdateTokenCount ");
174 if(Adapter->IdleMode || Adapter->bPreparingForLowPowerMode)
175 return; /* in idle mode */
177 // Check for Free Descriptors
178 if(atomic_read(&Adapter->CurrNumFreeTxDesc) <= MINIMUM_PENDING_DESCRIPTORS)
180 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, " No Free Tx Descriptor(%d) is available for Data pkt..",atomic_read(&Adapter->CurrNumFreeTxDesc));
181 return ;
184 spin_lock_bh(&psSF->SFQueueLock);
185 QueuePacket=psSF->FirstTxQueue;
187 if(QueuePacket)
189 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Dequeuing Data Packet");
191 if(psSF->bEthCSSupport)
192 iPacketLen = QueuePacket->len;
193 else
194 iPacketLen = QueuePacket->len-ETH_HLEN;
196 iPacketLen<<=3;
197 if(iPacketLen <= GetSFTokenCount(Adapter, psSF))
199 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Allowed bytes %d",
200 (iPacketLen >> 3));
202 DEQUEUEPACKET(psSF->FirstTxQueue,psSF->LastTxQueue);
203 psSF->uiCurrentBytesOnHost -= (QueuePacket->len);
204 psSF->uiCurrentPacketsOnHost--;
205 atomic_dec(&Adapter->TotalPacketCount);
206 spin_unlock_bh(&psSF->SFQueueLock);
208 Status = SendPacketFromQueue(Adapter, psSF, QueuePacket);
209 psSF->uiPendedLast = FALSE;
211 else
213 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "For Queue: %zd\n", psSF-Adapter->PackInfo);
214 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "\nAvailable Tokens = %d required = %d\n",
215 psSF->uiCurrentTokenCount, iPacketLen);
216 //this part indicates that because of non-availability of the tokens
217 //pkt has not been send out hence setting the pending flag indicating the host to send it out
218 //first next iteration .
219 psSF->uiPendedLast = TRUE;
220 spin_unlock_bh(&psSF->SFQueueLock);
223 else
225 spin_unlock_bh(&psSF->SFQueueLock);
228 else
231 if((atomic_read(&Adapter->CurrNumFreeTxDesc) > 0 ) &&
232 (atomic_read(&Adapter->index_rd_txcntrlpkt) !=
233 atomic_read(&Adapter->index_wr_txcntrlpkt))
236 pControlPacket = Adapter->txctlpacket
237 [(atomic_read(&Adapter->index_rd_txcntrlpkt)%MAX_CNTRL_PKTS)];
238 if(pControlPacket)
240 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Sending Control packet");
241 Status = SendControlPacket(Adapter, pControlPacket);
242 if(STATUS_SUCCESS==Status)
244 spin_lock_bh(&psSF->SFQueueLock);
245 psSF->NumOfPacketsSent++;
246 psSF->uiSentBytes+=((PLEADER)pControlPacket)->PLength;
247 psSF->uiSentPackets++;
248 atomic_dec(&Adapter->TotalPacketCount);
249 psSF->uiCurrentBytesOnHost -= ((PLEADER)pControlPacket)->PLength;
250 psSF->uiCurrentPacketsOnHost--;
251 atomic_inc(&Adapter->index_rd_txcntrlpkt);
252 spin_unlock_bh(&psSF->SFQueueLock);
254 else
255 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "SendControlPacket Failed\n");
257 else
259 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, " Control Pkt is not available, Indexing is wrong....");
266 /*******************************************************************
267 * Function - transmit_packets()
269 * Description - This function transmits the packets from different
270 * queues, if free descriptors are available on target.
272 * Parameters - Adapter: Pointer to the Adapter structure.
274 * Returns - None.
275 ********************************************************************/
276 VOID transmit_packets(PMINI_ADAPTER Adapter)
278 UINT uiPrevTotalCount = 0;
279 int iIndex = 0;
281 BOOLEAN exit_flag = TRUE ;
283 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "=====>");
285 if(NULL == Adapter)
287 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX,TX_PACKETS, DBG_LVL_ALL, "Got NULL Adapter");
288 return;
290 if(Adapter->device_removed == TRUE)
292 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Device removed");
293 return;
296 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "\nUpdateTokenCount ====>\n");
298 UpdateTokenCount(Adapter);
300 BCM_DEBUG_PRINT (Adapter, DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "\nPruneQueueAllSF ====>\n");
302 PruneQueueAllSF(Adapter);
304 uiPrevTotalCount = atomic_read(&Adapter->TotalPacketCount);
306 for(iIndex=HiPriority;iIndex>=0;iIndex--)
308 if( !uiPrevTotalCount || (TRUE == Adapter->device_removed))
309 break;
311 if(Adapter->PackInfo[iIndex].bValid &&
312 Adapter->PackInfo[iIndex].uiPendedLast &&
313 Adapter->PackInfo[iIndex].uiCurrentBytesOnHost)
315 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Calling CheckAndSendPacketFromIndex..");
316 CheckAndSendPacketFromIndex(Adapter, &Adapter->PackInfo[iIndex]);
317 uiPrevTotalCount--;
321 while(uiPrevTotalCount > 0 && !Adapter->device_removed)
323 exit_flag = TRUE ;
324 //second iteration to parse non-pending queues
325 for(iIndex=HiPriority;iIndex>=0;iIndex--)
327 if( !uiPrevTotalCount || (TRUE == Adapter->device_removed))
328 break;
330 if(Adapter->PackInfo[iIndex].bValid &&
331 Adapter->PackInfo[iIndex].uiCurrentBytesOnHost &&
332 !Adapter->PackInfo[iIndex].uiPendedLast )
334 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "Calling CheckAndSendPacketFromIndex..");
335 CheckAndSendPacketFromIndex(Adapter, &Adapter->PackInfo[iIndex]);
336 uiPrevTotalCount--;
337 exit_flag = FALSE;
341 if(Adapter->IdleMode || Adapter->bPreparingForLowPowerMode)
343 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "In Idle Mode\n");
344 break;
346 if(exit_flag == TRUE )
347 break ;
348 }/* end of inner while loop */
350 update_per_cid_rx (Adapter);
351 Adapter->txtransmit_running = 0;
352 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_TX, TX_PACKETS, DBG_LVL_ALL, "<======");