1e8c0ea8214154536a2d8f985ee075d6cb23bf95
[keystone-rtos/netapi.git] / ti / runtime / netapi / src / netcp_cfg.c
1 /**********************************************************
2  * file: netcp_cfg.c
3  * purpose: netcp configurations routines
4  **************************************************************
5  * FILE: netcp_cfg.c
6  * 
7  * DESCRIPTION:  netcp configuration main source file for user space transport
8  *               library
9  * 
10  * REVISION HISTORY:
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2013
13  * 
14  *  Redistribution and use in source and binary forms, with or without 
15  *  modification, are permitted provided that the following conditions 
16  *  are met:
17  *
18  *    Redistributions of source code must retain the above copyright 
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  *    Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the 
23  *    documentation and/or other materials provided with the   
24  *    distribution.
25  *
26  *    Neither the name of Texas Instruments Incorporated nor the names of
27  *    its contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
31  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
32  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
34  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
35  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
36  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
39  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
40  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************/
44 #include "netapi.h"
46 extern NETAPI_SHM_T* pnetapiShm;
48 /******************************************************************
49  ********************Netapi internal*************************************
50 *******************************************************************/
53 /********************************************************************
54  * FUNCTION PURPOSE:  Netapi internal function to get a free transaction id
55  ********************************************************************
56  * DESCRIPTION:  Netapi internal function to get a free transaction id.
57  ********************************************************************/
58 NetapiNwalTransInfo_t * netapip_getFreeTransInfo(NETAPI_HANDLE_T *p_handle,
59                                                  NETAPI_PROC_GLOBAL_T *p_proc_global,
60                                                  nwal_TransID_t *pTransId)
61 {
62     uint16_t    count=0;
64     count=0;
65     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
66     while(count < TUNE_NETAPI_MAX_NUM_TRANS)
67     {
68         if((p_proc_global->nwal_context.transInfos[count].inUse) != nwal_TRUE)
69         {
70             p_proc_global->nwal_context.transInfos[count].inUse = nwal_TRUE;
71             *pTransId = count;
72             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
73             return(&p_proc_global->nwal_context.transInfos[count]);
74         }
75         count++;
76     }
77     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
78     /* trouble.  need to wait for one to free up*/
79     /* to do: handle this by forcing a poll of cntrl queue*/
80     netapi_Log(">netcp_cfg: trying to get free transaction slot but all full!!\n");
81     return NULL;
83 }
85 /********************************************************************
86  * FUNCTION PURPOSE:  Netapi internal function to free a transaction id
87  ********************************************************************
88  * DESCRIPTION:  Netapi internal function to free a transaction id
89  ********************************************************************/
90 void netapip_freeTransInfo(NetapiNwalTransInfo_t *pTransInfo)
91 {
92     pTransInfo->inUse = nwal_FALSE;
93     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_IDLE;
94     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_NONE;
95 }
96 /********************************************************************
97  * FUNCTION PURPOSE:  Netapi internal function to build a route
98  ********************************************************************
99  * DESCRIPTION:  Netapi internal function to build a route
100  ********************************************************************/
101 void netapip_netcpCfgBuildRoute(NETCP_CFG_ROUTE_T * p_route,
102                                 int16_t *p_flow,
103                                 Qmss_QueueHnd *p_q,
104                                 nwalRouteType_t *p_type)
106     if (!p_route) return;
107     if (p_route->p_flow)  *p_flow= p_route->p_flow->flowid;
108     else *p_flow = CPPI_PARAM_NOT_SPECIFIED;
110     if (p_route->p_dest_q) *p_q = netapi_pktioGetQ(p_route->p_dest_q);
111     else *p_q=QMSS_PARAM_NOT_SPECIFIED;
113     *p_type = p_route->routeType;
117 /******************************************************/
118 /*----------------database management stuff           */
119 /******************************************************/
121 /*=====================POLICIES=============================*/
123 /********************************************************************
124  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for an SA
125  ********************************************************************
126  * DESCRIPTION:  Netapi internal function to find a free slot for an SA
127  ********************************************************************/
128 int netapip_netcpCfgFindPolicySlot(NETAPI_HANDLE_T *p_handle,
129                                    NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
130                                    int tunnel)
132     int i;
133     if ((tunnel <0 ) || (tunnel >=TUNE_NETAPI_MAX_SA))
134         return -1;
136     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
137     //find a free entry
138     for(i=0;i<TUNE_NETAPI_MAX_POLICY;i++)
139     {
140         if (!p->policy[i].in_use)
141         {
142             p->policy[i].in_use = 2; //pending
143             p->policy[i].tunnel= tunnel; //save tunnel this is linked to
144             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
145             return i;
146         }
147     }
148     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
149     return -1;
152 /********************************************************************
153  * FUNCTION PURPOSE:  Netapi internal function to delete a policy from policy list
154  ********************************************************************
155  * DESCRIPTION:  Netapi internal function to delete a policy from policy list
156  ********************************************************************/
157 void netapip_netcpCfgDeletePolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
158                                   int policy_slot)
160     if ((policy_slot <0 ) || (policy_slot >= TUNE_NETAPI_MAX_POLICY))
161     {
162         return ;
163     }
164     p->policy[policy_slot].in_use=0;
165     return;
168 /********************************************************************
169  * FUNCTION PURPOSE:  Netapi internal function to insert a policy to the policy list
170  ********************************************************************
171  * DESCRIPTION:  Netapi internal function to insert a policy to the policy list
172  ********************************************************************/
173 void netapip_netcpCfgInsertPolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
174                                   int policy_slot,
175                                   void * handle,
176                                   void * user_data)
178     p->policy[policy_slot].in_use=1;
179     p->policy[policy_slot].nwal_handle = handle;
180     p->policy[policy_slot].user_data = user_data;
181     return;
184 /************************************************************************
185  * FUNCTION PURPOSE:  Netapi internal function which returns NWAL handle for a policy
186  ************************************************************************
187  * DESCRIPTION:  Netapi internal function which returns NWAL handle for a policy
188  ************************************************************************/
189 void *netapip_netcpCfgGetPolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
190                                 int policy_slot)
192     if ((policy_slot <0 ) || (policy_slot >= TUNE_NETAPI_MAX_POLICY))
193         return NULL;
194     if (!p->policy[policy_slot].in_use)
195         return NULL;
196     return p->policy[policy_slot].nwal_handle;
199 /********************************************************************
200  * FUNCTION PURPOSE:  Netapi internal function to find a free slot in SA list for a SA
201  ********************************************************************
202  * DESCRIPTION:  Netapi internal function to find a free slot in SA list for an SA
203  ********************************************************************/
204 int netapip_netcpCfgFindSaSlot(NETAPI_HANDLE_T *p_handle,
205                                NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
206                                int iface)
207 {                                    
208    int i;
209    if (iface != NETCP_CFG_NO_INTERFACE)
210    {
211    if ((iface <0 ) || (iface >=TUNE_NETAPI_MAX_NUM_MAC)) return -1;
212    }
213    p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
214    //find a free entry
215    for(i=0;i<TUNE_NETAPI_MAX_SA;i++)
216    {                       
217        if (!p->tunnel[i].in_use)
218        {
219            p->tunnel[i].in_use = 2; //pending
220            p->tunnel[i].iface= iface; //save iface
221            p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
222            return i;
223        }
224    }
225    p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
226    return -1;
229 /********************************************************************
230  * FUNCTION PURPOSE:  Netapi internal function to delete an SA from SA list
231  ********************************************************************
232  * DESCRIPTION: Netapi internal function to delete an SA from SA list 
233  ********************************************************************/
234 void netapip_netcpCfgDeleteSa(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
235                               int sa_slot)
237     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
238     {
239         return ;
240     }
241     p->tunnel[sa_slot].in_use=0;
242     return;
246 /****************************************************************************
247  * FUNCTION PURPOSE:  Internal function to retrieve SB command info template
248  **************************************************************************
249  * DESCRIPTION:  Internal function to retrieve SB command info template
250  ***************************************************************************/
251 nwalTxDmPSCmdInfo_t* netapip_netcpCfgGetSaSBInfo(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
252                                                  NETCP_CFG_SA_T    sa_app_id)
254      int sa_slot = netapi_cfgGetMatchId(sa_app_id);
255     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
256     {
257         return NULL;
258     }
259     return (&p->tunnel[sa_slot].dmPSCmdInfo);
262 /****************************************************************************
263  * FUNCTION PURPOSE:  Internal function to retrieve Inflow mode 
264  *                    software information required to transmit packet
265  **************************************************************************
266  * DESCRIPTION:  Internal function to retrieve Inflow mode 
267  *                    software information required to transmit packet
268  ***************************************************************************/
269 int netapip_netcpCfgGetSaInflowInfo(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
270                                     NETCP_CFG_SA_T    sa_app_id,
271                                     uint32_t *swInfo0,
272                                     uint32_t *swInfo1)
274     int sa_slot = netapi_cfgGetMatchId(sa_app_id);
275     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
276     {
277         return 0;
278     }
279     *swInfo0 = p->tunnel[sa_slot].swInfo0;
280     *swInfo1 = p->tunnel[sa_slot].swInfo1;
281     return 1;
284 /********************************************************************
285  * FUNCTION PURPOSE:  Netapi internal function to insert an SA to the SA list
286  ********************************************************************
287  * DESCRIPTION: Netapi internal function to insert an SA to the SA list 
288  ********************************************************************/
289 void netapip_netcpCfgInsertSa(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
290                               int sa_slot,
291                               int dir,
292                               int mode,
293                               void * temp1,
294                               void * temp2,
295                               void * handle_inflow,
296                               void * handle_sideband,
297                               nwalTxDmPSCmdInfo_t *dmPSCmdInfo,
298                               uint32_t swInfo0,
299                               uint32_t swInfo1,
300                               void* user_data)
302     p->tunnel[sa_slot].in_use=1;
303     p->tunnel[sa_slot].inbound = dir;
304     p->tunnel[sa_slot].sa_mode = mode;
305     p->tunnel[sa_slot].sa_handle_inflow = handle_inflow;
306     p->tunnel[sa_slot].sa_handle_sideband = handle_sideband;
307     p->tunnel[sa_slot].swInfo0 = swInfo0;
308     p->tunnel[sa_slot].swInfo1 = swInfo1;
309     p->tunnel[sa_slot].user_data = user_data;
310     //netapi_Log("netapip_netcpCfgInsertSa: swInfo0 0x%x, swInfo1: 0x%x, user data: %d\n",
311 //        p->tunnel[sa_slot].swInfo0, p->tunnel[sa_slot].swInfo1, (uint32_t)p->tunnel[sa_slot].user_data);
313     if (dmPSCmdInfo)
314     {
315         memcpy(&p->tunnel[sa_slot].dmPSCmdInfo, dmPSCmdInfo, sizeof(nwalTxDmPSCmdInfo_t));
316     }
317     return;
320 /********************************************************************
321  * FUNCTION PURPOSE:  Netapi internal function to return NWAL handles for an SA
322  ********************************************************************
323  * DESCRIPTION: Netapi internal function to return NWAL handles for an SA
324  ********************************************************************/
325  void *netapip_netcpCfgGetSaHandles(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
326                                     int sa_slot,
327                                     void ** p_sideband)
329     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
330         return NULL;
331     if (!p->tunnel[sa_slot].in_use)
332         return NULL;
333     *p_sideband = p->tunnel[sa_slot].sa_handle_sideband;
334     return p->tunnel[sa_slot].sa_handle_inflow;
338 /*==============================FLOWS=============================*/
340 /********************************************************************
341  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for a flow
342  ********************************************************************
343  * DESCRIPTION: Netapi internal function to find a free slot for a flow
344  ********************************************************************/
345 static int netapip_netcpCfgFindFlowSlot(NETAPI_HANDLE_T *p_handle,
346                                         NETAPI_NWAL_GLOBAL_CONTEXT_T *p)
348     int i;
349     //find a free entry
350     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
351     for(i=0;i<TUNE_NETAPI_MAX_FLOWS;i++)
352     {
353         if (!p->flows[i].in_use)
354         {
355             p->flows[i].in_use = 2; /* pending */
356             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
357             return i;
358         }
359     }
360    p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
361     return -1;
364 /********************************************************************
365  * FUNCTION PURPOSE:  Netapi internal function to find delete a flow from flow list
366  ********************************************************************
367  * DESCRIPTION: Netapi internal function to find delete a flow from flow list
368  ********************************************************************/
369 static void netapip_netcpCfgDeleteFlow(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
370                                        int slot)
372     if ((slot >=0 ) && (slot < TUNE_NETAPI_MAX_FLOWS))
373     {
374         p->flows[slot].in_use = 0;
375     }
378 /********************************************************************
379  * FUNCTION PURPOSE:  Netapi internal function to  insert a flow to the  flow list
380  ********************************************************************
381  * DESCRIPTION: Netapi internal function to  delete a flow from flow list
382  ********************************************************************/
383 static NETCP_CFG_FLOW_HANDLE_T netapip_netcpCfgInsertFlow(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
384                                                           int slot,
385                                                           int dma_engine,
386                                                           void * handle)
388     p->flows[slot].in_use=1;
389     p->flows[slot].handle = handle;
390     p->flows[slot].flow.flowid  = Cppi_getFlowId(handle);
391     p->flows[slot].flow.dma_engine  = dma_engine;
392         return (NETCP_CFG_FLOW_HANDLE_T) &p->flows[slot].flow;
395 /********************************************************************
396  * FUNCTION PURPOSE:  Netapi internal function to find entry matching the flowid
397  ********************************************************************
398  * DESCRIPTION: Netapi internal function to find entry matching the flowid. Returns
399  * the slot number and the cPPI handle.
400  ********************************************************************/
401 static int netapip_netcpCfgFindFlow(NETAPI_HANDLE_T *p_handle,
402                                     NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
403                                     int flowid,  
404                                     int dma_engine,
405                                     void ** handle) 
407     int i;
408     *handle=NULL;
409     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
410     for(i=0;i<TUNE_NETAPI_MAX_FLOWS;i++)
411     {
412        if ((p->flows[i].in_use)&&
413            (p->flows[i].flow.flowid == flowid)&&
414            (p->flows[i].flow.dma_engine == dma_engine))
415         {
416             *handle = p->flows[i].handle;
417              p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
418             return i;
419         }
420     }
421     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
422     return -1;
427 /*============================IP ADDRESSES==========================*/
429 /***************************************************************************
430  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for IP rule in IP slot list
431  ***************************************************************************
432  * DESCRIPTION: NNetapi internal function to find a free slot for IP rule in IP slot list
433  ***************************************************************************/
434 static int netapip_netcpCfgFindIpSlot(NETAPI_HANDLE_T *p_handle,
435                                       NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
436                                       int iface_no)
438     int i;
440     //find a free entry
441     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
442     for(i=0;i<TUNE_NETAPI_MAX_NUM_IP;i++)
443     {
444         if (!p->ips[i].in_use)
445         {
446             p->ips[i].in_use = 2; //pending
447             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
448             return i;
449         }
450     }
451     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
452     return -1;
455 /***************************************************************************
456  * FUNCTION PURPOSE:  Netapi internal function to insert an IP address into interface
457  ***************************************************************************
458  * DESCRIPTION: Netapi internal function to insert an IP address into interface
459  ***************************************************************************/
460 static void netapip_netcpCfgInsertIp(NETAPI_NWAL_GLOBAL_CONTEXT_T *p, 
461                                      nwal_IpType ipType,
462                                      nwalIpAddr_t *ip_addr, 
463                                      nwalIpOpt_t *ip_qualifiers, 
464                                      int iface_no,
465                                      int ip_slot,
466                                      void * handle,
467                                      void * user_data)
469     p->ips[ip_slot].in_use=1;
470     memcpy(&p->ips[ip_slot].ip_addr, ip_addr, sizeof(nwalIpAddr_t));
471     if(ip_qualifiers)
472     {
473         memcpy(&p->ips[ip_slot].ip_qualifiers, ip_qualifiers, sizeof(nwalIpOpt_t));
474     }
475     else
476     {
477         memset(&p->ips[ip_slot].ip_qualifiers, 0, sizeof(nwalIpOpt_t));
478     }
479     p->ips[ip_slot].ip_type = ipType;
480     p->ips[ip_slot].nwal_handle = handle;
481     p->ips[ip_slot].user_data = user_data;
482     p->ips[ip_slot].ref_count++;
483     
486 /***************************************************************************
487  * FUNCTION PURPOSE:  Netapi internal function to free IP slot associated with IP address
488  ***************************************************************************
489  * DESCRIPTION: Netapi internal function to free IP slot associated with IP address
490  ***************************************************************************/
491 static void netapip_netcpCfgDeleteIp(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
492                                      int iface_no,
493                                      int ip_slot)
495     if ((ip_slot >=0) &&(ip_slot <=TUNE_NETAPI_MAX_NUM_IP))
496     {
497         p->ips[ip_slot].in_use=0;
498         p->ips[ip_slot].ref_count = 0;
499     }
502 /***************************************************************************
503  * FUNCTION PURPOSE:  Netapi internal function to get IP handle associated with IP address
504  ***************************************************************************
505  * DESCRIPTION: Netapi internal function to get IP handle associated with IP address
506  ***************************************************************************/
507 static void *netapip_netcpCfgGetIpHandle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
508                                          int iface_no,
509                                          int ip_slot)
511     if ((ip_slot <0)||(ip_slot>=TUNE_NETAPI_MAX_NUM_IP))
512         return NULL;
513     if (!p->ips[ip_slot].in_use)
514         return NULL;
515     return (void *) p->ips[ip_slot].nwal_handle;
518 /*==========================MAC INTERFACES======================*/
522 /*******************************************************************************
523  * FUNCTION PURPOSE:  Netapi internal function to insert an interface to the interface list
524  *******************************************************************************
525  * DESCRIPTION:  Netapi internal function to insert an interface to the interface list
526  *******************************************************************************/
527 static void netapip_netcpCfgInsertMac(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
528                                       unsigned char * p_mac,
529                                       int iface_no, 
530                                       int state, NETCP_CFG_VLAN_T vlan, 
531                                       void * handle)
533     if ((iface_no >=0 ) && (iface_no < TUNE_NETAPI_MAX_NUM_MAC))
534     {
535         memset(&p->interfaces[iface_no],0,sizeof(NETCP_INTERFACE_T));
536         p->interfaces[iface_no].in_use = 1;
537         memcpy(&p->interfaces[iface_no].mac[0], p_mac,6);
538         p->interfaces[iface_no].state = state;
539         //todo p->interfaces[iface_no].vlan = vlan;
540         p->interfaces[iface_no].nwal_handle = handle; //save handle assoicated with this rule
541         netapi_Log("netapip_netcpCfgInsertMac, global context 0x%x\n", p);
542     }
543     else 
544         netapi_Log(">netapip_netcpCfgInsertMac insert interface # out of range %d\n",iface_no);
548 /*******************************************************************************
549  * FUNCTION PURPOSE:  Netapi internal function to get handle associated with interface
550  *******************************************************************************
551  * DESCRIPTION:  Netapi internal function to get handle associated with interface
552  *******************************************************************************/
553 void* netapip_netcpCfgGetMacHandle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
554                                    int iface_no)
556     if (iface_no == NETCP_CFG_NO_INTERFACE)
557     {
558         return NULL;
559     }
560     if ((iface_no <0 ) || (iface_no >= TUNE_NETAPI_MAX_NUM_MAC))
561     {
562         return NULL;
563     }
564     else if ( p->interfaces[iface_no].in_use)
565     {
566       return (void *) p->interfaces[iface_no].nwal_handle;
567     }
568     //no valid entry in slot
569     return NULL;
571 /*******************************************************************************
572  * FUNCTION PURPOSE:  Netapi internal function to delete interface from interface list
573  *******************************************************************************
574  * DESCRIPTION:  Netapi internal function to delete interface from interface list
575  *******************************************************************************/
576 static void netapip_netcpCfgDeleteMac(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
577                                       int iface_no)
579     if ((iface_no >=0 ) && (iface_no < TUNE_NETAPI_MAX_NUM_MAC))
580     {
581         p->interfaces[iface_no].in_use = 0;
582     }
585 /*========================CLASSIFIERS==========================*/
587 /*******************************************************************************
588  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for classifier rule
589  *******************************************************************************
590  * DESCRIPTION:  Netapi internal function to find a free slot for classifier rule
591  *******************************************************************************/
592 static int netapip_netcpCfgFindClassSlot(NETAPI_HANDLE_T *p_handle,
593                                          NETAPI_NWAL_GLOBAL_CONTEXT_T *p)
595     int i;
596     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
597     for(i=0;i<TUNE_NETAPI_MAX_CLASSIFIERS;i++)
598     {
599         if (!p->classi[i].in_use)
600         {
601             p->classi[i].in_use = 2; //pending
602             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
603             return i;
604         }
605     }
606     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
607     return -1;
609 /*******************************************************************************
610  * FUNCTION PURPOSE:  Netapi internal function to find a delete a classifer from classifer list
611  *******************************************************************************
612  * DESCRIPTION:  Netapi internal function to find a delete a classifer from classifer list
613  *******************************************************************************/
614 static void netapip_netcpCfgDeleteClass(
615          NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
616          int class_slot )
618     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
619     {
620         return;
621     }
622     p->classi[class_slot].in_use=0;
625 /*******************************************************************************
626  * FUNCTION PURPOSE:  Netapi internal function to find a insert a classifer to classifer list
627  *******************************************************************************
628  * DESCRIPTION:  Netapi internal function to find a insert a classifer to classifer list
629  *******************************************************************************/
630 static void netapip_netcpCfgInsertClass(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
631                                         int class_slot,
632                                         int class_type,
633                                         void * L2_handle,
634                                         void * L3_handle,
635                                         void * L4_handle,
636                                         void * user_data)
638     if ((class_slot >=0 ) && (class_slot < TUNE_NETAPI_MAX_CLASSIFIERS))
639     {
640         p->classi[class_slot].in_use=1;
641         p->classi[class_slot].nwal_L2_handle = L2_handle;
642         p->classi[class_slot].nwal_L3_handle = L3_handle;
643         p->classi[class_slot].nwal_L4_handle = L4_handle;
644         p->classi[class_slot].class_type = class_type;
645         p->classi[class_slot].user_data = user_data;
646     }
649 /*******************************************************************************
650  * FUNCTION PURPOSE:  Netapi internal function to get L4 nwal handle for classifier
651  *******************************************************************************
652  * DESCRIPTION:  Netapi internal function to get L4 nwal handle for classifier
653  *******************************************************************************/
654 static void *netapip_netcpCfgGetL4Handle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
655                                          int class_slot)
657     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
658     {
659         return NULL;
660     }
661     if (!p->classi[class_slot].in_use)
662     {
663         return NULL;
664     }
665     return p->classi[class_slot].nwal_L4_handle;
668 /*******************************************************************************
669  * FUNCTION PURPOSE:  Netapi internal function to get L3 nwal handle for classifier
670  *******************************************************************************
671  * DESCRIPTION:  Netapi internal function to get L3 nwal handle for classifier
672  *******************************************************************************/
673 static void *netapip_netcpCfgGetL3Handle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
674                                          int class_slot)
676     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
677     {
678         return NULL;
679     }
680     if (!p->classi[class_slot].in_use)
681     {
682         return NULL;
683     }
684     return p->classi[class_slot].nwal_L3_handle;
688 /***********************************************************************************/
689 /****************************************API****************************************/
690 /***********************************************************************************/
693 /********************************************************************
694  * FUNCTION PURPOSE:  API to request statistics from NETCP
695  ********************************************************************
696  * DESCRIPTION:  API to request statistics from NETCP
697  ********************************************************************/
698 void netapi_netcpCfgReqStats(NETAPI_T  h, 
699                              NETCP_CFG_STATS_CB cb,
700                              int doClear,
701                              int *err) 
703     nwal_RetValue ret;
704     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
705     NetapiNwalTransInfo_t *pTransInfo;
706     nwal_TransID_t     transId;
707     if ((!n) || (!cb))
708     {
709         *err = NETAPI_ERR_BAD_INPUT;
710         return ;
711     }
712     *err =0;
714     pTransInfo = netapip_getFreeTransInfo(n,
715                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
716                                           &transId);
717     if (!pTransInfo) { *err =  NETAPI_ERR_BUSY; return ;}
718     pTransInfo->transType = NETAPI_NWAL_HANDLE_STAT_REQUEST;
719     pTransInfo->netapi_handle = h;
720     n->nwal_local.stats_cb = cb;
721     ret = nwal_getPAStats( ((NETAPI_GLOBAL_T *) n->global)->nwal_context.nwalInstHandle,
722                               transId,
723                               NULL,
724                               doClear);
725     if(ret !=  nwal_OK)
726     {
727         //pTransInfo->inUse = nwal_FALSE;
728         *err = NETAPI_ERR_BUSY;  //no resources??
729         netapi_Log("> netapi_netcpCfg reqStats failed, err=%d\n",ret);
730     }
731     netapip_freeTransInfo(pTransInfo);
733 /********************************************************************
734  * FUNCTION PURPOSE:  API to create a MAC interface
735  ********************************************************************
736  * DESCRIPTION:  API to create a MAC interface
737  ********************************************************************/
738 NETCP_CFG_MACIF_T  netapi_netcpCfgCreateMacInterface(NETAPI_T  h,
739                                                      uint8_t *p_mac,
740                                                      int  iface_no, 
741                                                      int switch_port, 
742                                                      NETCP_CFG_ROUTE_HANDLE_T  route,
743                                                      NETCP_CFG_VLAN_T  vlan,  //future
744                                                      int state,  //0=down, 1=up  //ignored
745                                                      int * err)
747     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
748     nwalMacParam_t   macInfo=
749     {
750         0,      /* validParams */
751         0,      /* ifNum */
752         0,      /* vlanId      */
753         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      /* Local mac */
754         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      /* Remote mac */
755         NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
756         NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
757         CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
758         QMSS_PARAM_NOT_SPECIFIED,                     /* Use default queue configured to NWAL if packet is routed to host */
759         0
760     };
762     nwal_RetValue       retValue;
763     NetapiNwalTransInfo_t *pTransInfo;
764     nwal_TransID_t     trans_id;
766     if ((!n) || (!p_mac)) {*err = NETAPI_ERR_BAD_INPUT; return -1;}
767     *err =0;
769     pTransInfo = netapip_getFreeTransInfo(n,
770                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
771                                           &trans_id);
772     if (!pTransInfo)
773     {
774         *err =  NETAPI_ERR_BUSY;
775         return -1;
776     }
777     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_MAC;
778     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
779     pTransInfo->inUse = nwal_TRUE;
780     pTransInfo->netapi_handle = h;
782     /* set up MacInfo */
783     memcpy(&macInfo.macAddr,p_mac,6); 
784     /* todo: vlan */
785     if (switch_port)
786     {
787         macInfo.validParams |= NWAL_SET_MAC_VALID_PARAM_IFNUM ;
788         macInfo.ifNum = switch_port;  /* */
789     }
791     if (route != NULL)
792     {
793         if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
794             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
795         {
796            macInfo.validParams |= 
797                     NWAL_SET_MAC_VALID_PARAM_ROUTE_TYPE;
798         }
799         netapip_netcpCfgBuildRoute(route,
800                                    &macInfo.appRxPktFlowId,
801                                    &macInfo.appRxPktQueue,
802                                    &macInfo.routeType);
803     }
804     retValue = nwal_setMacIface( ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
805                                   trans_id,
806                                   (nwal_AppId) (NETAPI_NETCP_MATCH_GENERIC_MAC | iface_no),
807                                   &macInfo,
808                                   &pTransInfo->handle);
809     if(retValue !=  nwal_OK)
810     {
811         *err = NETAPI_ERR_NWAL_ERR0;
812         netapi_Log ("netapi_netcpCfg - ERROR: nwal_setMacIface returned Error Code %d\n",
813                     retValue);
814         netapip_freeTransInfo(pTransInfo);
815         return -1;
816     }
818     //wait here until its done since scheduler isn't running yet most likely..
819     // todo:  make this handled by scheduler poll later ??
820     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
821     {
822         n->nwal_local.numPendingCfg++;
823         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
824                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
825         {
826             // if response is there, then this poll squirts out in the CTl poll callback, 
827             // which handles the rest (including decrmenting #pending!!
828             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
829         }
830         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
831         {
832             netapip_freeTransInfo(pTransInfo);
833             *err = NETAPI_ERR_PA_FW;
834             netapi_Log (">netapi_netcpCfgCreateMacInterface - ERROR returned by NETCP PA firmware %d\n",
835                     *err);
836             return -1;
837         }
839     }
840     netapi_Log ("netapi_netcpCfg: MAC i/f %d added\n", iface_no);
841     netapip_netcpCfgInsertMac(&netapi_get_global()->nwal_context, 
842                           p_mac, iface_no, state,vlan,
843                           (void *) pTransInfo->handle);
844     netapip_freeTransInfo(pTransInfo);
845     return  (NETAPI_NETCP_MATCH_GENERIC_MAC | iface_no);
848 /********************************************************************
849  * FUNCTION PURPOSE:  API to delete MAC  interface
850  ********************************************************************
851  * DESCRIPTION:  API to delete MAC  interface
852  ********************************************************************/
853 void netapi_netcpCfgDelMac(NETAPI_T h,
854                            int iface_no,
855                            int *err)
857     nwal_RetValue ret;
858     NetapiNwalTransInfo_t *pTransInfo;
859     nwal_TransID_t     trans_id;
860     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
861     void * ifHandle;
863     //get the nwal handle assoicated with this iface
864     ifHandle = netapip_netcpCfgGetMacHandle(&netapi_get_global()->nwal_context, iface_no );
865     if(!ifHandle) 
866     {
867         *err = NETAPI_ERR_BAD_INPUT; return ;
868     }
869     *err =0;
871     //get a transaction id
872     pTransInfo = netapip_getFreeTransInfo(n,
873                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
874                                           &trans_id);
875     if (!pTransInfo)
876     {
877         *err =  NETAPI_ERR_BUSY;
878         return ;
879     }
880     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_MAC;
881     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
882     pTransInfo->inUse = nwal_TRUE;
883     pTransInfo->netapi_handle = h;
885     //issue request
886     ret = nwal_delMacIface(
887                 ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
888                 trans_id,
889                 ifHandle);
890     if(ret !=  nwal_OK)
891     {
892         *err = NETAPI_ERR_NWAL_ERR0;
893         netapi_Log ("netapi_netcpCfg - ERROR: nwal_delMacIface returned Error Code %d\n",
894                     ret);
895         netapip_freeTransInfo(pTransInfo);
896         return;
897     }
898     //wait here until its done since scheduler isn't running yet most likely..
899     // todo:  make this handled by scheduler poll later ??
900     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
901     {
902         n->nwal_local.numPendingCfg++;
903         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
904                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
905         {
906             // if response is there, then this poll squirts out in the CTl poll callback, 
907             // which handles the rest (including decrmenting #pending!!
908             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
909         }
910         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
911         {
912             netapip_freeTransInfo(pTransInfo);
913             *err = NETAPI_ERR_PA_FW;
914             netapi_Log (">netapi_netcpCfgCreateMacInterface - ERROR returned by NETCP PA firmware %d\n",
915                     *err);
916             netapip_netcpCfgDeleteMac(&netapi_get_global()->nwal_context,  iface_no);
917             return;
918         }
919     }
920     netapi_Log ("netapi_netcpCfg: MAC i/f %d deleted\n",iface_no);
921     netapip_freeTransInfo(pTransInfo);
922     //zap the entry
923     netapip_netcpCfgDeleteMac(&netapi_get_global()->nwal_context,  iface_no);
924     return ;
928 /********************************************************************
929  * FUNCTION PURPOSE:  NETAPi internal function to Add IP to MAC interface
930  ********************************************************************
931  * DESCRIPTION:  NETAPi internal function to Add IP to MAC interface
932  ********************************************************************/
933 static NETCP_CFG_IP_T  netapip_netcpCfgAddIpInternal(NETAPI_T  h,
934                                                      int  iface_no,
935                                                      nwal_IpType ipType,
936                                                      nwalIpAddr_t  * ip_addr,
937                                                      nwalIpAddr_t  *ip_rem_addr,
938                                                      nwalIpOpt_t * ip_qualifiers,
939                                                      NETCP_CFG_ROUTE_HANDLE_T  route,/*NULL for default*/
940                                                      void * user_data,
941                                                      int * err,
942                                                      int  flag) /*TRUE: add IP to iface,
943                                                      FALSE: add IP as part of classifier */
945     static unsigned char zeroIP4[]={0,0,0,0};
946     static unsigned char zeroIP6[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
947     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
948     void * n_handle=NULL;
949     nwalIpParam_t    nwalIpParam= {
950     0,
951     pa_IPV4,      /* IP Type */
952     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Dest IP */
953     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Source IP */
954     { 0x0,0,0,0},/* IP Options */
955     NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
956     NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
957     CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
958     QMSS_PARAM_NOT_SPECIFIED,                     /* Use default queue configured to NWAL if packet is routed to host */
959     0
960 };
961     nwal_RetValue       retValue;
962     NetapiNwalTransInfo_t *pTransInfo;
963     nwal_TransID_t     trans_id;
964     int ip_slot=-1;
965     NETCP_CFG_IP_T ip_rule_id;
966     NETCP_CFG_IP_T temp;
968     //verify that iface has been configured 
969     if (iface_no != NETCP_CFG_NO_INTERFACE)
970     {
971         if ((iface_no<0) || (iface_no>= TUNE_NETAPI_MAX_NUM_MAC)) {*err = NETAPI_ERR_BAD_INPUT; return -1;}
972     }
974     if (iface_no != NETCP_CFG_NO_INTERFACE)
975     {
976         netapi_Log("netapip_netcpCfgAddIpInternal, in_use %d\n", netapi_get_global()->nwal_context.interfaces[iface_no].in_use);
977         netapi_Log("netapip_netcpCfgAddIpInternal, p 0x%x\n", &netapi_get_global()->nwal_context);
978         if(netapi_get_global()->nwal_context.interfaces[iface_no].in_use)
979         {
980             n_handle =  netapi_get_global()->nwal_context.interfaces[iface_no].nwal_handle;
981         }
982         else
983         {
984             *err = NETAPI_ERR_BAD_INPUT;
985             return -1;
986         }
987     }
988     if (flag) //if adding IP to MAC then reserve a slot to save info
989     {
990         //find free slot for IP & reserve
991          ip_slot= netapip_netcpCfgFindIpSlot(n,
992                                              &netapi_get_global()->nwal_context, 
993                                              iface_no);
994         if (ip_slot <0) 
995         {
996             *err= NETAPI_ERR_NOMEM;  //no room 
997             return -1;
998         }
999     }
1001     //get a transaction object for config action
1002     pTransInfo = netapip_getFreeTransInfo(n, 
1003                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1004                                           &trans_id);
1005     if (!pTransInfo)
1006     {
1007         *err =  NETAPI_ERR_BUSY;
1008         if (flag)
1009         {
1010             netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,
1011                          iface_no,
1012                          ip_slot);
1013         }
1014         return -1;
1015     }
1016     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_IP;
1017     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
1018     pTransInfo->inUse = nwal_TRUE;
1019     pTransInfo->netapi_handle = h;
1021     //build nwalIpParam
1022     memcpy(&nwalIpParam.locIpAddr,ip_addr, sizeof(nwalIpAddr_t));
1023     if (ip_rem_addr)
1024     {
1025         if (ipType == nwal_IPV4)
1026         {
1027             if(memcmp(ip_rem_addr, zeroIP4, sizeof(nwalIpv4Addr_t)))
1028             {
1029                 nwalIpParam.validParams |= NWAL_SET_IP_VALID_PARAM_REMOTE_IP;
1030                 memcpy(&nwalIpParam.remIpAddr,ip_rem_addr, sizeof(nwalIpAddr_t));
1031             }
1032         }
1033         else
1034         {
1035             if(memcmp(ip_rem_addr, zeroIP6, sizeof(nwalIpv6Addr_t)))
1036             {
1037                 nwalIpParam.validParams |= NWAL_SET_IP_VALID_PARAM_REMOTE_IP;
1038                 memcpy(&nwalIpParam.remIpAddr,ip_rem_addr, sizeof(nwalIpAddr_t));
1039             }
1040         }
1041     }
1042     nwalIpParam.ipType=ipType;
1043     if(route)
1044     {
1045         if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1046             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1047         {
1048            nwalIpParam.validParams |= 
1049                     NWAL_SET_IP_VALID_PARAM_ROUTE_TYPE;
1050         }
1051         netapip_netcpCfgBuildRoute(route,
1052                                    &nwalIpParam.appRxPktFlowId,
1053                                    &nwalIpParam.appRxPktQueue,
1054                                    &nwalIpParam.routeType);
1055     }
1056     else{} //use nwal defaults
1058     if (ip_qualifiers)
1059         memcpy(&nwalIpParam.ipOpt,ip_qualifiers, sizeof(nwalIpOpt_t)); 
1060     else
1061         memset(&nwalIpParam.ipOpt,0, sizeof(nwalIpOpt_t));
1063     //build the rule id that will be returned when a packet matches 
1064     if (flag)
1065         ip_rule_id = NETAPI_NETCP_MATCH_GENERIC_IP | iface_no | ((ip_slot&& NETAPI_NETCP_MATCH_ID_MASK) << NETAPI_NETCP_MATCH_ID_SHIFT);
1066     else
1067         ip_rule_id = (NETAPI_NETCP_MATCH_CLASS_L3 | iface_no);
1069     retValue = nwal_setIPAddr(   netapi_get_global()->nwal_context.nwalInstHandle,
1070                                   trans_id,
1071                                   (nwal_AppId) (ip_rule_id),
1072                                   n_handle,
1073                                   &nwalIpParam,
1074                                   &pTransInfo->handle);
1076     if(retValue !=  nwal_OK)
1077     {
1078         *err = NETAPI_ERR_NWAL_ERR0;
1079         netapi_Log ("netcp_cfg: nwal_setIP returned Error Code %d\n",
1080                     retValue);
1081         netapip_freeTransInfo(pTransInfo);
1082         //zap the entry
1083         if (flag)
1084         {
1085             netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,
1086                          iface_no,
1087                          ip_slot);
1088         }
1089         return -1;
1090     }
1091     //wait here until its done since scheduler isn't running yet most likely..
1092     // todo:  make this handled by scheduler poll later ??
1093     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1094     {
1095         n->nwal_local.numPendingCfg++;
1096         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1097         (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
1098         {
1099             // if response is there, then this poll squirts out in the CTl poll callback, 
1100             // which handles the rest (including decrmenting #pending!!
1101             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1102         }
1103         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1104         {
1105             netapip_freeTransInfo(pTransInfo);
1106             *err = NETAPI_ERR_PA_FW;
1107             netapi_Log ("netapip_netcpCfgAddIpInternal - ERROR returned by NETCP PA firmware %d\n", *err);
1108             return -1;
1109         }
1110     }
1111     if (flag)
1112     {
1113         netapi_Log ("netcp_cfg: IP added to interface %d (slot%d)\n", iface_no, ip_slot);
1114         netapip_netcpCfgInsertIp(&netapi_get_global()->nwal_context,
1115                                  ipType, 
1116                                  ip_addr,
1117                                  ip_qualifiers,
1118                                  iface_no,
1119                                  ip_slot,
1120                                  pTransInfo->handle,
1121                                  user_data);
1122     }
1124     temp = (NETCP_CFG_IP_T) pTransInfo->handle;
1125     netapip_freeTransInfo(pTransInfo);
1126     return  (flag ? ip_rule_id:  temp);
1129 /********************************************************************
1130  * FUNCTION PURPOSE:  API to Add IP to MAC interface
1131  ********************************************************************
1132  * DESCRIPTION:  API to Add IP to MAC interface
1133  ********************************************************************/
1134 NETCP_CFG_IP_T  netapi_netcpCfgAddIp(NETAPI_T                  h,
1135                                      int                       iface_no,
1136                                      nwal_IpType               ipType,
1137                                      nwalIpAddr_t*             ip_addr,
1138                                      nwalIpAddr_t*             ip_rem_addr,
1139                                      nwalIpOpt_t*              ip_qualifiers,
1140                                      NETCP_CFG_ROUTE_HANDLE_T  route,  //NULL for default
1141                                      void*                     user_data,
1142                                      int*                      err)
1144     return netapip_netcpCfgAddIpInternal(h,
1145                                          iface_no,
1146                                          ipType,
1147                                          ip_addr,
1148                                          ip_rem_addr,
1149                                          ip_qualifiers, 
1150                                          route, 
1151                                          user_data,
1152                                          err,
1153                                          1);
1156 /********************************************************************
1157  * FUNCTION PURPOSE:  NETAPI internal function to detach IP from MAC interface
1158  ********************************************************************
1159  * DESCRIPTION:  NETAPI internal function to detach IP from MAC interface
1160  ********************************************************************/
1161 static void netapip_netcpCfgDelIpInternal(NETAPI_T h, 
1162                                           int iface_no,
1163                                           nwal_IpType ipType,
1164                                           nwalIpAddr_t  * ip_addr,
1165                                           nwalIpOpt_t * ip_qualifiers, 
1166                                           NETCP_CFG_IP_T  ip_rule_id,
1167                                           int *err, 
1168                                           void * handle,   /* if flag==0, handle must be valid */
1169                                           int flag)        /* flag==0 => delete IP rule that was part of classifier, not interface */
1171     nwal_RetValue ret;
1172     NetapiNwalTransInfo_t *pTransInfo;
1173     nwal_TransID_t     trans_id;
1174     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1175     void * ifHandle;
1176     int ip_slot = netapi_cfgGetMatchId(ip_rule_id);
1177     //get the nwal handle assoicated with this ip   
1178     if (flag)
1179     {
1180         ifHandle = netapip_netcpCfgGetIpHandle(
1181                                         &netapi_get_global()->nwal_context, iface_no,
1182                                         ip_slot );
1183     }
1184     else 
1185     {
1186         ifHandle = handle;
1187     }
1188     if(!ifHandle)
1189     {
1190         *err = NETAPI_ERR_BAD_INPUT;
1191         return ;
1192     }
1193     *err =0;
1195     //get a transaction id
1196     pTransInfo = netapip_getFreeTransInfo(n,
1197                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1198                                           &trans_id);
1199     if (!pTransInfo)
1200     {
1201         *err =  NETAPI_ERR_BUSY;
1202         return;
1203     }
1204     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_IP;
1205     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
1206     pTransInfo->inUse = nwal_TRUE;
1207     pTransInfo->netapi_handle = h;
1208     //issue request
1209     ret = nwal_delIPAddr(
1210                 ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1211                 trans_id,
1212                 ifHandle);
1213     if(ret !=  nwal_OK)
1214     {
1215         *err = NETAPI_ERR_NWAL_ERR0;
1216         netapi_Log ("netcp_cfg - ERROR: nwal_delMacIface returned Error Code %d\n",
1217                     ret);
1218         netapip_freeTransInfo(pTransInfo);
1219         return ;
1220     }
1221     //wait here until its done since scheduler isn't running yet most likely..
1222     // todo:  make this handled by scheduler poll later ??
1223     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1224     {
1225         n->nwal_local.numPendingCfg++;
1226         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1227                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
1228         {
1229             // if response is there, then this poll squirts out in the CTl poll callback, 
1230             // which handles the rest (including decrmenting #pending!!
1231             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1232         }
1233         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1234         {
1235             netapip_freeTransInfo(pTransInfo);
1236             *err = NETAPI_ERR_PA_FW;
1237             netapi_Log (">netapip_netcpCfgDelIpInternal - ERROR returned by NETCP PA firmware %d\n", *err);
1238             return;
1239         }
1240     }
1242     netapip_freeTransInfo(pTransInfo);
1244     //zap the entry
1245     if (flag)
1246         netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,  
1247                          iface_no,
1248                          ip_slot);
1249     return ;
1252 /********************************************************************
1253  * FUNCTION PURPOSE:  API to detach IP from MAC interface
1254  ********************************************************************
1255  * DESCRIPTION:  API  to detach IP from MAC interface
1256  ********************************************************************/
1257 void netapi_netcpCfgDelIp(NETAPI_T              h,
1258                           int                   iface_no,
1259                           nwal_IpType           ipType,
1260                           nwalIpAddr_t*         ip_addr,
1261                           nwalIpOpt_t*          ip_qualifiers,
1262                           NETCP_CFG_IP_T        ip_rule_id,
1263                           int*                  err)
1265     netapip_netcpCfgDelIpInternal( h, iface_no, ipType,
1266                   ip_addr, ip_qualifiers, ip_rule_id,
1267                   err, NULL, 1);
1268     return;
1272 /********************************************************************
1273  * FUNCTION PURPOSE:  API to attach a classifier rule to NETCP
1274  ********************************************************************
1275  * DESCRIPTION:  API to attach a classifier rule to NETCP
1276  ********************************************************************/
1277 NETCP_CFG_CLASS_T netapi_netcpCfgAddClass(NETAPI_T                  h,
1278                                           NETCP_CFG_CLASSIFIER_T*   p_class,
1279                                           NETCP_CFG_ROUTE_HANDLE_T  route,
1280                                           int                       action, 
1281                                           void*                     user_data, 
1282                                           int*                      err)
1284     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1285     void * l3_handle=NULL;  //ip handle
1286     nwal_RetValue       retValue;
1287     NetapiNwalTransInfo_t *pTransInfo;
1288     nwal_TransID_t     trans_id;
1289     int class_slot=-1;
1290     int iface_no;
1291     int ip_slot=-1;
1292     NETCP_CFG_CLASS_T  classHandle;  //returned by us
1293     nwal_appProtoType_t proto;
1294     nwalRxConnCfg_t tempCfg=
1295     {
1296         0,  //nwal_handle: to be filled in
1297         0, // l4 ports: to be filled in
1298         0,  //core id (NA)
1299         0, //action
1300         CPPI_PARAM_NOT_SPECIFIED, //flow id
1301         QMSS_PARAM_NOT_SPECIFIED, //dest queue
1302         0,
1303         0, // l4 ports: to be filled in
1304     };
1306     *err = NETAPI_ERR_OK;
1308     if(!p_class)
1309     {
1310         *err=NETAPI_ERR_BAD_INPUT;
1311         return -1;
1312     }
1313     switch(p_class->classType)
1314     {
1315         default:
1316             netapi_Log(">netapi_netcpCfg : classifier type %d not supported\n",p_class->classType);
1317             break;
1318         case(NETCP_CFG_CLASS_TYPE_L3_L4):
1319         case(NETCP_CFG_CLASS_TYPE_L4):
1320             //assume just type l4 only (L2, L3 defined by iface, l3 id )
1321             iface_no = p_class->u.c_l4.iface;
1322             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1323             {
1324                 ip_slot = netapi_cfgGetMatchId(p_class->u.c_l4.ip);
1325             }
1327             //verify that iface has been configured 
1328             if (iface_no != NETCP_CFG_NO_INTERFACE)
1329             {
1330                 if(!netapi_get_global()->nwal_context.interfaces[iface_no].in_use)
1331                 {
1332                     *err = NETAPI_ERR_BAD_INPUT;
1333                     return -1;
1334                 }
1335             }
1337             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1338             {
1339                 //verify that ip has been configured and get its handle
1340                 l3_handle = netapip_netcpCfgGetIpHandle(
1341                       &netapi_get_global()->nwal_context, iface_no,
1342                       ip_slot );
1343             }
1344             else
1345             {
1346                 nwalIpParam_t tempParam=
1347                 {
1348                     0,
1349                     pa_IPV4,      /* IP Type */
1350                     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Dest IP */
1351                     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Source IP */
1352                     { 0x0,0,0,0},/* IP Options */
1353                     NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
1354                     NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
1355                     CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
1356                     QMSS_PARAM_NOT_SPECIFIED,                    /* Use default queue configured to NWAL if packet is routed to host */
1357                     0
1358                 };
1359                 //build nwalIpParam
1360                 memcpy(&tempParam.locIpAddr,p_class->u.c_l3_l4.ip_addr, sizeof(nwalIpAddr_t));
1361                 tempParam.ipType=p_class->u.c_l3_l4.ipType;
1362                 //use nwal defauls for route
1363                 if (p_class->u.c_l3_l4.ip_qualifiers)
1364                     memcpy(&tempParam.ipOpt,p_class->u.c_l3_l4.ip_qualifiers, sizeof(nwalIpOpt_t));
1365                 else
1366                     memset(&tempParam.ipOpt,0, sizeof(nwalIpOpt_t));
1368                 //find if we have a matching L3 handle for IP classifier; if not create it
1369                 retValue =  nwal_getIPAddr (((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1370                             &tempParam,
1371                             netapip_netcpCfgGetMacHandle(&netapi_get_global()->nwal_context, iface_no ),
1372                             &l3_handle); 
1373                 if (retValue != nwal_TRUE) 
1374                 {
1375                     int ret;
1376                     //**NEW IP RULE  
1377                     //need to attach this IP RULE to the MAC
1378                     l3_handle= (void *) netapip_netcpCfgAddIpInternal(
1379                                       h, iface_no, 
1380                                       p_class->u.c_l3_l4.ipType,
1381                                       p_class->u.c_l3_l4.ip_addr,
1382                                       NULL,
1383                                       p_class->u.c_l3_l4.ip_qualifiers,
1384                                       p_class->u.c_l3_l4.p_fail_route,
1385                                       user_data,
1386                                       &ret,
1387                                       FALSE);
1388                     if(!ret)
1389                     {
1390                         l3_handle=NULL;
1391                     }
1392                 }
1393             }
1394             if(!l3_handle)
1395             {
1396                 *err = NETAPI_ERR_BAD_INPUT; 
1397                 return -1 ;
1398             }
1401             //find free slot for CLASS & reserve
1402             class_slot= netapip_netcpCfgFindClassSlot(n,
1403                                                       &netapi_get_global()->nwal_context);
1404             if(class_slot<0) {*err = NETAPI_ERR_NOMEM; return -1;}
1405             classHandle = NETAPI_NETCP_MATCH_CLASS |
1406                           (class_slot << NETAPI_NETCP_MATCH_ID_SHIFT) | 
1407                           (iface_no & NETAPI_NETCP_MATCH_LOGICAL_MAC_IFACE_MASK);
1408             //build request from template
1409             tempCfg.inHandle=l3_handle;
1410             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1411             {
1412                 memcpy(&tempCfg.appProto,&p_class->u.c_l4.appProto,sizeof(nwalAppProto_t));
1413                 proto = p_class->u.c_l4.proto;
1414             }
1415             else
1416             {
1417                 memcpy(&tempCfg.appProto,&p_class->u.c_l3_l4.appProto,sizeof(nwalAppProto_t));
1418                 proto = p_class->u.c_l3_l4.proto;
1419             }
1421             tempCfg.matchAction = (action==NETCP_CFG_ACTION_TO_SW)  ? NWAL_MATCH_ACTION_HOST : NWAL_MATCH_ACTION_DISCARD;
1422             if (route)
1423             {
1424                 if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1425                     NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1426                 {
1427                     tempCfg.validParams |= 
1428                         NWAL_SET_CONN_VALID_PARAM_ROUTE_TYPE;
1429                 }
1430                 netapip_netcpCfgBuildRoute(route,
1431                                            &tempCfg.appRxPktFlowId,
1432                                            &tempCfg.appRxPktQueue,
1433                                            &tempCfg.routeType);
1434             }
1437             //get a transaction id
1438             pTransInfo = netapip_getFreeTransInfo(n,
1439                                                   (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1440                                                   &trans_id);
1441             if (!pTransInfo)
1442             {
1443                 *err =  NETAPI_ERR_BUSY; 
1444                 return -1;
1445             }
1446             pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_PORT;
1447             pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
1448             pTransInfo->inUse = nwal_TRUE;
1449             pTransInfo->netapi_handle = h;
1450                 //issue request
1451             retValue = nwal_addConn(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1452                             trans_id,
1453                             (nwal_AppId) classHandle,
1454                             proto,
1455                             &tempCfg,
1456                             NULL,
1457                             &pTransInfo->handle);
1458             if(retValue !=  nwal_OK)
1459             {
1460                 *err = NETAPI_ERR_NWAL_ERR0;
1461                 netapi_Log ("netcp_cfg - ERROR: nwal_delMacIface returned Error Code %d\n",
1462                         retValue);
1463                 netapip_freeTransInfo(pTransInfo);
1464                 netapip_netcpCfgDeleteClass(&netapi_get_global()->nwal_context, class_slot);
1465                 return -1;
1466             }
1467             //wait here until its done since scheduler isn't running yet most likely..
1468            // todo:  make this handled by scheduler poll later ??
1469             if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1470             {
1471                 n->nwal_local.numPendingCfg++;
1472                 while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1473                     (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
1474                 {
1475                     // if response is there, then this poll squirts out in the CTl poll callback, 
1476                     // which handles the rest (including decrmenting #pending!!
1477                     nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1478                 }
1479                 if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1480                 {
1481                     netapip_freeTransInfo(pTransInfo);
1482                     *err = NETAPI_ERR_PA_FW;
1483                     netapip_netcpCfgDeleteClass(&netapi_get_global()->nwal_context, class_slot);
1484                     netapi_Log (">netcp_cfgAddClass - ERROR returned by NETCP PA firmware %d\n", *err);
1485                     return -1;
1486                 }
1487             }
1488             netapi_Log ("netcp_cfg: L4 Classifer added to interface %d ip %d (slot%d)\n", iface_no, ip_slot, class_slot);
1489             netapip_netcpCfgInsertClass(&netapi_get_global()->nwal_context, 
1490                                    class_slot,
1491                                  p_class->classType, 
1492                                 NULL,  //L2 we have
1493                                 (p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4? l3_handle : NULL),
1494                                   pTransInfo->handle,
1495                                   user_data);
1497             netapip_freeTransInfo(pTransInfo);
1498             return classHandle;
1499     } //end switch
1501 return -1;
1504 /********************************************************************
1505  * FUNCTION PURPOSE:  API to delete a preconfigured classifier
1506  ********************************************************************
1507  * DESCRIPTION:  API to delete a preconfigured classifier
1508  ********************************************************************/
1509 void netapi_netcpCfgDelClass(NETAPI_T           h,
1510                              NETCP_CFG_CLASS_T  classId,
1511                              int*               err)
1513     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1514     void * L4_handle;  //class handle -> L4
1515     void * L3_handle;  //class handle -> L3
1516     nwal_RetValue       retValue;
1517     NetapiNwalTransInfo_t *pTransInfo;
1518     nwal_TransID_t     trans_id;
1519     int class_slot=-1;
1521     class_slot = netapi_cfgGetMatchId(classId);
1522     L4_handle=netapip_netcpCfgGetL4Handle(&netapi_get_global()->nwal_context, class_slot);
1523     if(!L4_handle)
1524     {
1525         *err = NETAPI_ERR_BAD_INPUT; 
1526         goto ERR_netapi_netcpCfgDelClass;
1527     }
1528     L3_handle = netapip_netcpCfgGetL3Handle( &netapi_get_global()->nwal_context, class_slot );
1529     /* l3 handle might be NULL,, depending on type of classifier */
1531     netapip_netcpCfgDeleteClass(
1532                         &netapi_get_global()->nwal_context,
1533                         class_slot );
1534     //get a transaction id
1535     pTransInfo = netapip_getFreeTransInfo(n,
1536                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1537                                           &trans_id);
1538     if (!pTransInfo)
1539     {
1540         *err =  NETAPI_ERR_BUSY; 
1541         goto ERR_netapi_netcpCfgDelClass;
1542     }
1543     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_PORT;
1544     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
1545     pTransInfo->inUse = nwal_TRUE;
1546     pTransInfo->netapi_handle = h;
1547     //issue request for L4
1548     retValue = nwal_delConn(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1549                                                   trans_id,
1550                                                 L4_handle);
1551     if(retValue !=  nwal_OK)
1552     {
1553         *err = NETAPI_ERR_NWAL_ERR0;
1554          netapi_Log ("netcp_cfg - ERROR: nwal_delMacIface returned Error Code %d\n", retValue);
1555          netapip_freeTransInfo(pTransInfo);
1556          goto ERR_netapi_netcpCfgDelClass;  /* todo: what about the L3? */
1557     }
1558     //wait here until its done since scheduler isn't running yet most likely..
1559     // todo:  make this handled by scheduler poll later ??
1560     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1561     {
1562         n->nwal_local.numPendingCfg++;
1563         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1564                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
1565         {
1566             // if response is there, then this poll squirts out in the CTl poll callback, 
1567             // which handles the rest (including decrmenting #pending!!
1568             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1569         }
1570     }
1571     if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1572     {
1573         netapip_freeTransInfo(pTransInfo);
1574         *err = NETAPI_ERR_PA_FW;
1575          netapi_Log (">netapi_netcpCfgDelClass - ERROR returned by NETCP PA firmware %d\n", *err);
1576          goto ERR_netapi_netcpCfgDelClass;
1577     }
1578     netapi_Log ("netcp_cfg: Classifer deleted\n");
1579     pTransInfo->state =  NETAPI_NWAL_HANDLE_STATE_IDLE;
1580     pTransInfo->inUse = nwal_FALSE;
1582     /* delete L3 if we have to */
1583     if (L3_handle)
1584     {
1585         netapip_netcpCfgDelIpInternal( h, 0, 0,
1586                   NULL, NULL, 0,
1587                   err, L3_handle,  0);
1588     }
1589     netapip_freeTransInfo(pTransInfo);
1591 ERR_netapi_netcpCfgDelClass:
1592     return;
1595 /********************************************************************
1596  * FUNCTION PURPOSE:  API to add a flow
1597  ********************************************************************
1598  * DESCRIPTION:  API to add a flow
1599  ********************************************************************/
1600 /*--------------flow management--------*/
1601 // ADD A Flow
1602 NETCP_CFG_FLOW_HANDLE_T netapi_netcpCfgAddFlow(NETAPI_T                 h,
1603                                                int                      n,
1604                                                Pktlib_HeapHandle        handles[],
1605                                                int                      sizes[],
1606                                                NETCP_CFG_FLOW_CONFIG_T* p_cfg,
1607                                                int*                     err )
1609     Cppi_RxFlowCfg  rxFlowCfg;
1610     Uint8           isAlloc;
1611     Qmss_QueueHnd   rxBufQ[TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW];
1612     Uint32          rxBufSize[TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW];
1613     int             i;
1614     Cppi_FlowHnd FlowHnd;
1615     int  slot;
1616     NETCP_CFG_FLOW_HANDLE_T retVal;
1617     NETAPI_HANDLE_T * pHandle = (NETAPI_HANDLE_T *) h;
1618     *err= 0;  /* ok */
1619     if (!p_cfg)
1620     {
1621         *err= NETAPI_ERR_BAD_INPUT;
1622         return NULL;
1623     }
1624     //get a slot to save new flow
1625     slot = netapip_netcpCfgFindFlowSlot(pHandle,
1626                                         &netapi_get_global()->nwal_context);
1628     if (slot<0) { *err= NETAPI_ERR_NOMEM;  return NULL; }
1630     //configure flow
1631     memset(&rxFlowCfg,0,sizeof(Cppi_RxFlowCfg));
1632     for (i = 0; i < TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW; i++)
1633     {
1634         if (i >= n)
1635         {
1636             rxBufQ[i] = 0;
1637             rxBufSize[i] = 0;
1638         }
1639         else
1640         {
1641             rxBufQ[i] =   Pktlib_getInternalHeapQueue(handles[i]);
1642             //todo: verity sizes< heapsize
1643             //todo: verify order
1644             rxBufSize[i]= sizes[i];
1645         }
1646        if (i && (rxBufQ[i] <= 0))
1647        {
1648             rxBufQ[i] = rxBufQ[i-1];
1649             rxBufSize[i] = 0;
1650        }
1651     }
1653     /* Configure Rx flow */
1654     rxFlowCfg.flowIdNum      = p_cfg->flow_index;/*CPPI_PARAM_NOT_SPECIFIED*/;
1655     rxFlowCfg.rx_dest_qnum   = netapi_pktioGetQ(p_cfg->p_dest_q);
1656     rxFlowCfg.rx_dest_qmgr   = Qmss_getQueueNumber(rxFlowCfg.rx_dest_qnum).qMgr; 
1657     rxFlowCfg.rx_sop_offset  = p_cfg->recv_offset;
1658     rxFlowCfg.rx_ps_location = Cppi_PSLoc_PS_IN_DESC;
1659     rxFlowCfg.rx_desc_type   = Cppi_DescType_HOST;
1660     rxFlowCfg.rx_error_handling = p_cfg->block;
1662     rxFlowCfg.rx_psinfo_present = 1;
1663     rxFlowCfg.rx_einfo_present  = 1;
1665     rxFlowCfg.rx_dest_tag_lo = 0;
1666     rxFlowCfg.rx_dest_tag_hi = 0;
1667     rxFlowCfg.rx_src_tag_lo  = 0;
1668     rxFlowCfg.rx_src_tag_hi  = 0;
1670     rxFlowCfg.rx_size_thresh0_en = rxBufSize[1] ? 1 : 0;
1671     rxFlowCfg.rx_size_thresh1_en = rxBufSize[2] ? 1 : 0;
1672     rxFlowCfg.rx_size_thresh2_en = rxBufSize[3] ? 1 : 0;
1674   if (p_cfg->dma_index==NETAPI_DMA_NETCP )
1675     rxFlowCfg.rx_dest_tag_lo_sel =0;
1676   else
1677     rxFlowCfg.rx_dest_tag_lo_sel = 0x4;
1678     rxFlowCfg.rx_dest_tag_hi_sel = 0;
1679     rxFlowCfg.rx_src_tag_lo_sel  = 0;
1680     rxFlowCfg.rx_src_tag_hi_sel  = 0;
1682     rxFlowCfg.rx_fdq1_qnum = rxBufQ[1];
1683     rxFlowCfg.rx_fdq1_qmgr = (rxBufQ[1] ? Qmss_getQueueNumber(rxBufQ[1]).qMgr: 0);
1684     rxFlowCfg.rx_fdq2_qnum = rxBufQ[2];
1685     rxFlowCfg.rx_fdq2_qmgr = (rxBufQ[2] ? Qmss_getQueueNumber(rxBufQ[2]).qMgr: 0);
1686     rxFlowCfg.rx_fdq3_qnum = rxBufQ[3];
1687     rxFlowCfg.rx_fdq3_qmgr = (rxBufQ[3] ? Qmss_getQueueNumber(rxBufQ[3]).qMgr: 0);
1689     rxFlowCfg.rx_size_thresh0 = rxBufSize[1] ? rxBufSize[0] : 0;
1690     rxFlowCfg.rx_size_thresh1 = rxBufSize[2] ? rxBufSize[1] : 0;
1691     rxFlowCfg.rx_size_thresh2 = rxBufSize[3] ? rxBufSize[2] : 0;
1693     rxFlowCfg.rx_fdq0_sz0_qnum = rxBufQ[0];
1694     rxFlowCfg.rx_fdq0_sz0_qmgr =  (rxBufQ[0] ? Qmss_getQueueNumber(rxBufQ[0]).qMgr: 0);
1695     rxFlowCfg.rx_fdq0_sz1_qnum = rxBufQ[1];
1696     rxFlowCfg.rx_fdq0_sz1_qmgr =  (rxBufQ[1] ? Qmss_getQueueNumber(rxBufQ[1]).qMgr:0);
1697     rxFlowCfg.rx_fdq0_sz2_qnum = rxBufQ[2];
1698     rxFlowCfg.rx_fdq0_sz2_qmgr =  (rxBufQ[2] ? Qmss_getQueueNumber(rxBufQ[2]).qMgr:0);
1699     rxFlowCfg.rx_fdq0_sz3_qnum = rxBufQ[3];
1700     rxFlowCfg.rx_fdq0_sz3_qmgr =  (rxBufQ[3] ? Qmss_getQueueNumber(rxBufQ[3]).qMgr:0);
1701     {
1702         //todo: replace this with a nwal call to get global cntx info
1703         Cppi_CpDmaInitCfg cpdmaCfg;
1704         Cppi_Handle cppiHnd;
1705         memset(&cpdmaCfg,0,sizeof(Cppi_CpDmaInitCfg));
1706         cpdmaCfg.dmaNum   = (p_cfg->dma_index==NETAPI_DMA_NETCP ) ?Cppi_CpDma_PASS_CPDMA: Cppi_CpDma_QMSS_CPDMA;
1707         cppiHnd= Cppi_open (&cpdmaCfg);
1708         if (cppiHnd == NULL)
1709         {
1710               netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context, slot);
1711               *err= NETAPI_ERR_NORES;
1712               return NULL;
1713         }
1714         FlowHnd = Cppi_configureRxFlow (cppiHnd, &rxFlowCfg, &isAlloc);
1715     }
1716     if (FlowHnd == NULL)
1717     {
1718         *err= NETAPI_ERR_NORES;
1719         netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context, slot);
1720         return (NULL);
1721     }
1723     //update slot
1724     retVal = netapip_netcpCfgInsertFlow(&netapi_get_global()->nwal_context,
1725                                         slot,
1726                                         p_cfg->dma_index,
1727                                         (void*) FlowHnd);
1728     netapi_Log(">netcp cfg:  flow %d created in dma index %d\n",
1729         ((NETCP_CFG_FLOW_T *) retVal)->flowid, p_cfg->dma_index);
1730     return (retVal);
1733 /********************************************************************
1734  * FUNCTION PURPOSE:  API to delete a flow
1735  ********************************************************************
1736  * DESCRIPTION:  API to delete a flow
1737  ********************************************************************/
1738 void netapi_netcpCfgDelFlow(NETAPI_T                h,
1739                             NETCP_CFG_FLOW_HANDLE_T f,
1740                             int*                    err)
1742     int slot;
1743     void * handle;
1744     *err=0;
1745     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1746     /* find entry */
1747         slot = netapip_netcpCfgFindFlow(n,
1748                                         &netapi_get_global()->nwal_context,
1749                                         ((NETCP_CFG_FLOW_T *) f) ->flowid,
1750                                         ((NETCP_CFG_FLOW_T *) f) ->dma_engine,
1751                                         &handle);
1752     if (slot<0)
1753     {
1754         *err = NETAPI_ERR_BAD_INPUT;
1755         return;
1756     }
1758     Cppi_closeRxFlow( (Cppi_FlowHnd) handle);
1759     netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context,
1760                                slot);
1761     netapi_Log(">netcp cfg:  flow %d (dma index %d) deleted\n",
1762                 ((NETCP_CFG_FLOW_T *) f)->flowid,
1763                 ((NETCP_CFG_FLOW_T *) f)->dma_engine);
1764     return;
1767 /******************************************************************************
1768  * FUNCTION PURPOSE:  API to configure NETCP with global rules for exception packet handling
1769  ******************************************************************************
1770  * DESCRIPTION:  API to configure NETCP with global rules for exception packet handling
1771  *****************************************************************************/
1772 NETCP_CFG_EXCEPTION_PKT_T netapi_netcpCfgExceptions(NETAPI_T                    h,
1773                                                     int                         exception_id,
1774                                                     nwal_matchAction_t          action,
1775                                                     NETCP_CFG_ROUTE_HANDLE_T    p_route)
1777     nwalCtlInfo_t   ctrl;
1778     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1779     nwal_RetValue retVal = 0;
1780     memset(&ctrl, 0, sizeof(nwalCtlInfo_t));
1783     if (p_route != NULL)
1784     {
1785         if((p_route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1786             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1787         {
1788            ctrl.validParams |= 
1789                     NWAL_CONTROL_VALID_PARAM_ROUTE_TYPE;
1790            ctrl.routeType = p_route->routeType;
1791         }
1792         netapip_netcpCfgBuildRoute(p_route,
1793                                    &ctrl.appRxPktFlowId,
1794                                    &ctrl.appRxPktQueue,
1795                                    &ctrl.routeType);
1796     }
1797     else
1798     {
1799         ctrl.appRxPktFlowId = NWAL_FLOW_NOT_SPECIFIED;
1800         ctrl.appRxPktQueue = NWAL_QUEUE_NOT_SPECIFIED;
1801     }
1803     ctrl.appId = (void*)(NETAPI_NETCP_CFG_MATCH_EXCEPTION | exception_id);
1805     ctrl.matchAction = action;
1807     if (exception_id == NETCP_CFG_ALL_EXCEPTIONS)
1808     {
1809         ctrl.pktCtl = NWAL_CTRL_CFG_ALL_EXCEPTIONS;
1810     }
1811     else
1812     {
1813             ctrl.pa_EROUTE_Id = exception_id;
1814             ctrl.pktCtl = NWAL_CTRL_CFG_SINGLE_EXCEPTION;
1815     }
1817     retVal = nwal_control(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle, &ctrl);
1819     return (NETCP_CFG_EXCEPTION_PKT_T) ctrl.appId;
1823 /*************************************************************************/
1824 /*********************************INTERNAL*******************************/
1825 /************************************************************************/
1827 /********************************************************************
1828  * FUNCTION PURPOSE:  NETAPI internal function, NETCP command reply callback
1829  ********************************************************************
1830  * DESCRIPTION:  NETAPI internal function, NETCP command reply callback
1831  ********************************************************************/
1832 void netapip_netcpCfgNWALCmdCallBack(nwal_AppId        appHandle,
1833                             uint16_t            trans_id,
1834                             nwal_RetValue     ret)
1836     NetapiNwalTransInfo_t * p_trans;
1837     NETAPI_NWAL_LOCAL_CONTEXT_T *p_local=NULL;
1839     if(trans_id == NWAL_TRANSID_SPIN_WAIT)
1840     {
1841         netapi_get_proc_global()->nwal_context.numBogusTransIds++;
1842         return;
1843     }
1845     p_trans= &netapi_get_proc_global()->nwal_context.transInfos[trans_id];
1846     p_local =&((NETAPI_HANDLE_T*) (p_trans->netapi_handle))->nwal_local;
1848     if(ret != nwal_OK)
1849     {
1850         netapi_Log ("netcp_cfg : netapip_netcpCfgNWALCmdCallBack returned Error Code %d for trans_id %d\n",
1851                     ret, trans_id);
1852         // update error code that is fialed  in p_trans */
1853         //todo: atomic inc
1854         p_trans->state = NETAPI_NWAL_HANDLE_STATE_ERR;
1855         netapi_get_proc_global()->nwal_context.numCmdFail++;
1856     }
1857     else
1858     {
1859         //todo: atomic inc
1860         netapi_get_proc_global()->nwal_context.numCmdPass++;
1861         switch(p_trans->transType)
1862         {
1863             case NETAPI_NWAL_HANDLE_TRANS_MAC:
1864             {
1865                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1866                 {
1867                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1868                 }
1869                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1870                 {
1871                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1872                 }
1873                 break;
1874             }
1875             case NETAPI_NWAL_HANDLE_TRANS_IP:
1876             {
1877                  if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1878                 {
1879                     p_trans->state = NETAPI_NWAL_HANDLE_STATE_OPEN;
1880                 }
1881                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1882                 {
1883                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1884                 }
1885                 break;
1886             }
1887             case NETAPI_NWAL_HANDLE_TRANS_PORT:
1888             {
1889                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1890                 {
1891                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1892                 }
1893                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1894                 {
1895                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1896                 }
1897                 break;
1898             }
1899             case NETAPI_NWAL_HANDLE_TRANS_SA:
1900             {
1901                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1902                 {
1903                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1904                 }
1905                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1906                 {
1907                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1908                 }
1909                 break;
1910             }
1911             case NETAPI_NWAL_HANDLE_TRANS_SA_POLICY:
1912             {
1913                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1914                 {
1915                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1916                 }
1917                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1918                 {
1919                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1920                 }
1921                 break;
1922             }
1923             default:
1924             {
1925                 netapi_Log ("netcp cfg> Invalid transaction type %d for trans_id: %d\n",
1926                     p_trans->transType,trans_id);
1927                 break;
1928             }
1929         }
1930     }
1932     p_local->numPendingCfg--;
1934     if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_IDLE)
1935     {
1936         p_trans->inUse = nwal_FALSE;
1937     }
1941 /********************************************************************
1942  * FUNCTION PURPOSE:  NETAPI internal function, PA stats reply callback
1943  ********************************************************************
1944  * DESCRIPTION:  NETAPI internal function, PA stats reply callback
1945  ********************************************************************/
1946 void netapip_netcpCfgNWALCmdPaStatsReply(nwal_AppId        appHandle,
1947                                 nwal_TransID_t    trans_id,
1948                                 paSysStats_t      *stats)
1950     NetapiNwalTransInfo_t * p_trans;
1951     NETAPI_NWAL_LOCAL_CONTEXT_T *p_local=NULL;
1953     if(trans_id == NWAL_TRANSID_SPIN_WAIT)
1954     {
1955         netapi_get_proc_global()->nwal_context.numBogusTransIds++;
1956         return;
1957     }
1959     p_trans= &netapi_get_proc_global()->nwal_context.transInfos[trans_id];
1960     p_trans->inUse = nwal_FALSE;
1961     p_local =&((NETAPI_HANDLE_T*) (p_trans->netapi_handle))->nwal_local;
1963     //save a local copy  of some stuff*/
1964     p_local->numL2PktsRecvd=stats->classify1.nPackets;
1965     p_local->numL3PktsRecvd=stats->classify1.nIpv4Packets;
1967     //callout result to application !!
1968     if (p_local->stats_cb)
1969     {
1970         (*p_local->stats_cb)(p_trans->netapi_handle,stats);
1971     }