Merge branch 'for-linus' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[firefly-linux-kernel-4.4.55.git] / drivers / staging / bcm / Qos.c
1 /**
2 @file Qos.C
3 This file contains the routines related to Quality of Service.
4 */
5 #include "headers.h"
6
7 static void EThCSGetPktInfo(struct bcm_mini_adapter *Adapter, PVOID pvEthPayload, struct bcm_eth_packet_info *pstEthCsPktInfo);
8 static bool EThCSClassifyPkt(struct bcm_mini_adapter *Adapter, struct sk_buff* skb, struct bcm_eth_packet_info *pstEthCsPktInfo, struct bcm_classifier_rule *pstClassifierRule, B_UINT8 EthCSCupport);
9
10 static USHORT   IpVersion4(struct bcm_mini_adapter *Adapter, struct iphdr *iphd,
11                            struct bcm_classifier_rule *pstClassifierRule);
12
13 static VOID PruneQueue(struct bcm_mini_adapter *Adapter, INT iIndex);
14
15
16 /*******************************************************************
17 * Function    - MatchSrcIpAddress()
18 *
19 * Description - Checks whether the Source IP address from the packet
20 *                               matches with that of Queue.
21 *
22 * Parameters  - pstClassifierRule: Pointer to the packet info structure.
23 *               - ulSrcIP           : Source IP address from the packet.
24 *
25 * Returns     - TRUE(If address matches) else FAIL .
26 *********************************************************************/
27 static bool MatchSrcIpAddress(struct bcm_classifier_rule *pstClassifierRule, ULONG ulSrcIP)
28 {
29         UCHAR ucLoopIndex = 0;
30
31         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
32
33         ulSrcIP = ntohl(ulSrcIP);
34         if (0 == pstClassifierRule->ucIPSourceAddressLength)
35                 return TRUE;
36         for (ucLoopIndex = 0; ucLoopIndex < (pstClassifierRule->ucIPSourceAddressLength); ucLoopIndex++)
37         {
38                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Src Ip Address Mask:0x%x PacketIp:0x%x and Classification:0x%x", (UINT)pstClassifierRule->stSrcIpAddress.ulIpv4Mask[ucLoopIndex], (UINT)ulSrcIP, (UINT)pstClassifierRule->stSrcIpAddress.ulIpv6Addr[ucLoopIndex]);
39                 if ((pstClassifierRule->stSrcIpAddress.ulIpv4Mask[ucLoopIndex] & ulSrcIP) ==
40                                 (pstClassifierRule->stSrcIpAddress.ulIpv4Addr[ucLoopIndex] & pstClassifierRule->stSrcIpAddress.ulIpv4Mask[ucLoopIndex]))
41                 {
42                         return TRUE;
43                 }
44         }
45         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Src Ip Address Not Matched");
46         return false;
47 }
48
49
50 /*******************************************************************
51 * Function    - MatchDestIpAddress()
52 *
53 * Description - Checks whether the Destination IP address from the packet
54 *                               matches with that of Queue.
55 *
56 * Parameters  - pstClassifierRule: Pointer to the packet info structure.
57 *               - ulDestIP    : Destination IP address from the packet.
58 *
59 * Returns     - TRUE(If address matches) else FAIL .
60 *********************************************************************/
61 static bool MatchDestIpAddress(struct bcm_classifier_rule *pstClassifierRule, ULONG ulDestIP)
62 {
63         UCHAR ucLoopIndex = 0;
64         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
65
66         ulDestIP = ntohl(ulDestIP);
67         if (0 == pstClassifierRule->ucIPDestinationAddressLength)
68                 return TRUE;
69         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Destination Ip Address 0x%x 0x%x 0x%x  ", (UINT)ulDestIP, (UINT)pstClassifierRule->stDestIpAddress.ulIpv4Mask[ucLoopIndex], (UINT)pstClassifierRule->stDestIpAddress.ulIpv4Addr[ucLoopIndex]);
70
71         for (ucLoopIndex = 0; ucLoopIndex < (pstClassifierRule->ucIPDestinationAddressLength); ucLoopIndex++)
72         {
73                 if ((pstClassifierRule->stDestIpAddress.ulIpv4Mask[ucLoopIndex] & ulDestIP) ==
74                                 (pstClassifierRule->stDestIpAddress.ulIpv4Addr[ucLoopIndex] & pstClassifierRule->stDestIpAddress.ulIpv4Mask[ucLoopIndex]))
75                 {
76                         return TRUE;
77                 }
78         }
79         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Destination Ip Address Not Matched");
80         return false;
81 }
82
83
84 /************************************************************************
85 * Function    - MatchTos()
86 *
87 * Description - Checks the TOS from the packet matches with that of queue.
88 *
89 * Parameters  - pstClassifierRule   : Pointer to the packet info structure.
90 *               - ucTypeOfService: TOS from the packet.
91 *
92 * Returns     - TRUE(If address matches) else FAIL.
93 **************************************************************************/
94 static bool MatchTos(struct bcm_classifier_rule *pstClassifierRule, UCHAR ucTypeOfService)
95 {
96
97         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
98         if (3 != pstClassifierRule->ucIPTypeOfServiceLength)
99                 return TRUE;
100
101         if (((pstClassifierRule->ucTosMask & ucTypeOfService) <= pstClassifierRule->ucTosHigh) && ((pstClassifierRule->ucTosMask & ucTypeOfService) >= pstClassifierRule->ucTosLow))
102         {
103                 return TRUE;
104         }
105         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Type Of Service Not Matched");
106         return false;
107 }
108
109
110 /***************************************************************************
111 * Function    - MatchProtocol()
112 *
113 * Description - Checks the protocol from the packet matches with that of queue.
114 *
115 * Parameters  - pstClassifierRule: Pointer to the packet info structure.
116 *               - ucProtocol    : Protocol from the packet.
117 *
118 * Returns     - TRUE(If address matches) else FAIL.
119 ****************************************************************************/
120 bool MatchProtocol(struct bcm_classifier_rule *pstClassifierRule, UCHAR ucProtocol)
121 {
122         UCHAR ucLoopIndex = 0;
123         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
124         if (0 == pstClassifierRule->ucProtocolLength)
125                 return TRUE;
126         for (ucLoopIndex = 0; ucLoopIndex < pstClassifierRule->ucProtocolLength; ucLoopIndex++)
127         {
128                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Protocol:0x%X Classification Protocol:0x%X", ucProtocol, pstClassifierRule->ucProtocol[ucLoopIndex]);
129                 if (pstClassifierRule->ucProtocol[ucLoopIndex] == ucProtocol)
130                 {
131                         return TRUE;
132                 }
133         }
134         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Protocol Not Matched");
135         return false;
136 }
137
138
139 /***********************************************************************
140 * Function    - MatchSrcPort()
141 *
142 * Description - Checks, Source port from the packet matches with that of queue.
143 *
144 * Parameters  - pstClassifierRule: Pointer to the packet info structure.
145 *               - ushSrcPort    : Source port from the packet.
146 *
147 * Returns     - TRUE(If address matches) else FAIL.
148 ***************************************************************************/
149 bool MatchSrcPort(struct bcm_classifier_rule *pstClassifierRule, USHORT ushSrcPort)
150 {
151         UCHAR ucLoopIndex = 0;
152
153         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
154
155
156         if (0 == pstClassifierRule->ucSrcPortRangeLength)
157                 return TRUE;
158         for (ucLoopIndex = 0; ucLoopIndex < pstClassifierRule->ucSrcPortRangeLength; ucLoopIndex++)
159         {
160                 if (ushSrcPort <= pstClassifierRule->usSrcPortRangeHi[ucLoopIndex] &&
161                         ushSrcPort >= pstClassifierRule->usSrcPortRangeLo[ucLoopIndex])
162                 {
163                         return TRUE;
164                 }
165         }
166         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Src Port: %x Not Matched ", ushSrcPort);
167         return false;
168 }
169
170
171 /***********************************************************************
172 * Function    - MatchDestPort()
173 *
174 * Description - Checks, Destination port from packet matches with that of queue.
175 *
176 * Parameters  - pstClassifierRule: Pointer to the packet info structure.
177 *               - ushDestPort   : Destination port from the packet.
178 *
179 * Returns     - TRUE(If address matches) else FAIL.
180 ***************************************************************************/
181 bool MatchDestPort(struct bcm_classifier_rule *pstClassifierRule, USHORT ushDestPort)
182 {
183         UCHAR ucLoopIndex = 0;
184         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
185
186         if (0 == pstClassifierRule->ucDestPortRangeLength)
187                 return TRUE;
188
189         for (ucLoopIndex = 0; ucLoopIndex < pstClassifierRule->ucDestPortRangeLength; ucLoopIndex++)
190         {
191                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Matching Port:0x%X   0x%X  0x%X", ushDestPort, pstClassifierRule->usDestPortRangeLo[ucLoopIndex], pstClassifierRule->usDestPortRangeHi[ucLoopIndex]);
192
193                 if (ushDestPort <= pstClassifierRule->usDestPortRangeHi[ucLoopIndex] &&
194                         ushDestPort >= pstClassifierRule->usDestPortRangeLo[ucLoopIndex])
195                 {
196                         return TRUE;
197                 }
198         }
199         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Dest Port: %x Not Matched", ushDestPort);
200         return false;
201 }
202 /**
203 @ingroup tx_functions
204 Compares IPV4 Ip address and port number
205 @return Queue Index.
206 */
207 static USHORT   IpVersion4(struct bcm_mini_adapter *Adapter,
208                            struct iphdr *iphd,
209                            struct bcm_classifier_rule *pstClassifierRule)
210 {
211         struct bcm_transport_header *xprt_hdr = NULL;
212         bool    bClassificationSucceed = false;
213
214         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "========>");
215
216         xprt_hdr = (struct bcm_transport_header *)((PUCHAR)iphd + sizeof(struct iphdr));
217
218         do {
219                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Trying to see Direction = %d %d",
220                         pstClassifierRule->ucDirection,
221                         pstClassifierRule->usVCID_Value);
222
223                 //Checking classifier validity
224                 if (!pstClassifierRule->bUsed || pstClassifierRule->ucDirection == DOWNLINK_DIR)
225                         break;
226
227                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "is IPv6 check!");
228                 if (pstClassifierRule->bIpv6Protocol)
229                         break;
230
231                 //**************Checking IP header parameter**************************//
232                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Trying to match Source IP Address");
233                 if (!MatchSrcIpAddress(pstClassifierRule, iphd->saddr))
234                         break;
235                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Source IP Address Matched");
236
237                 if (!MatchDestIpAddress(pstClassifierRule, iphd->daddr))
238                         break;
239                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Destination IP Address Matched");
240
241                 if (!MatchTos(pstClassifierRule, iphd->tos)) {
242                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "TOS Match failed\n");
243                         break;
244                 }
245                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "TOS Matched");
246
247                 if (!MatchProtocol(pstClassifierRule, iphd->protocol))
248                         break;
249                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Protocol Matched");
250
251                 //if protocol is not TCP or UDP then no need of comparing source port and destination port
252                 if (iphd->protocol != TCP && iphd->protocol != UDP) {
253                         bClassificationSucceed = TRUE;
254                         break;
255                 }
256                 //******************Checking Transport Layer Header field if present *****************//
257                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Source Port %04x",
258                         (iphd->protocol == UDP) ? xprt_hdr->uhdr.source : xprt_hdr->thdr.source);
259
260                 if (!MatchSrcPort(pstClassifierRule,
261                                   ntohs((iphd->protocol == UDP) ?
262                                   xprt_hdr->uhdr.source : xprt_hdr->thdr.source)))
263                         break;
264                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Src Port Matched");
265
266                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Destination Port %04x",
267                         (iphd->protocol == UDP) ? xprt_hdr->uhdr.dest :
268                         xprt_hdr->thdr.dest);
269                 if (!MatchDestPort(pstClassifierRule,
270                                    ntohs((iphd->protocol == UDP) ?
271                                    xprt_hdr->uhdr.dest : xprt_hdr->thdr.dest)))
272                         break;
273                 bClassificationSucceed = TRUE;
274         } while (0);
275
276         if (TRUE == bClassificationSucceed)
277         {
278                 INT iMatchedSFQueueIndex = 0;
279                 iMatchedSFQueueIndex = SearchSfid(Adapter, pstClassifierRule->ulSFID);
280                 if (iMatchedSFQueueIndex >= NO_OF_QUEUES)
281                 {
282                         bClassificationSucceed = false;
283                 }
284                 else
285                 {
286                         if (false == Adapter->PackInfo[iMatchedSFQueueIndex].bActive)
287                         {
288                                 bClassificationSucceed = false;
289                         }
290                 }
291         }
292
293         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "IpVersion4 <==========");
294
295         return bClassificationSucceed;
296 }
297
298 VOID PruneQueueAllSF(struct bcm_mini_adapter *Adapter)
299 {
300         UINT iIndex = 0;
301
302         for (iIndex = 0; iIndex < HiPriority; iIndex++)
303         {
304                 if (!Adapter->PackInfo[iIndex].bValid)
305                         continue;
306
307                 PruneQueue(Adapter, iIndex);
308         }
309 }
310
311
312 /**
313 @ingroup tx_functions
314 This function checks if the max queue size for a queue
315 is less than number of bytes in the queue. If so -
316 drops packets from the Head till the number of bytes is
317 less than or equal to max queue size for the queue.
318 */
319 static VOID PruneQueue(struct bcm_mini_adapter *Adapter, INT iIndex)
320 {
321         struct sk_buff* PacketToDrop = NULL;
322         struct net_device_stats *netstats;
323
324         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, PRUNE_QUEUE, DBG_LVL_ALL, "=====> Index %d", iIndex);
325
326         if (iIndex == HiPriority)
327                 return;
328
329         if (!Adapter || (iIndex < 0) || (iIndex > HiPriority))
330                 return;
331
332         /* To Store the netdevice statistic */
333         netstats = &Adapter->dev->stats;
334
335         spin_lock_bh(&Adapter->PackInfo[iIndex].SFQueueLock);
336
337         while (1)
338 //      while((UINT)Adapter->PackInfo[iIndex].uiCurrentPacketsOnHost >
339 //              SF_MAX_ALLOWED_PACKETS_TO_BACKUP)
340         {
341                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, PRUNE_QUEUE, DBG_LVL_ALL, "uiCurrentBytesOnHost:%x uiMaxBucketSize :%x",
342                 Adapter->PackInfo[iIndex].uiCurrentBytesOnHost,
343                 Adapter->PackInfo[iIndex].uiMaxBucketSize);
344
345                 PacketToDrop = Adapter->PackInfo[iIndex].FirstTxQueue;
346
347                 if (PacketToDrop == NULL)
348                         break;
349                 if ((Adapter->PackInfo[iIndex].uiCurrentPacketsOnHost < SF_MAX_ALLOWED_PACKETS_TO_BACKUP) &&
350                         ((1000*(jiffies - *((B_UINT32 *)(PacketToDrop->cb)+SKB_CB_LATENCY_OFFSET))/HZ) <= Adapter->PackInfo[iIndex].uiMaxLatency))
351                         break;
352
353                 if (PacketToDrop)
354                 {
355                         if (netif_msg_tx_err(Adapter))
356                                 pr_info(PFX "%s: tx queue %d overlimit\n",
357                                         Adapter->dev->name, iIndex);
358
359                         netstats->tx_dropped++;
360
361                         DEQUEUEPACKET(Adapter->PackInfo[iIndex].FirstTxQueue,
362                                                 Adapter->PackInfo[iIndex].LastTxQueue);
363                         /// update current bytes and packets count
364                         Adapter->PackInfo[iIndex].uiCurrentBytesOnHost -=
365                                 PacketToDrop->len;
366                         Adapter->PackInfo[iIndex].uiCurrentPacketsOnHost--;
367                         /// update dropped bytes and packets counts
368                         Adapter->PackInfo[iIndex].uiDroppedCountBytes += PacketToDrop->len;
369                         Adapter->PackInfo[iIndex].uiDroppedCountPackets++;
370                         dev_kfree_skb(PacketToDrop);
371
372                 }
373
374                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, PRUNE_QUEUE, DBG_LVL_ALL, "Dropped Bytes:%x Dropped Packets:%x",
375                         Adapter->PackInfo[iIndex].uiDroppedCountBytes,
376                         Adapter->PackInfo[iIndex].uiDroppedCountPackets);
377
378                 atomic_dec(&Adapter->TotalPacketCount);
379         }
380
381         spin_unlock_bh(&Adapter->PackInfo[iIndex].SFQueueLock);
382
383         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, PRUNE_QUEUE, DBG_LVL_ALL, "TotalPacketCount:%x",
384                 atomic_read(&Adapter->TotalPacketCount));
385         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, PRUNE_QUEUE, DBG_LVL_ALL, "<=====");
386 }
387
388 VOID flush_all_queues(struct bcm_mini_adapter *Adapter)
389 {
390         INT             iQIndex;
391         UINT    uiTotalPacketLength;
392         struct sk_buff*                 PacketToDrop = NULL;
393
394         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, DUMP_INFO, DBG_LVL_ALL, "=====>");
395
396 //      down(&Adapter->data_packet_queue_lock);
397         for (iQIndex = LowPriority; iQIndex < HiPriority; iQIndex++)
398         {
399                 struct net_device_stats *netstats = &Adapter->dev->stats;
400
401                 spin_lock_bh(&Adapter->PackInfo[iQIndex].SFQueueLock);
402                 while (Adapter->PackInfo[iQIndex].FirstTxQueue)
403                 {
404                         PacketToDrop = Adapter->PackInfo[iQIndex].FirstTxQueue;
405                         if (PacketToDrop)
406                         {
407                                 uiTotalPacketLength = PacketToDrop->len;
408                                 netstats->tx_dropped++;
409                         }
410                         else
411                                 uiTotalPacketLength = 0;
412
413                         DEQUEUEPACKET(Adapter->PackInfo[iQIndex].FirstTxQueue,
414                                                 Adapter->PackInfo[iQIndex].LastTxQueue);
415
416                         /* Free the skb */
417                         dev_kfree_skb(PacketToDrop);
418
419                         /// update current bytes and packets count
420                         Adapter->PackInfo[iQIndex].uiCurrentBytesOnHost -= uiTotalPacketLength;
421                         Adapter->PackInfo[iQIndex].uiCurrentPacketsOnHost--;
422
423                         /// update dropped bytes and packets counts
424                         Adapter->PackInfo[iQIndex].uiDroppedCountBytes += uiTotalPacketLength;
425                         Adapter->PackInfo[iQIndex].uiDroppedCountPackets++;
426
427                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, DUMP_INFO, DBG_LVL_ALL, "Dropped Bytes:%x Dropped Packets:%x",
428                                         Adapter->PackInfo[iQIndex].uiDroppedCountBytes,
429                                         Adapter->PackInfo[iQIndex].uiDroppedCountPackets);
430                         atomic_dec(&Adapter->TotalPacketCount);
431                 }
432                 spin_unlock_bh(&Adapter->PackInfo[iQIndex].SFQueueLock);
433         }
434 //      up(&Adapter->data_packet_queue_lock);
435         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_OTHERS, DUMP_INFO, DBG_LVL_ALL, "<=====");
436 }
437
438 USHORT ClassifyPacket(struct bcm_mini_adapter *Adapter, struct sk_buff* skb)
439 {
440         INT                     uiLoopIndex = 0;
441         struct bcm_classifier_rule *pstClassifierRule = NULL;
442         struct bcm_eth_packet_info stEthCsPktInfo;
443         PVOID pvEThPayload = NULL;
444         struct iphdr *pIpHeader = NULL;
445         INT       uiSfIndex = 0;
446         USHORT  usIndex = Adapter->usBestEffortQueueIndex;
447         bool    bFragmentedPkt = false, bClassificationSucceed = false;
448         USHORT  usCurrFragment = 0;
449
450         struct bcm_tcp_header *pTcpHeader;
451         UCHAR IpHeaderLength;
452         UCHAR TcpHeaderLength;
453
454         pvEThPayload = skb->data;
455         *((UINT32*) (skb->cb) +SKB_CB_TCPACK_OFFSET) = 0;
456         EThCSGetPktInfo(Adapter, pvEThPayload, &stEthCsPktInfo);
457
458         switch (stEthCsPktInfo.eNwpktEthFrameType)
459         {
460                 case eEth802LLCFrame:
461                 {
462                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "ClassifyPacket : 802LLCFrame\n");
463                         pIpHeader = pvEThPayload + sizeof(struct bcm_eth_llc_frame);
464                         break;
465                 }
466
467                 case eEth802LLCSNAPFrame:
468                 {
469                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "ClassifyPacket : 802LLC SNAP Frame\n");
470                         pIpHeader = pvEThPayload + sizeof(struct bcm_eth_llc_snap_frame);
471                         break;
472                 }
473                 case eEth802QVLANFrame:
474                 {
475                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "ClassifyPacket : 802.1Q VLANFrame\n");
476                         pIpHeader = pvEThPayload + sizeof(struct bcm_eth_q_frame);
477                         break;
478                 }
479                 case eEthOtherFrame:
480                 {
481                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "ClassifyPacket : ETH Other Frame\n");
482                         pIpHeader = pvEThPayload + sizeof(struct bcm_ethernet2_frame);
483                         break;
484                 }
485                 default:
486                 {
487                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "ClassifyPacket : Unrecognized ETH Frame\n");
488                         pIpHeader = pvEThPayload + sizeof(struct bcm_ethernet2_frame);
489                         break;
490                 }
491         }
492
493         if (stEthCsPktInfo.eNwpktIPFrameType == eIPv4Packet)
494         {
495                 usCurrFragment = (ntohs(pIpHeader->frag_off) & IP_OFFSET);
496                 if ((ntohs(pIpHeader->frag_off) & IP_MF) || usCurrFragment)
497                         bFragmentedPkt = TRUE;
498
499                 if (bFragmentedPkt)
500                 {
501                                 //Fragmented  Packet. Get Frag Classifier Entry.
502                         pstClassifierRule = GetFragIPClsEntry(Adapter, pIpHeader->id, pIpHeader->saddr);
503                         if (pstClassifierRule)
504                         {
505                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "It is next Fragmented pkt");
506                                         bClassificationSucceed = TRUE;
507                         }
508                         if (!(ntohs(pIpHeader->frag_off) & IP_MF))
509                         {
510                                 //Fragmented Last packet . Remove Frag Classifier Entry
511                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "This is the last fragmented Pkt");
512                                 DelFragIPClsEntry(Adapter, pIpHeader->id, pIpHeader->saddr);
513                         }
514                 }
515         }
516
517         for (uiLoopIndex = MAX_CLASSIFIERS - 1; uiLoopIndex >= 0; uiLoopIndex--)
518         {
519                 if (bClassificationSucceed)
520                         break;
521                 //Iterate through all classifiers which are already in order of priority
522                 //to classify the packet until match found
523                 do
524                 {
525                         if (false == Adapter->astClassifierTable[uiLoopIndex].bUsed)
526                         {
527                                 bClassificationSucceed = false;
528                                 break;
529                         }
530                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "Adapter->PackInfo[%d].bvalid=True\n", uiLoopIndex);
531
532                         if (0 == Adapter->astClassifierTable[uiLoopIndex].ucDirection)
533                         {
534                                 bClassificationSucceed = false;//cannot be processed for classification.
535                                 break;                                          // it is a down link connection
536                         }
537
538                         pstClassifierRule = &Adapter->astClassifierTable[uiLoopIndex];
539
540                         uiSfIndex = SearchSfid(Adapter, pstClassifierRule->ulSFID);
541                         if (uiSfIndex >= NO_OF_QUEUES) {
542                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "Queue Not Valid. SearchSfid for this classifier Failed\n");
543                                 break;
544                         }
545
546                         if (Adapter->PackInfo[uiSfIndex].bEthCSSupport)
547                         {
548
549                                 if (eEthUnsupportedFrame == stEthCsPktInfo.eNwpktEthFrameType)
550                                 {
551                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, " ClassifyPacket : Packet Not a Valid Supported Ethernet Frame\n");
552                                         bClassificationSucceed = false;
553                                         break;
554                                 }
555
556
557
558                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "Performing ETH CS Classification on Classifier Rule ID : %x Service Flow ID : %lx\n", pstClassifierRule->uiClassifierRuleIndex, Adapter->PackInfo[uiSfIndex].ulSFID);
559                                 bClassificationSucceed = EThCSClassifyPkt(Adapter, skb, &stEthCsPktInfo, pstClassifierRule, Adapter->PackInfo[uiSfIndex].bEthCSSupport);
560
561                                 if (!bClassificationSucceed)
562                                 {
563                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ClassifyPacket : Ethernet CS Classification Failed\n");
564                                         break;
565                                 }
566                         }
567
568                         else // No ETH Supported on this SF
569                         {
570                                 if (eEthOtherFrame != stEthCsPktInfo.eNwpktEthFrameType)
571                                 {
572                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, " ClassifyPacket : Packet Not a 802.3 Ethernet Frame... hence not allowed over non-ETH CS SF\n");
573                                         bClassificationSucceed = false;
574                                         break;
575                                 }
576                         }
577
578                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "Proceeding to IP CS Clasification");
579
580                         if (Adapter->PackInfo[uiSfIndex].bIPCSSupport)
581                         {
582
583                                 if (stEthCsPktInfo.eNwpktIPFrameType == eNonIPPacket)
584                                 {
585                                         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, " ClassifyPacket : Packet is Not an IP Packet\n");
586                                         bClassificationSucceed = false;
587                                         break;
588                                 }
589                                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "Dump IP Header :\n");
590                                 DumpFullPacket((PUCHAR)pIpHeader, 20);
591
592                                 if (stEthCsPktInfo.eNwpktIPFrameType == eIPv4Packet)
593                                         bClassificationSucceed = IpVersion4(Adapter, pIpHeader, pstClassifierRule);
594                                 else if (stEthCsPktInfo.eNwpktIPFrameType == eIPv6Packet)
595                                         bClassificationSucceed = IpVersion6(Adapter, pIpHeader, pstClassifierRule);
596                         }
597
598                 } while (0);
599         }
600
601         if (bClassificationSucceed == TRUE)
602         {
603                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "CF id : %d, SF ID is =%lu", pstClassifierRule->uiClassifierRuleIndex, pstClassifierRule->ulSFID);
604
605                 //Store The matched Classifier in SKB
606                 *((UINT32*)(skb->cb)+SKB_CB_CLASSIFICATION_OFFSET) = pstClassifierRule->uiClassifierRuleIndex;
607                 if ((TCP == pIpHeader->protocol) && !bFragmentedPkt && (ETH_AND_IP_HEADER_LEN + TCP_HEADER_LEN <= skb->len))
608                 {
609                          IpHeaderLength   = pIpHeader->ihl;
610                          pTcpHeader = (struct bcm_tcp_header *)(((PUCHAR)pIpHeader)+(IpHeaderLength*4));
611                          TcpHeaderLength  = GET_TCP_HEADER_LEN(pTcpHeader->HeaderLength);
612
613                         if ((pTcpHeader->ucFlags & TCP_ACK) &&
614                            (ntohs(pIpHeader->tot_len) == (IpHeaderLength*4)+(TcpHeaderLength*4)))
615                         {
616                                 *((UINT32*) (skb->cb) + SKB_CB_TCPACK_OFFSET) = TCP_ACK;
617                         }
618                 }
619
620                 usIndex = SearchSfid(Adapter, pstClassifierRule->ulSFID);
621                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "index is  =%d", usIndex);
622
623                 //If this is the first fragment of a Fragmented pkt, add this CF. Only This CF should be used for all other fragment of this Pkt.
624                 if (bFragmentedPkt && (usCurrFragment == 0))
625                 {
626                         //First Fragment of Fragmented Packet. Create Frag CLS Entry
627                         struct bcm_fragmented_packet_info stFragPktInfo;
628                         stFragPktInfo.bUsed = TRUE;
629                         stFragPktInfo.ulSrcIpAddress = pIpHeader->saddr;
630                         stFragPktInfo.usIpIdentification = pIpHeader->id;
631                         stFragPktInfo.pstMatchedClassifierEntry = pstClassifierRule;
632                         stFragPktInfo.bOutOfOrderFragment = false;
633                         AddFragIPClsEntry(Adapter, &stFragPktInfo);
634                 }
635
636
637         }
638
639         if (bClassificationSucceed)
640                 return usIndex;
641         else
642                 return INVALID_QUEUE_INDEX;
643 }
644
645 static bool EthCSMatchSrcMACAddress(struct bcm_classifier_rule *pstClassifierRule, PUCHAR Mac)
646 {
647         UINT i = 0;
648         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
649         if (pstClassifierRule->ucEthCSSrcMACLen == 0)
650                 return TRUE;
651         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s\n", __FUNCTION__);
652         for (i = 0; i < MAC_ADDRESS_SIZE; i++)
653         {
654                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "SRC MAC[%x] = %x ClassifierRuleSrcMAC = %x Mask : %x\n", i, Mac[i], pstClassifierRule->au8EThCSSrcMAC[i], pstClassifierRule->au8EThCSSrcMACMask[i]);
655                 if ((pstClassifierRule->au8EThCSSrcMAC[i] & pstClassifierRule->au8EThCSSrcMACMask[i]) !=
656                         (Mac[i] & pstClassifierRule->au8EThCSSrcMACMask[i]))
657                         return false;
658         }
659         return TRUE;
660 }
661
662 static bool EthCSMatchDestMACAddress(struct bcm_classifier_rule *pstClassifierRule, PUCHAR Mac)
663 {
664         UINT i = 0;
665         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
666         if (pstClassifierRule->ucEthCSDestMACLen == 0)
667                 return TRUE;
668         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "%s\n", __FUNCTION__);
669         for (i = 0; i < MAC_ADDRESS_SIZE; i++)
670         {
671                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "SRC MAC[%x] = %x ClassifierRuleSrcMAC = %x Mask : %x\n", i, Mac[i], pstClassifierRule->au8EThCSDestMAC[i], pstClassifierRule->au8EThCSDestMACMask[i]);
672                 if ((pstClassifierRule->au8EThCSDestMAC[i] & pstClassifierRule->au8EThCSDestMACMask[i]) !=
673                         (Mac[i] & pstClassifierRule->au8EThCSDestMACMask[i]))
674                         return false;
675         }
676         return TRUE;
677 }
678
679 static bool EthCSMatchEThTypeSAP(struct bcm_classifier_rule *pstClassifierRule, struct sk_buff* skb, struct bcm_eth_packet_info *pstEthCsPktInfo)
680 {
681         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
682         if ((pstClassifierRule->ucEtherTypeLen == 0) ||
683                 (pstClassifierRule->au8EthCSEtherType[0] == 0))
684                 return TRUE;
685
686         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s SrcEtherType:%x CLS EtherType[0]:%x\n", __FUNCTION__, pstEthCsPktInfo->usEtherType, pstClassifierRule->au8EthCSEtherType[0]);
687         if (pstClassifierRule->au8EthCSEtherType[0] == 1)
688         {
689                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s  CLS EtherType[1]:%x EtherType[2]:%x\n", __FUNCTION__, pstClassifierRule->au8EthCSEtherType[1], pstClassifierRule->au8EthCSEtherType[2]);
690
691                 if (memcmp(&pstEthCsPktInfo->usEtherType, &pstClassifierRule->au8EthCSEtherType[1], 2) == 0)
692                         return TRUE;
693                 else
694                         return false;
695         }
696
697         if (pstClassifierRule->au8EthCSEtherType[0] == 2)
698         {
699                 if (eEth802LLCFrame != pstEthCsPktInfo->eNwpktEthFrameType)
700                         return false;
701
702                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s  EthCS DSAP:%x EtherType[2]:%x\n", __FUNCTION__, pstEthCsPktInfo->ucDSAP, pstClassifierRule->au8EthCSEtherType[2]);
703                 if (pstEthCsPktInfo->ucDSAP == pstClassifierRule->au8EthCSEtherType[2])
704                         return TRUE;
705                 else
706                         return false;
707
708         }
709
710         return false;
711
712 }
713
714 static bool EthCSMatchVLANRules(struct bcm_classifier_rule *pstClassifierRule, struct sk_buff* skb, struct bcm_eth_packet_info *pstEthCsPktInfo)
715 {
716         bool bClassificationSucceed = false;
717         USHORT usVLANID;
718         B_UINT8 uPriority = 0;
719         struct bcm_mini_adapter *Adapter = GET_BCM_ADAPTER(gblpnetdev);
720
721         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s  CLS UserPrio:%x CLS VLANID:%x\n", __FUNCTION__, ntohs(*((USHORT *)pstClassifierRule->usUserPriority)), pstClassifierRule->usVLANID);
722
723         /* In case FW didn't receive the TLV, the priority field should be ignored */
724         if (pstClassifierRule->usValidityBitMap & (1<<PKT_CLASSIFICATION_USER_PRIORITY_VALID))
725         {
726                 if (pstEthCsPktInfo->eNwpktEthFrameType != eEth802QVLANFrame)
727                                 return false;
728
729                 uPriority = (ntohs(*(USHORT *)(skb->data + sizeof(struct bcm_eth_header))) & 0xF000) >> 13;
730
731                 if ((uPriority >= pstClassifierRule->usUserPriority[0]) && (uPriority <= pstClassifierRule->usUserPriority[1]))
732                                 bClassificationSucceed = TRUE;
733
734                 if (!bClassificationSucceed)
735                         return false;
736         }
737
738         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS 802.1 D  User Priority Rule Matched\n");
739
740         bClassificationSucceed = false;
741
742         if (pstClassifierRule->usValidityBitMap & (1<<PKT_CLASSIFICATION_VLANID_VALID))
743         {
744                 if (pstEthCsPktInfo->eNwpktEthFrameType != eEth802QVLANFrame)
745                                 return false;
746
747                 usVLANID = ntohs(*(USHORT *)(skb->data + sizeof(struct bcm_eth_header))) & 0xFFF;
748
749                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "%s  Pkt VLANID %x Priority: %d\n", __FUNCTION__, usVLANID, uPriority);
750
751                 if (usVLANID == ((pstClassifierRule->usVLANID & 0xFFF0) >> 4))
752                         bClassificationSucceed = TRUE;
753
754                 if (!bClassificationSucceed)
755                         return false;
756         }
757
758         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS 802.1 Q VLAN ID Rule Matched\n");
759
760         return TRUE;
761 }
762
763
764 static bool EThCSClassifyPkt(struct bcm_mini_adapter *Adapter, struct sk_buff* skb,
765                                 struct bcm_eth_packet_info *pstEthCsPktInfo,
766                                 struct bcm_classifier_rule *pstClassifierRule,
767                                 B_UINT8 EthCSCupport)
768 {
769         bool bClassificationSucceed = false;
770         bClassificationSucceed = EthCSMatchSrcMACAddress(pstClassifierRule, ((struct bcm_eth_header *)(skb->data))->au8SourceAddress);
771         if (!bClassificationSucceed)
772                 return false;
773         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS SrcMAC Matched\n");
774
775         bClassificationSucceed = EthCSMatchDestMACAddress(pstClassifierRule, ((struct bcm_eth_header *)(skb->data))->au8DestinationAddress);
776         if (!bClassificationSucceed)
777                 return false;
778         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS DestMAC Matched\n");
779
780         //classify on ETHType/802.2SAP TLV
781         bClassificationSucceed = EthCSMatchEThTypeSAP(pstClassifierRule, skb, pstEthCsPktInfo);
782         if (!bClassificationSucceed)
783                 return false;
784
785         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS EthType/802.2SAP Matched\n");
786
787         //classify on 802.1VLAN Header Parameters
788
789         bClassificationSucceed = EthCSMatchVLANRules(pstClassifierRule, skb, pstEthCsPktInfo);
790         if (!bClassificationSucceed)
791                 return false;
792         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "ETH CS 802.1 VLAN Rules Matched\n");
793
794         return bClassificationSucceed;
795 }
796
797 static void EThCSGetPktInfo(struct bcm_mini_adapter *Adapter, PVOID pvEthPayload,
798                             struct bcm_eth_packet_info *pstEthCsPktInfo)
799 {
800         USHORT u16Etype = ntohs(((struct bcm_eth_header *)pvEthPayload)->u16Etype);
801
802         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "EthCSGetPktInfo : Eth Hdr Type : %X\n", u16Etype);
803         if (u16Etype > 0x5dc)
804         {
805                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "EthCSGetPktInfo : ETH2 Frame\n");
806                 //ETH2 Frame
807                 if (u16Etype == ETHERNET_FRAMETYPE_802QVLAN)
808                 {
809                         //802.1Q VLAN Header
810                         pstEthCsPktInfo->eNwpktEthFrameType = eEth802QVLANFrame;
811                         u16Etype = ((struct bcm_eth_q_frame *)pvEthPayload)->EthType;
812                         //((ETH_CS_802_Q_FRAME*)pvEthPayload)->UserPriority
813                 }
814                 else
815                 {
816                         pstEthCsPktInfo->eNwpktEthFrameType = eEthOtherFrame;
817                         u16Etype = ntohs(u16Etype);
818                 }
819
820         }
821         else
822         {
823                 //802.2 LLC
824                 BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL, "802.2 LLC Frame\n");
825                 pstEthCsPktInfo->eNwpktEthFrameType = eEth802LLCFrame;
826                 pstEthCsPktInfo->ucDSAP = ((struct bcm_eth_llc_frame *)pvEthPayload)->DSAP;
827                 if (pstEthCsPktInfo->ucDSAP == 0xAA && ((struct bcm_eth_llc_frame *)pvEthPayload)->SSAP == 0xAA)
828                 {
829                         //SNAP Frame
830                         pstEthCsPktInfo->eNwpktEthFrameType = eEth802LLCSNAPFrame;
831                         u16Etype = ((struct bcm_eth_llc_snap_frame *)pvEthPayload)->usEtherType;
832                 }
833         }
834         if (u16Etype == ETHERNET_FRAMETYPE_IPV4)
835                 pstEthCsPktInfo->eNwpktIPFrameType = eIPv4Packet;
836         else if (u16Etype == ETHERNET_FRAMETYPE_IPV6)
837                 pstEthCsPktInfo->eNwpktIPFrameType = eIPv6Packet;
838         else
839                 pstEthCsPktInfo->eNwpktIPFrameType = eNonIPPacket;
840
841         pstEthCsPktInfo->usEtherType = ((struct bcm_eth_header *)pvEthPayload)->u16Etype;
842         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "EthCsPktInfo->eNwpktIPFrameType : %x\n", pstEthCsPktInfo->eNwpktIPFrameType);
843         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "EthCsPktInfo->eNwpktEthFrameType : %x\n", pstEthCsPktInfo->eNwpktEthFrameType);
844         BCM_DEBUG_PRINT(Adapter, DBG_TYPE_TX, IPV4_DBG, DBG_LVL_ALL,  "EthCsPktInfo->usEtherType : %x\n", pstEthCsPktInfo->usEtherType);
845 }
846
847
848