9e89c3f6e0211560cba27df7a3f2b29e3a482436
[o-du/l2.git] / src / mt / ss_msg.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18 \f
19 /********************************************************************20**
20  
21      Name:     System Services -- Message manipulation functions
22  
23      Type:     C source file
24  
25      Desc:     Source Code for message related functions.
26  
27      File:     ss_msg.c
28  
29 *********************************************************************21*/
30  
31
32 \f
33 /* header include files (.h) */
34
35 #include "envopt.h"        /* environment options */
36 #include "envdep.h"        /* environment dependent */
37 #include "envind.h"        /* environment independent */
38   
39 #include "gen.h"           /* general layer */
40 #include "ssi.h"           /* system services */
41
42 #include "ss_err.h"        /* errors */
43 #include "ss_dep.h"        /* implementation-specific */
44 #include "ss_queue.h"      /* queues */
45 #include "ss_strm.h"       /* STREAMS */
46 #include "ss_msg.h"        /* messaging */
47 #include "ss_mem.h"        /* memory management interface */
48 #include "ss_gen.h"        /* general */
49 #include "cm_mem.h"
50 #include "cm_llist.h"
51
52 #ifdef RGL_SPECIFIC_CHANGES
53 #ifdef TENB_DPDK_BUF
54 #include "ntl_lib.h"
55 #endif
56 #endif
57
58
59 /* header/extern include files (.x) */
60
61 #include "gen.x"           /* general layer */
62 #include "ssi.x"           /* system services */
63
64 #include "ss_dep.x"        /* implementation-specific */
65 #include "ss_queue.x"      /* queues */
66 #include "ss_task.x"       /* tasking */
67 #include "ss_timer.x"      /* timers */
68 #include "ss_strm.x"       /* STREAMS */
69 #include "ss_msg.x"        /* messaging */
70 #include "ss_mem.x"        /* memory management interface */
71 #include "ss_drvr.x"       /* driver tasks */
72 #ifdef SS_LOCKLESS_MEMORY
73 #include "cm_llist.x"
74 #include "cm_hash.x"
75 #include "cm_mem_wl.x"        /* common memory manager */
76 #else
77 #include "cm_mem.x"        /* common memory manager */
78 #endif /* SS_LOCKLESS_MEMORY */
79 #include "ss_gen.x"        /* general */
80
81 /*ss004.301: Cavium changes */
82 #ifdef SS_SEUM_CAVIUM
83 /* cvmx includes files */
84 #include "cvmx-config.h"
85 #include "cvmx.h"
86 #include "cvmx-pow.h"
87 #include "cvmx-tim.h"
88 #include "cvmx-fpa.h"
89 #include "cvmx-helper-fpa.h"
90 #include "cvmx-malloc.h"
91 #endif /* SS_SEUM_CAVIUM */
92 #include <pthread.h>
93 #ifdef XEON_SPECIFIC_CHANGES
94 uint32_t startMemLeak=0; 
95 pthread_mutex_t  memLock;
96 #endif
97
98 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
99 #include "ss_rbuf.h"
100 #include "ss_rbuf.x"
101 #endif
102 #ifdef L2_L3_SPLIT
103 #include "mt_plat_t33.h"
104 #include "mt_plat_t33.x"
105 #endif
106 /* forward declarations */
107 S16 ssGetDBufOfSize ARGS((Region region, Size size, Buffer **dBuf));
108 S16 SIncMsgRef(Buffer *srcBuf,Region dstRegion, Pool dstPool,Buffer **dstBuf);
109 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
110 uint32_t GetNextFreeIdx ARGS((StaticMemAllocInfo * memAllocInfo));
111 void LogForStaticMemLeak ARGS((StaticMemAllocInfo* memAllocInfo, char* 
112          file, uint32_t line, uint32_t size, void* ptr, uint32_t idx));
113 void FreeIdx ARGS((uint8_t* ptr, uint32_t idx, StaticMemAllocInfo*
114          memAllocInfo,uint32_t size, char* file, uint32_t line));
115 #endif
116
117 #ifdef SS_USE_ZBC_MEMORY
118 #ifdef T2K_MEM_LEAK_DBG
119 static S16 SPutZbcDBufNew ARGS((Region region, Buffer *buf,char*,uint32_t));
120
121 #else
122 static S16 SPutZbcDBuf ARGS((Region region, Buffer *buf));
123 S16 SAttachPtrToBuf ARGS(( Region   region, Pool     pool, Data    *ptr,
124          MsgLen   totalLen, Buffer** mBuf));
125 #endif
126 #endif
127
128 /* ss012.13: Addition */
129 #ifdef SS_M_PROTO_REGION 
130 #ifdef T2K_MEM_LEAK_DBG
131 #define DupMsg(region,buffer) DupMsgNew(region,buffer,file,line)
132 static Buffer *DupMsgNew ARGS((Region region, Buffer *buffer,char*,uint32_t));
133 #else
134 #ifdef INTEL_WLS
135 #ifdef T2K_MEM_LEAK_DBG
136 S16 SAttachPtrToMBuf1 ARGS(( Region   region, Pool     pool, Data    *ptr, MsgLen   totalLen,
137                                    MsgLen   ptrLen, Buffer** mBuf, char* file, uint32_t line));
138 #else
139 S16 SAttachPtrToMBuf ARGS(( Region   region, Pool     pool, Data    *ptr, MsgLen   totalLen,
140                                    MsgLen   ptrLen, Buffer** mBuf));
141 #endif
142 #ifdef T2K_MEM_LEAK_DBG
143 S16 SAttachWlsPtrToMBuf1 ARGS(( Region   region, Pool     pool, Data    *ptr, 
144                                       Data    *readPtr,    MsgLen   totalLen,
145                                       MsgLen   ptrLen, Buffer** mBuf, char* file, uint32_t line));
146
147 S16 SAttachWlsPtrToMBuf ARGS(( Region   region, Pool     pool, Data    *ptr, 
148                                       Data    *readPtr,    MsgLen   totalLen,
149                                       MsgLen   ptrLen, Buffer** mBuf));
150 #endif
151 #ifdef TENB_DPDK_BUF
152 S16 SDetachDpdkPtrFrmMBuf ARGS((Buffer   *mBuf, Data     **ptr));
153 S16 SDetachDpdkPtrFrmDBuf ARGS((Buffer   *mBuf, Data     **ptr));
154 S16 SPutSBufDpdk          ARGS(( Data    *ptr));
155 #endif  /* TENB_DPDK_BUF */
156
157
158 #endif /* INTEL_WLS */
159
160 static Buffer *DupMsg ARGS((Region region, Buffer *buffer));
161 #endif
162 #endif /* SS_M_PROTO_REGION */
163
164 /* local defines */
165 #define MBSIZE   (sizeof(SsMblk))
166 #define MDBSIZE  (sizeof(SsDblk) + sizeof(SsMblk))
167 #define MSGSIZE  (MDBSIZE + sizeof(SsMsgInfo))
168
169 /*ss014.301 SSI-4GMX specific changes*/
170 #if (defined(SS_4GMX_LCORE) && defined (RB_HEAD_ROOM))
171 #define INITMBLK(mp, dp, dat)   {\
172    mp->b_next = NULLP;\
173    mp->b_prev = NULLP;\
174    mp->b_cont = NULLP;\
175    mp->b_rptr = (dat + RB_MSG_HEAD_ROOM);\
176    mp->b_wptr = (dat + RB_MSG_HEAD_ROOM);\
177    mp->b_datap = dp;\
178 }
179 #else
180 #define INITMBLK(mp, dp, dat)   {\
181    mp->b_next = NULLP;\
182    mp->b_prev = NULLP;\
183    mp->b_cont = NULLP;\
184    mp->b_rptr = dat;\
185    mp->b_wptr = dat;\
186    mp->b_datap = dp;\
187 }
188 #endif
189
190 /* ss008.301 */
191 #ifdef SS_DBLK_FREE_RTN
192 #define INITDBLK(dp, dat, size, frtn)   {\
193    dp->db_frtnp = frtn;\
194    dp->db_base = dat;\
195    dp->db_lim = (dat == NULLP ? dat : (dat + size));\
196    dp->db_ref = 1;\
197    dp->shared = FALSE; \
198    dp->db_type = SS_M_DATA;\
199 }
200 #else
201 #define INITDBLK(dp, dat, size, frtn)   {\
202    dp->db_base = dat;\
203    dp->db_lim = (dat == NULLP ? dat : (dat + size));\
204    dp->db_ref = 1;\
205    dp->shared = FALSE; \
206    dp->db_type = SS_M_DATA;\
207 }
208 #endif /* SS_DBLK_FREE_RTN */
209
210 #define INITB(mp, dp, dat, size, frtn)   {\
211    INITMBLK(mp, dp, dat)\
212    INITDBLK(dp, dat, size, frtn)\
213 }
214
215
216 pthread_t tmpRegTidMap[20];
217 #define CM_MEM_GET_REGION(_region)                        \
218 {                                                         \
219    uint8_t  _regCnt;                                           \
220    _region = 0xFF;                                        \
221                                                           \
222    for(_regCnt = 0; _regCnt < 12; _regCnt++)              \
223    {                                                      \
224       if(tmpRegTidMap[_regCnt] == pthread_self())         \
225       {                                                   \
226          _region = _regCnt;                               \
227          break;                                           \
228       }                                                   \
229    }                                                      \
230 }
231
232 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF) || defined(L2_OPTMZ))
233 S32 clusterMode;
234 #endif
235 /*
236 *
237 *       Fun:   ssGetDBufOfSize
238 *
239 *       Desc:  This function gets a message buffer from specified region and
240 *              size
241 *
242 *       Ret:   ROK      - ok
243 *              RFAILED  - failed, general (optional)
244 *              ROUTRES  - failed, out of resources (optional)
245 *
246 *       Notes: message is created. message is returned via message buffer
247 *              pointer. Buffer type is SS_M_DATA.
248 *              return is ok.
249 *
250 *       File:  ss_msg.c
251 *
252 */
253 #ifdef T2K_MEM_LEAK_DBG
254 S16 ssGetDBufOfSizeNew
255 (
256 Region region,
257 Size size,
258 Buffer **dBuf,
259 char* file,
260 uint32_t line
261 )
262 #else
263 S16 ssGetDBufOfSize
264 (
265 Region region,
266 Size size,
267 Buffer **dBuf
268 )
269 #endif
270 {
271    Size mdsize;
272    Data *data;
273    SsDblk *dptr;
274
275 #if (ERRCLASS & ERRCLS_INT_PAR)
276    /* check buffer pointer */
277    if (!dBuf)
278    {
279       SSLOGERROR(ERRCLS_INT_PAR, ESS047, ERRZERO, "ssGetDBufOfSize : Null\
280                  Buffer");
281       return RFAILED;
282    }
283    if (region >= SS_MAX_REGS)
284    {
285       SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "ssGetDBufOfSize : Invalid\
286                 region id");
287       return RFAILED;
288    }
289  
290    if (size <= 0)
291    {
292       SSLOGERROR(ERRCLS_INT_PAR, ESS049, ERRZERO, "ssGetDBufOfSize : Invalid\
293                  size");
294       return RFAILED;
295    }
296 #endif
297
298    /* ss006.301 : optmized this function */
299    mdsize = MDBSIZE + size;
300 #ifdef SS_HISTOGRAM_SUPPORT 
301    if (SAlloc(region, &mdsize, 0, (Data **) dBuf, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
302 #else
303    if (SAlloc(region, &mdsize, 0, (Data **) dBuf) != ROK)
304 #endif /* SS_HISTOGRAM_SUPPORT */
305    {
306       return (ROUTRES);
307    }
308    data = (Data *) (*dBuf) + MDBSIZE;
309    size = mdsize - MDBSIZE;
310  
311    dptr = (SsDblk*) (((Data *) (*dBuf)) + MBSIZE);
312  
313    INITB((*dBuf), dptr, data, size, NULLP)
314 #ifndef SS_DBUF_REFLOCK_DISABLE
315    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
316    {
317       SSLOGERROR(ERRCLS_DEBUG, ESS056, ERRZERO,"Falied to initalize lock");
318    }
319 #endif
320    
321    return ROK;
322 } /* ssGetDBufOfSize */
323
324
325 /*
326 *
327 *       Fun:   SGetMsgNew
328 *
329 *       Desc:  This function gets a message
330 *
331 *       Ret:   ROK      - ok
332 *              RFAILED  - failed, general (optional)
333 *              ROUTRES  - failed, out of resources (optional)
334 *
335 *       Notes: message is created. message is set to type SS_M_PROTO.
336 *              message is returned via message buffer pointer.
337 *              return is ok.
338 *
339 *       File:  ss_msg.c
340 *
341 */
342 /* ss001.301: additions */
343 #ifdef SS_HISTOGRAM_SUPPORT 
344 S16 SGetMsgNew
345 (
346 Region region,              /* region id */
347 Pool pool,                  /* pool id */
348 Buffer **mBuf,              /* pointer to message buffer */
349 uint32_t    line,
350 uint8_t     *fileName
351 )
352 #else /* SS_HISTOGRAM_SUPPORT */
353 #ifdef T2K_MEM_LEAK_DBG
354 S16 SGetMsgNew
355 (
356 Region region,              /* region id */
357 Pool pool,                  /* pool id */
358 Buffer **mBuf,              /* pointer to message buffer */
359 char* file,
360 uint32_t line
361 )
362 #else
363 S16 SGetMsg
364 (
365 Region region,              /* region id */
366 Pool pool,                  /* pool id */
367 Buffer **mBuf              /* pointer to message buffer */
368 )
369 #endif
370 #endif /* SS_HISTOGRAM_SUPPORT */
371 {
372    SsMsgInfo *minfo;
373    Size size = MSGSIZE;
374    SsDblk *dptr;
375    Data *data;
376 /* ss001.301: additions */
377 #ifdef SS_HISTOGRAM_SUPPORT
378         Ent entId = 0;
379 #endif /* SS_HISTOGRAM_SUPPORT */
380
381    /* ss021.103 - Addition of return value */
382 #ifndef SS_PERF
383 #if (ERRCLASS & ERRCLS_INT_PAR)
384    S16 ret;
385 #endif
386 #endif
387
388
389 #ifdef XEON_SPECIFIC_CHANGES
390    region = 0;
391    pool   = 0;
392 #endif
393
394 #if (ERRCLASS & ERRCLS_INT_PAR)
395    /* check buffer pointer */
396    if (!mBuf)
397    {
398       SSLOGERROR(ERRCLS_INT_PAR, ESS050, ERRZERO, "SGetMsg : Null Buffer");
399       return RFAILED;
400    }
401    if (region >= SS_MAX_REGS)
402    {
403       SSLOGERROR(ERRCLS_INT_PAR, ESS051, ERRZERO, "SGetMsg : Invalid region\
404                                                    id");
405       return RFAILED;
406    }
407  
408    if (pool >= SS_MAX_POOLS_PER_REG)
409    {
410       SSLOGERROR(ERRCLS_INT_PAR, ESS052, ERRZERO, "SGetMsg : Invalid pool id");
411       return RFAILED;
412    }
413
414 /* ss037.103 Removed the semaphore operation for performance enhancement */
415
416 #ifndef SS_PERF
417    /* ss021.103 - Addition to check if region is registered */
418    /* acquire one semaphore, to protect against deregistration */
419    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
420    if (ret != ROK)
421    {
422
423 #if (ERRCLASS & ERRCLS_DEBUG)
424       SSLOGERROR(ERRCLS_DEBUG, ESS053, (ErrVal) ret,
425                   "Could not lock region table");
426 #endif
427
428       return RFAILED;
429    }
430 #endif
431 #endif
432 #if (ERRCLASS & ERRCLS_INT_PAR)
433    /* verify that this region is present */
434    if (osCp.regionTbl[region].used == FALSE)
435    {
436 /* ss037.103 Removed the semaphore operation for performance enhancement */
437
438 #ifndef SS_PERF
439       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
440       {
441 #if (ERRCLASS & ERRCLS_DEBUG)
442          SSLOGERROR(ERRCLS_DEBUG, ESS054, ERRZERO,
443                   "Could not release semaphore");
444          return RFAILED;
445 #endif
446       }
447 #endif
448       SSLOGERROR(ERRCLS_INT_PAR, ESS055, region, "Region not registered");
449       return RFAILED;
450    }
451 /* ss037.103 Removed the semaphore operation for performance enhancement */
452
453 #ifndef SS_PERF
454    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
455    {
456 #if (ERRCLASS & ERRCLS_DEBUG)
457       SSLOGERROR(ERRCLS_DEBUG, ESS056, ERRZERO,
458                   "Could not release semaphore");
459       return RFAILED;
460 #endif
461    }
462 #endif
463 #endif
464
465 /* ss001.301: additions */
466 #ifdef SS_HISTOGRAM_SUPPORT 
467    SGetEntInd(&entId, fileName);
468 #endif /* SS_HISTOGRAM_SUPPORT */
469
470 /* ss012.13: Addition */
471 #ifdef SS_M_PROTO_REGION
472 /* ss001.301: additions */
473 #ifdef SS_HISTOGRAM_SUPPORT
474    if (SAlloc(region, &size, 0, (Data**) mBuf, line, fileName, entId) != ROK)
475 #else
476    if (SAlloc(region, &size, 0, (Data**) mBuf) != ROK)
477 #endif /* SS_HISTOGRAM_SUPPORT */
478    {
479       SSLOGERROR(ERRCLS_DEBUG, ESS058, ERRZERO, "SGetMsg:Failed in SAlloc");
480       return (ROUTRES);
481    }
482 #else /* SS_M_PROTO_REGION */  
483 /* ss001.301: additions */
484 #ifdef SS_HISTOGRAM_SUPPORT 
485    if (SAlloc(SS_DFLT_REGION, &size, 0, (Data**) mBuf, line, fileName, entId) != ROK)
486 #else
487    if (SAlloc(region, &size, 0, (Data**) mBuf) != ROK)
488 #endif /* SS_HISTOGRAM_SUPPORT */
489    {
490       SSLOGERROR(ERRCLS_DEBUG, ESS059, ERRZERO, "SGetMsg:Failed in SAlloc");
491       return (ROUTRES);
492    }
493 #endif /* SS_M_PROTO_REGION */
494
495    dptr = (SsDblk*) (((Data *) (*mBuf)) + MBSIZE);
496    data = (Data*) (((Data *) (*mBuf)) + MDBSIZE);
497  
498    /* INITB initialises and sets up the message blk */
499    INITB((*mBuf), dptr, data, sizeof(SsMsgInfo), NULLP)
500
501    (*mBuf)->b_datap->db_type = SS_M_PROTO;
502    (*mBuf)->b_wptr = (*mBuf)->b_rptr + sizeof(SsMsgInfo);
503
504    /* initialise message info of mBuf */
505    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
506
507    minfo->region = region;
508    minfo->pool = pool;
509    minfo->len = 0;
510    minfo->endptr = NULLP;
511    minfo->eventInfo.event = SS_EVNT_DATA;
512    /* ss021.103 - Addition to initialize next and route */
513    minfo->next = NULLP;
514 #ifdef L2_OPTMZ
515    {
516       if(clusterMode == RADIO_CLUSTER_MODE)
517       {
518          (*mBuf)->refCnt = 1;
519       }
520    }
521 #endif
522    return ROK;
523 }
524
525 #ifdef RGL_SPECIFIC_CHANGES
526 S16 SSetMBufPool(Buffer *mBuf, Pool pool)
527 {
528
529     SsMsgInfo *minfo;
530
531     minfo = (SsMsgInfo*) mBuf->b_rptr; 
532     minfo->pool   = pool;
533
534     return ROK;
535
536 #endif
537
538 /* #ifdef SS_LOCKLESS_MEMORY */
539
540 /*
541 *
542 *       Fun:   SPutMsgNew
543 *
544 *       Desc:  This function deallocates a message back.
545 *
546 *       Ret:   ROK      - ok
547 *              RFAILED  - failed, general (optional)
548 *
549 *       Notes: all data attached to message is returned to memory.
550 *              message is returned to memory. return is ok.
551 *
552 *       File:  ss_msg.c
553 *
554 */
555 /* ss001.301: additions */
556 #ifdef SS_HISTOGRAM_SUPPORT 
557 S16 SPutMsgNew
558 (
559 Buffer *mBuf,
560 uint32_t    line,
561 uint8_t     *fileName
562 )
563 #else /* SS_HISTOGRAM_SUPPORT */
564 #ifdef T2K_MEM_LEAK_DBG
565 S16 SPutMsgNew
566 (
567 Buffer *mBuf,
568 char* file,
569 uint32_t line
570 )
571 #else
572 S16 SPutMsg
573 (
574 Buffer *mBuf
575 )
576 #endif
577 #endif /* SS_HISTOGRAM_SUPPORT */
578 {
579    Buffer *tmp;
580    SsMsgInfo *minfo;
581 #ifdef SS_MEM_WL_DEBUG
582    uint8_t     tmpThrReg;
583 #endif
584
585 /* ss001.301: additions */
586 #ifdef SS_HISTOGRAM_SUPPORT
587         Ent entId = 0;
588 #endif /* SS_HISTOGRAM_SUPPORT */
589
590 #ifdef L2_OPTMZ
591 //#ifdef RADIO_CLUSTER
592       if(clusterMode == RADIO_CLUSTER_MODE)
593       {
594          if(mBuf->refCnt)
595          {
596             mBuf->refCnt -= 1;
597          }
598          if (mBuf->refCnt > 0)
599          {
600             return ROK;
601          }
602       }
603 #endif
604 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
605    {
606       pthread_t gMacTId,gRlcTId;
607       S32 clusterMode;
608       if(clusterMode == RADIO_CLUSTER_MODE)
609       {
610
611 #ifdef MAC_FREE_RING_BUF
612          if(pthread_self() == gMacTId)
613          //if(pthread_equal(pthread_self(),gMacTId))
614          {
615             if(ROK == mtAddBufToRing(SS_RNG_BUF_MAC_FREE_RING,(Void *)mBuf,0))
616             {
617                return ROK;
618             }
619          }
620 #endif
621 #ifdef RLC_FREE_RING_BUF
622          else if(pthread_self() == gRlcTId)
623          {
624             if(ROK == mtAddBufToRing(SS_RNG_BUF_RLC_FREE_RING,(Void *)mBuf,0))
625             {
626                return ROK;
627             }
628          }
629 #endif
630       }
631    }
632 #endif
633
634 #if (ERRCLASS & ERRCLS_INT_PAR)
635    if (mBuf == NULLP)
636    {
637       SSLOGERROR(ERRCLS_INT_PAR, ESS060, ERRZERO, "SPutMsg: Null Buffer");
638       return RFAILED;
639    }
640    if (mBuf->b_datap->db_type != SS_M_PROTO)
641    {
642       SSLOGERROR(ERRCLS_INT_PAR, ESS061, ERRZERO, "SPutMsg: Incorrect buffer\
643                                                    type");
644       return RFAILED;
645    }
646 #endif
647
648 #ifdef SS_MEM_WL_DEBUG
649    CM_MEM_GET_REGION(tmpThrReg)
650    if(tmpThrReg == 0xFF)
651    {
652       printf("\n Not able to get region \n");
653       return RFAILED;
654    }
655 #endif
656
657    /* get the message info */
658    minfo = (SsMsgInfo *) mBuf->b_rptr;
659
660    while ((tmp = mBuf->b_cont))
661    {
662       /* set b_cont of mBuf to point to the b_cont of tmp */
663       mBuf->b_cont = tmp->b_cont;
664 #ifdef SS_MEM_WL_DEBUG
665       (Void) SPutDBuf(tmpThrReg, minfo->pool, tmp);
666 #else
667       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
668 #endif
669    }
670
671 /* ss001.301: additions */
672 #ifdef SS_HISTOGRAM_SUPPORT 
673    SGetEntInd(&entId, fileName);
674 #endif /* SS_HISTOGRAM_SUPPORT */
675
676 /* ss012.13: Addition */
677 #ifdef SS_M_PROTO_REGION
678    /* ss021.103 - Addition to check return value of SFree */
679 #ifdef SS_HISTOGRAM_SUPPORT
680    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE, line, fileName, entId) == RFAILED)
681 #else
682 #ifdef SS_MEM_WL_DEBUG
683    if (SFree(tmpThrReg, (Data *) mBuf, MSGSIZE) == RFAILED)
684 #else
685    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE) == RFAILED)
686 #endif
687 #endif /* SS_HISTOGRAM_SUPPORT */
688       return RFAILED;
689 #else /* SS_M_PROTO_REGION */
690    /* ss021.103 - Addition to check return value of SFree */
691 #ifdef SS_HISTOGRAM_SUPPORT
692    if (SFree(SS_DFLT_REGION, (Data *) mBuf, MSGSIZE, line, fileName, entId) == RFAILED)
693 #else
694    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE) == RFAILED)
695 #endif /* SS_HISTOGRAM_SUPPORT */
696       return RFAILED;
697 #endif /* SS_M_PROTO_REGION */
698
699    return ROK;
700 }
701
702 #ifdef SS_LOCKLESS_MEMORY
703 /*
704 *
705 *       Fun:   SPutStaticBuffer
706 *
707 *       Desc:  Returns a buffer to the specified static pool in the
708 *              specified memory region.
709 *
710 *       Ret:   ROK      - ok
711 *              RFAILED  - failed, general (optional)
712 *
713 *       Notes:
714 *
715 *       File:  ss_msg.c
716 *
717 */
718 /* ss001.301: additions */
719 #ifdef T2K_MEM_LEAK_DBG
720 S16 SPutStaticBufferNew
721 (
722 Region region,                  /* region ID */
723 Pool pool,                      /* pool ID */
724 Data *ptr,                      /* pointer to buffer */
725 Size size,                      /* size */
726 uint8_t   memType,                    /* memory type used if shareable or not */
727 char* file,
728 uint32_t line
729 )
730 #else
731 S16 SPutStaticBuffer
732 (
733 Region region,                  /* region ID */
734 Pool pool,                      /* pool ID */
735 Data *ptr,                      /* pointer to buffer */
736 Size size,                      /* size */
737 uint8_t   memType                    /* memory type used if shareable or not */
738 )
739 #endif
740 {
741    S16 ret = RFAILED;
742
743 #if (ERRCLASS & ERRCLS_INT_PAR)
744    /* validate region ID */
745    if (region >= SS_MAX_REGS)
746    {
747       SSLOGERROR(ERRCLS_INT_PAR, ESS220, region, "Invalid region");
748       return RFAILED;
749    }
750
751    /* validate pool ID */
752    if (pool >= SS_MAX_POOLS_PER_REG)
753    {
754       SSLOGERROR(ERRCLS_INT_PAR, ESS221, pool, "Invalid pool");
755       return RFAILED;
756    }
757    /* validate data pointer */
758    if (ptr == NULLP)
759    {
760       SSLOGERROR(ERRCLS_INT_PAR, ESS223, (ErrVal)0, "NULL data pointe");
761       return RFAILED;
762    }
763
764    /* validate size */
765    if (size <= 0)
766    {
767       SSLOGERROR(ERRCLS_INT_PAR, ESS224, size, "Invalid size");
768       return RFAILED;
769    }
770 #endif
771 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
772    {
773       pthread_t gMacTId,gRlcTId;
774
775       if(clusterMode == RADIO_CLUSTER_MODE)
776       {
777 #ifdef MAC_FREE_RING_BUF
778          if(pthread_self() == gMacTId)
779          //if(pthread_equal(pthread_self(),gMacTId))
780          {
781             if(ROK == mtAddBufToRing(SS_RNG_BUF_MAC_FREE_RING,(Void *)ptr,1))
782             {
783                return ROK;
784             }
785          }
786 #endif
787 #ifdef RLC_FREE_RING_BUF
788          else if(pthread_self() == gRlcTId)
789          {
790             if(ROK == mtAddBufToRing(SS_RNG_BUF_RLC_FREE_RING,(Void *)ptr,1))
791             {
792                return ROK;
793             }
794          }
795       }
796    }
797 #endif
798 #endif
799    region = SS_GET_THREAD_MEM_REGION();
800    
801
802 #if (ERRCLASS & ERRCLS_INT_PAR)
803    /* verify that this region is present */
804    if (((memType == SS_NON_SHARABLE_MEMORY) && 
805        (osCp.regionTbl[region].used == FALSE)) ||
806        ((memType == SS_SHARABLE_MEMORY) && 
807        (osCp.dynRegionTbl[region].used == FALSE)))
808    {
809       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
810       return (ret);
811    }
812 #endif
813
814    /* call the memory manager, to allocate this memory */
815 #ifndef SS_DBUF_REFLOCK_DISABLE
816    if(memType == SS_NON_SHARABLE_MEMORY)
817    {
818     #ifndef T2K_MEM_LEAK_DBG
819     ret = (osCp.regionTbl[region].free)
820                  (osCp.regionTbl[region].regCb, ptr, size);
821     #else
822     ret = (osCp.regionTbl[region].free)
823                  (osCp.regionTbl[region].regCb, ptr, size, file, line);
824    
825     #endif
826    }
827    else if(memType == SS_SHARABLE_MEMORY)
828    {
829 #endif
830
831 #ifndef T2K_MEM_LEAK_DBG
832       ret = (osCp.dynRegionTbl[region].free)
833               (osCp.dynRegionTbl[region].regCb, ptr, size);
834 #else
835       ret = (osCp.dynRegionTbl[region].free)
836               (osCp.dynRegionTbl[region].regCb, ptr, size,file,line);
837 #endif
838 #ifndef SS_DBUF_REFLOCK_DISABLE
839    }
840 #endif
841
842    return (ret);
843 }
844
845 /*
846 *
847 *       Fun:   SGetStaticBuffer
848 *
849 *       Desc:  Allocates a buffer from the specified static memory pool
850 *              in the specified region. Depending on the type wheather 
851 *              the buffer is sharable or non-sharable indicated by the 
852 *              memType prameter allocation region is choosen
853 *              done
854 *
855 *       Ret:   ROK      - ok
856 *              RFAILED  - failed, general (optional)
857 *
858 *       Notes: 
859 *
860 *       File:  ss_msg.c
861 *
862 */
863 #ifdef T2K_MEM_LEAK_DBG
864 S16 SGetStaticBufferNew
865 (
866 Region region,                  /* region ID */
867 Pool pool,                      /* pool ID */
868 Data **ptr,                     /* pointer to buffer */
869 Size size,                      /* size requested */
870 uint8_t   memType,                    /* memory type used if shareable or not */
871 char* file,
872 uint32_t line
873 )
874 #else
875 S16 SGetStaticBuffer
876 (
877 Region region,                  /* region ID */
878 Pool pool,                      /* pool ID */
879 Data **ptr,                     /* pointer to buffer */
880 Size size,                      /* size requested */
881 uint8_t   memType                    /* memory type used if shareable or not */
882 )
883 #endif
884 {
885    S16 ret = RFAILED;
886    uint32_t flags;
887
888 #if (ERRCLASS & ERRCLS_INT_PAR)
889    /* validate region ID */
890    if (region >= SS_MAX_REGS)
891    {
892       SSLOGERROR(ERRCLS_INT_PAR, ESS211, region, "Invalid region");
893       return (ret);
894    }
895
896    /* validate pool ID */
897    if (pool >= SS_MAX_POOLS_PER_REG)
898    {
899       SSLOGERROR(ERRCLS_INT_PAR, ESS212, pool, "Invalid pool");
900       return (ret);
901    }
902    /* validate data pointer */
903    if (ptr == NULLP)
904    {
905       SSLOGERROR(ERRCLS_INT_PAR, ESS214, (ErrVal)0 , "NULL data pointer");
906       return (ret);
907    }
908
909    /* validate size */
910    if (size <= 0)
911    {
912       SSLOGERROR(ERRCLS_INT_PAR, ESS215, size, "Invalid size");
913       return (ret);
914    }
915 #endif
916
917 #if (ERRCLASS & ERRCLS_INT_PAR)
918    /* verify that this region is present */
919    if (((memType == SS_NON_SHARABLE_MEMORY) && 
920        (osCp.regionTbl[region].used == FALSE)) ||
921        ((memType == SS_SHARABLE_MEMORY) && 
922        (osCp.dynRegionTbl[region].used == FALSE)))
923    {
924       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
925       return (ret);
926    }
927 #endif
928
929    flags = 0;
930
931    region = SS_GET_THREAD_MEM_REGION();
932 #ifdef USE_MEMCAL
933    if (region == SS_STATIC_REGION)
934       flags = 1;
935 #endif
936    
937    /* call the memory manager, to allocate this memory */
938 #ifndef SS_DBUF_REFLOCK_DISABLE
939    if(memType == SS_NON_SHARABLE_MEMORY)
940    {
941 #ifndef T2K_MEM_LEAK_DBG
942       ret = (osCp.regionTbl[region].alloc)
943                  (osCp.regionTbl[region].regCb, &size, flags, ptr);
944 #else
945       ret = (osCp.regionTbl[region].alloc)
946                  (osCp.regionTbl[region].regCb, &size, flags, ptr, file,line);
947 #endif
948    }
949    else if(memType == SS_SHARABLE_MEMORY)
950    {
951 #endif
952 #ifndef T2K_MEM_LEAK_DBG
953       ret = (osCp.dynRegionTbl[region].alloc)
954               (osCp.dynRegionTbl[region].regCb, &size, flags, ptr);
955 #else
956       ret = (osCp.dynRegionTbl[region].alloc)
957               (osCp.dynRegionTbl[region].regCb, &size, flags, ptr,file,line);
958 #endif
959 #ifndef SS_DBUF_REFLOCK_DISABLE
960    }
961 #endif
962
963    return (ret);
964 }
965 #endif /* SS_LOCKLESS_MEMORY */
966
967 #ifdef INTEL_WLS_MEM 
968 #ifndef SS_LOCKLESS_MEMORY
969 S16 SGetStaticBuffer
970 (
971 Region region,                  /* region ID */
972 Pool pool,                      /* pool ID */
973 Data **ptr,                     /* pointer to buffer */
974 Size size,                      /* size requested */
975 uint8_t   memType                    /* memory type used if shareable or not */
976 )
977 {
978     S16  ret;
979
980     ret = SGetSBuf(region, pool, ptr, size);
981
982     return (ret);
983 }
984
985
986 S16 SPutStaticBuffer
987 (
988 Region region,                  /* region ID */
989 Pool pool,                      /* pool ID */
990 Data *ptr,                      /* pointer to buffer */
991 Size size,                      /* size */
992 uint8_t   memType                    /* memory type used if shareable or not */
993 )
994 {
995
996    S16   ret;
997  
998    ret = SPutSBuf(region, pool, ptr, size);
999
1000    return (ret);
1001
1002 }
1003 #endif
1004 #ifdef T2K_MEM_LEAK_DBG
1005 S16 SGetSBufWls1
1006 (
1007 Region region,                  /* region ID */
1008 Pool pool,                      /* pool ID */
1009 Data **ptr,                     /* pointer to buffer */
1010 Size size,                       /* size requested */
1011 char* file,
1012 uint32_t line
1013 )
1014 #else
1015 S16 SGetSBufWls
1016 (
1017 Region region,                  /* region ID */
1018 Pool pool,                      /* pool ID */
1019 Data **ptr,                     /* pointer to buffer */
1020 Size size                       /* size requested */
1021 )
1022 #endif
1023 {
1024     S16   ret;
1025 #ifndef SS_LOCKLESS_MEMORY
1026     uint32_t   flags = 0;
1027 #endif
1028
1029 #ifdef SS_LOCKLESS_MEMORY
1030     region = SS_GET_THREAD_MEM_REGION();
1031     ret    = SAlloc(region, &size, 0, ptr);
1032 #else
1033     region = 0;
1034 #ifdef T2K_MEM_LEAK_DBG
1035     ret = (osCp.regionTbl[region].alloc)
1036                (osCp.regionTbl[region].regCb, &size, flags, ptr,file,line);
1037 #else
1038     ret = (osCp.regionTbl[region].alloc)
1039                (osCp.regionTbl[region].regCb, &size, flags, ptr);
1040 #endif
1041 #endif
1042
1043     return (ret);
1044 }
1045
1046 #ifdef T2K_MEM_LEAK_DBG
1047 S16 SPutSBufWls1
1048 (
1049 Region region,                  /* region ID */
1050 Pool pool,                      /* pool ID */
1051 Data *ptr,                      /* pointer to buffer */
1052 Size size,                      /* size */
1053 char* file,
1054 uint32_t line
1055 )
1056 #else
1057 S16 SPutSBufWls
1058 (
1059 Region region,                  /* region ID */
1060 Pool pool,                      /* pool ID */
1061 Data *ptr,                      /* pointer to buffer */
1062 Size size                      /* size */
1063 )
1064 #endif
1065 {
1066    S16   ret;
1067
1068 #ifdef SS_LOCKLESS_MEMORY
1069     region = SS_GET_THREAD_MEM_REGION();
1070     ret    = SFree(region, ptr, size);
1071 #else
1072    region = 0;
1073 #ifdef T2K_MEM_LEAK_DBG
1074    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size,file,line);
1075 #else
1076    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size);
1077 #endif
1078 #endif
1079
1080    return (ret);
1081 }
1082
1083 #endif  /* INTEL_WLS */
1084
1085 \f
1086 /*
1087 *
1088 *       Fun:   SGetSBufNew
1089 *
1090 *       Desc:  Allocates a buffer from the specified static memory pool
1091 *              in the specified region.
1092 *
1093 *       Ret:   ROK      - ok
1094 *              RFAILED  - failed, general (optional)
1095 *
1096 *       Notes: The new memory management scheme eliminates the concept
1097 *              of pools. This call maps directly to a call to the memory
1098 *              manager.
1099 *
1100 *       File:  ss_msg.c
1101 *
1102 */
1103 /* ss001.301: additions */
1104 #ifdef SS_HISTOGRAM_SUPPORT
1105 S16 SGetSBufNew
1106 (
1107 Region region,                  /* region ID */
1108 Pool pool,                      /* pool ID */
1109 Data **ptr,                     /* pointer to buffer */
1110 Size size,                       /* size requested */
1111 uint32_t    line,
1112 uint8_t     *fileName
1113 )
1114 #else /* SS_HISTOGRAM_SUPPORT */
1115 #if (defined(SSI_STATIC_MEM_LEAK_DETECTION) || defined(T2K_MEM_LEAK_DBG))
1116 S16 SGetSBuf1
1117 (
1118 Region region,                  /* region ID */
1119 Pool pool,                      /* pool ID */
1120 Data **ptr,                     /* pointer to buffer */
1121 Size size,                       /* size requested */
1122 char* file,
1123 uint32_t line
1124 )
1125 #else
1126 S16 SGetSBuf
1127 (
1128 Region region,                  /* region ID */
1129 Pool pool,                      /* pool ID */
1130 Data **ptr,                     /* pointer to buffer */
1131 Size size                       /* size requested */
1132 )
1133 #endif
1134 #endif /* SS_HISTOGRAM_SUPPORT */
1135 {
1136    S16 ret;
1137    uint32_t flags;
1138 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1139    Size tmpSize;
1140 #endif
1141
1142 #ifdef SS_HISTOGRAM_SUPPORT
1143         Ent entId = 0;
1144         Bool hstReg = FALSE;
1145 #endif /* SS_HISTOGRAM_SUPPORT */
1146
1147
1148    region = SS_GET_THREAD_MEM_REGION();
1149 #ifdef INTEL_WLS
1150    region = 1;
1151 #endif /* INTEL_WLS */
1152
1153 #if (ERRCLASS & ERRCLS_INT_PAR)
1154    /* validate region ID */
1155    if (region >= SS_MAX_REGS)
1156    {
1157       SSLOGERROR(ERRCLS_INT_PAR, ESS211, region, "Invalid region");
1158       return RFAILED;
1159    }
1160
1161    /* validate pool ID */
1162    if (pool >= SS_MAX_POOLS_PER_REG)
1163    {
1164       SSLOGERROR(ERRCLS_INT_PAR, ESS212, pool, "Invalid pool");
1165       return RFAILED;
1166    }
1167 /* ss008.13: addition */
1168    /* validate data pointer */
1169    if (ptr == NULLP)
1170    {
1171       SSLOGERROR(ERRCLS_INT_PAR, ESS214, (ErrVal)0 , "NULL data pointer");
1172       return RFAILED;
1173    }
1174
1175    /* validate size */
1176    if (size <= 0)
1177    {
1178       SSLOGERROR(ERRCLS_INT_PAR, ESS215, size, "Invalid size");
1179       return RFAILED;
1180    }
1181 #endif
1182 /* ss037.103 Removed the semaphore operation for performance enhancement */
1183
1184 #ifndef RGL_SPECIFIC_CHANGES
1185    region = SS_GET_THREAD_MEM_REGION();
1186 #endif
1187 #ifndef SS_PERF
1188    /* acquire one semaphore, to protect against deregistration */
1189    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
1190    if (ret != ROK)
1191    {
1192
1193 #if (ERRCLASS & ERRCLS_DEBUG)
1194       SSLOGERROR(ERRCLS_DEBUG, ESS216, (ErrVal) ret,
1195                   "Could not lock region table");
1196 #endif
1197
1198       return RFAILED;
1199    }
1200
1201 #endif
1202
1203 #if (ERRCLASS & ERRCLS_INT_PAR)
1204    /* verify that this region is present */
1205    if (osCp.regionTbl[region].used == FALSE)
1206    {
1207
1208 #ifndef SS_PERF
1209 /* ss006.13: addition */
1210       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1211       {
1212 #if (ERRCLASS & ERRCLS_DEBUG)
1213          SSLOGERROR(ERRCLS_DEBUG, ESS217, ERRZERO,
1214                   "Could not release semaphore");
1215          return RFAILED;
1216 #endif
1217       }
1218
1219       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
1220       return RFAILED;
1221 #endif
1222    }
1223 #endif
1224
1225
1226    flags = 0;
1227
1228    /* ss024.103 - Addition for memory calculator tool */
1229 #ifdef USE_MEMCAL
1230    if (region == SS_STATIC_REGION)
1231       flags = 1;
1232 #endif
1233
1234 /* ss001.301: additions */
1235 #ifdef SS_HISTOGRAM_SUPPORT 
1236
1237       SGetEntInd(&entId, fileName);
1238       /* Get the Information from the oscp that the tapa task with the entity 
1239          id (entid) is registed for histogram or not */
1240       SGetHstGrmInfo(&entId, &hstReg);
1241    /* call the memory manager, to allocate this memory */
1242 #ifdef SSI_DEBUG_LEVEL1
1243    ret = (osCp.regionTbl[region].alloc)
1244                (osCp.regionTbl[region].regCb, &size, flags, ptr, 
1245                 SS_STATIC_MEM_FLAG, line, fileName, entId, hstReg);
1246 #else
1247    ret = (osCp.regionTbl[region].alloc)
1248                (osCp.regionTbl[region].regCb, &size, flags, ptr, 
1249                 line, fileName, entId, hstReg);
1250 #endif /* SSI_DEBUG_LEVEL1 */
1251
1252 #else 
1253    
1254    /* call the memory manager, to allocate this memory */
1255 /* ss036.103 - addition for passing additional parameter memType as static */
1256 #ifdef SSI_DEBUG_LEVEL1
1257    ret = (osCp.regionTbl[region].alloc)
1258                (osCp.regionTbl[region].regCb, &size, flags, ptr, SS_STATIC_MEM_FLAG);
1259 #else
1260 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1261    /* Static mem leak detection changes */
1262    tmpSize = size + 4;
1263 #ifdef T2K_MEM_LEAK_DBG
1264    ret = (osCp.regionTbl[region].alloc)
1265                (osCp.regionTbl[region].regCb, &tmpSize, flags, ptr, file, line);
1266 #else
1267    ret = (osCp.regionTbl[region].alloc)
1268                (osCp.regionTbl[region].regCb, &tmpSize, flags, ptr);
1269 #endif
1270    /*size = tmpSize - 4;*/
1271    {
1272       /*printf("\nptr = %p *ptr = %p\n",ptr,*ptr);*/
1273 #ifdef XEON_SPECIFIC_CHANGES
1274       pthread_mutex_lock(&(memLock));
1275 #endif      
1276       uint32_t idx = GetNextFreeIdx(&SMemLeakInfo[region]);
1277       uint8_t* allocatedPtr = *ptr;
1278       void* actualPtr = allocatedPtr + 4;
1279       *ptr = actualPtr;
1280       /* store the index in the memory allocated itself */
1281       /**((uint32_t*)*((uint32_t*)allocatedPtr)) = idx;*/
1282       *((uint32_t*)allocatedPtr) = idx;
1283       /*printf("\nregion = %d idx = %d ptr = %p *ptr = %p actual = %p allocated = %p content = %d\n",region, idx, ptr, *ptr, actualPtr, allocatedPtr, *((uint32_t*)allocatedPtr));*/
1284
1285       LogForStaticMemLeak(&SMemLeakInfo[region],
1286                           file,
1287                           line,
1288                           size,
1289                           *ptr,
1290                           idx);
1291 #ifdef XEON_SPECIFIC_CHANGES
1292       pthread_mutex_unlock(&(memLock));
1293 #endif      
1294    }
1295 #else
1296 #ifndef T2K_MEM_LEAK_DBG
1297    ret = (osCp.regionTbl[region].alloc)
1298                (osCp.regionTbl[region].regCb, &size, flags, ptr);
1299 #else
1300    ret = (osCp.regionTbl[region].alloc)
1301                (osCp.regionTbl[region].regCb, &size, flags, ptr, file, line);
1302 #endif
1303
1304 #endif
1305 #endif /* SSI_DEBUG_LEVEL1 */
1306
1307 #endif /* SS_HISTOGRAM_SUPPORT */
1308
1309    /* release the semaphore we took */
1310
1311 #ifndef SS_PERF
1312 /* ss006.13: addition */
1313    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1314    {
1315 #if (ERRCLASS & ERRCLS_DEBUG)
1316       SSLOGERROR(ERRCLS_DEBUG, ESS219, ERRZERO,
1317                   "Could not release semaphore");
1318       return RFAILED;
1319 #endif
1320    }
1321 #endif
1322 /* ss036.103 - Addition to handle the memory trampling return value
1323 * This in turn might invoke SRegMemErrHdlr  
1324 */
1325 #ifdef SSI_DEBUG_LEVEL1
1326     if (ret == RTRAMPLINGNOK)
1327     {
1328        SRegMemErrHdlr( region, *ptr, ret);
1329     }
1330 #endif /* SSI_DEBUG_LEVEL1 */
1331
1332    return (ret);
1333 }
1334
1335 \f
1336 /*
1337 *
1338 *       Fun:   SPutSBufNew
1339 *
1340 *       Desc:  Returns a buffer to the specified static pool in the
1341 *              specified memory region.
1342 *
1343 *       Ret:   ROK      - ok
1344 *              RFAILED  - failed, general (optional)
1345 *
1346 *       Notes:
1347 *
1348 *       File:  ss_msg.c
1349 *
1350 */
1351 /* ss001.301: additions */
1352 #ifdef SS_HISTOGRAM_SUPPORT
1353 S16 SPutSBufNew
1354 (
1355 Region region,                  /* region ID */
1356 Pool pool,                      /* pool ID */
1357 Data *ptr,                      /* pointer to buffer */
1358 Size size,                      /* size */
1359 uint32_t    line,
1360 uint8_t     *fileName
1361 )
1362 #else  /* SS_HISTOGRAM_SUPPORT  */
1363 #if (defined(SSI_STATIC_MEM_LEAK_DETECTION) || defined(T2K_MEM_LEAK_DBG))
1364 S16 SPutSBuf1
1365 (
1366 Region region,                  /* region ID */
1367 Pool pool,                      /* pool ID */
1368 Data *ptr,                      /* pointer to buffer */
1369 Size size,                      /* size */
1370 char* file,
1371 uint32_t line
1372 )
1373 #else
1374 S16 SPutSBuf
1375 (
1376 Region region,                  /* region ID */
1377 Pool pool,                      /* pool ID */
1378 Data *ptr,                      /* pointer to buffer */
1379 Size size                      /* size */
1380 )
1381 #endif
1382 #endif /* SS_HISTOGRAM_SUPPORT */
1383 {
1384    S16 ret;
1385
1386 #ifdef SS_HISTOGRAM_SUPPORT
1387         Ent entId = 0;
1388         Bool hstReg = FALSE;
1389 #endif /* SS_HISTOGRAM_SUPPORT */
1390
1391    region = SS_GET_THREAD_MEM_REGION();
1392 #if (!defined(SS_LOCKLESS_MEMORY) && defined(INTEL_WLS))
1393    region = 1;
1394 #endif /* INTEL_WLS */
1395
1396 #if (ERRCLASS & ERRCLS_INT_PAR)
1397    /* validate region ID */
1398    if (region >= SS_MAX_REGS)
1399    {
1400       SSLOGERROR(ERRCLS_INT_PAR, ESS220, region, "Invalid region");
1401       return RFAILED;
1402    }
1403
1404    /* validate pool ID */
1405    if (pool >= SS_MAX_POOLS_PER_REG)
1406    {
1407       SSLOGERROR(ERRCLS_INT_PAR, ESS221, pool, "Invalid pool");
1408       return RFAILED;
1409    }
1410 /* ss008.13: addition */
1411    /* validate data pointer */
1412    if (ptr == NULLP)
1413    {
1414       SSLOGERROR(ERRCLS_INT_PAR, ESS223, (ErrVal)0, "NULL data pointe");
1415       return RFAILED;
1416    }
1417
1418    /* validate size */
1419    if (size <= 0)
1420    {
1421       SSLOGERROR(ERRCLS_INT_PAR, ESS224, size, "Invalid size");
1422       return RFAILED;
1423    }
1424 #endif
1425 /* ss037.103 Removed the semaphore operation for performance enhancement */
1426 #ifndef RGL_SPECIFIC_CHANGES
1427    region = SS_GET_THREAD_MEM_REGION();
1428 #endif
1429 #ifndef SS_PERF
1430    /* acquire one semaphore, to protect against deregistration */
1431    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
1432    if (ret != ROK)
1433    {
1434
1435 #if (ERRCLASS & ERRCLS_DEBUG)
1436       SSLOGERROR(ERRCLS_DEBUG, ESS225, (ErrVal) ret,
1437                   "Could not lock region table");
1438 #endif
1439
1440       return RFAILED;
1441    }
1442 #endif
1443
1444 #if (ERRCLASS & ERRCLS_INT_PAR)
1445    /* verify that this region is around */
1446    if (osCp.regionTbl[region].used == FALSE)
1447    {
1448 #ifndef SS_PERF
1449
1450 /* ss006.13: addition */
1451       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1452       {
1453 #if (ERRCLASS & ERRCLS_DEBUG)
1454          SSLOGERROR(ERRCLS_DEBUG, ESS226, ERRZERO,
1455                   "Could not release semaphore");
1456          return RFAILED;
1457 #endif
1458       }
1459 #endif
1460       SSLOGERROR(ERRCLS_INT_PAR, ESS227, region, "Region not registered");
1461       return RFAILED;
1462    }
1463 #endif
1464
1465 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1466 /* Static mem leak detection changes */
1467    {
1468 #ifdef XEON_SPECIFIC_CHANGES
1469       pthread_mutex_lock(&(memLock));
1470 #endif      
1471       uint32_t idx = *((uint32_t*)((uint8_t *)ptr - 4));
1472       FreeIdx(ptr,idx,&SMemLeakInfo[region],size,file,line);
1473 #ifdef XEON_SPECIFIC_CHANGES
1474       pthread_mutex_unlock(&(memLock));
1475 #endif      
1476    }
1477 /* Static mem leak detection changes */
1478 #endif
1479
1480 /* ss001.301: additions */
1481 #ifdef SS_HISTOGRAM_SUPPORT 
1482      SGetEntInd(&entId, fileName);
1483     /* Get the Information from the oscp that the tapa task with the entity 
1484        id (entid) is registed for histogram or not */
1485       SGetHstGrmInfo(&entId, &hstReg);
1486
1487    /* call the memory manager to free this memory */
1488    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size,
1489                                                line, fileName, entId, hstReg);
1490 #else
1491    /* call the memory manager to free this memory */
1492 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1493 #ifdef T2K_MEM_LEAK_DBG
1494    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr - 4, size + 4, file, line);
1495 #else 
1496    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr - 4, size + 4);
1497 #endif
1498 #else
1499 #ifndef T2K_MEM_LEAK_DBG
1500    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size);
1501 #else
1502    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size, file, line);
1503 #endif
1504 #endif
1505 #endif /* SS_HISTOGRAM_SUPPORT */
1506 /* ss037.103 Removed the semaphore operation for performance enhancement */
1507
1508 #ifndef SS_PERF
1509    /* release the semaphore we took */
1510
1511 /* ss006.13: addition */
1512    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1513    {
1514 #if (ERRCLASS & ERRCLS_DEBUG)
1515       SSLOGERROR(ERRCLS_DEBUG, ESS228, ERRZERO,
1516                   "Could not release semaphore");
1517       return RFAILED;
1518 #endif
1519    }
1520 #endif
1521 /* ss036.103 - addition to handle double free and trampling return values
1522 * This might invoke SRegMemErrHdlr 
1523 */
1524 #ifdef SSI_DEBUG_LEVEL1
1525     /* handle the double free error here by calling the OS specific error handling function */
1526     if ((ret == RDBLFREE) || (ret == RTRAMPLINGNOK))
1527     {
1528        SRegMemErrHdlr( region,  ptr,  ret);
1529     }
1530 #endif /* SSI_DEBUG_LEVEL1 */
1531
1532    return (ret);
1533 }
1534
1535
1536 /*
1537 *
1538 *       Fun:   SInitMsg
1539 *
1540 *       Desc:  This function deallocates a message back and then
1541 *              reinitializes the message.
1542 *
1543 *       Ret:   ROK      - ok
1544 *              RFAILED  - failed, general (optional)
1545 *
1546 *       Notes: all data attached to message is returned to memory.
1547 *              message is set to empty. message is not returned to
1548 *              memory. return is ok.
1549 *
1550 *       File:  ss_msg.c
1551 *
1552 */
1553
1554 S16 SInitMsg(Buffer *mBuf)
1555 {
1556    SsMsgInfo *minfo;
1557    Buffer *tmp;
1558    uint8_t     tmpRegId;
1559
1560 #if (ERRCLASS & ERRCLS_INT_PAR)
1561    /* check message buffer */
1562    if (mBuf == NULLP)
1563    {
1564       SSLOGERROR(ERRCLS_INT_PAR, ESS062, ERRZERO, "SInitMsg: Null Buffer");
1565       return RFAILED;
1566    }
1567    if (mBuf->b_datap->db_type != SS_M_PROTO)
1568    {
1569       SSLOGERROR(ERRCLS_INT_PAR, ESS063, ERRZERO, "SInitMsg: Incorrect buffer\
1570                                                    type");
1571       return RFAILED;
1572    }
1573 #endif
1574
1575    /* get the message info */
1576 #ifdef YYYY
1577 #else
1578    CM_MEM_GET_REGION(tmpRegId);
1579    if(tmpRegId == 0xFF)
1580    {
1581       return RFAILED;
1582    }
1583 #endif
1584    minfo = (SsMsgInfo*) mBuf->b_rptr;
1585
1586    /* free all SS_M_DATA blks */
1587    while ((tmp = mBuf->b_cont))
1588    {
1589       mBuf->b_cont = tmp->b_cont;
1590 /* #ifdef YYYY */
1591 #if 1
1592 #ifdef T2K_MEM_LEAK_DBG
1593    char * file = __FILE__;
1594    uint32_t  line   = __LINE__;
1595 #endif
1596
1597       (Void) SPutDBuf(tmpRegId, minfo->pool, tmp);
1598 #else
1599       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
1600 #endif
1601    }
1602    /* initialise the length and endptr variables of mBuf */
1603
1604    minfo->len = 0;
1605    minfo->endptr = NULLP;
1606    minfo->next = NULLP;
1607
1608    return ROK;
1609 }
1610
1611 /* ss016.301 - Added a new API - SAddPreMsgMultInOrder. This preserves the
1612  * byte order while adding the data bytes to the beginning of the message.
1613  */
1614 /*
1615 *
1616 *       Fun:   SAddPreMsgMultInOrder
1617 *
1618 *       Desc:  This function copies consecutive bytes of data to the
1619 *              beginning of a message and keeps the bytes order preserved.
1620 *
1621 *       Ret:   ROK      - Appended the bytes to the beginning of the message.
1622 *              RFAILED  - Failed to append the bytes.
1623 *              ROUTRES  - Out of resources - Possibly insufficient memory.
1624 *
1625 *       Notes: If the message is empty,data is placed in the message. Message
1626 *              length is incremented. Return is ROK.
1627 *
1628 *              If the message is not empty,data is read by source pointer
1629 *              and appended at the beginning of the message.
1630 *              Message length is incremented. Return is ROK.
1631 *
1632 *       File:  ss_msg.c
1633 *
1634 */
1635 S16 SAddPreMsgMultInOrder
1636 (
1637 Data *src,
1638 MsgLen cnt,
1639 Buffer *mBuf
1640 )
1641 {
1642    SsMsgInfo *minfo;   /* Message info */
1643    Buffer *tmp;
1644    Buffer *curblk=NULLP;
1645    Buffer *newblk;
1646    Buffer *prevblk;
1647    MsgLen numBytes;  /* no. of bytes to be copied */
1648    MsgLen len;
1649    MsgLen offset;
1650    Data *rptr;
1651    Data *revSrc;
1652
1653 #if (ERRCLASS & ERRCLS_INT_PAR)
1654       /* check message buffer */
1655    if (mBuf == NULLP)
1656    {
1657       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SAddPreMsgMultInOrder:\
1658                                                    Null Buffer");
1659       return RFAILED;
1660    }
1661    /* check source */
1662    if (src == NULLP)
1663    {
1664       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SAddPreMsgMultInOrder:\
1665                                                    Null Buffer");
1666       return RFAILED;
1667    }
1668    /* check count */
1669    if (cnt <= 0)
1670    {
1671       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SAddPreMsgMultInOrder:\
1672                                                    Invalid count");
1673       return RFAILED;
1674    }
1675    if (mBuf->b_datap->db_type != SS_M_PROTO)
1676    {
1677       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SAddPreMsgMultInOrder:\
1678                                                    Incorrect buffer type");
1679       return RFAILED;
1680    }
1681 #endif
1682
1683    /* get the SsMsgInfo of mBuf */
1684    minfo = (SsMsgInfo*) mBuf->b_rptr;
1685 #ifdef RGL_SPECIFIC_CHANGES
1686    minfo->region = 0;
1687 #endif
1688
1689    /* store cnt in length */
1690    len = cnt;
1691    offset = 0;
1692    revSrc = NULL;
1693
1694    /* point to the end of the source buffer */
1695    revSrc = src + cnt ;
1696
1697    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
1698          (tmp->b_datap->db_base < tmp->b_rptr))
1699    {
1700       /* store the offset of the read pointer of tmp */
1701       offset = tmp->b_rptr - tmp->b_datap->db_base;
1702
1703       /* determine the number of bytes to copy */
1704       numBytes = MIN(cnt, offset);
1705
1706       /* traverse back from the end of the source buffer*/
1707       revSrc -= numBytes;
1708
1709       /* derement cnt */
1710       cnt -= numBytes;
1711
1712       /* move the read pointer towards the left */
1713       tmp->b_rptr -= numBytes ;
1714       SMemCpy((Void *)(tmp->b_rptr ), (Void *) revSrc, (size_t) numBytes);
1715
1716       if (!cnt)
1717       {
1718          minfo->len += len;
1719          return ROK;
1720       }
1721    }
1722    newblk = prevblk = NULLP;
1723    while (cnt)
1724    {
1725       /* allocate a message blk */
1726       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
1727       {
1728          while ((curblk = prevblk))
1729          {
1730             prevblk = prevblk->b_cont;
1731 #ifdef T2K_MEM_LEAK_DBG
1732    char * file = __FILE__;
1733    uint32_t  line   = __LINE__;
1734 #endif
1735
1736             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
1737          }
1738
1739          if (tmp)
1740          {
1741             tmp->b_rptr = tmp->b_datap->db_base + offset;
1742          }
1743
1744          return (ROUTRES);
1745       }
1746       /* attach curblk in the newblk chain */
1747       if (prevblk)
1748       { 
1749          curblk->b_cont = prevblk; /* stack them up */
1750       }
1751       else
1752       {
1753          newblk = curblk;
1754       }
1755       prevblk = curblk;
1756
1757       /* set the read and write pointers to the end of the data buffer */
1758       /* subsequent prepends have all the buffer to insert data into */
1759       curblk->b_wptr = curblk->b_datap->db_lim;
1760       rptr = curblk->b_datap->db_lim;
1761
1762       /* copy data */
1763       numBytes = MIN(cnt, (rptr - curblk->b_datap->db_base));
1764
1765       /* decrement cnt */
1766       cnt -= numBytes;
1767
1768       /* move the read pointer towards the left */
1769       rptr -= numBytes;
1770
1771       /* traverse back the source buffer */
1772       revSrc -= numBytes;
1773
1774       SMemCpy((Void *)(rptr), (Void *) revSrc, (size_t) numBytes);
1775
1776       curblk->b_rptr = rptr;
1777    }
1778
1779    /* attach the newblk chain into mBuf */
1780    newblk->b_cont = tmp;
1781    mBuf->b_cont = curblk;
1782
1783    /* update endptr of mBuf */
1784    if (!tmp)
1785    {
1786       minfo->endptr = newblk;
1787    }
1788    /* update length of message */
1789    minfo->len += len;
1790
1791    return ROK;
1792 }
1793 /*
1794 *
1795 *       Fun:   SAddPreMsg
1796 *
1797 *       Desc:  This function copies one byte of data to the
1798 *              beginning of a message.
1799 *
1800 *       Ret:   ROK      - ok
1801 *              RFAILED  - failed, general (optional)
1802 *              ROUTRES  - failed, out of resources (optional)
1803 *
1804 *       Notes: if message is empty: data is placed in the message. message
1805 *              length is incremented. return is ok.
1806 *
1807 *              if message is not empty: data is placed in front of all
1808 *              other data in message. message length is incremented.
1809 *              return is ok.
1810 *
1811 *       File:  ss_msg.c
1812 *
1813 */
1814
1815  
1816 S16 SAddPreMsg
1817 (
1818 Data data,
1819 Buffer *mBuf
1820 )
1821 {
1822    SsMsgInfo *minfo;
1823    Buffer *tmp;
1824    Buffer *newb;
1825
1826 #if (ERRCLASS & ERRCLS_INT_PAR)
1827    /* check message buffer */
1828    if (!mBuf)
1829    {
1830       SSLOGERROR(ERRCLS_INT_PAR, ESS064, ERRZERO, "SAddPreMsg: Null Buffer");
1831       return RFAILED;
1832    }
1833    if (mBuf->b_datap->db_type != SS_M_PROTO)
1834    {
1835       SSLOGERROR(ERRCLS_INT_PAR, ESS065, ERRZERO, "SAddPreMsg: Incorrect\
1836                                                    buffer type");
1837       return RFAILED; 
1838    }
1839 #endif
1840
1841    minfo = (SsMsgInfo *) mBuf->b_rptr;
1842
1843    /* ss021.103 - Addition to check if exceeding maximum message length */
1844 #ifdef LONG_MSG
1845    if (minfo->len == 0x7FFFFFFF)
1846 #else
1847    if (minfo->len == 0x7FFF)
1848 #endif
1849    {
1850       return (ROUTRES);
1851    }
1852       
1853    /*
1854     * allocate a message blk using SGetDBuf(), if there are no data blks in the
1855     * message, mBuf, or if the reference count of the first data blk is greater
1856     * than 1, or if there is no space to append databytes in front of the read
1857     * pointer of the first data blk
1858    */
1859    if (!(tmp = mBuf->b_cont) || (tmp->b_datap->db_ref > 1) ||
1860         (tmp->b_rptr == tmp->b_datap->db_base))
1861    {
1862       if (SGetDBuf(minfo->region, minfo->pool, &newb) != ROK)
1863       {
1864          SSLOGERROR(ERRCLS_DEBUG, ESS066, ERRZERO, "SAddPreMsg:Failed in\
1865                     SGetDBuf");
1866          return (ROUTRES);
1867       }
1868       /* set the read and write pointers to end of data buffer */
1869       /* subsequent prepends have all the buffer to insert data into */
1870       newb->b_wptr = newb->b_datap->db_lim;
1871       newb->b_rptr = newb->b_datap->db_lim;
1872
1873       /* insert newb before tmp */
1874       newb -> b_cont = tmp;
1875       mBuf->b_cont = newb;
1876
1877       /* if endptr of mBuf is NULLP, set it to newb */
1878       if (tmp == NULLP)
1879          minfo->endptr = newb;
1880       tmp = newb;
1881    }
1882    /* insert data, increment length */
1883    *--tmp->b_rptr = data;
1884    minfo->len++;
1885
1886    return ROK;
1887 }
1888
1889 /*
1890 *
1891 *       Fun:   SAddPstMsg
1892 *
1893 *       Desc:  This function copies one byte of data to the
1894 *              end of a message.
1895 *
1896 *       Ret:   ROK      - ok
1897 *              RFAILED  - failed, general (optional)
1898 *              ROUTRES  - failed, out of resources (optional)
1899 *
1900 *       Notes: if message is empty: data is placed in the message. message
1901 *              length is incremented. return is ok.
1902 *
1903 *              if message is not empty: data is placed in back of all
1904 *              other data in message. message length is incremented.
1905 *              return is ok.
1906 *
1907 *       File:  ss_msg.c
1908 *
1909 */
1910
1911  
1912 S16 SAddPstMsg
1913 (
1914 Data data,
1915 Buffer *mBuf
1916 )
1917 {
1918    SsMsgInfo *minfo;
1919    Buffer *tmp;
1920    Buffer *newb;
1921
1922 #if (ERRCLASS & ERRCLS_INT_PAR)
1923    /* check message buffer */
1924    if (!mBuf)
1925    {
1926       SSLOGERROR(ERRCLS_INT_PAR, ESS067, ERRZERO, "SAddPstMsg: Null Buffer");
1927       return RFAILED;
1928    }
1929    if (mBuf->b_datap->db_type != SS_M_PROTO)
1930    {
1931       SSLOGERROR(ERRCLS_INT_PAR, ESS068, ERRZERO, "SAddPstMsg: Incorrect\
1932                                                    buffer type");
1933       return RFAILED;
1934    }
1935 #endif
1936
1937    /* get the message info */
1938    minfo = (SsMsgInfo *) mBuf->b_rptr;
1939  
1940    if (!(tmp = minfo->endptr) || (tmp->b_datap->db_ref > 1) ||
1941         (tmp->b_wptr == tmp->b_datap->db_lim))
1942    {
1943       if (SGetDBuf(minfo->region, minfo->pool, &newb) != ROK)
1944       {
1945          SSLOGERROR(ERRCLS_DEBUG, ESS069, ERRZERO, "SAddPstMsg: Failed in\
1946                                                     SGetDBuf()");
1947          return (ROUTRES);
1948       }
1949
1950       /* append newb to the end of the mBuf chain */
1951       if (tmp)
1952          tmp->b_cont = newb;
1953       else
1954          mBuf->b_cont = newb;
1955
1956       /* set the endptr of mBuf to newb */
1957       minfo->endptr = newb;
1958
1959       tmp = newb;
1960    }
1961    /* insert data, increment length */
1962    *tmp->b_wptr++ = data;
1963    minfo->len++;
1964
1965    return ROK;
1966 }
1967
1968 /*
1969 *
1970 *       Fun:   SAddPreMsgMult
1971 *
1972 *       Desc:  This function copies consecutive bytes of data to the
1973 *              beginning of a message.
1974 *
1975 *       Ret:   ROK      - ok
1976 *              RFAILED  - failed, general (optional)
1977 *              ROUTRES  - failed, out of resources (optional)
1978 *
1979 *       Notes: if message is empty: data is placed in the message. message
1980 *              length is incremented. return is ok.
1981 *
1982 *              if message is not empty: data is read by source pointer,
1983 *              data is placed in front of all other data in message.
1984 *              message length is incremented. return is ok.
1985 *
1986 *              the first byte of data pointed to by the source pointer will
1987 *              be placed at the front of the message first, the last byte of
1988 *              data pointed to by the source pointer will be placed at the
1989 *              front of the message last (i.e. it will become the first
1990 *              byte of the message).
1991 *
1992 *       File:  ss_msg.c
1993 *
1994 */
1995  
1996 #ifdef T2K_MEM_LEAK_DBG
1997 S16 SAddPreMsgMult1
1998 (
1999 Data *src,
2000 MsgLen cnt,
2001 Buffer *mBuf,
2002 char   *file,
2003 uint32_t    line
2004 )
2005
2006 #else
2007 S16 SAddPreMsgMult
2008 (
2009 Data *src,
2010 MsgLen cnt,
2011 Buffer *mBuf
2012 )
2013 #endif
2014 {
2015    SsMsgInfo *minfo;   /* Message info */
2016    Buffer *tmp;
2017    Buffer *curblk=NULLP;
2018    Buffer *newblk;
2019    Buffer *prevblk;
2020    MsgLen numBytes;  /* no. of bytes to be copied */
2021    MsgLen len;
2022    MsgLen offset;
2023    Data *rptr;
2024
2025 #if (ERRCLASS & ERRCLS_INT_PAR)
2026    /* check message buffer */
2027    if (mBuf == NULLP)
2028    {
2029       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SAddPreMsgMult:Null Buffer");
2030       return RFAILED;
2031    }
2032    /* check source */
2033    if (src == NULLP)
2034    {
2035       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SAddPreMsgMult:Null Buffer");
2036       return RFAILED;
2037    }
2038    /* check count */
2039    if (cnt <= 0)
2040    {
2041       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SAddPreMsgMult: Invalid\
2042                                                    count");
2043       return RFAILED;
2044    }
2045    if (mBuf->b_datap->db_type != SS_M_PROTO)
2046    {
2047       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SAddPreMsgMult: Incorrect\
2048                                                    buffer type");
2049       return RFAILED;
2050    }
2051 #endif
2052
2053    /* get the SsMsgInfo of mBuf */
2054    minfo = (SsMsgInfo*) mBuf->b_rptr;
2055
2056    /* store cnt in length */
2057    len = cnt;
2058 /* ss014.13: Addition */
2059    offset = 0;
2060
2061    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
2062        (tmp->b_datap->db_base < tmp->b_rptr))
2063    {
2064       /* store the offset of the read pointer of tmp */
2065       offset = tmp->b_rptr - tmp->b_datap->db_base;
2066
2067       /* determine the number of bytes to copy */
2068       numBytes = MIN(cnt, offset);
2069
2070       /* decrement cnt */
2071       cnt -= numBytes;
2072
2073       /* copy data */
2074       while (numBytes--)
2075          *--tmp->b_rptr = *src++;
2076       if (!cnt)
2077       {
2078          minfo->len += len;
2079          return ROK;
2080       }
2081    }
2082    newblk = prevblk = NULLP;
2083    while (cnt)
2084    {
2085       /* allocate a message blk */
2086       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
2087       {
2088          while ((curblk = prevblk))
2089          {
2090             prevblk = prevblk->b_cont;
2091             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
2092          }
2093
2094          if (tmp)
2095          {
2096             tmp->b_rptr = tmp->b_datap->db_base + offset;
2097          }
2098
2099          return (ROUTRES);
2100       }
2101       /* attach curblk in the newblk chain */
2102       if (prevblk)
2103          curblk->b_cont = prevblk; /* stack them up */
2104       else
2105          newblk = curblk;
2106       prevblk = curblk;
2107
2108       /* set the read and write pointers to the end of the data buffer */
2109       /* subsequent prepends have all the buffer to insert data into */
2110       curblk->b_wptr = curblk->b_datap->db_lim;
2111       rptr = curblk->b_datap->db_lim;
2112
2113       /* copy data */
2114       numBytes = MIN(cnt, (rptr - curblk->b_datap->db_base));
2115
2116       /* decrement cnt */
2117       cnt -= numBytes;
2118
2119       while (numBytes--)
2120          *--rptr = *src++;
2121       curblk->b_rptr = rptr;
2122    }
2123
2124    /* attach the newblk chain into mBuf */
2125    newblk -> b_cont = tmp;
2126    mBuf -> b_cont = curblk;
2127
2128    /* update endptr of mBuf */
2129    if (!tmp)
2130       minfo -> endptr = newblk;
2131
2132    /* update length of message */
2133    minfo->len += len;
2134
2135    return ROK;
2136 }
2137
2138 /*
2139 *
2140 *       Fun:   SAddPstMsgMult
2141 *
2142 *       Desc:  This function copies consecutive bytes of data to the
2143 *              end of a message.
2144 *
2145 *       Ret:   ROK      - ok
2146 *              RFAILED  - failed, general (optional)
2147 *              ROUTRES  - failed, out of resources (optional)
2148 *
2149 *       Notes: if message is empty: data is placed in the message. message
2150 *              length is incremented. return is ok.
2151 *
2152 *              if message is not empty: data is read by source pointer,
2153 *              data is placed in back of all other data in message.
2154 *              message length is incremented. return is ok.
2155 *
2156 *              the first byte of data pointed to by the source pointer will
2157 *              be placed at the back of the message first, the last byte of
2158 *              data pointed to by the source pointer will be placed at the
2159 *              back of the message last (i.e. it will become the last
2160 *              byte of the message).
2161 *
2162 *       File:  ss_msg.c
2163 *
2164 */
2165
2166 #ifdef T2K_MEM_LEAK_DBG
2167 S16 SAddPstMsgMult1
2168 (
2169 Data *src,
2170 MsgLen cnt,
2171 Buffer *mBuf,
2172 char   *file,
2173 uint32_t    line
2174 )
2175 #else
2176 S16 SAddPstMsgMult
2177 (
2178 Data *src,
2179 MsgLen cnt,
2180 Buffer *mBuf
2181 )
2182 #endif
2183 {
2184    SsMsgInfo *minfo;
2185    Buffer *tmp;
2186    Buffer *newblk;
2187    Buffer *curblk=NULLP;
2188    Buffer *prevblk;
2189    MsgLen len;
2190    MsgLen offset;
2191    MsgLen numBytes;
2192    Data *wptr;
2193
2194 #if (ERRCLASS & ERRCLS_INT_PAR)
2195    /* check message buffer */
2196    if (mBuf == NULLP)
2197    {
2198       SSLOGERROR(ERRCLS_INT_PAR, ESS074, ERRZERO, "SAddPstMsgMult:Null Buffer");
2199       return RFAILED;
2200    }
2201    /* check source */
2202    if (src == NULLP)
2203    {
2204       SSLOGERROR(ERRCLS_INT_PAR, ESS075, ERRZERO, "SAddPstMsgMult:Null Buffer");
2205       return RFAILED;
2206    }
2207    /* check count */
2208    if (cnt <= 0)
2209    {
2210       SSLOGERROR(ERRCLS_INT_PAR, ESS076, ERRZERO, "SAddPstMsgMult:Invalid\
2211                                                    count");
2212       return RFAILED;
2213    }
2214    if (mBuf->b_datap->db_type != SS_M_PROTO)
2215    {
2216       SSLOGERROR(ERRCLS_INT_PAR, ESS077, ERRZERO, "SAddPstMsgMult: Incorrect\
2217                                                    buffer type");
2218       return RFAILED;
2219    }
2220 #endif
2221  
2222    /* get the SsMsgInfo of mBuf */
2223    minfo = (SsMsgInfo*) mBuf->b_rptr;
2224
2225    /* store cnt in len */
2226    len = cnt;
2227
2228 /* ss014.13: Addition */
2229    offset = 0;
2230
2231    if ((tmp = minfo->endptr) && (tmp->b_datap->db_ref == 1) &&
2232        (tmp->b_datap->db_lim > tmp->b_wptr))
2233    {
2234       /* store offset of the write pointer */
2235       /* incase subsequent allocations fail, offset is read reset to original */
2236       offset = tmp->b_datap->db_lim - tmp->b_wptr;
2237
2238       /* determine the number of bytes to copy */
2239       numBytes = MIN(cnt, offset);
2240
2241       /* decrement cnt */
2242       cnt -= numBytes;
2243
2244
2245 /* ss002.13: addition */
2246
2247   /* ss004.13: addition */
2248       SMemCpy((Void *) tmp->b_wptr, (Void *) src, (size_t) numBytes);
2249
2250       tmp->b_wptr += numBytes;
2251       src += numBytes;
2252
2253       if (!cnt)
2254       {
2255          minfo->len += len;
2256          return ROK;
2257       }
2258    }
2259
2260    newblk = prevblk = NULLP;
2261
2262    while (cnt)
2263    {
2264       /* allocate a message blk */
2265       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
2266       {
2267          while ((curblk = newblk))
2268          {
2269             newblk = newblk->b_cont;
2270             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
2271          }
2272
2273          if (tmp)
2274          {
2275             tmp->b_wptr = tmp->b_datap->db_lim - offset;
2276          }
2277
2278          return (ROUTRES);
2279       }
2280
2281       /* insert curblk in the newblk chain */
2282       if (prevblk)
2283          prevblk->b_cont = curblk; /* stack them down */
2284       else
2285          newblk = curblk;
2286       prevblk = curblk;
2287
2288       /* copy data */
2289       wptr = curblk->b_wptr;
2290       numBytes = MIN(cnt, curblk->b_datap->db_lim - wptr);
2291
2292       /* decrement cnt */
2293       cnt -= numBytes;
2294
2295
2296 /* ss002.13: addition */
2297 /* ss003.13: addition */
2298       SMemCpy( (Void *) wptr, (Void *) src, (size_t) numBytes);
2299
2300       src = src + numBytes;
2301
2302       curblk->b_wptr +=  numBytes;  
2303  }
2304    /* insert newblk chain into mBuf */
2305    if (tmp)
2306       tmp->b_cont = newblk;
2307    else
2308       mBuf->b_cont = newblk;
2309    minfo->endptr = curblk;
2310
2311    /* update length */
2312    minfo->len += len;
2313
2314    return ROK;
2315 }
2316
2317 /* #ifdef SS_LOCKLESS_MEMORY */
2318
2319
2320
2321 /*
2322 *
2323 *       Fun:   SRemPreMsg
2324 *
2325 *       Desc:  This function copies and then removes one byte of
2326 *              data from the beginning of a message.
2327 *
2328 *       Ret:   ROK      - ok
2329 *              ROKDNA   - ok, data not available
2330 *              RFAILED  - failed, general (optional)
2331 *
2332 *       Notes: if message is empty: message is unchanged. return is ok,
2333 *              data not available.
2334 *
2335 *              if message is not empty: data is removed from front of
2336 *              message, data is returned via pointer to data. message
2337 *              length is decremented. return is ok.
2338 *
2339 *       File:  ss_msg.c
2340 *
2341 */
2342
2343  
2344 S16 SRemPreMsg
2345 (
2346 Data *dataPtr,
2347 Buffer *mBuf
2348 )
2349 {
2350    SsMsgInfo *minfo;
2351    Buffer *tmp;
2352 #ifdef T2K_MEM_LEAK_DBG
2353    char* file = __FILE__;
2354    uint32_t line = __LINE__;
2355 #endif
2356
2357 #if (ERRCLASS & ERRCLS_INT_PAR)
2358    /* check data pointer */
2359    if (!dataPtr)
2360    {
2361       SSLOGERROR(ERRCLS_INT_PAR, ESS078, ERRZERO, "SRemPreMsg : Null Buffer");
2362       return RFAILED;
2363    }
2364    /* check message buffer */
2365    if (!mBuf)
2366    {
2367       SSLOGERROR(ERRCLS_INT_PAR, ESS079, ERRZERO, "SRemPreMsg : Null Buffer");
2368       return RFAILED;
2369    }
2370    if (mBuf->b_datap->db_type != SS_M_PROTO)
2371    {
2372       SSLOGERROR(ERRCLS_INT_PAR, ESS080, ERRZERO, "SRemPreMsg : Incorrect\
2373                                                    buffer type");
2374       return RFAILED;
2375    }
2376 #endif
2377
2378    if (!(tmp = mBuf->b_cont))
2379       return (ROKDNA);
2380
2381    /* get SsMsgInfo of mBuf */
2382    minfo = (SsMsgInfo *) mBuf->b_rptr;
2383
2384    /* read databyte into dataPtr and incrment read ptr */
2385    *dataPtr = *tmp->b_rptr++;
2386
2387    /* if all data is exhausted, release the blk */
2388    if (tmp->b_rptr == tmp->b_wptr)
2389    {
2390       mBuf->b_cont = tmp->b_cont;
2391       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2392    }
2393    /* update SsMsgInfo */
2394    if (!--minfo->len)
2395       minfo->endptr = NULLP;
2396
2397    return ROK;
2398 }
2399
2400 /*
2401 *
2402 *       Fun:   SRemPstMsg
2403 *
2404 *       Desc:  This function copies and then removes one byte of
2405 *              data from the end of a message.
2406 *
2407 *       Ret:   ROK      - ok
2408 *              ROKDNA   - ok, data not available
2409 *              RFAILED  - failed, general (optional)
2410 *
2411 *       Notes: if message is empty: message is unchanged. return is ok,
2412 *              data not available.
2413 *
2414 *              if message is not empty: data is removed from back of
2415 *              message, data is returned via pointer to data. message
2416 *              length is decremented. return is ok.
2417 *
2418 *       File:  ss_msg.c
2419 *
2420 */
2421
2422
2423 S16 SRemPstMsg
2424 (
2425 Data *dataPtr,              /* pointer to data */
2426 Buffer *mBuf
2427 )
2428 {
2429    SsMsgInfo *minfo;
2430    Buffer *tmp;
2431    Buffer *last;
2432 #ifdef T2K_MEM_LEAK_DBG
2433    char* file = __FILE__;
2434    uint32_t line = __LINE__;
2435 #endif
2436
2437 #if (ERRCLASS & ERRCLS_INT_PAR)
2438    /* check data pointer */
2439    if (dataPtr == NULLP)
2440    {
2441       SSLOGERROR(ERRCLS_INT_PAR, ESS081, ERRZERO, "SRemPstMsg : Null Buffer");
2442       return RFAILED;
2443    }
2444    /* check message buffer */
2445    if (mBuf == NULLP)
2446    {
2447       SSLOGERROR(ERRCLS_INT_PAR, ESS082, ERRZERO, "SRemPstMsg : Null Buffer");
2448       return RFAILED;
2449    }
2450    if (mBuf->b_datap->db_type != SS_M_PROTO)
2451    {
2452       SSLOGERROR(ERRCLS_INT_PAR, ESS083, ERRZERO, "SRemPstMsg : Incorrect\
2453                                                    buffer type");
2454       return RFAILED;
2455    }
2456 #endif
2457
2458    /* get the SsMsgInfo */
2459    minfo = (SsMsgInfo*) mBuf->b_rptr;
2460
2461    if (!(last = minfo->endptr))
2462       return (ROKDNA);
2463
2464    /* read databyte into dataPtr and decrement write ptr */
2465    *dataPtr = *--last->b_wptr;
2466
2467    /* if all data is exhausted, release the blk */
2468    if (last->b_rptr == last->b_wptr)
2469    {
2470       for (tmp = mBuf; tmp->b_cont != last;)
2471          tmp = tmp->b_cont;
2472       tmp->b_cont = NULLP;
2473       (Void) SPutDBuf(minfo->region, minfo->pool, last);
2474
2475       /* update endptr */
2476       if (mBuf->b_cont)
2477          minfo->endptr = tmp;
2478       else
2479          minfo->endptr = NULLP;
2480    }
2481    /* update SsMsgInfo */
2482    minfo->len--;
2483
2484    return ROK;
2485 }
2486
2487
2488 /*
2489 *
2490 *       Fun:   SRemPreMsgMult
2491 *
2492 *       Desc:  This function copies and then removes consecutive bytes of
2493 *              data from the beginning of a message.
2494 *
2495 *       Ret:   ROK      - ok
2496 *              ROKDNA   - ok, data not available
2497 *              RFAILED  - failed, general (optional)
2498 *
2499 *       Notes: if message is empty: message is unchanged. return is ok,
2500 *              data not available.
2501 *
2502 *              if the destination buffer is NULL, data is not copied.
2503 *
2504 *              if message is not empty: data is removed from front of
2505 *              message, data is returned by destination pointer. message
2506 *              length is decremented. return is ok.
2507 *
2508 *              the first byte of data read from the message will be placed
2509 *              in the destination buffer first (i.e. this was the first byte
2510 *              of the message), the last byte of data read from the message
2511 *              will be placed in the destination buffer last.
2512 *
2513 *       File:  ss_msg.c
2514 *
2515 */
2516
2517 S16 SRemPreMsgMult
2518 (
2519 Data *dst,                  /* destination */
2520 MsgLen cnt,                 /* count */
2521 Buffer *mBuf
2522 )
2523 {
2524    SsMsgInfo *minfo = NULLP;
2525    Buffer *tmp = NULLP;
2526    MsgLen numBytes =0;
2527 #ifdef T2K_MEM_LEAK_DBG
2528    char* file = __FILE__;
2529    uint32_t line = __LINE__;
2530 #endif
2531
2532
2533    /* ss023.103 - Modification of SRemPreMsgMult for bug fix */
2534
2535 #if (ERRCLASS & ERRCLS_INT_PAR)
2536    /* check count */
2537    if (cnt <= 0)
2538    {
2539       SSLOGERROR(ERRCLS_INT_PAR, ESS084, ERRZERO, "SRemPreMsgMult:Invalid\
2540                                                    count");
2541       return RFAILED;
2542    }
2543    /* check message buffer */
2544    if (!mBuf)
2545    {
2546       SSLOGERROR(ERRCLS_INT_PAR, ESS085, ERRZERO, "SRemPreMsgMult:Null Buffer");
2547       return RFAILED;
2548    }
2549    /* ss021.103 - Addition to check for NULL pointer */
2550    /* check data pointer */
2551    /* ss022.103 - Removed check for NULL pointer */
2552    if (mBuf->b_datap->db_type != SS_M_PROTO)
2553    {
2554       SSLOGERROR(ERRCLS_INT_PAR, ESS086, ERRZERO, "SRemPreMsgMult : Incorrect\
2555                                                    buffer type");
2556       return RFAILED;
2557    }
2558 #endif
2559
2560    /* get the SsMsgInfo */
2561    minfo = (SsMsgInfo*) mBuf->b_rptr;
2562
2563    /* check if data present */
2564    if (minfo->len < cnt)
2565       return (ROKDNA);
2566    else
2567       minfo->len -= cnt;
2568
2569    while (cnt)
2570    {
2571       /* get the first SS_M_DATA blk */
2572       tmp = mBuf->b_cont;
2573
2574       /* determine the number of bytes to be copy */
2575       numBytes = MIN(cnt, tmp->b_wptr - tmp->b_rptr);
2576
2577       /* decrement cnt */
2578       cnt -= numBytes;
2579
2580       /* move data into dst */
2581       if (dst != NULLP)
2582       {
2583
2584         SMemCpy( (Void *) dst, (Void *) tmp->b_rptr,  (size_t) numBytes);
2585         dst += numBytes;
2586       }
2587       
2588       tmp->b_rptr += numBytes;
2589
2590       if (tmp->b_rptr == tmp->b_wptr)
2591       {
2592          mBuf->b_cont = tmp->b_cont;
2593         (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2594       }
2595    }
2596    /* update SsMsgInfo */
2597    if (!minfo->len)
2598       minfo->endptr = NULLP;
2599
2600    return ROK;
2601 }
2602
2603 /*
2604 *
2605 *       Fun:   SRemPstMsgMult
2606 *
2607 *       Desc:  This function copies and then removes consecutive bytes of
2608 *              data from the end of a message.
2609 *
2610 *       Ret:   ROK      - ok
2611 *              ROKDNA   - ok, data not available
2612 *              RFAILED  - failed, general (optional)
2613 *
2614 *       Notes: if message is empty: message is unchanged. return is ok,
2615 *              data not available.
2616 *
2617 *              if the destination buffer is NULL, data is not copied.
2618 *
2619 *              if message is not empty: data is removed from front of
2620 *              message, data is returned by destination pointer. message
2621 *              length is decremented. return is ok.
2622 *
2623 *              the first byte of data read from the message will be placed
2624 *              in the destination buffer first (i.e. this was the last byte
2625 *              of the message), the last byte of data read from the message
2626 *              will be placed in the destination buffer last.
2627 *
2628 *       File:  ss_msg.c
2629 *
2630 */
2631
2632 S16 SRemPstMsgMult
2633 (
2634 Data *dst,                  /* destination */
2635 MsgLen cnt,                 /* count */
2636 Buffer *mBuf
2637 )
2638 {
2639    SsMsgInfo *minfo;
2640    Buffer *tmp;
2641    Buffer *prev;
2642    MsgLen count;
2643    MsgLen numBytes;
2644    Data *cptr;
2645 #ifdef T2K_MEM_LEAK_DBG
2646    char* file = __FILE__;
2647    uint32_t line = __LINE__;
2648 #endif
2649
2650
2651 #if (ERRCLASS & ERRCLS_INT_PAR)
2652    /* check count */
2653    if (cnt <= 0)
2654    {
2655       SSLOGERROR(ERRCLS_INT_PAR, ESS087, ERRZERO, "SRemPstMsgMult:Invalid\
2656                                                    count");
2657       return RFAILED;
2658    }
2659    /* check message buffer */
2660    if (mBuf == NULLP)
2661    {
2662       SSLOGERROR(ERRCLS_INT_PAR, ESS088, ERRZERO, "SRemPstMsgMult:Null Buffer");
2663       return RFAILED;
2664    }
2665    if (mBuf->b_datap->db_type != SS_M_PROTO)
2666    {
2667       SSLOGERROR(ERRCLS_INT_PAR, ESS089, ERRZERO, "SRemPstMsgMult : Incorrect\
2668                                                    buffer type");
2669       return RFAILED;
2670    }
2671    /* ss021.103 - Addition to check dst data pointer */
2672    /* check data pointer */
2673    /* ss022.103 - Removed check for NULL destination pointer */
2674 #endif
2675  
2676    /* get the SsMsgInfo */
2677    minfo = (SsMsgInfo*) mBuf->b_rptr;
2678  
2679    /* check if data present */
2680    if (minfo->len < cnt)
2681       return (ROKDNA);
2682    else
2683    {
2684       minfo->len -= cnt;
2685       count = minfo->len;
2686       prev = mBuf;
2687       tmp = mBuf->b_cont;
2688    }
2689
2690    /* determine blk containing offset, and prev node */
2691    FIND_OFFSET_AND_PREV(prev, tmp, count)
2692
2693    if (dst != NULLP)
2694       dst += cnt;
2695
2696    while (cnt)
2697    {
2698       numBytes = MIN(cnt, (tmp->b_wptr - tmp->b_rptr - count));
2699
2700       tmp->b_wptr -= numBytes;
2701
2702       cnt -= numBytes;
2703
2704       /* copy data */
2705       cptr = tmp->b_wptr;
2706       if (dst != NULLP)
2707       {
2708          while (numBytes--)
2709             *--dst = *cptr++;
2710       }
2711
2712       if (tmp->b_rptr == tmp->b_wptr)
2713       {
2714          prev->b_cont = tmp->b_cont;
2715          (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2716          tmp = prev;
2717       }
2718       prev = tmp;
2719       tmp = tmp->b_cont;
2720
2721       count = 0;
2722    }
2723    if (mBuf == prev)
2724       minfo->endptr = NULLP;
2725    else
2726       minfo->endptr = prev;
2727
2728    return ROK;
2729 }
2730
2731 /*
2732 *
2733 *       Fun:   SExamMsg
2734 *
2735 *       Desc:  This function copies one byte of data from a message
2736 *              without modifying the message.
2737 *
2738 *       Ret:   ROK      - ok
2739 *              ROKDNA   - ok, data not available
2740 *              RFAILED  - failed, general (optional)
2741 *
2742 *       Notes: index is 0 based and indicates location in message
2743 *
2744 *              if index is less than the length of the message:
2745 *              message is unchanged and data is examined at specified
2746 *              index and returned via pointer to data. message length
2747 *              is unchanged. return is ok.
2748 *
2749 *              if index is greater than or equal to
2750 *              the length of the message: message is unchanged and 0
2751 *              is returned via pointer to data. return is ok, data
2752 *              not available.
2753 *
2754 *       File:  ss_msg.c
2755 *
2756 */
2757
2758
2759 S16 SExamMsg
2760 (
2761 Data *dataPtr,              /* pointer to data */
2762 Buffer *mBuf,               /* message buffer */
2763 MsgLen idx
2764 )
2765 {
2766    SsMsgInfo *minfo;
2767    Buffer *tmp;
2768
2769 #if (ERRCLASS & ERRCLS_INT_PAR)
2770    /* check data pointer */
2771    if (!dataPtr)
2772    {
2773       SSLOGERROR(ERRCLS_INT_PAR, ESS090, ERRZERO, "SExamMsg : Null Buffer");
2774       return RFAILED;
2775    }
2776    /* check message buffer */
2777    if (!mBuf)
2778    {
2779       SSLOGERROR(ERRCLS_INT_PAR, ESS091, ERRZERO, "SExamMsg : Null Buffer");
2780       return RFAILED;
2781    }
2782    /* check index */
2783    if (idx < 0)
2784    {
2785       SSLOGERROR(ERRCLS_INT_PAR, ESS092, ERRZERO, "SExamMsg : Invalid Index");
2786       return RFAILED;
2787    }
2788    if (mBuf->b_datap->db_type != SS_M_PROTO)
2789    {
2790       SSLOGERROR(ERRCLS_INT_PAR, ESS093, ERRZERO, "SExamMsg : Incorrect buffer\
2791                                                    type");
2792       return RFAILED;
2793    }
2794 #endif
2795  
2796    /* get the SsMsgInfo */
2797    minfo = (SsMsgInfo*) mBuf->b_rptr;
2798
2799    if (minfo->len <= idx)
2800    {
2801       return (ROKDNA);
2802    }
2803
2804    /* get the first SS_M_DATA blk */
2805    tmp = mBuf->b_cont;
2806
2807    /* determine offset */
2808    FIND_OFFSET(tmp, idx)
2809
2810    *dataPtr = *(tmp->b_rptr + idx);
2811
2812    return ROK;
2813 }
2814
2815
2816 /* s002.301 */
2817 /*
2818 *
2819 *       Fun:   SGetDataFrmMsg
2820 *
2821 *       Desc:  This function copies requested byte of data from a message
2822 *              without modifying the message.
2823 *
2824 *       Ret:   ROK      - ok
2825 *              ROKDNA   - ok, data not available
2826 *              RFAILED  - failed, general (optional)
2827 *
2828 *       Notes: index is 0 based and indicates location in message
2829 *
2830 *              if index is less than the length of the message:
2831 *              message is unchanged and data is examined at specified
2832 *              index and returned via pointer to data. message length
2833 *              is unchanged. return is ok.
2834 *
2835 *              if index is greater than or equal to
2836 *              the length of the message: message is unchanged and 0
2837 *              is returned via pointer to data. return is ok, data
2838 *              not available.
2839 *
2840 *       File:  sm_msg.c
2841 *
2842 */
2843
2844
2845 S16 SGetDataFrmMsg 
2846 (
2847 Buffer *mBuf,               /* message buffer */
2848 Data *dataPtr,              /* pointer to data */
2849 MsgLen idx,
2850 MsgLen dataLen
2851 )
2852 {
2853    SsMsgInfo *minfo;
2854    Buffer *tmp;
2855         MsgLen offSetLen;
2856         Data   *tmpDataPtr = dataPtr;
2857
2858 #if (ERRCLASS & ERRCLS_INT_PAR)
2859    /* check data pointer */
2860    if (!dataPtr)
2861    {
2862       SSLOGERROR(ERRCLS_INT_PAR, ESS090, ERRZERO, "SGetDataFrmMsg: Null Buffer");
2863       return RFAILED;
2864    }
2865    /* check message buffer */
2866    if (!mBuf)
2867    {
2868       SSLOGERROR(ERRCLS_INT_PAR, ESS091, ERRZERO, "SGetDataFrmMsg: Null Buffer");
2869       return RFAILED;
2870    }
2871    /* check index */
2872    if (idx < 0)
2873    {
2874       SSLOGERROR(ERRCLS_INT_PAR, ESS092, ERRZERO, "SGetDataFrmMsg: Invalid Index");
2875       return RFAILED;
2876    }
2877    if (mBuf->b_datap->db_type != SS_M_PROTO)
2878    {
2879       SSLOGERROR(ERRCLS_INT_PAR, ESS093, ERRZERO, "SGetDataFrmMsg: Incorrect buffer\
2880                                                    type");
2881       return RFAILED;
2882    }
2883 #endif
2884  
2885    /* get the SsMsgInfo */
2886    minfo = (SsMsgInfo*) mBuf->b_rptr;
2887
2888    if (minfo->len <= (idx + dataLen) )
2889    {
2890       return (ROKDNA);
2891    }
2892
2893    /* get the first SS_M_DATA blk */
2894    tmp = mBuf->b_cont;
2895
2896    /* determine offset */
2897    if(tmp == NULLP)
2898         {
2899                 return (ROKDNA);        
2900         }
2901         else
2902         {
2903           FIND_OFFSET(tmp, idx)
2904                  offSetLen = ((tmp->b_wptr - (tmp->b_rptr + idx)));
2905
2906           for(;(offSetLen < dataLen && tmp != NULLP);)
2907           {
2908                  SMemCpy((Void *)tmpDataPtr, (Void *)(tmp->b_rptr + idx), (size_t)offSetLen);
2909                  dataLen = dataLen - offSetLen;
2910                  tmp = tmp->b_cont;
2911                  idx = 0;
2912                  tmpDataPtr = tmpDataPtr + offSetLen;
2913                  offSetLen = tmp->b_wptr - tmp->b_rptr;
2914           }
2915           if( tmp == NULLP )
2916           {
2917             return (ROKDNA);    
2918           }
2919      SMemCpy((Void *)tmpDataPtr, (Void *)(tmp->b_rptr + idx), (size_t)dataLen);
2920         }
2921
2922    return ROK;
2923 } /* End of SGetDataFrmMsg() */
2924
2925 /*
2926 *
2927 *       Fun:   SFndLenMsg
2928 *
2929 *       Desc:  This function determines the length of data within
2930 *              a message.
2931 *
2932 *       Ret:   ROK      - ok
2933 *              RFAILED  - failed, general (optional)
2934 *
2935 *       Notes: length of message is determined, message is unchanged
2936 *              and length is returned via pointer to length. return is ok.
2937 *
2938 *       File:  ss_msg.c
2939 *
2940 */
2941
2942
2943 S16 SFndLenMsg
2944 (
2945 REG1 Buffer *mBuf,          /* message buffer */
2946 MsgLen *lngPtr
2947 )
2948 {
2949    SsMsgInfo *minfo;
2950
2951 #if (ERRCLASS & ERRCLS_INT_PAR)
2952    /* check message buffer */
2953    if (mBuf == NULLP)
2954    {
2955       SSLOGERROR(ERRCLS_INT_PAR, ESS094, ERRZERO, "SFndLenMsg : Null Buffer");
2956       return RFAILED;
2957    }
2958    /* check length pointer */
2959    if (lngPtr == NULLP)
2960    {
2961       SSLOGERROR(ERRCLS_INT_PAR, ESS095, ERRZERO, "SFndLenMsg : Null Buffer");
2962       return RFAILED;
2963    }
2964    if (mBuf->b_datap->db_type != SS_M_PROTO)
2965    {
2966       SSLOGERROR(ERRCLS_INT_PAR, ESS096, ERRZERO, "SFndLenMsg : Incorrect\
2967                                                    buffer type");
2968       return RFAILED;
2969    }
2970 #endif
2971  
2972    /* get the SsMsgInfo */
2973    minfo = (SsMsgInfo*) mBuf->b_rptr;
2974
2975    /* read length */
2976    *lngPtr = minfo->len;
2977
2978    return ROK;
2979 }
2980
2981
2982 /* #ifdef SS_LOCKLESS_MEMORY */
2983
2984 /*
2985 *
2986 *       Fun:   SSegMsg
2987 *
2988 *       Desc:  This function will segment one specified message into two
2989 *              messages.
2990 *
2991 *       Ret:   ROK     - ok
2992 *              ROKDNA  - ok, data not available
2993 *              RFAILED - failed, general (optional)
2994 *              ROUTRES - failed, out of resources (optional)
2995 *
2996 *       Notes: message 1 is the original message.
2997 *
2998 *              message 2 is the new message.
2999 *
3000 *              index is 0 based and indicates location in message 1
3001 *              from which message 2 will be created.
3002 *
3003 *              if index is equal to 0: message 2 is created and all data
3004 *              attached to message 1 is moved to message 2. message 1
3005 *              is not returned to memory. return is ok.
3006 *
3007 *              if index is not equal to 0 and less than the length of
3008 *              the message minus 1: message 2 is created, all data
3009 *              attached to message 1 from index (inclusive) is moved to
3010 *              message 2. message 1 contains data from index 0 to index
3011 *              minus 1. return is ok.
3012 *
3013 *              if index is not equal to 0 and greater than or equal to
3014 *              the length of the message minus 1: message 1 is unchanged.
3015 *              message 2 is set to null. return is ok, data not available.
3016 *
3017 *       File:  ss_msg.c
3018 *
3019 */
3020 #ifdef T2K_MEM_LEAK_DBG
3021 S16 SSegMsgNew
3022 (
3023 Buffer *mBuf1,              /* message 1 */
3024 MsgLen idx,                 /* index */
3025 Buffer **mBuf2,
3026 char* file,
3027 uint32_t line
3028 )
3029 #else
3030 S16 SSegMsg
3031 (
3032 Buffer *mBuf1,              /* message 1 */
3033 MsgLen idx,                 /* index */
3034 Buffer **mBuf2
3035 )
3036 #endif
3037 {
3038    SsMsgInfo *minfo1;
3039    SsMsgInfo *minfo2;
3040    Buffer *tmp;
3041    Buffer *prev;
3042    Buffer *next;
3043
3044
3045 #if (ERRCLASS & ERRCLS_INT_PAR)
3046    /* check message buffer 1 */
3047    if (mBuf1 == NULLP)
3048    {
3049       SSLOGERROR(ERRCLS_INT_PAR, ESS097, ERRZERO, "SSegMsg : Null Buffer");
3050       return RFAILED;
3051    }
3052    /* check message buffer 2 */
3053    if (mBuf2 == NULLP)
3054    {
3055       SSLOGERROR(ERRCLS_INT_PAR, ESS098, ERRZERO, "SSegMsg : Null Buffer");
3056       return RFAILED;
3057    }
3058    if (idx < 0)
3059    {
3060       SSLOGERROR(ERRCLS_INT_PAR, ESS099, ERRZERO, "SSegMsg : Invalid index");
3061       return RFAILED;
3062    }
3063    if (mBuf1->b_datap->db_type != SS_M_PROTO)
3064    {
3065       SSLOGERROR(ERRCLS_INT_PAR, ESS100, ERRZERO, "SSegMsg : Incorrect buffer\
3066                                                    type");
3067       return RFAILED;
3068    }
3069 #endif
3070
3071    /* get the SsMsgInfo of mBuf1 */
3072    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
3073 #ifdef RGL_SPECIFIC_CHANGES
3074    minfo1->region = 0;
3075 #endif   
3076
3077    /* if index > length of mBuf, return */
3078    if (idx >= minfo1->len)
3079    {
3080       *mBuf2 = NULLP;
3081       return (ROKDNA);
3082    }
3083    /* allocate message buffer */
3084    if (SGetMsg(minfo1->region, minfo1->pool, mBuf2) != ROK)
3085    {
3086       SSLOGERROR(ERRCLS_DEBUG, ESS101, ERRZERO, "SSegMsg : SGetMsg failed");
3087       return RFAILED;
3088    }
3089
3090    /* get the SsMsgInfo of mBuf2 */
3091    minfo2 = (SsMsgInfo*) (*mBuf2)->b_rptr;
3092
3093    /* adjust the lengths of mBuf1, mBuf2 */
3094    minfo2->len = minfo1->len - idx;
3095    minfo1->len = idx;
3096
3097    /* set the endptr of mBuf2 to mBuf1 */
3098    minfo2->endptr = minfo1->endptr;
3099
3100    /* if index is zero ... */
3101    if (!idx)
3102    {
3103       (*mBuf2)->b_cont = mBuf1->b_cont;
3104
3105       /* set the endptr and b_cont of mBuf1 to NULLP */
3106       minfo1->endptr = NULLP;
3107       mBuf1->b_cont = NULLP;
3108
3109       return ROK;
3110    }
3111
3112    /* get the first SS_M_DATA blk */
3113    tmp = mBuf1->b_cont;
3114    prev = mBuf1;
3115
3116    FIND_OFFSET_AND_PREV(prev, tmp, idx)
3117
3118    /* segmented at the start of a blk */
3119    if (!idx)
3120    {
3121       (*mBuf2)->b_cont = tmp;
3122       prev->b_cont = NULLP;
3123       minfo1->endptr = prev;
3124    }
3125    else
3126    {
3127 #ifndef SS_MULTICORE_SUPPORT
3128       /* allocate a message blk without a data blk */
3129       /* ssDupB internally increments the reference count */
3130 #ifdef SS_M_PROTO_REGION
3131       if (!(next = DupMsg(minfo1->region, tmp)))
3132 #else
3133       if (!(next = ssDupB(tmp)))
3134 #endif /* SS_M_PROTO_REGION */
3135       {
3136          /* reset length */
3137          minfo1->len += minfo2->len;
3138          (Void) SPutMsg(*mBuf2);
3139          return (ROUTRES);
3140       }
3141
3142       (*mBuf2)->b_cont = next;
3143
3144       tmp->b_cont = NULLP;
3145
3146       tmp->b_wptr = tmp->b_rptr + idx;
3147       next->b_rptr = tmp->b_wptr;
3148
3149       /* If the index was in the last mblk of the message, the
3150        *  end pointer of the new message needs to be set to the
3151        *  dup'ped mblk. Otherwise, the end pointer of the first
3152        *  message will be set to the mblk in which the index
3153        *  was found, and the end pointer of the new message can
3154        *  remain where it is.
3155        */
3156       if (minfo1->endptr == tmp)
3157       {
3158          minfo2->endptr = next;
3159       }
3160       else
3161       {
3162          minfo1->endptr = tmp;
3163       }
3164 #else /*SS_MULTICORE_SUPPORT*/
3165 /* 
3166  * SDeRegTTsk patch
3167  */
3168 #ifdef SS_M_PROTO_REGION
3169                 if (!(next = DupMsg(minfo1->region, tmp)))
3170 #else
3171                   if (!(next = ssDupB(tmp)))
3172 #endif /* SS_M_PROTO_REGION */
3173       {
3174          /* reset length */
3175          minfo1->len += minfo2->len;
3176          (Void) SPutMsg(*mBuf2);
3177          return (ROUTRES);
3178       }
3179       (*mBuf2)->b_cont = next;
3180       tmp->b_wptr = tmp->b_rptr + idx;
3181       next->b_rptr += idx;
3182       prev = tmp;
3183       tmp = tmp->b_cont;
3184       /* copy rest of the blocks */
3185       if(tmp)
3186       {
3187          next->b_cont = tmp;
3188          prev->b_cont = NULLP;
3189          minfo2->endptr = minfo1->endptr; 
3190          minfo1->endptr = prev;
3191       }
3192       else
3193       {
3194          next->b_cont = NULLP;
3195          minfo2->endptr = next;
3196       }
3197
3198 #endif /*SS_MULTICORE_SUPPORT*/
3199    }
3200
3201    return ROK;
3202 }
3203
3204 /*
3205 *
3206 *       Fun:   SCpyFixMsg
3207 *
3208 *       Desc:  This function copies data from a fixed buffer to a
3209 *              message.
3210 *
3211 *       Ret:   ROK      - ok
3212 *              RFAILED  - failed, general (optional)
3213 *
3214 *       Notes: None
3215 *
3216 *       File:  ss_msg.c
3217 *
3218 */
3219
3220 S16 SCpyFixMsg
3221 (
3222 Data *srcBuf,               /* source buffer */
3223 Buffer *dstMbuf,            /* destination message buffer */
3224 MsgLen dstIdx,              /* destination index */
3225 MsgLen cnt,                 /* count */
3226 MsgLen *cCnt
3227 )
3228 {
3229    S16 ret;
3230    SsMsgInfo *minfo;
3231    Buffer *right;
3232
3233 #if (ERRCLASS & ERRCLS_INT_PAR)
3234    /* check source message buffer */
3235    if (srcBuf == NULLP)
3236    {
3237       SSLOGERROR(ERRCLS_INT_PAR, ESS102, ERRZERO, "SCpyFixMsg : Null Buffer");
3238       return RFAILED;
3239    }
3240    /* check destination message buffer */
3241    if (dstMbuf == NULLP)
3242    {
3243       SSLOGERROR(ERRCLS_INT_PAR, ESS103, ERRZERO, "SCpyFixMsg : Null Buffer");
3244       return RFAILED;
3245    }
3246    /* check copied count buffer */
3247    if (cCnt == NULLP)
3248    {
3249       SSLOGERROR(ERRCLS_INT_PAR, ESS104, ERRZERO, "SCpyFixMsg : Null Buffer");
3250       return RFAILED;
3251    }
3252    /* check copy count */
3253    if (cnt <= 0)
3254    {
3255       SSLOGERROR(ERRCLS_INT_PAR, ESS105, ERRZERO, "SCpyFixMsg : Invalid count");
3256       return RFAILED;
3257    }
3258    if (dstMbuf->b_datap->db_type != SS_M_PROTO)
3259    {
3260       SSLOGERROR(ERRCLS_INT_PAR, ESS106, ERRZERO, "SCpyFixMsg : Incorrect\
3261                                                    buffer type");
3262       return RFAILED;
3263    }
3264 #endif
3265
3266    minfo = (SsMsgInfo*) dstMbuf->b_rptr;
3267
3268    if (minfo->len < dstIdx)
3269    {
3270 #if (ERRCLASS & ERRCLS_DEBUG)
3271       SSLOGERROR(ERRCLS_DEBUG, ESS107, ERRZERO, "SCpyFixMsg : Invalid Index");
3272 #endif
3273       return RFAILED;
3274    }
3275
3276 /* ss021.103 - Addition test if message length will exceed max msg length */
3277 #if (ERRCLASS & ERRCLS_INT_PAR)
3278 #ifdef LONG_MSG
3279    if (minfo->len > 0x7FFFFFFF - cnt)
3280 #else
3281    if (minfo->len > 0x7FFF - cnt)
3282 #endif
3283    {
3284       *cCnt = 0;
3285
3286       SSLOGERROR(ERRCLS_INT_PAR, ESS108, ERRZERO, "SCpyFixMsg : msgLen + cnt > maxS16");
3287       return (ROUTRES);
3288    }
3289 #endif
3290    
3291    /* add data at the start of dst buffer */
3292    if (!dstIdx)
3293    {
3294       if ((ret = SAddPreMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3295       {
3296 #if (ERRCLASS & ERRCLS_DEBUG)
3297          SSLOGERROR(ERRCLS_DEBUG, ESS109, ERRZERO, "SCpyFixMsg : Failed in\
3298                                                     SAddPreMsgMult");
3299 #endif
3300          return (ret);
3301       }
3302       *cCnt = cnt;
3303
3304       return ROK;
3305    }
3306
3307    /* add data at the end of the dst buffer */
3308    if (minfo->len == dstIdx)
3309    {
3310       if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3311       {
3312          return (ret);
3313       }
3314       *cCnt = cnt;
3315
3316       return ROK;
3317    }
3318
3319    /* segment the message into dstMbuf and right */
3320    if ((ret = SSegMsg(dstMbuf, dstIdx, &right)) != ROK)
3321    {
3322       return (ret);
3323    }
3324
3325    /* append data at the end of dstMbuf */
3326    if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3327    {
3328       /* ss020.103 - Addition for cleanup */
3329       (Void) SPutMsg(right);
3330       return (ret);
3331    }
3332
3333    /* cancatenate dstMbuf and right */
3334    if ((ret = SCatMsg(dstMbuf, right, M1M2)) != ROK)
3335    {
3336       /* ss020.103 - Addition for cleanup */
3337       (Void) SPutMsg(right);
3338       return (ret);
3339    }
3340
3341    *cCnt = cnt;
3342
3343    (Void) SPutMsg(right);
3344
3345    return ROK;
3346 }
3347
3348 /*
3349 *
3350 *       Fun:   SCpyMsgFix
3351 *
3352 *       Desc:  This function copies data from a message
3353 *              into a fixed buffer.
3354 *
3355 *       Ret:   ROK      - ok
3356 *              RFAILED  - failed, general (optional)
3357 *
3358 *       Notes: None
3359 *
3360 *       File:  ss_msg.c
3361 *
3362 */
3363
3364 S16 SCpyMsgFix
3365 (
3366 Buffer *srcMbuf,            /* source message buffer */
3367 MsgLen srcIdx,              /* source index */
3368 MsgLen cnt,                 /* count */
3369 Data *dstBuf,               /* destination buffer */
3370 MsgLen *cCnt
3371 )
3372 {
3373    Data *cptr = NULLP;
3374    Buffer *tmp = NULLP;
3375    SsMsgInfo *minfo = NULLP;
3376    MsgLen numBytes =0;
3377
3378 #if (ERRCLASS & ERRCLS_INT_PAR)
3379    /* check source message buffer */
3380    if (srcMbuf == NULLP)
3381    {
3382       SSLOGERROR(ERRCLS_INT_PAR, ESS110, ERRZERO, "SCpyMsgFix : Null Buffer");
3383       return RFAILED;
3384    }
3385    /* check destination message buffer */
3386    if (dstBuf == NULLP)
3387    {
3388       SSLOGERROR(ERRCLS_INT_PAR, ESS111, ERRZERO, "SCpyMsgFix : Null Buffer");
3389       return RFAILED;
3390    }
3391    if (cnt <= 0)
3392    {
3393       SSLOGERROR(ERRCLS_INT_PAR, ESS112, ERRZERO, "SCpyMsgFix : Invalid Index");
3394       return RFAILED;
3395    }
3396  
3397    if (srcIdx < 0)
3398    {
3399       SSLOGERROR(ERRCLS_INT_PAR, ESS113, ERRZERO, "SCpyMsgFix : Invalid Index");
3400       return RFAILED;
3401    }
3402    if (!cCnt)
3403    {
3404       SSLOGERROR(ERRCLS_INT_PAR, ESS114, ERRZERO, "SCpyMsgFix : Null Buffer");
3405       return RFAILED;
3406    }
3407    if (srcMbuf->b_datap->db_type != SS_M_PROTO)
3408    {
3409       SSLOGERROR(ERRCLS_INT_PAR, ESS115, ERRZERO, "SCpyMsgFix : Incorrect\
3410                                                    buffer type");
3411       return RFAILED;
3412    }
3413 #endif
3414
3415    /* get SsMsgInfo */
3416    minfo = (SsMsgInfo*) srcMbuf->b_rptr;
3417
3418    if ((srcIdx + cnt) > minfo->len)
3419    {
3420       *cCnt = 0;
3421       return (ROKDNA);
3422    }
3423
3424    /* get the first SS_M_DATA blk */
3425    tmp = srcMbuf->b_cont;
3426
3427    /* get to the srcIdx-th offset */
3428    FIND_OFFSET(tmp, srcIdx)
3429
3430    *cCnt = cnt;
3431
3432    /* set cptr to the read ptr of tmp + offset */
3433    cptr = tmp->b_rptr + srcIdx;
3434
3435    while (cnt)
3436    {
3437       /* determine the number of bytes to be copied */
3438       numBytes = MIN(cnt, (tmp->b_wptr - cptr));
3439
3440       /* decrement cnt */
3441       cnt -= numBytes;
3442
3443       /* copy data */
3444
3445 /* ss002.13 addition */
3446
3447    /* ss003.13 addition */
3448       SMemCpy((Void *) dstBuf, (Void *) cptr, (size_t) numBytes);
3449
3450       cptr += numBytes;
3451       dstBuf += numBytes;
3452
3453
3454       /* get the next blk */
3455       if ((tmp = tmp->b_cont))
3456          /* set cptr to the read ptr of tmp */
3457          cptr = tmp->b_rptr;
3458       else
3459          break;
3460    }
3461
3462    return ROK;
3463 }
3464
3465 /*
3466 *
3467 *       Fun:   SCpyMsgMsg
3468 *
3469 *       Desc:  This function is used to copy a message into
3470 *              a new region and or pool of memory.
3471 *
3472 *       Ret:   ROK      - ok
3473 *              RFAILED  - failed, general (optional)
3474 *              ROUTRES  - failed, out of resources (optional)
3475 *
3476 *       Notes: None
3477 *
3478 *       File:  ss_msg.c
3479 *
3480 */
3481
3482 #ifdef T2K_MEM_LEAK_DBG
3483 S16 SCpyMsgMsgNew
3484 (
3485 Buffer *srcBuf,
3486 Region dstRegion,
3487 Pool dstPool,
3488 Buffer **dstBuf,
3489 char* file,
3490 uint32_t line
3491 )
3492 #else
3493 S16 SCpyMsgMsg
3494 (
3495 Buffer *srcBuf,
3496 Region dstRegion,
3497 Pool dstPool,
3498 Buffer **dstBuf
3499 )
3500 #endif
3501 {
3502    SsMsgInfo *minfo1;
3503    SsMsgInfo *minfo2;
3504    Buffer *tmp;
3505    Buffer *curblk;
3506    Buffer *newblk;
3507    Buffer *prevblk;
3508  
3509    /* ss021.103 - Addition of return value */
3510 #ifndef SS_PERF
3511 #if (ERRCLASS & ERRCLS_INT_PAR)
3512    S16 ret;
3513 #endif
3514 #endif
3515  
3516 #if (ERRCLASS & ERRCLS_INT_PAR)
3517    if (!srcBuf)
3518    {
3519       SSLOGERROR(ERRCLS_INT_PAR, ESS116, ERRZERO, "SCpyMsgMsg : Null Buffer");
3520       return RFAILED;
3521    }
3522    if (srcBuf->b_datap->db_type != SS_M_PROTO)
3523    {
3524       SSLOGERROR(ERRCLS_INT_PAR, ESS117, ERRZERO, "SCpyMsgMsg : Incorrect\
3525                                                    buffer type");
3526       return RFAILED;
3527    }
3528    /* ss021.103 - Addition to validate region and pool */
3529    if (dstRegion >= SS_MAX_REGS)
3530    {
3531       SSLOGERROR(ERRCLS_INT_PAR, ESS118, ERRZERO, 
3532                  "SCpyMsgMsg : Invalid region id");
3533       return RFAILED;
3534    }
3535  
3536    if (dstPool >= SS_MAX_POOLS_PER_REG)
3537    {
3538       SSLOGERROR(ERRCLS_INT_PAR, ESS119, ERRZERO, 
3539                  "SCpyMsgMsg : Invalid pool id");
3540       return RFAILED;
3541    }
3542 /* ss037.103 Removed the semaphore operation for performance enhancement */
3543
3544 #ifndef SS_PERF
3545    /* ss021.103 - Addition to check if region is valid */
3546    /* acquire one semaphore, to protect against deregistration */
3547    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
3548    if (ret != ROK)
3549    {
3550
3551 #if (ERRCLASS & ERRCLS_DEBUG)
3552       SSLOGERROR(ERRCLS_DEBUG, ESS120, (ErrVal) ret,
3553                   "Could not lock region table");
3554 #endif
3555
3556       return RFAILED;
3557    }
3558 #endif
3559
3560 #if (ERRCLASS & ERRCLS_INT_PAR)
3561    /* verify that this region is present */
3562    if (osCp.regionTbl[dstRegion].used == FALSE)
3563    {
3564 #ifndef SS_PERF
3565       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
3566       {
3567 #if (ERRCLASS & ERRCLS_DEBUG)
3568          SSLOGERROR(ERRCLS_DEBUG, ESS121, ERRZERO,
3569                   "Could not release semaphore");
3570          return RFAILED;
3571 #endif
3572       }
3573 #endif
3574       SSLOGERROR(ERRCLS_INT_PAR, ESS122, dstRegion, "Region not registered");
3575       return RFAILED;
3576    }
3577 #endif
3578 /* ss037.103 Removed the semaphore operation for performance enhancement */
3579
3580 #ifndef SS_PERF
3581    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
3582    {
3583 #if (ERRCLASS & ERRCLS_DEBUG)
3584       SSLOGERROR(ERRCLS_DEBUG, ESS123, ERRZERO,
3585                   "Could not release semaphore");
3586       return RFAILED;
3587 #endif
3588    }
3589 #endif
3590 #endif
3591
3592 #ifdef XEON_SPECIFIC_CHANGES
3593    dstRegion = 0;
3594    dstPool   = 0;
3595 #endif   
3596    /* allocate a message buffer */
3597    if (SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
3598    {
3599 #if (ERRCLASS & ERRCLS_DEBUG)
3600       SSLOGERROR(ERRCLS_DEBUG, ESS124, ERRZERO, "SCpyMsgMsg : SGetMsg failed");
3601 #endif
3602       return RFAILED;
3603    }
3604    /* get the SsMsgInfo from srcBuf */
3605    minfo1 = (SsMsgInfo*) srcBuf->b_rptr;
3606
3607    /* get the SsMsgInfo from srcBuf */
3608    minfo2 = (SsMsgInfo*) (*dstBuf)->b_rptr;
3609
3610    /* get the first SS_M_DATA blk of srcBuf */
3611    tmp = srcBuf->b_cont;
3612
3613    /* if srcBuf and dstBuf belong to the same region, increment the reference
3614     * count
3615     */
3616       newblk = NULLP;
3617       curblk = NULLP;
3618       prevblk = NULLP;
3619
3620       while (tmp)
3621       {
3622 #ifdef SS_M_PROTO_REGION 
3623          if ((curblk = DupMsg(dstRegion,tmp)) == NULLP)
3624 #else
3625          if ((curblk = ssDupB(tmp)) == NULLP)
3626 #endif /* SS_M_PROTO_REGION */
3627          {
3628             while (newblk)
3629             {
3630                curblk = newblk->b_cont;
3631                (Void) SPutDBuf(minfo1->region, minfo1->pool, newblk);
3632                newblk = curblk;
3633             }
3634             printf("\nFailed to get the buffer of size %s %d\n", __FILE__, __LINE__);
3635             (Void) SPutMsg(*dstBuf);
3636             return (ROUTRES);
3637          }
3638
3639          if (!prevblk)
3640             newblk = curblk;
3641          else
3642             prevblk->b_cont = curblk;
3643          prevblk = curblk;
3644
3645          tmp = tmp->b_cont;
3646       }
3647       if (curblk)
3648          curblk->b_cont = NULLP;
3649
3650       minfo2->len = minfo1->len;
3651       minfo2->endptr = curblk;
3652       (*dstBuf)->b_cont = newblk;
3653
3654       return ROK;
3655 }
3656
3657
3658
3659 /*
3660 *
3661 *       Fun:   SAddMsgRef
3662 *
3663 *       Desc:  This function is used to copy a message into
3664 *              a new region and or pool of memory.
3665 *
3666 *       Ret:   ROK      - ok
3667 *              RFAILED  - failed, general (optional)
3668 *              ROUTRES  - failed, out of resources (optional)
3669 *
3670 *       Notes: None
3671 *
3672 *       File:  ss_msg.c
3673 *
3674 */
3675 #ifdef T2K_MEM_LEAK_DBG
3676 S16 SAddMsgRefNew
3677 (
3678 Buffer *srcBuf,
3679 Region dstRegion,
3680 Pool dstPool,
3681 Buffer **dstBuf,
3682 char* file,
3683 uint32_t line
3684 )
3685 #else
3686 S16 SAddMsgRef
3687 (
3688 Buffer *srcBuf,
3689 Region dstRegion,
3690 Pool dstPool,
3691 Buffer **dstBuf
3692 )
3693 #endif
3694 {
3695    SsMsgInfo *minfo1;
3696    SsMsgInfo *minfo2;
3697    Buffer *tmp;
3698    Buffer *dBuf;
3699    Buffer *curblk;
3700    Buffer *newblk;
3701    Buffer *prevblk;
3702    Data *cptr;
3703    MsgLen numBytes;
3704
3705
3706 #if (ERRCLASS & ERRCLS_INT_PAR)
3707    if (!srcBuf)
3708    {
3709       SSLOGERROR(ERRCLS_INT_PAR, ESS126, ERRZERO, "SAddMsgRef : Null Buffer");
3710       return RFAILED;
3711    }
3712    if (srcBuf->b_datap->db_type != SS_M_PROTO)
3713    {
3714       SSLOGERROR(ERRCLS_INT_PAR, ESS127, ERRZERO, "SAddMsgRef : Incorrect\
3715                                                    buffer type");
3716       return RFAILED;
3717    }
3718 #endif
3719 #ifdef XEON_SPECIFIC_CHANGES
3720    dstRegion = 0;
3721    dstPool   = 0;
3722 #endif   
3723
3724    /* allocate a message buffer */
3725    if (SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
3726    {
3727 #if (ERRCLASS & ERRCLS_DEBUG)
3728       SSLOGERROR(ERRCLS_DEBUG, ESS128, ERRZERO, "SAddMsgRef : SGetMsg failed");
3729 #endif
3730       return RFAILED;
3731    }
3732    /* get the SsMsgInfo from srcBuf */
3733    minfo1 = (SsMsgInfo*) srcBuf->b_rptr;
3734
3735    /* get the SsMsgInfo from srcBuf */
3736    minfo2 = (SsMsgInfo*) (*dstBuf)->b_rptr;
3737
3738    /* get the first SS_M_DATA blk of srcBuf */
3739    tmp = srcBuf->b_cont;
3740
3741    /* if srcBuf and dstBuf belong to the same region, increment the reference
3742     * count
3743     */
3744    if (dstRegion == minfo1->region)
3745    {
3746       newblk = NULLP;
3747       curblk = NULLP;
3748       prevblk = NULLP;
3749
3750       while (tmp)
3751       {
3752 #ifdef SS_M_PROTO_REGION 
3753          if ((curblk = DupMsg(dstRegion, tmp)) == NULLP)
3754 #else
3755          if ((curblk = ssDupB(tmp)) == NULLP)
3756 #endif /* SS_M_PROTO_REGION */
3757          {
3758             while (newblk)
3759             {
3760                curblk = newblk->b_cont;
3761                (Void) SPutDBuf(minfo1->region, minfo1->pool, newblk);
3762                newblk = curblk;
3763             }
3764             (Void) SPutMsg(*dstBuf);
3765             return (ROUTRES);
3766          }
3767
3768          if (!prevblk)
3769             newblk = curblk;
3770          else
3771             prevblk->b_cont = curblk;
3772          prevblk = curblk;
3773
3774          tmp = tmp->b_cont;
3775       }
3776       if (curblk)
3777          curblk->b_cont = NULLP;
3778
3779       minfo2->len = minfo1->len;
3780       minfo2->endptr = curblk;
3781       (*dstBuf)->b_cont = newblk;
3782
3783       return ROK;
3784    }
3785
3786    /* allocate a data buffer */
3787    if (ssGetDBufOfSize(dstRegion, minfo1->len, &dBuf) != ROK)
3788    {
3789 /* ss016.13: addition */
3790       (Void) SPutMsg(*dstBuf);
3791       SSLOGERROR(ERRCLS_DEBUG, ESS129, ERRZERO, "SAddMsgRef : ssGetDBufOfSize\
3792                  failed");
3793       return (ROUTRES);
3794    }
3795    dBuf->b_datap->db_type = SS_M_DATA;
3796
3797    while (tmp)
3798    {
3799       numBytes = tmp->b_wptr - tmp->b_rptr;
3800       cptr = tmp->b_rptr;
3801       while (numBytes--)
3802          *dBuf->b_wptr++ = *cptr++;
3803       tmp = tmp->b_cont;
3804    }
3805    minfo2->len = minfo1->len;
3806    /* set the endptr and b_cont of dstBuf to point to dBuf */
3807    minfo2->endptr = dBuf;
3808    (*dstBuf)->b_cont = dBuf;
3809
3810    return ROK;
3811 }
3812 /* ss012.13: Addition */
3813 #ifdef SS_M_PROTO_REGION 
3814 /*
3815 *
3816 *       Fun:   DupMsg
3817 *
3818 *       Desc:  Duplicates the specified message block, copying it
3819 *              into a newly-allocated message block. Increments
3820 *              the reference count of the data block that is pointed
3821 *              at by the original message block descriptor.
3822 *
3823 *       Ret:   non-NULL - ok
3824 *              NULL     - failure
3825 *
3826 *       Notes:
3827 *
3828 *       File:  ss_msg.c
3829 *
3830 */
3831 #ifdef T2K_MEM_LEAK_DBG
3832 static Buffer *DupMsgNew
3833 (
3834 Region region,              /* region id */
3835 Buffer *mp,                  /* message block */
3836 char* file,
3837 uint32_t line
3838 )
3839 #else
3840 static Buffer *DupMsg
3841 (
3842 Region region,              /* region id */
3843 Buffer *mp                  /* message block */
3844 )
3845 #endif
3846 {
3847    Buffer *bp;                  /* mblk for iteration */
3848    S16 r;                       /* return value */
3849    Size m;                      /* temporary */
3850
3851
3852 #if (ERRCLASS & ERRCLS_INT_PAR)
3853    if (mp == NULLP)
3854    {
3855       SSLOGERROR(ERRCLS_INT_PAR, ESS130, ERRZERO, "Null pointer");
3856       return (NULLP);
3857    }
3858
3859   if (region >= SS_MAX_REGS)
3860    {
3861       SSLOGERROR(ERRCLS_INT_PAR, ESS131, ERRZERO, "DupMsg : Invalid region\
3862                                                    id");
3863       return (NULLP);
3864    }
3865 #endif
3866
3867
3868 /* allocate a single block for the mblock and the dblock */
3869 #if 1
3870    m = (sizeof(SsMblk) + sizeof(SsDblk));
3871 #else
3872    numBytes = mp->b_wptr - mp->b_rptr;
3873    m = MDBSIZE + numBytes;
3874 #endif /* SS_MULTICORE_SUPPORT */
3875 /* ss001.301: additions */
3876 #ifdef SS_HISTOGRAM_SUPPORT 
3877    r = SAlloc(region, &m, 0, (Data **)&bp, __LINE__, (uint8_t*) __FILE__, ENTNC);
3878 #else
3879    r = SAlloc(region, &m, 0, (Data **)&bp);
3880 #endif /* SS_HISTOGRAM_SUPPORT */
3881    if (r != ROK)
3882    {
3883 #if (ERRCLASS & ERRCLS_ADD_RES)
3884       SSLOGERROR(ERRCLS_ADD_RES, ESS132, (ErrVal) r, "SAlloc() failed");
3885 #endif
3886
3887      return (NULLP);
3888    }
3889 /* generic set-up-message function */
3890 #if 1
3891 #ifndef SS_DBUF_REFLOCK_DISABLE
3892    SS_STRM_INITB(bp, (SsDblk *)(((uint8_t *)bp) + sizeof(SsMblk)), NULLP, 0, NULLP);
3893 #endif
3894 #else
3895    data = (Data *) (bp) + MDBSIZE;
3896    SS_STRM_INITB(bp,(SsDblk *)(((uint8_t *)bp) + sizeof(SsMblk)), data, numBytes, NULLP);
3897 #endif /* SS_MULTICORE_SUPPORT */
3898
3899
3900    /*  make the new message block identical to the one to be dup'ed.
3901     *  notice that an mblk/dblk pair is allocated but only the mblk
3902     *  is used,, this is for optimum performance in the average case.
3903     */
3904    SMemCpy( (Void *)bp, (Void *)mp, (size_t)sizeof(SsMblk));
3905
3906    /* ss006.301 -  added the Lock */
3907 #ifndef SS_DBUF_REFLOCK_DISABLE
3908    if((SLock(&(mp->b_datap->dBufLock))) != ROK)
3909    {
3910       SSLOGERROR(ERRCLS_DEBUG, ESS335, ERRZERO,
3911                      "Could not lock the mBuf Ref Lock");
3912       return (NULLP);
3913    }
3914 #endif
3915    /* increment the reference count of the dblock */
3916    /* increment the reference count of the dblock */
3917    mp->b_datap->db_ref++;
3918    mp->b_datap->shared = TRUE;
3919 #ifndef SS_DBUF_REFLOCK_DISABLE
3920    if((SUnlock(&(mp->b_datap->dBufLock))) != ROK)
3921    {
3922       SSLOGERROR(ERRCLS_DEBUG, ESS335, ERRZERO,
3923                      "Could not lock the mBuf Ref Lock");
3924       return (NULLP);
3925    }
3926 #endif
3927
3928
3929    return (bp);
3930 } /* DupMsg */
3931 #endif /* SS_M_PROTO_REGION */
3932
3933 /*
3934 *
3935 *       Fun:   SGetDBuf
3936 *
3937 *       Desc:  This function allocates a buffer from the dynamic
3938 *              memory pool indicated by the caller.
3939 *
3940 *       Ret:   ROK      - ok
3941 *              RFAILED  - failed, general (optional)
3942 *              ROUTRES  - failed, out of resources (optional)
3943 *
3944 *       Notes: The dynamic memory pools are used to create and
3945 *              manipulate messages.
3946 *
3947 *              SGetDBuf is never called by a protocol layer.
3948 *
3949 *              SGetDBuf assumes that interrupts are already disabled.
3950 *
3951 *       File:  ss_msg.c
3952 *
3953 */
3954
3955 #ifdef T2K_MEM_LEAK_DBG
3956 S16 SGetDBufNew
3957 (
3958 Region region,              /* region id */
3959 Pool pool,                  /* pool id */
3960 Buffer **bufPtr,
3961 char* file,
3962 uint32_t line
3963 )
3964 #else
3965 S16 SGetDBuf
3966 (
3967 Region region,              /* region id */
3968 Pool pool,                  /* pool id */
3969 Buffer **bufPtr
3970 )
3971 #endif
3972 {
3973    Size size;
3974    Size mdsize;
3975    Data *data;
3976    SsDblk *dptr;
3977 #ifdef SS_LOCKLESS_MEMORY
3978 #ifdef SS_USE_ICC_MEMORY
3979    CmMmDynRegCb   *regCb;
3980 #else
3981    CmMmGlobRegCb  *regCb;
3982 #endif
3983 #else
3984    SsRegionEntry *regp;
3985 #endif /* SS_LOCKLESS_MEMORY */
3986       /* ss021.103 - Addition of return value */
3987 #ifndef SS_PERF
3988 #if (ERRCLASS & ERRCLS_INT_PAR)
3989    S16 ret;
3990 #endif
3991 #endif
3992    
3993 #if (ERRCLASS & ERRCLS_INT_PAR)
3994    /* check buffer pointer */
3995    if (!bufPtr)
3996    {
3997       SSLOGERROR(ERRCLS_INT_PAR, ESS133, ERRZERO, "SGetDBuf : Null Buffer");
3998       return RFAILED;
3999    }
4000    if (region >= SS_MAX_REGS)
4001    {
4002       SSLOGERROR(ERRCLS_INT_PAR, ESS134, ERRZERO, "SGetDBuf : Invalid region\
4003                                                    id");
4004       return RFAILED;
4005    }
4006  
4007    if (pool >= SS_MAX_POOLS_PER_REG)
4008    {
4009       SSLOGERROR(ERRCLS_INT_PAR, ESS135, ERRZERO, "SGetDBuf : Invalid pool id");
4010       return RFAILED;
4011    }
4012 /* ss037.103 Removed the semaphore operation for performance enhancement */
4013
4014 #ifndef SS_PERF
4015    /* ss021.103 - Addition to check if region is registered */
4016    /* acquire one semaphore, to protect against deregistration */
4017    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
4018    if (ret != ROK)
4019    {
4020
4021 #if (ERRCLASS & ERRCLS_DEBUG)
4022       SSLOGERROR(ERRCLS_DEBUG, ESS136, (ErrVal) ret,
4023                   "Could not lock region table");
4024 #endif
4025
4026       return RFAILED;
4027    }
4028
4029 #endif
4030
4031 #if (ERRCLASS & ERRCLS_INT_PAR)
4032    /* verify that this region is present */
4033    if (osCp.regionTbl[region].used == FALSE)
4034    {
4035 /* ss037.103 Removed the semaphore operation for performance enhancement */
4036
4037 #ifndef SS_PERF
4038       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
4039       {
4040 #if (ERRCLASS & ERRCLS_DEBUG)
4041          SSLOGERROR(ERRCLS_DEBUG, ESS137, ERRZERO,
4042                   "Could not release semaphore");
4043          return RFAILED;
4044 #endif
4045       }
4046 #endif
4047       SSLOGERROR(ERRCLS_INT_PAR, ESS138, region, "Region not registered");
4048       return RFAILED;
4049    }
4050 #endif
4051 /* ss037.103 Removed the semaphore operation for performance enhancement */
4052
4053 #ifndef SS_PERF
4054    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
4055    {
4056 #if (ERRCLASS & ERRCLS_DEBUG)
4057       SSLOGERROR(ERRCLS_DEBUG, ESS139, ERRZERO,
4058                   "Could not release semaphore");
4059       return RFAILED;
4060 #endif
4061    }
4062 #endif
4063 #endif
4064
4065 #ifdef SS_LOCKLESS_MEMORY
4066 #ifdef SS_USE_ICC_MEMORY
4067    regCb = (CmMmDynRegCb *)(osCp.dynRegionTbl[region].regCb);
4068    mdsize = regCb->bktSize[pool];
4069 #else  /* SS_USE_ICC_MEMORY */
4070    regCb = osCp.globRegCb;
4071 #ifdef SS_MEM_WL_DEBUG
4072    mdsize = regCb->bktTbl[pool].size - 4;
4073 #else  /* SS_MEM_WL_DEBUG */
4074    mdsize = regCb->bktTbl[pool].size;
4075 #endif /* SS_MEM_WL_DEBUG */
4076 #endif /* SS_USE_ICC_MEMORY */
4077 #else
4078    regp = &osCp.regionTbl[region];
4079    size = regp->poolTbl[pool].u.dpool.size;
4080    /* ss006.301 : optimized this function */
4081    mdsize = MDBSIZE + size;
4082 #endif /* SS_LOCKLESS_MEMORY */
4083
4084    /* ss006.301 : optimized this function */
4085 /* ss001.301: additions */
4086 #ifdef SS_HISTOGRAM_SUPPORT 
4087     if (SAlloc(region, &mdsize, 0, (Data **) bufPtr, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
4088 #else
4089     if (SAlloc(region, &mdsize, 0, (Data **) bufPtr) != ROK)
4090 #endif /* SS_HISTOGRAM_SUPPORT */
4091     {
4092        return (ROUTRES);
4093     }
4094     data = (Data *) (*bufPtr) + MDBSIZE;
4095     size = mdsize - MDBSIZE;
4096
4097    dptr = (SsDblk*) (((Data *) (*bufPtr)) + MBSIZE);
4098
4099    INITB((*bufPtr), dptr, data, size, NULLP)
4100 #ifndef SS_DBUF_REFLOCK_DISABLE
4101    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
4102    {
4103       printf("\nFalied to destroy lock\n");
4104    }
4105 #endif
4106
4107    return ROK;
4108 }
4109
4110 /*
4111 *
4112 *       Fun:   SPutDBuf
4113 *
4114 *       Desc:  This function deallocates a buffer back to the
4115 *              dynamic memory pool indicated by the caller.
4116 *
4117 *       Ret:   ROK      - ok
4118 *              RFAILED  - failed, general (optional)
4119 *
4120 *       Notes: The dynamic memory pools are used to create and
4121 *              manipulate messages.
4122 *
4123 *              SPutDBuf is never called by a protocol layer.
4124 *
4125 *              SPutDBuf assumes that interrupts are already disabled.
4126 *
4127 *       File:  ss_msg.c
4128 *
4129 */
4130
4131 #ifdef T2K_MEM_LEAK_DBG
4132 S16 SPutDBufNew
4133 (
4134 Region region,
4135 Pool pool,
4136 Buffer *buf,
4137 char* file,
4138 uint32_t line
4139 )
4140 #else
4141 S16 SPutDBuf
4142 (
4143 Region region,
4144 Pool pool,
4145 Buffer *buf
4146 )
4147 #endif
4148 {
4149    register SsDblk *dptr = NULLP;
4150    /* ss021.103 - Addition to check return value of SFree */
4151    S16 ret = ROK;
4152 #if (defined(SS_USE_ZBC_MEMORY) && defined (TENB_DPDK_BUF))
4153    Data  *dpdkBuf = NULLP;
4154 #endif   
4155  
4156  
4157    /* ss021.103 - Addition of ret initialization */
4158    ret = ROK;
4159  
4160 #if (ERRCLASS & ERRCLS_INT_PAR)
4161    if (region >= SS_MAX_REGS)
4162    {
4163       SSLOGERROR(ERRCLS_INT_PAR, ESS141, ERRZERO, "SPutDBuf:Invalid region");
4164       return RFAILED;
4165    }
4166  
4167    if (pool >= SS_MAX_POOLS_PER_REG)
4168    {
4169       SSLOGERROR(ERRCLS_INT_PAR, ESS142, ERRZERO, "SPutDBuf:Invalid pool");
4170       return RFAILED;
4171    }
4172  
4173    if (buf == NULLP)
4174    {
4175       SSLOGERROR(ERRCLS_INT_PAR, ESS143, ERRZERO, "SPutDBuf:Null pointer");
4176       return RFAILED;
4177    }
4178
4179    if ( (buf->b_datap->db_type != SS_M_DATA) && (buf->b_datap->db_type != SS_M_PROTO))
4180    {
4181       SSLOGERROR(ERRCLS_INT_PAR, ESS144, ERRZERO, "SPutDBuf:Incorrect\
4182                  buffer type");
4183       return RFAILED;
4184    }
4185 #endif
4186 /* ss016.13: Addition */
4187    dptr = buf->b_datap; 
4188
4189 #ifdef SS_USE_ZBC_MEMORY
4190    if(dptr->db_type == SS_MEM_TYPE_SSI_ZBC)
4191    {
4192       ret = SPutZbcDBuf(region, buf);
4193       return (ret);
4194    }
4195
4196 #ifdef TENB_DPDK_BUF
4197    if(dptr->db_type == SS_MEM_TYPE_DPDK_ZBC)
4198    {
4199       /* Not considering referances for DPDK buffer for now */
4200       SDetachDpdkPtrFrmDBuf(buf, &dpdkBuf);
4201       SPutSBufDpdk(dpdkBuf);
4202       ret = SFree(region, (Data *) buf, MDBSIZE);
4203       return (ret);
4204    }
4205 #endif  /* TENB_DPDK_BUF */
4206 #endif /* SS_USE_ZBC_MEMORY */
4207
4208    /* ss028.103 - Addition of lock for mBuf reference count */
4209     /* ss006.301 : optimized this funciton */
4210 #if 1
4211    if(!dptr->shared)
4212    {
4213 #ifndef SS_DBUF_REFLOCK_DISABLE
4214        SDestroyLock(&dptr->dBufLock);
4215 #endif
4216       /* if the data block is not shared, free the buffer, checks not reqd */
4217 #ifdef SS_HISTOGRAM_SUPPORT 
4218       ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4219          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4220 #else
4221       ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4222 #endif /* SS_HISTOGRAM_SUPPORT */
4223    }
4224    else
4225    {
4226 #ifndef SS_DBUF_REFLOCK_DISABLE
4227         if((ret=SLock(&dptr->dBufLock)))
4228         {
4229            SSLOGERROR(ERRCLS_DEBUG, ESSXXX, ERRZERO,
4230                 "Could not lock the mBuf Ref Lock");
4231            return RFAILED;
4232         }
4233 #endif
4234       --dptr->db_ref;
4235       /* if buffer's message blk is obtained during dupb */
4236       if (buf != (SsMblk *) (((Data*) dptr) - MBSIZE))
4237       {
4238         
4239          SsDblk* dupdptr = (SsDblk *)((uint8_t *)buf + MBSIZE);
4240          dupdptr->db_ref--;
4241          if(dupdptr->db_ref == 0)
4242          {
4243
4244 #ifdef SS_HISTOGRAM_SUPPORT 
4245          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, ENTNC);
4246 #else
4247          ret = SFree(region, (Data *) buf, MDBSIZE);
4248
4249 #endif /* SS_HISTOGRAM_SUPPORT */
4250          }
4251          buf = (SsMblk *) (((Data*) dptr) - MBSIZE);
4252       }
4253       /* if reference count falls to zero */
4254       if (!dptr->db_ref)
4255       {
4256 #ifndef SS_DBUF_REFLOCK_DISABLE
4257         ret = SUnlock(&dptr->dBufLock) ;
4258         if((SDestroyLock(&dptr->dBufLock)) != 0)
4259         {
4260             printf("\nFalied to destroy lock\n");
4261         }
4262 #endif
4263       /* free buffer to region */
4264 #ifdef SS_HISTOGRAM_SUPPORT 
4265          ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4266          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4267 #else
4268          ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4269 #endif /* SS_HISTOGRAM_SUPPORT */
4270          return (ret);
4271       }
4272 #ifndef SS_DBUF_REFLOCK_DISABLE
4273       ret = SUnlock(&(dptr->dBufLock));
4274 #endif
4275    }
4276 #else /* SS_MULTICORE_SUPPORT */
4277    /* If MultiCore Support enabled, Dblk never be shared */
4278 #ifdef SS_HISTOGRAM_SUPPORT 
4279    ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4280          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4281 #else
4282    ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4283 #endif /* SS_HISTOGRAM_SUPPORT */
4284 #endif /* SS_MULTICORE_SUPPORT */
4285    return (ret);
4286 }
4287
4288
4289 /* #ifdef SS_LOCKLESS_MEMORY */
4290
4291
4292 /*
4293 *
4294 *       Fun:   SCatMsg
4295 *
4296 *       Desc:  This function will concatenate the two specified messages
4297 *              into one message.
4298 *
4299 *       Ret:   ROK      - ok
4300 *              RFAILED  - failed, general (optional)
4301 *
4302 *       Notes: if order equal M1M2: all data attached to message 2 is
4303 *              moved to the end of message 1. message 2 is set to empty.
4304 *              message 1 length is increased by length of message 2.
4305 *              message 2 length is set to zero. message 2 is not returned
4306 *              to memory. return is ok.
4307 *
4308 *              if order equal M2M1: all data attached to message 2 is
4309 *              moved to the front of message 1. message 2 is set to empty.
4310 *              message 1 length is increased by length of message 2.
4311 *              message 2 length is set to zero. message 2 is not returned
4312 *              to memory. return is ok.
4313 *
4314 *       File:  ss_msg.c
4315 *
4316 */
4317
4318
4319 S16 SCatMsg
4320 (
4321 Buffer *mBuf1,              /* message 1 */
4322 Buffer *mBuf2,              /* message 2 */
4323 Order order
4324 )
4325 {
4326    SsMsgInfo *minfo1;
4327    SsMsgInfo *minfo2;
4328    Buffer *tmp;
4329    Buffer *newb;
4330
4331 #if (ERRCLASS & ERRCLS_INT_PAR)
4332    /* check message buffer 1 */
4333    if (mBuf1 == NULLP)
4334    {
4335       SSLOGERROR(ERRCLS_INT_PAR, ESS148, ERRZERO, "SCatMsg : Null Buffer");
4336       return RFAILED;
4337    }
4338    /* check message buffer 2 */
4339    if (mBuf2 == NULLP)
4340    {
4341       SSLOGERROR(ERRCLS_INT_PAR, ESS149, ERRZERO, "SCatMsg : Null Buffer");
4342       return RFAILED;
4343    }
4344    /* ss021.103 - Addition to test if same buffer */
4345    /* check message buffer 1 and 2 not same buffer */
4346    if (mBuf1 == mBuf2)
4347    {
4348       SSLOGERROR(ERRCLS_INT_PAR, ESS150, ERRZERO, "SCatMsg : mBuf1 == mBuf2");
4349       return RFAILED;
4350    }
4351    if ((order != M1M2) && (order != M2M1))
4352    {
4353       SSLOGERROR(ERRCLS_INT_PAR, ESS151, ERRZERO, "SCatMsg : Invalid Order");
4354       return RFAILED;
4355    }
4356    if ((mBuf1->b_datap->db_type != SS_M_PROTO) ||
4357        (mBuf2->b_datap->db_type != SS_M_PROTO))
4358    {
4359       SSLOGERROR(ERRCLS_INT_PAR, ESS152, ERRZERO, 
4360                                          "SCatMsg : Incorrect buffer type");
4361       return RFAILED;
4362    }
4363 #endif
4364
4365    /* no data to append or prepend */
4366    if (!mBuf2->b_cont)
4367       return ROK;
4368
4369    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
4370    minfo2 = (SsMsgInfo*) mBuf2->b_rptr;
4371
4372 /* ss021.103 - Addition to test max length of message is not exceeded */
4373 #if (ERRCLASS & ERRCLS_INT_PAR)
4374 #ifdef LONG_MSG
4375    if (minfo1->len > 0x7FFFFFFF - minfo2->len)
4376 #else
4377    if (minfo1->len > 0x7FFF - minfo2->len)
4378 #endif
4379    {
4380       SSLOGERROR(ERRCLS_INT_PAR, ESS153, ERRZERO, "SCpyFixMsg : messages too big");
4381       return (ROUTRES);
4382    }
4383 #endif
4384    
4385    if (minfo1->region != minfo2->region)
4386    {
4387       /* duplicate mBuf2 from the region/pool of mBuf1, initialise mBuf2 */
4388 /*ss015.13: addition */
4389       if (SCpyMsgMsg(mBuf2, minfo1->region, minfo1->pool, &newb) != ROK)
4390          return RFAILED;
4391
4392       minfo2 = (SsMsgInfo*) newb->b_rptr;
4393    }
4394    else
4395       newb = mBuf2;
4396
4397    if ((tmp = mBuf1->b_cont) == NULLP)
4398    {
4399       mBuf1->b_cont = newb->b_cont;
4400       minfo1->endptr = minfo2->endptr;
4401    }
4402    else
4403    {
4404       if (order == M1M2)
4405       /* attach newb after mBuf1 */
4406       {
4407          minfo1->endptr->b_cont = newb->b_cont;
4408          minfo1->endptr = minfo2->endptr;
4409       }
4410       else
4411       {
4412          if (order == M2M1)
4413          /* attach newb before mBuf1 */
4414          {
4415             minfo2->endptr->b_cont = mBuf1->b_cont;
4416             mBuf1->b_cont = newb->b_cont;
4417          }
4418          else /* invalid order */
4419          {
4420 #if (ERRCLASS & ERRCLS_DEBUG)
4421             SSLOGERROR(ERRCLS_DEBUG, ESS154, ERRZERO, "SCatMsg:Invalid order");
4422 #endif
4423             if (newb && (newb != mBuf2))
4424                (Void) SPutMsg(newb);
4425             return RFAILED;
4426          }
4427       }
4428    }
4429    minfo1->len += minfo2->len;
4430
4431
4432    minfo2->endptr = NULLP;
4433    minfo2->len = 0;
4434  
4435    newb->b_cont = NULLP;
4436
4437    if (newb != mBuf2)
4438    {
4439       (Void) SPutMsg(newb);
4440       (Void) SInitMsg(mBuf2);
4441    }
4442
4443    return ROK;
4444 }
4445
4446 /*
4447 *
4448 *       Fun:   SRepMsg
4449 *
4450 *       Desc:  This function replaces one byte of data in a message.
4451 *
4452 *       Ret:   ROK      - ok
4453 *              ROKDNA   - ok, data not available
4454 *              RFAILED  - failed, general (optional)
4455 *
4456 *       Notes: index is 0 based and indicates location in message
4457 *
4458 *               if index is less than the length of the message:
4459 *              data is replaced at specified index. message length
4460 *              is unchanged. return is ok.
4461 *
4462 *              if index is greater than or equal to
4463 *              the length of the message: message is unchanged.
4464 *              return is ok, data not available.
4465 *
4466 *       File:  ss_msg.c
4467 *
4468 */
4469
4470
4471 S16 SRepMsg
4472 (
4473 Data data,                  /* data */
4474 Buffer *mBuf,               /* message buffer */
4475 MsgLen idx
4476 )
4477 {
4478    SsMsgInfo *minfo;
4479    Buffer *tmp;
4480    Buffer *newb;
4481    Buffer *prev;
4482    MsgLen numBytes;
4483
4484
4485 #ifdef T2K_MEM_LEAK_DBG
4486    char* file = __FILE__;
4487    uint32_t line = __LINE__;
4488 #endif
4489 #if ( ERRCLASS & ERRCLS_INT_PAR)
4490    /* check message buffer */
4491    if (mBuf == NULLP)
4492    {
4493       SSLOGERROR(ERRCLS_INT_PAR, ESS155, ERRZERO, "SRepMsg : Null Buffer");
4494       return RFAILED;
4495    }
4496    if (idx < 0)
4497    {
4498       SSLOGERROR(ERRCLS_INT_PAR, ESS156, ERRZERO, "SRepMsg : Invalid index");
4499       return RFAILED;
4500    }
4501    if (mBuf->b_datap->db_type != SS_M_PROTO)
4502    {
4503       SSLOGERROR(ERRCLS_INT_PAR, ESS157, ERRZERO, "SRepMsg : Incorrect buffer\
4504                                                    type");
4505       return RFAILED;
4506    }
4507 #endif
4508
4509    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4510
4511    /* if index > length of the buffer */
4512    if (minfo->len <= idx)
4513    {
4514       return (ROKDNA);
4515    }
4516
4517    tmp = mBuf->b_cont; /* get the first SS_M_DATA blk */
4518    prev = mBuf;        /* parent */
4519
4520    FIND_OFFSET_AND_PREV(prev, tmp, idx)
4521
4522    /* if ref cnt is greater than 1, duplicate tmp */
4523    if (tmp->b_datap->db_ref > 1)
4524    {
4525       /* allocate a message blk of message size of tmp */
4526       numBytes = tmp->b_wptr - tmp->b_rptr;
4527       if (ssGetDBufOfSize(minfo->region, numBytes, &newb) != ROK)
4528       {
4529          SSLOGERROR(ERRCLS_DEBUG, ESS158, ERRZERO, "SRepMsg : ssGetDBufOfSize\
4530                     failed");
4531          return RFAILED;
4532       }
4533       while (numBytes--)
4534          *newb->b_wptr++ = *tmp->b_rptr++;
4535
4536       newb->b_cont = tmp->b_cont;
4537       prev->b_cont = newb;
4538       if (minfo->endptr == tmp)
4539          minfo->endptr = newb;
4540
4541       /* free tmp */
4542       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
4543       tmp = newb;
4544    }
4545    *(tmp->b_rptr + idx) = data;
4546
4547    return ROK;
4548 }
4549
4550 /*
4551 *
4552 *       Fun:   SUpdMsg
4553 *
4554 *       Desc:  Update a message with a new dBuf
4555 *
4556 *       Ret:   ROK/RFAILED
4557 *
4558 *       Notes:
4559 *
4560 *       File:  ss_msg.c
4561 *
4562 */
4563
4564
4565 S16 SUpdMsg
4566 (
4567 Buffer *mBuf,                   /* message buffer */
4568 Buffer *dBuf,                   /* data buffer */
4569 MsgLen dLen
4570 )
4571 {
4572    SsMsgInfo *minfo;
4573
4574 #if (ERRCLASS & ERRCLS_INT_PAR)
4575    if (!mBuf)
4576    {
4577       SSLOGERROR(ERRCLS_INT_PAR, ESS159, ERRZERO, "SUpdMsg : Null Buffer");
4578       return RFAILED;
4579    }
4580    if (!dBuf)
4581    {
4582       SSLOGERROR(ERRCLS_INT_PAR, ESS160, ERRZERO, "SUpdMsg : Null Buffer");
4583       return RFAILED;
4584    }
4585    if (dLen < 0)
4586    {
4587       SSLOGERROR(ERRCLS_INT_PAR, ESS161, ERRZERO, "SUpdMsg : Invalid length");
4588       return RFAILED;
4589    }
4590    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
4591        (dBuf->b_datap->db_type != SS_M_DATA))
4592    {
4593       SSLOGERROR(ERRCLS_INT_PAR, ESS162, ERRZERO, "SUpdMsg : Incorrect buffer\
4594                                                    type");
4595       return RFAILED;
4596    }
4597 #endif
4598
4599    /* get the message info of mBuf */
4600    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4601
4602   /* accept zero length data */
4603
4604    /* buffer offset out of bounds */
4605    if ((dBuf->b_rptr + dLen) > dBuf->b_datap->db_lim)
4606    {
4607       SSLOGERROR(ERRCLS_DEBUG, ESS163, ERRZERO, "SUpdMsg:Offset out of bounds");
4608       return RFAILED;
4609    }
4610
4611    /* offset write ptr from read ptr by dLen */
4612    dBuf->b_wptr = dBuf->b_rptr + dLen;
4613
4614    /* attach dBuf at the end of mBuf */
4615    if (minfo->endptr)
4616       minfo->endptr->b_cont = dBuf;
4617    else
4618       mBuf->b_cont = dBuf;
4619
4620    /* update SsMsgInfo */
4621    minfo->endptr = dBuf;
4622
4623    minfo->len += dLen;
4624
4625    return ROK;
4626 }
4627
4628 /*
4629 *
4630 *       Fun:   SAddDBufPst
4631 *
4632 *       Desc:  This function queues a data buffer to the
4633 *              back of the specified message buffer .
4634 *
4635 *       Ret:   ROK     - ok
4636 *              RFAILED - failed, general (optional)
4637 *
4638 *       Notes: if queue is empty: buffer is placed in the queue.
4639 *              queue length is incremented.
4640 *
4641 *              if queue is not empty: buffer is placed behind all
4642 *              other buffers in queue. queue length is incremented.
4643 *
4644 *       File:  ss_msg.c
4645 *
4646 */
4647
4648  
4649 S16 SAddDBufPst
4650 (
4651 Buffer *mBuf,                   /* message buffer */
4652 Buffer *dBuf
4653 )
4654 {
4655    SsMsgInfo *minfo;
4656
4657 #if (ERRCLASS & ERRCLS_INT_PAR)
4658    /* check buffer queue */
4659    if (!mBuf || !dBuf)
4660    {
4661       SSLOGERROR(ERRCLS_INT_PAR, ESS164, ERRZERO, "SAddDBufPst : Null Buffer");
4662       return RFAILED;
4663    }
4664    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
4665        (dBuf->b_datap->db_type != SS_M_DATA))
4666    {
4667       SSLOGERROR(ERRCLS_INT_PAR, ESS165, ERRZERO, "SAddDBufPst : Incorrect\
4668                                                    buffer type");
4669       return RFAILED;
4670    }
4671 #endif
4672
4673    /* no data, return */
4674    if (dBuf->b_wptr == dBuf->b_rptr)
4675       return ROK;
4676
4677    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4678  
4679    /* attach dBuf at the end of mBuf */
4680    if (minfo->endptr)
4681       minfo->endptr->b_cont = dBuf;
4682    else
4683       mBuf->b_cont = dBuf;
4684
4685    /* update SsMsgInfo */
4686    minfo->endptr = dBuf;
4687
4688    minfo->len += dBuf->b_wptr - dBuf->b_rptr;
4689
4690    return ROK;
4691 }
4692
4693 /*
4694 *
4695 *       Fun:   SAddDBufPre
4696 *
4697 *       Desc:  This function queues a data buffer to the
4698 *              front of the specified message buffer.
4699 *
4700 *       Ret:   ROK     - ok
4701 *              RFAILED - failed, general (optional)
4702 *
4703 *       Notes: if buffer queue is empty: buffer is placed in the queue. queue
4704 *              length is incremented.
4705 *
4706 *              if buffer queue is not empty: buffer is placed in front of all
4707 *              other buffers in queue. queue length is incremented.
4708 *
4709 *       File:  ss_msg.c
4710 *
4711 */
4712
4713  
4714 S16 SAddDBufPre
4715 (
4716 Buffer *mBuf,                    /* message buffer */
4717 Buffer *dBuf
4718 )
4719 {
4720    SsMsgInfo *minfo;
4721    Buffer *tmp;
4722
4723 #if (ERRCLASS & ERRCLS_INT_PAR)
4724    /* check buffer queue */
4725    if (!mBuf || !dBuf)
4726    {
4727       SSLOGERROR(ERRCLS_INT_PAR, ESS166, ERRZERO, "SAddDBufPre : Null Buffer");
4728       return RFAILED;
4729    }
4730    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
4731        (dBuf->b_datap->db_type != SS_M_DATA))
4732    {
4733       SSLOGERROR(ERRCLS_INT_PAR, ESS167, ERRZERO, "SAddDBufPre : Incorrect\
4734                                                    buffer type");
4735       return RFAILED;
4736    }
4737 #endif
4738  
4739    /* no data, return */
4740    if (dBuf->b_wptr == dBuf->b_rptr)
4741       return ROK;
4742  
4743    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4744
4745    tmp = mBuf->b_cont;
4746
4747    /* attach dBuf at the start of mBuf */
4748    mBuf->b_cont = dBuf;
4749    dBuf->b_cont = tmp;
4750
4751    minfo->len += dBuf->b_wptr - dBuf->b_rptr;
4752
4753    if (!tmp)
4754       minfo->endptr = dBuf;
4755
4756    return ROK;
4757 }
4758  
4759 /*
4760 *
4761 *       Fun:   SRemDBufPre
4762 *
4763 *       Desc:  This function dequeues a data buffer from
4764 *              the front of the specified message buffer.
4765 *
4766 *       Ret:   ROK     - ok
4767 *              ROKDNA  - ok, data not available
4768 *              RFAILED - failed, general (optional)
4769 *
4770 *       Notes: if queue is empty: pointer to buffer is set to null and
4771 *              return is ok, data not available. queue length is unchanged.
4772 *
4773 *              if queue is not empty: pointer to buffer is set to first
4774 *              buffer in queue, first buffer in queue is removed and
4775 *              return is ok. queue length is decremented.
4776 *
4777 *       File:  ss_msg.c
4778 *
4779 */
4780
4781  
4782 S16 SRemDBufPre
4783 (
4784 Buffer *mBuf,                   /* message buffer */
4785 Buffer **dBufPtr
4786 )
4787 {
4788    SsMsgInfo *minfo;
4789
4790 #if (ERRCLASS & ERRCLS_INT_PAR)
4791    /* check buffer pointer */
4792    if (dBufPtr == NULLP)
4793    {
4794       SSLOGERROR(ERRCLS_INT_PAR, ESS168, ERRZERO, "SRemDBufPre : Null Buffer");
4795       return RFAILED;
4796    }
4797    /* check queue */
4798    if (mBuf == NULLP)
4799    {
4800       SSLOGERROR(ERRCLS_INT_PAR, ESS169, ERRZERO, "SRemDBufPre : Null Buffer");
4801       return RFAILED;
4802    }
4803    if (mBuf->b_datap->db_type != SS_M_PROTO)
4804    {
4805       SSLOGERROR(ERRCLS_INT_PAR, ESS170, ERRZERO, "SRemDBufPre : Incorrect\
4806                                                    buffer type");
4807       return RFAILED;
4808    }
4809 #endif
4810
4811    /* no data blk, return */
4812    if ((*dBufPtr = mBuf->b_cont) == NULLP)
4813    {
4814       return ROKDNA;
4815    }
4816    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4817
4818    mBuf->b_cont = (*dBufPtr)->b_cont;
4819    (*dBufPtr)->b_cont = NULLP;
4820
4821    /* update SsMsgInfo */
4822    if (!(minfo->len -= (*dBufPtr)->b_wptr - (*dBufPtr)->b_rptr))
4823       minfo->endptr = NULLP;
4824
4825    return ROK;
4826 }
4827
4828 /*
4829 *
4830 *       Fun:   SRemDBufPst
4831 *
4832 *       Desc:  This function dequeues a data or message buffer from the
4833 *              back of the specified message buffer.
4834 *
4835 *       Ret:   ROK     - ok
4836 *              ROKDNA  - ok, data not available
4837 *              RFAILED - failed, general (optional)
4838 *
4839 *       Notes: if queue is empty: pointer to buffer is set to null and
4840 *              return is ok, data not available. queue length is unchanged.
4841 *
4842 *              if queue is not empty: pointer to buffer is set to last
4843 *              buffer in queue, last buffer in queue is removed and
4844 *              return is ok. queue length is decremented.
4845 *
4846 *       File:  ss_msg.c
4847 *
4848 */
4849
4850  
4851 S16 SRemDBufPst
4852 (
4853 Buffer *mBuf,                   /* message buffer */
4854 Buffer **dBufPtr
4855 )
4856 {
4857    SsMsgInfo *minfo;
4858    Buffer *tmp;
4859
4860 #if (ERRCLASS & ERRCLS_INT_PAR)
4861    /* check buffer pointer */
4862    if (!dBufPtr)
4863    {
4864       SSLOGERROR(ERRCLS_INT_PAR, ESS171, ERRZERO, "SRemDBufPst : Null Buffer");
4865       return RFAILED;
4866    }
4867    /* check message */
4868    if (!mBuf)
4869    {
4870       SSLOGERROR(ERRCLS_INT_PAR, ESS172, ERRZERO, "SRemDBufPst : Null Buffer");
4871       return RFAILED;
4872    }
4873    if (mBuf->b_datap->db_type != SS_M_PROTO)
4874    {
4875       SSLOGERROR(ERRCLS_INT_PAR, ESS173, ERRZERO, "SRemDBufPst : Incorrect\
4876                                                    buffer type");
4877       return RFAILED;
4878    }
4879 #endif
4880  
4881    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4882  
4883    /* no data blk, return */
4884    if ((*dBufPtr = minfo->endptr) == NULLP)
4885    {
4886       return ROKDNA;
4887    }
4888    for (tmp = mBuf; tmp->b_cont != (*dBufPtr); )
4889       tmp = tmp->b_cont;
4890
4891    tmp->b_cont = NULLP;
4892
4893    /* update SsMsgInfo */
4894    if (!(minfo->len -= (*dBufPtr)->b_wptr - (*dBufPtr)->b_rptr))
4895       minfo->endptr = NULLP;
4896    else
4897       minfo->endptr = tmp;
4898
4899    return ROK;
4900 }
4901
4902 /*
4903 *
4904 *       Fun:   SInitNxtDBuf
4905 *
4906 *       Desc:  Initialize next Data Buffer Id
4907 *
4908 *       Ret:   ROK/RFAILED
4909 *
4910 *       Notes: Must be called prior to SGetNxtDBuf
4911 *
4912 *       File:  ss_msg.c
4913 *
4914 */
4915
4916  
4917 S16 SInitNxtDBuf
4918 (
4919 Buffer *mBuf
4920 )
4921 {
4922    SsMsgInfo *minfo;
4923
4924 #if (ERRCLASS & ERRCLS_INT_PAR)
4925    if (!mBuf)
4926    {
4927       SSLOGERROR(ERRCLS_INT_PAR, ESS174, ERRZERO, "SInitNxtDBuf : Null Buffer");
4928       return RFAILED;
4929    }
4930    if (mBuf->b_datap->db_type != SS_M_PROTO)
4931    {
4932       SSLOGERROR(ERRCLS_INT_PAR, ESS175, ERRZERO, "SInitNxtDBuf : Incorrect\
4933                                                    buffer type");
4934       return RFAILED;
4935    }
4936 #endif
4937
4938    minfo = (SsMsgInfo*) mBuf->b_rptr;
4939
4940    /* set the next ptr of mBuf to point to the first SS_M_DATA blk */
4941    minfo->next = mBuf->b_cont;
4942
4943    return ROK;
4944 }
4945
4946 /*
4947 *
4948 *       Fun:   SGetNxtDBuf
4949 *
4950 *       Desc:  Get next dBuf in message chain
4951 *
4952 *       Ret:   ROK/RFAILED
4953 *
4954 *       Notes: Must be called after SInitNxtDBuf
4955 *
4956 *       File:  ss_msg.c
4957 *
4958 */
4959
4960  
4961 S16 SGetNxtDBuf
4962 (
4963 Buffer *mBuf,                   /* message buffer */
4964 Buffer **dBuf
4965 )
4966 {
4967    SsMsgInfo *minfo;
4968
4969 #if (ERRCLASS & ERRCLS_INT_PAR)
4970    if (!mBuf)
4971    {
4972       SSLOGERROR(ERRCLS_INT_PAR, ESS176, ERRZERO, "SGetNxtDBuf : Null Buffer");
4973       return RFAILED;
4974    }
4975    if (!dBuf)
4976    {
4977       SSLOGERROR(ERRCLS_INT_PAR, ESS177, ERRZERO, "SGetNxtDBuf : Null Buffer");
4978       return RFAILED;
4979    }
4980    if (mBuf->b_datap->db_type != SS_M_PROTO)
4981    {
4982       SSLOGERROR(ERRCLS_INT_PAR, ESS178, ERRZERO, "SGetNxtDBuf : Incorrect\
4983                                                    buffer type");
4984       return RFAILED;
4985    }
4986 #endif
4987
4988    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4989
4990    /* if next ptr of mBuf is NULLP, return */
4991    if ((*dBuf = minfo->next) == NULLP)
4992       return (ROKDNA);
4993
4994    /* update next */
4995    minfo->next = (*dBuf)->b_cont;
4996
4997    return ROK;
4998 }
4999
5000 /*
5001 *
5002 *       Fun:   SChkNxtDBuf
5003 *
5004 *       Desc:  check if next data buffer exists.
5005 *
5006 *       Ret:   ROK/ROKDNA/RFAILED
5007 *
5008 *       Notes: doesn't modify nxtDBuf
5009 *
5010 *       File:  ss_msg.c
5011 *
5012 */
5013
5014 S16 SChkNxtDBuf
5015 (
5016 Buffer *mBuf
5017 )
5018 {
5019    SsMsgInfo *minfo;
5020
5021 #if (ERRCLASS & ERRCLS_INT_PAR)
5022    if (!mBuf)
5023    {
5024       SSLOGERROR(ERRCLS_INT_PAR, ESS179, ERRZERO, "SChkNxtDBuf : Null Buffer");
5025       return RFAILED;
5026    }
5027    if (mBuf->b_datap->db_type != SS_M_PROTO)
5028    {
5029       SSLOGERROR(ERRCLS_INT_PAR, ESS180, ERRZERO, "SChkNxtDBuf : Incorrect\
5030                                                    buffer type");
5031       return RFAILED;
5032    }
5033 #endif /* ERRCLASS */
5034
5035    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5036
5037    /* if next is valid, return ROK */
5038    if (minfo->next)
5039       return ROK;
5040    else
5041       return (ROKDNA);
5042 }
5043
5044 /*
5045 *
5046 *       Fun:   SGetDataRx
5047 *
5048 *       Desc:  Given a data buffer, return a pointer to the
5049 *              data payload, and the length of the payload
5050 *
5051 *
5052 *       Ret:   ROK ok
5053 *              RFAILED error
5054 *
5055 *       Notes: This assumes an uninitialized dBuf
5056 *
5057 *       File:  ss_msg.c
5058 *
5059 */
5060
5061  
5062 S16 SGetDataRx
5063 (
5064 Buffer *dBuf,                   /* data buffer */
5065 MsgLen pad,                     /* pad */
5066 Data **retDatPtr,               /* return data pointer */
5067 MsgLen *retDatLen
5068 )
5069 {
5070
5071 #if (ERRCLASS & ERRCLS_INT_PAR)
5072    if (!dBuf)
5073    {
5074       SSLOGERROR(ERRCLS_INT_PAR, ESS181, ERRZERO, "SGetDataRx : Null Buffer");
5075       return RFAILED;
5076    }
5077    if (!retDatLen || (pad < 0))
5078    {
5079       SSLOGERROR(ERRCLS_INT_PAR, ESS182, ERRZERO, "SGetDataRx : Null Buffer");
5080       return RFAILED;
5081    }
5082    if (!retDatPtr)
5083    {
5084       SSLOGERROR(ERRCLS_INT_PAR, ESS183, ERRZERO, "SGetDataRx : Null Buffer");
5085       return RFAILED;
5086    }
5087    if (dBuf->b_datap->db_type != SS_M_DATA)
5088    {
5089       SSLOGERROR(ERRCLS_INT_PAR, ESS184, ERRZERO, "SGetDataRx : Incorrect\
5090                                                    buffer type");
5091       return RFAILED;
5092    }
5093 #endif /* ERRCLASS */
5094
5095    if (dBuf->b_datap->db_ref > 1)   /* cannot write to a shared buffer */
5096    {
5097 #if (ERRCLASS & ERRCLS_DEBUG)
5098       SSLOGERROR(ERRCLS_DEBUG, ESS185, ERRZERO, "SGetDataRx : Reference\
5099                                                  count > 1");
5100 #endif
5101       return RFAILED;
5102    }
5103
5104    /* return the write ptr loc(with padding) if there is data to write to */
5105    if ((*retDatLen = (dBuf->b_datap->db_lim - dBuf->b_wptr - pad)) > 0)
5106       *retDatPtr = dBuf->b_wptr + pad;
5107    else
5108    {
5109       *retDatPtr = NULLP;
5110 #if (ERRCLASS & ERRCLS_DEBUG)
5111       SSLOGERROR(ERRCLS_DEBUG, ESS186, ERRZERO, "SGetDataRx:No data available");
5112 #endif
5113       return RFAILED;
5114    }
5115
5116    return ROK;
5117 }
5118
5119 /*
5120 *
5121 *       Fun:   SGetDataTx
5122 *
5123 *       Desc:  Given a data buffer, return a pointer to the
5124 *              data payload, and the length of the payload
5125 *
5126 *
5127 *       Ret:   ROK ok
5128 *              RFAILED error
5129 *
5130 *       Notes: This assumes an initialized dBuf
5131 *
5132 *       File:  ss_msg.c
5133 *
5134 */
5135
5136  
5137 S16 SGetDataTx
5138 (
5139 Buffer *dBuf,                   /* data buffer */
5140 Data **retDatPtr,               /* return data pointer */
5141 MsgLen *retDatLen               /* return data length */
5142 )
5143 {
5144
5145 #if (ERRCLASS & ERRCLS_INT_PAR)
5146    /* ss021.103 - Modification to check parameters */
5147    if (!retDatPtr)
5148    {
5149       SSLOGERROR(ERRCLS_INT_PAR, ESS187, ERRZERO, "SGetDataTx : Null Buffer");
5150       return RFAILED;
5151    }
5152    if (!dBuf)
5153    {
5154       SSLOGERROR(ERRCLS_INT_PAR, ESS188, ERRZERO, "SGetDataTx : Null Buffer");
5155       *retDatPtr = (Data *)NULLP;
5156       return RFAILED;
5157    }
5158    if (!retDatLen)
5159    {
5160       SSLOGERROR(ERRCLS_INT_PAR, ESS189, ERRZERO, "SGetDataTx : Null Buffer");
5161       return RFAILED;
5162    }
5163    if (dBuf->b_datap->db_type != SS_M_DATA)
5164    {
5165       SSLOGERROR(ERRCLS_INT_PAR, ESS190, ERRZERO, "SGetDataTx : Incorrect\
5166                                                    buffer type");
5167       *retDatPtr = (Data *)NULLP;
5168       return RFAILED;
5169    }
5170 #endif /* ERRCLASS */
5171
5172    /* if there is data, return read ptr */
5173    if ((*retDatLen = dBuf->b_wptr - dBuf->b_rptr))
5174       *retDatPtr = dBuf->b_rptr;
5175    else
5176    {
5177       *retDatPtr = NULLP;
5178 #if (ERRCLASS & ERRCLS_DEBUG)
5179       SSLOGERROR(ERRCLS_DEBUG, ESS191, ERRZERO, "SGetDataTx : Buffer empty");
5180 #endif
5181       return RFAILED;
5182    }
5183
5184    return ROK;
5185 }
5186
5187 #ifndef SS_ENABLE_MACROS
5188 \f
5189 /*
5190 *
5191 *       Fun:   SGetBufRegionPool
5192 *
5193 *       Desc:  returns the region and pool of the message buffer
5194 *
5195 *       Ret:   ROK on success
5196 *              RFAILED on error
5197 *
5198 *       Notes: None
5199 *
5200 *       File:  ss_msg.c
5201 *
5202 */
5203 S16 SGetBufRegionPool
5204 (
5205 Buffer *mBuf,                   /* message buffer */
5206 Region *region,                 /* region */
5207 Pool   *pool                    /* pool */
5208 )
5209 {
5210    SsMsgInfo *mInfo;            /* message info pointer */
5211
5212
5213 #if (ERRCLASS & ERRCLS_INT_PAR)
5214
5215    if (mBuf == NULLP)
5216    {
5217       SSLOGERROR(ERRCLS_INT_PAR, ESS192, ERRZERO, 
5218                  "SGetBufRegionPool : Null Buffer");
5219       return RFAILED;
5220    }
5221    if ((region == NULLP) && (pool == NULLP))
5222    {
5223       SSLOGERROR(ERRCLS_INT_PAR, ESS193, ERRZERO, 
5224                  "SGetBufRegionPool : Null region and pool pointers");
5225       return RFAILED;
5226    }
5227    if (mBuf->b_datap->db_type != SS_M_PROTO)
5228    {
5229       SSLOGERROR(ERRCLS_INT_PAR, ESS194, ERRZERO, 
5230                  "SUpdMsg : Incorrect buffer type");
5231       return RFAILED;
5232    }
5233 #endif /* (ERRCLASS & ERRCLS_INT_PAR */
5234
5235    /* get message info of mBuf */
5236    if ((mInfo = (SsMsgInfo *) mBuf->b_rptr) == NULLP)
5237    {
5238 #if (ERRCLASS & ERRCLS_DEBUG)
5239       SSLOGERROR(ERRCLS_DEBUG, ESS195, ERRZERO, 
5240                  "SGetBufRegionPool : mBuf's control data is null");
5241 #endif
5242       return RFAILED;
5243    }
5244
5245    if (region != NULLP)
5246       *region = mInfo->region;
5247    if (pool != NULLP)
5248       *pool   = mInfo->pool;
5249
5250    return ROK;
5251 } /* end of SGetBufRegionPool */
5252
5253 #endif /* SS_ENABLE_MACROS */
5254 \f
5255 /*
5256 *
5257 *       Fun:   SCompressMsg
5258 *
5259 *       Desc:  This function is used to compress a message into
5260 *              the minimum number of data buffers needed.
5261 *
5262 *       Ret:   ROK      - ok
5263 *              RFAILED  - failed, general (optional)
5264 *              ROUTRES  - failed, out of resources (optional)
5265 *
5266 *       Notes: None
5267 *
5268 *       File:  ss_msg.c
5269 *
5270 */
5271
5272  
5273 S16 SCompressMsg
5274 (
5275 Buffer *mBuf
5276 )
5277 {
5278    SsMsgInfo *minfo;
5279    MsgLen numBytes;
5280    Buffer *dBuf;
5281    Buffer *tmp;
5282    Buffer *next;
5283    S16 ret;
5284
5285 #ifdef T2K_MEM_LEAK_DBG
5286    char* file = __FILE__;
5287    uint32_t line = __LINE__;
5288 #endif
5289
5290 #if (ERRCLASS & ERRCLS_INT_PAR)
5291    if (!mBuf)
5292    {
5293       SSLOGERROR(ERRCLS_INT_PAR, ESS196, ERRZERO, "SCompressMsg : Null Buffer");
5294       return RFAILED;
5295    }
5296    if (mBuf->b_datap->db_type != SS_M_PROTO)
5297    {
5298       SSLOGERROR(ERRCLS_INT_PAR, ESS197, ERRZERO, "SCompressMsg : Incorrect\
5299                                                    buffer type");
5300       return RFAILED;
5301    }
5302 #endif
5303
5304    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5305
5306    if ((tmp = mBuf->b_cont) == minfo->endptr)
5307    {
5308       return ROK;
5309    }
5310
5311    /* allocate a data buffer of size bytes*/
5312    if ((ret = ssGetDBufOfSize(minfo->region, minfo->len, &dBuf)) != ROK)
5313    {
5314       SSLOGERROR(ERRCLS_DEBUG, ESS198, ERRZERO, "SAddMsgRef : ssGetDBufOfSize\
5315                  failed");
5316       return RFAILED;
5317    }
5318    dBuf->b_datap->db_type = SS_M_DATA;
5319
5320    while (tmp)
5321    {
5322       /* determine number of bytes to be copied */
5323       numBytes = tmp->b_wptr - tmp->b_rptr;
5324
5325       /* copy data */
5326
5327 /* ss002.13: addition */
5328       /* ss003.13: addition */
5329       SMemCpy( (Void *)dBuf->b_wptr, (Void *)tmp->b_rptr, (size_t)numBytes);
5330
5331       dBuf->b_wptr += numBytes;
5332       tmp->b_rptr += numBytes;
5333
5334
5335       next = tmp->b_cont;
5336       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
5337       tmp = next;
5338    }
5339    /* update mBuf and SsMsgInfo */
5340    mBuf->b_cont = dBuf;
5341    minfo->endptr = dBuf;
5342
5343    return ROK;
5344 }
5345
5346 /*
5347 *
5348 *       Fun:   SPrntMsg
5349 *
5350 *       Desc:  This function prints the contents of a message. The
5351 *              following information is printed: queue length,
5352 *              message length, direction, hexadecimal and ASCII
5353 *              (if appropriate) values of all bytes in the message.
5354 *
5355 *              This function should be used for debugging only.
5356 *
5357 *       Ret:   ROK      - ok
5358 *
5359 *       Notes: None
5360 *
5361 *       File:  ss_msg.c
5362 *
5363 */
5364   
5365 S16 SPrntMsg
5366 (
5367 Buffer *mBuf,               /* message buffer */
5368 S16 src,                    /* source id */
5369 S16 dst                     /* destination id */
5370 )
5371 {
5372    QLen qlen =0;               /* queue length */
5373    MsgLen mlen =0;             /* message length */
5374 /*
5375  * SDeRegTTsk patch
5376  */
5377         /* ss038.103 : 102061 Changed to MsgLen from S16 */
5378    MsgLen i =0;                /* counter */
5379    S16 j =0;                   /* counter */
5380    S16 k =0;                   /* counter */
5381    uint8_t data =0;                 /* data */
5382    uint8_t tdata[16] = {0};            /* temporary data */
5383    S8 prntBuf[256] ={0};         /* print buffer */
5384    Buffer *tmp = NULLP;             /* buffer ptr */
5385    Data *cptr =NULLP;
5386 /* ss012.13: Addition */
5387    Data reg =0;
5388
5389
5390
5391    if (mBuf == NULLP)
5392    {
5393       sprintf(prntBuf,"\nmsg: empty\n");
5394       SPrint(prntBuf);
5395       SPrint( (S8*)"\n\n");
5396       return ROK;
5397    }
5398
5399    for (qlen = 0, tmp = mBuf->b_cont; tmp; qlen++)
5400       tmp = tmp->b_cont;
5401    mlen = ((SsMsgInfo*)(mBuf->b_rptr))->len;
5402    reg = ((SsMsgInfo*)(mBuf->b_rptr))->region;
5403    /*ss013.301: Fixed Warnings for 32/64 bit compilation*/
5404    sprintf(prntBuf,"\nmsg: qlen: %04d mlen: %04d   %02d-->%02d region: %02d\n",
5405            (uint16_t)qlen,(uint16_t)mlen,src,dst,reg);
5406    SPrint( prntBuf);
5407 #ifdef XEON_SPECIFIC_CHANGES
5408    printf("%s\n", prntBuf);
5409 #endif   
5410    SPrint((S8*) "dat: ");
5411 #ifdef XEON_SPECIFIC_CHANGES
5412    printf("\ndat: ");
5413 #endif   
5414
5415    if (mlen == 0)
5416    {
5417       sprintf(prntBuf," empty\n");
5418       SPrint(prntBuf);
5419       SPrint( (S8*)"\n\n");
5420       return ROK;
5421    }
5422    tmp = mBuf->b_cont;
5423    cptr = tmp->b_rptr;
5424    data= *cptr++; 
5425    i = 0;
5426    while( i < mlen  ) 
5427    {
5428       j = 0;
5429       for( j = 0; j < 16; j++)
5430       {
5431          if( i < mlen )
5432          { 
5433             /* print hex */
5434             tdata[j]=data;
5435             sprintf( prntBuf,"%02x ",(uint16_t) data);
5436             SPrint( prntBuf);
5437 #ifdef XEON_SPECIFIC_CHANGES
5438    printf("%s\n", prntBuf);
5439 #endif   
5440             if (cptr == tmp->b_wptr)
5441             {
5442                tmp = tmp->b_cont;
5443                if (tmp)
5444                   cptr = tmp->b_rptr;
5445             }
5446        /* ss024.103 - Modification to fix bug */
5447             i++;
5448        if ( i < mlen )
5449                data = *cptr++;
5450          }
5451          else
5452          {
5453             sprintf( prntBuf,"   ");
5454             SPrint( prntBuf );
5455 #ifdef XEON_SPECIFIC_CHANGES
5456    printf("%s\n", prntBuf);
5457 #endif   
5458          }
5459       } 
5460       for (k = 0; k < 16; k++)
5461       {
5462          if( (i >= mlen) && (k>=(mlen%16)) && mlen%16 != 0)
5463             break;
5464          if (AIsAscii(tdata[k]))
5465          {
5466             /* print character if printable */
5467             sprintf(prntBuf,"%c",tdata[k]);
5468             SPrint( prntBuf);
5469 #ifdef XEON_SPECIFIC_CHANGES
5470    printf("%s\n", prntBuf);
5471 #endif   
5472          }
5473          else
5474          {
5475             /* print . if non printable */
5476             SPrint((S8*) ".");
5477 #ifdef XEON_SPECIFIC_CHANGES
5478    printf(".");
5479 #endif   
5480          }
5481       }
5482       sprintf(prntBuf,"\n     ");
5483       SPrint(prntBuf);
5484 #ifdef XEON_SPECIFIC_CHANGES
5485    printf("%s\n", prntBuf);
5486 #endif   
5487    }
5488    return ROK;
5489
5490 } /* end of SPrntMsg */
5491
5492
5493
5494 /*
5495 *
5496 *       Fun:   SGetPstMsgMult
5497 *
5498 *       Desc:  This function allocates consecutive bytes of data at the
5499 *              end of a message.
5500 *
5501 *       Ret:   ROK      - ok
5502 *              RFAILED  - failed, general (optional)
5503 *              ROUTRES  - failed, out of resources (optional)
5504 *
5505 *       Notes: if message is empty: message length is incremented.
5506 *              return is ok.
5507 *
5508 *              if message is not empty: space is allocated in back of
5509 *              all other data in message.  message length is incremented.
5510 *              return is ok.
5511 *
5512 *       File:  ss_msg.c
5513 *
5514 */
5515 S16 SGetPstMsgMult
5516 (
5517 MsgLen cnt,                 /* count */
5518 Buffer *mBuf                /* message buffer */
5519 )
5520 {
5521    Buffer *tmp;
5522    Buffer *newb;
5523    SsMsgInfo *minfo;
5524    MsgLen numBytes;
5525    MsgLen avail;
5526
5527
5528 #if ( ERRCLASS & ERRCLS_INT_PAR )
5529    /* check message buffer */
5530    if (mBuf == NULLP)
5531    {
5532       return RFAILED;
5533    }
5534    /* check count */
5535    if (cnt <= 0)
5536    {
5537       return RFAILED;
5538    }
5539    if (mBuf->b_datap->db_type != SS_M_PROTO)
5540    {
5541       return RFAILED;
5542    }
5543 #endif
5544    /* get the message info */
5545    minfo = (SsMsgInfo *) (mBuf->b_rptr);
5546
5547    /* init avail */
5548    avail = 0;
5549
5550    if ((tmp = minfo->endptr) && (tmp->b_datap->db_ref == 1) &&
5551        ((avail = tmp->b_datap->db_lim - tmp->b_wptr)))
5552    {
5553       numBytes = MIN(cnt, avail);
5554
5555       cnt -= numBytes;
5556       minfo->len += numBytes;
5557
5558
5559 /* ss002.13 addition */
5560 /* ss003.13 addition */
5561       SMemSet( (Void *)tmp->b_wptr, (Data)'\0', (size_t)numBytes);
5562
5563       tmp->b_wptr += numBytes;
5564       if (!cnt)
5565          return ROK;
5566    }
5567    if (ssGetDBufOfSize(minfo->region, cnt, &newb) != ROK)
5568    {
5569       /* ss027.103 - Modification to fix bug in SGetPstMsgMult */
5570       if ((avail) && (tmp))
5571          tmp->b_wptr = tmp->b_datap->db_lim - avail;
5572       minfo->len -= avail;
5573       return (ROUTRES);
5574    }
5575
5576    minfo->len += cnt;
5577
5578
5579 /* ss002.13: addition */
5580    /* ss003.13: addition */
5581    SMemSet( (Void *)newb->b_wptr, (Data)'\0', (size_t)cnt);
5582
5583    newb->b_wptr += cnt;
5584
5585
5586    if (tmp)
5587       tmp->b_cont = newb;
5588    else
5589       mBuf->b_cont = newb;
5590
5591    minfo->endptr = newb;
5592
5593    return ROK;
5594 }
5595
5596 /*
5597 *
5598 *       Fun:   SChkMsg
5599 *
5600 *       Desc:  Check Message
5601 *
5602 *       Ret:   ROK on success
5603 *              RFAILED on error
5604 *
5605 *       Notes: Check that the first buffer in a message
5606 *              contains at least two bytes. This check is required
5607 *              by 68302/68360 processors to insure accurate fisu
5608 *              generation.
5609 *
5610 *       File:  ss_msg.c
5611 *
5612 */
5613 S16 SChkMsg
5614 (
5615 Buffer *mBuf
5616 )
5617 {
5618    SsMsgInfo *minfo;
5619    Buffer *tmp;
5620
5621  
5622 #if ( ERRCLASS & ERRCLS_INT_PAR )
5623    /* check message buffer */
5624    if (mBuf == NULLP)
5625    {
5626       return RFAILED;
5627    }
5628    if (mBuf->b_datap->db_type != SS_M_PROTO)
5629    {
5630       return RFAILED;
5631    }
5632 #endif
5633    /* get the message info */
5634    minfo = (SsMsgInfo *) (mBuf->b_rptr);
5635
5636    if (minfo->len < 2)
5637       return RFAILED;
5638
5639    /* get the first M_DATA blk*/
5640    tmp = mBuf->b_cont;
5641
5642    if (((tmp->b_rptr + 2) <= tmp->b_wptr) || (SCompressMsg(mBuf) == ROK))
5643       return ROK;
5644
5645    return RFAILED;
5646 }
5647
5648 /*
5649 *
5650 *       Fun:   SAlignDBufEven
5651 *
5652 *       Desc:  align data portion of a data buffer on an even
5653 *              byte boundary.
5654 *
5655 *       Ret:   ROK/RFAILED
5656 *
5657 *       Notes: required for SS7 microcode on the 68302
5658 *
5659 *       File:  ss_msg.c
5660 *
5661 */
5662 S16 SAlignDBufEven
5663 (
5664 Buffer *dBuf                      /* data buffer */
5665 )
5666 {
5667    MsgLen len;
5668    Data *src;
5669
5670 #if (ERRCLASS & ERRCLS_INT_PAR)
5671    if (!dBuf)
5672    {
5673       SSLOGERROR(ERRCLS_INT_PAR, ESS199, ERRZERO, "SAlignDBufEven : Null\
5674                  Buffer");
5675       return RFAILED;
5676    }
5677    if (dBuf->b_datap->db_type != SS_M_DATA)
5678    {
5679       SSLOGERROR(ERRCLS_INT_PAR, ESS200, ERRZERO, "SAlignDBufEven : Incorrect\
5680                                                    buffer type");
5681       return RFAILED;
5682    }
5683 #endif
5684
5685    src = dBuf->b_rptr;
5686 /* ss008.13: addition */
5687    if (!((PTR)src % (PTR)2))
5688       return ROK;
5689
5690    if (dBuf->b_datap->db_ref > 1)
5691       return RFAILED;
5692
5693    len = dBuf->b_wptr - dBuf->b_rptr;
5694
5695    if (dBuf->b_datap->db_base < dBuf->b_rptr)
5696    {
5697       dBuf->b_wptr = --dBuf->b_rptr;
5698       while (len--)
5699          *dBuf->b_wptr++ = *src++;
5700    }
5701    else
5702       if (dBuf->b_datap->db_lim > dBuf->b_wptr)
5703       {
5704          src = dBuf->b_wptr - 1;
5705          dBuf->b_rptr = ++dBuf->b_wptr;
5706          while (len--)
5707             *--dBuf->b_rptr = *src--;
5708       }
5709       else
5710          return RFAILED;
5711
5712    return ROK;
5713 }
5714
5715 /* ss004.13: addition */
5716 /*
5717 *
5718 *       Fun:   SAlignDBuf
5719 *
5720 *       Desc:  Align data portion of a data buffer on the specified 
5721 *              boundary. No restriction is imposed on the alignment.
5722 *
5723 *       Ret:   ROK/RFAILED
5724 *
5725 *       Notes: required by drivers (68360, 860)
5726 *
5727 *       File:  ss_msg.c
5728 *
5729 */
5730 S16 SAlignDBuf
5731 (
5732 Buffer *dBuf,                      /* data buffer */
5733 uint32_t    align                       /* alignemnt required */
5734 )
5735 {
5736    MsgLen len;
5737    Data   *src;
5738    uint32_t    upShift;                 /* no. of bytes to be shifted up */
5739    uint32_t    downShift;               /* no. of bytes to be shifted down */
5740
5741 #if (ERRCLASS & ERRCLS_INT_PAR)
5742    if (!dBuf)
5743    {
5744       SSLOGERROR(ERRCLS_INT_PAR, ESS201, ERRZERO, "SAlignDBuf: Null\
5745                  Buffer");
5746       return RFAILED;
5747    }
5748    if (dBuf->b_datap->db_type != SS_M_DATA)
5749    {
5750       SSLOGERROR(ERRCLS_INT_PAR, ESS202, ERRZERO, "SAlignDBuf: Incorrect\
5751                                                    buffer type");
5752       return RFAILED;
5753    }
5754 #endif
5755
5756    src = dBuf->b_rptr;
5757 /* ss008.13: addition */
5758    upShift = (PTR)src % (PTR)align; /* no. of bytes by which to shift up
5759                                      * the read and write pointers */
5760
5761    if (!upShift)
5762       return ROK;
5763
5764    if (dBuf->b_datap->db_ref > 1)
5765       return RFAILED;
5766
5767    downShift = align - upShift;   /* no of bytes by which to shift down
5768                                    * the read and write pointers */
5769    len = dBuf->b_wptr - dBuf->b_rptr;
5770
5771    if (dBuf->b_datap->db_base <= (dBuf->b_rptr - upShift))
5772    {
5773       /* there is space to shift up the read and write pointers */
5774
5775       dBuf->b_rptr -= upShift;   /* align the read pointer */
5776       dBuf->b_wptr = dBuf->b_rptr;
5777       SMemCpy( (Void *) dBuf->b_wptr, (Void *) src,  (size_t) len);
5778       dBuf->b_wptr = dBuf->b_wptr + len;
5779    }
5780    else
5781    {
5782       if (dBuf->b_datap->db_lim > dBuf->b_wptr + downShift)
5783       {
5784          src = dBuf->b_wptr - 1;
5785          dBuf->b_wptr = dBuf->b_wptr + downShift;
5786          dBuf->b_rptr = dBuf->b_wptr;
5787          while (len--)
5788             *--dBuf->b_rptr = *src--;
5789       }
5790       else
5791          return RFAILED;
5792    }
5793
5794    return ROK;
5795 }
5796
5797 \f
5798 /*
5799 *
5800 *       Fun:   SGetSMem
5801 *
5802 *       Desc:  Allocates a static buffer pool within the specified
5803 *              memory region.
5804 *
5805 *       Ret:   ROK      - ok
5806 *              RFAILED  - failed, general (optional)
5807 *
5808 *       Notes: The new memory management scheme makes this function
5809 *              meaningless. It merely sets the pool ID to zero and
5810 *              returns.
5811 *
5812 *       File:  ss_msg.c
5813 *
5814 */
5815 S16 SGetSMem
5816 (
5817 Region region,                  /* region ID */
5818 Size size,                      /* size */
5819 Pool *pool                      /* pointer to pool ID */
5820 )
5821 {
5822
5823    UNUSED(size);
5824
5825
5826 #if (ERRCLASS & ERRCLS_INT_PAR)
5827    /* validate region ID */
5828    if (region >= SS_MAX_REGS)
5829    {
5830       SSLOGERROR(ERRCLS_INT_PAR, ESS203, region, "Invalid region");
5831       return RFAILED;
5832    }
5833
5834    /* validate pointer to pool ID */
5835    if (pool == NULLP)
5836    {
5837       SSLOGERROR(ERRCLS_INT_PAR, ESS204, region, "Null pointer");
5838       return RFAILED;
5839    }
5840 #endif
5841
5842
5843    /* set the pool ID to zero and return success */
5844    *pool = 0;
5845
5846
5847    return ROK;
5848 }
5849
5850 \f
5851 /*
5852 *
5853 *       Fun:   SPutSMem
5854 *
5855 *       Desc:  Deallocates a static buffer pool within the specified
5856 *              memory region.
5857 *
5858 *       Ret:   ROK      - ok
5859 *              RFAILED  - failed, general (optional)
5860 *
5861 *       Notes: The new memory management scheme makes this function
5862 *              meaningless. It does nothing.
5863 *
5864 *       File:  ss_msg.c
5865 *
5866 */
5867 S16 SPutSMem
5868 (
5869 Region region,                  /* region ID */
5870 Pool pool                       /* pool ID */
5871 )
5872 {
5873    /* ss021.103 - Addition of return value */
5874 #if (ERRCLASS & ERRCLS_INT_PAR)
5875    S16   ret;
5876 #endif
5877
5878
5879 #if (ERRCLASS & ERRCLS_INT_PAR)
5880    /* validate region ID */
5881    if (region >= SS_MAX_REGS)
5882    {
5883       SSLOGERROR(ERRCLS_INT_PAR, ESS205, region, "Invalid region");
5884       return RFAILED;
5885    }
5886
5887    /* validate pool ID */
5888    if (pool >= SS_MAX_POOLS_PER_REG)
5889    {
5890       SSLOGERROR(ERRCLS_INT_PAR, ESS206, region, "Invalid pool");
5891       return RFAILED;
5892    }
5893
5894    /* ss021.103 - Addition to check if region is registered */
5895    /* acquire one semaphore, to protect against deregistration */
5896    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
5897    if (ret != ROK)
5898    {
5899
5900 #if (ERRCLASS & ERRCLS_DEBUG)
5901       SSLOGERROR(ERRCLS_DEBUG, ESS207, (ErrVal) ret,
5902                   "Could not lock region table");
5903 #endif
5904
5905       return RFAILED;
5906    }
5907
5908
5909 #if (ERRCLASS & ERRCLS_INT_PAR)
5910    /* verify that this region is present */
5911    if (osCp.regionTbl[region].used == FALSE)
5912    {
5913       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
5914       {
5915 #if (ERRCLASS & ERRCLS_DEBUG)
5916          SSLOGERROR(ERRCLS_DEBUG, ESS208, ERRZERO,
5917                   "Could not release semaphore");
5918          return RFAILED;
5919 #endif
5920       }
5921
5922       SSLOGERROR(ERRCLS_INT_PAR, ESS209, region, "Region not registered");
5923       return RFAILED;
5924    }
5925 #endif
5926
5927    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
5928    {
5929 #if (ERRCLASS & ERRCLS_DEBUG)
5930       SSLOGERROR(ERRCLS_DEBUG, ESS210, ERRZERO,
5931                   "Could not release semaphore");
5932       return RFAILED;
5933 #endif
5934    }
5935 #endif
5936
5937    return ROK;
5938 }
5939
5940 \f
5941 /*
5942 *
5943 *       Fun:   SChkRes
5944 *
5945 *       Desc:  Checks the available system resources (memory).
5946 *
5947 *       Ret:   ROK      - ok
5948 *              RFAILED  - failed, general (optional)
5949 *
5950 *       Notes:
5951 *
5952 *       File:  ss_msg.c
5953 *
5954 */
5955 S16 SChkRes
5956 (
5957 Region region,                  /* region ID */
5958 Pool pool,                      /* pool ID */
5959 Status *status                  /* pointer to status */
5960 )
5961 {
5962    S16 ret;
5963    SMemCtl mctl;
5964
5965
5966
5967 #if (ERRCLASS & ERRCLS_INT_PAR)
5968    /* validate region ID */
5969    if (region >= SS_MAX_REGS)
5970    {
5971       SSLOGERROR(ERRCLS_INT_PAR, ESS229, region, "Invalid region");
5972       return RFAILED;
5973    }
5974
5975    /* validate pool ID */
5976    if (pool >= SS_MAX_POOLS_PER_REG)
5977    {
5978       SSLOGERROR(ERRCLS_INT_PAR, ESS230, region, "Invalid pool");
5979       return RFAILED;
5980    }
5981
5982    /* validate status pointer */
5983    if (status == NULLP)
5984    {
5985       SSLOGERROR(ERRCLS_INT_PAR, ESS231, ERRZERO, "Null pointer");
5986       return RFAILED;
5987    }
5988 #endif
5989 /* ss037.103 Removed the semaphore operation for performance enhancement */
5990
5991 #ifndef SS_PERF
5992    /* acquire one semaphore, to protect against deregistration */
5993    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
5994    if (ret != ROK)
5995    {
5996
5997 #if (ERRCLASS & ERRCLS_DEBUG)
5998       SSLOGERROR(ERRCLS_DEBUG, ESS232, (ErrVal) ret,
5999                   "Could not lock region table");
6000 #endif
6001
6002       return RFAILED;
6003    }
6004
6005
6006 #if (ERRCLASS & ERRCLS_INT_PAR)
6007    /* verify that this region is around */
6008    if (osCp.regionTbl[region].used == FALSE)
6009    {
6010
6011 /* ss006.13: addition */
6012       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6013       {
6014 #if (ERRCLASS & ERRCLS_DEBUG)
6015          SSLOGERROR(ERRCLS_DEBUG, ESS233, ERRZERO,
6016                   "Could not release semaphore");
6017          return RFAILED;
6018 #endif
6019       }
6020
6021       SSLOGERROR(ERRCLS_INT_PAR, ESS234, region, "Region not registered");
6022       return RFAILED;
6023    }
6024
6025    /* verify that this is a valid pool */
6026    if (osCp.regionTbl[region].poolTbl[pool].type != SS_POOL_DYNAMIC)
6027    {
6028       SSLOGERROR(ERRCLS_DEBUG, ESS235, ERRZERO, "Invalid pool");
6029       return RFAILED;
6030    }
6031 #endif
6032 #endif
6033
6034    /* call the memory manager to check resources */
6035    mctl.op = SS_MEM_CHK_RES;
6036    mctl.u.chkres.size = osCp.regionTbl[region].poolTbl[pool].u.dpool.size;
6037    mctl.u.chkres.status = status;
6038    ret = (osCp.regionTbl[region].ctl)
6039              (osCp.regionTbl[region].regCb, SS_MEM_CHK_RES, &mctl);
6040 #ifndef SS_PERF
6041    /* release the semaphore we took */
6042
6043 /* ss006.13: addition */
6044    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6045    {
6046 #if (ERRCLASS & ERRCLS_DEBUG)
6047       SSLOGERROR(ERRCLS_DEBUG, ESS236, ERRZERO,
6048                   "Could not release semaphore");
6049       return RFAILED;
6050 #endif
6051    }
6052 #endif
6053
6054    return (ret);
6055 }
6056
6057 /*
6058 *
6059 *       Fun:   SSwapMsg
6060 *
6061 *       Desc:  This function will swap two message data contents.
6062 *              The original mBuf pointers are unchanged.
6063 *
6064 *       Ret:   ROK     - ok
6065 *              RFAILED - failed
6066 *
6067 *       Notes: 
6068 *
6069 *       File:  ss_msg.c
6070 *
6071 */
6072
6073
6074 S16 SSwapMsg
6075 (
6076 Buffer *mBuf1,              /* message 1 */
6077 Buffer *mBuf2               /* message 2 */
6078 )
6079 {
6080    SsMsgInfo *minfop;
6081    SsMsgInfo minfo;
6082 #if (ERRCLASS & ERRCLS_INT_PAR)
6083    SsMsgInfo *minfo1;
6084    SsMsgInfo *minfo2;
6085 #endif
6086    Buffer *tmp;
6087    uint8_t tmp2;
6088    
6089
6090 #if (ERRCLASS & ERRCLS_INT_PAR)
6091    /* check message buffer 1 */
6092    if (mBuf1 == NULLP)
6093    {
6094       SSLOGERROR(ERRCLS_INT_PAR, ESS237, ERRZERO, "SSwapMsg : Null Buffer");
6095       return RFAILED;
6096    }
6097    /* check message buffer 2 */
6098    if (mBuf2 == NULLP)
6099    {
6100       SSLOGERROR(ERRCLS_INT_PAR, ESS238, ERRZERO, "SSwapMsg : Null Buffer");
6101       return RFAILED;
6102    }
6103    if (mBuf1->b_datap->db_type != SS_M_PROTO)
6104    {
6105       SSLOGERROR(ERRCLS_INT_PAR, ESS239, ERRZERO, "SSwapMsg: Incorrect buffer\
6106                                                    type");
6107       return RFAILED;
6108    }
6109    if (mBuf2->b_datap->db_type != SS_M_PROTO)
6110    {
6111       SSLOGERROR(ERRCLS_INT_PAR, ESS240, ERRZERO, "SSwapMsg: Incorrect buffer\
6112                                                    type");
6113       return RFAILED;
6114    }
6115    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
6116    minfo2 = (SsMsgInfo*) mBuf2->b_rptr;
6117    if (minfo1->region != minfo2->region)
6118    {
6119       SSLOGERROR(ERRCLS_INT_PAR, ESS241, ERRZERO, "SSwapMsg: differnt regions\
6120                                                    for messages");
6121       return RFAILED;
6122    }
6123 #endif
6124
6125
6126    tmp = mBuf1->b_next;
6127    mBuf1->b_next = mBuf2->b_next;
6128    mBuf2->b_next = tmp;
6129
6130    tmp = mBuf1->b_prev;
6131    mBuf1->b_prev = mBuf2->b_prev;
6132    mBuf2->b_prev = tmp;
6133
6134    tmp = mBuf1->b_cont;
6135    mBuf1->b_cont = mBuf2->b_cont;
6136    mBuf2->b_cont = tmp;
6137
6138 /* ss008.301 */
6139 #ifdef SS_DBLK_FREE_RTN 
6140    tmp = (Buffer *) mBuf1->b_datap->db_frtnp;
6141    mBuf1->b_datap->db_frtnp = mBuf2->b_datap->db_frtnp;
6142    mBuf2->b_datap->db_frtnp = (struct ssfree_rtn *) tmp;
6143 #endif   
6144
6145    tmp2 = mBuf1->b_datap->db_ref;
6146    mBuf1->b_datap->db_ref = mBuf2->b_datap->db_ref;
6147    mBuf2->b_datap->db_ref = tmp2;
6148
6149
6150    minfop = & minfo;
6151    
6152    SMemCpy((Void *) minfop, (Void *) (mBuf1->b_datap->db_base), (size_t)(sizeof(SsMsgInfo)));
6153    SMemCpy((Void *) (mBuf1->b_datap->db_base), (Void *) (mBuf2->b_datap->db_base), (size_t)(sizeof(SsMsgInfo)));
6154    SMemCpy((Void *) (mBuf2->b_datap->db_base), (Void *) minfop, (size_t)(sizeof(SsMsgInfo)));
6155
6156    return ROK;
6157 }
6158 /* ss004.301 : Cavium changes */
6159 #ifdef SS_SEUM_CAVIUM
6160
6161 /*
6162  *
6163  *       Fun:   SConvPtrPhy
6164  *
6165  *       Desc:  This function will converts the pointer to
6166  *              physical address.
6167  *
6168  *       Ret:   ROK      - ok
6169  *              RFAILED  - failed, general (optional)
6170  *
6171  *       Notes: Function to convert the Pointer (Virtual address) to
6172  *              Physical Address. This will take the Buffer type as
6173  *              input and will convert all pointer associated with
6174  *              that to Physical Address 
6175  *
6176  *       File:  ss_msg.c
6177  *
6178  */
6179 S16 SConvPtrPhy
6180 (
6181  Buffer  **mBuf
6182 )
6183 {
6184
6185   Buffer      *curPtr;
6186   Buffer      *nextPtr;
6187   SsMsgInfo   *minfoPtr;
6188   SsDblk      *dblkPtr;
6189   SsFrtn      *frtnPtr;
6190
6191
6192   /* check mBuf for NULLP */
6193   if ( (mBuf == NULLP) || (*mBuf == NULLP ) )
6194   {
6195          return RFAILED;
6196   }
6197
6198   /* first block in Buffer is head */
6199   curPtr = *mBuf;
6200   nextPtr = curPtr->b_cont;
6201
6202   /* Get the physical address of the Pointer */
6203   if(curPtr->b_cont)
6204   {
6205          curPtr->b_cont = (Buffer*)cvmx_ptr_to_phys (curPtr->b_cont);
6206   }
6207
6208   if(curPtr->b_next)
6209   {
6210          curPtr->b_next = (Buffer*)cvmx_ptr_to_phys (curPtr->b_next);
6211   }
6212
6213   if(curPtr->b_prev)
6214   {
6215          curPtr->b_prev = (Buffer*)cvmx_ptr_to_phys (curPtr->b_prev);
6216   }
6217
6218   /* Convert the pointers of Minfo to Physical addr */
6219   minfoPtr = (SsMsgInfo*)curPtr->b_rptr;
6220
6221   if (minfoPtr->endptr)
6222   {
6223          minfoPtr->endptr = (Buffer*)cvmx_ptr_to_phys (minfoPtr->endptr);
6224   }
6225   if (minfoPtr->next)
6226   {
6227          minfoPtr->next = (Buffer*)cvmx_ptr_to_phys (minfoPtr->next);
6228   }
6229
6230   curPtr->b_rptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_rptr);
6231
6232   curPtr->b_wptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_wptr);
6233
6234   /* Convert the pointers of Dblock to Physical addr */
6235   dblkPtr = (SsDblk*)curPtr->b_datap;
6236
6237 /* ss008.301 */
6238 #ifdef SS_DBLK_FREE_RTN 
6239   frtnPtr = dblkPtr->db_frtnp;
6240   if( frtnPtr != NULLP)
6241   {
6242          if (frtnPtr->free_func)
6243          {
6244                 frtnPtr->free_func = (Void*)cvmx_ptr_to_phys (frtnPtr->free_func);
6245          }
6246          if (frtnPtr->free_arg)
6247          {
6248                 frtnPtr->free_arg = (S8*)cvmx_ptr_to_phys (frtnPtr->free_arg);
6249          }
6250
6251          dblkPtr->db_frtnp = (SsFrtn*)cvmx_ptr_to_phys (dblkPtr->db_frtnp);
6252   }
6253 #endif
6254
6255   dblkPtr->db_base = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_base);
6256   dblkPtr->db_lim = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_lim);
6257
6258   curPtr->b_datap = (SsDblk*)cvmx_ptr_to_phys (curPtr->b_datap);
6259
6260   /* second block onwards  is dblk */
6261   curPtr = nextPtr;
6262
6263   while(curPtr)
6264   {
6265          nextPtr = curPtr->b_cont;
6266
6267          /* Get the physical address of the Pointer */
6268          if(curPtr->b_cont)
6269          {
6270                 curPtr->b_cont = (Buffer*)cvmx_ptr_to_phys (curPtr->b_cont);
6271          }
6272
6273          curPtr->b_rptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_rptr);
6274          curPtr->b_wptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_wptr);
6275
6276          /* Convert the pointers of Dblock to Physical addr */
6277          dblkPtr = (SsDblk*)curPtr->b_datap;
6278
6279 /* ss008.301 */
6280 #ifdef SS_DBLK_FREE_RTN 
6281          frtnPtr = dblkPtr->db_frtnp;
6282          if( frtnPtr != NULLP)
6283          {
6284                 if (frtnPtr->free_func)
6285                 {
6286                   frtnPtr->free_func = (Void*)cvmx_ptr_to_phys (frtnPtr->free_func);
6287                 }
6288                 if (frtnPtr->free_arg)
6289                 {
6290                   frtnPtr->free_arg = (S8*)cvmx_ptr_to_phys (frtnPtr->free_arg);
6291                 }
6292
6293                 dblkPtr->db_frtnp = (SsFrtn*)cvmx_ptr_to_phys (dblkPtr->db_frtnp);
6294          }
6295 #endif    
6296
6297          dblkPtr->db_base = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_base);
6298          dblkPtr->db_lim = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_lim);
6299
6300          curPtr->b_datap = (SsDblk*)cvmx_ptr_to_phys (curPtr->b_datap);
6301
6302          curPtr = nextPtr;
6303
6304   }
6305
6306   *mBuf = (Buffer*)cvmx_ptr_to_phys (*mBuf);
6307
6308   return ROK;
6309
6310 } /* SConvPtrPhy */
6311
6312 /*
6313  *
6314  *       Fun:   SConvPhyPtr
6315  *
6316  *       Desc:  This function will converts the physical address
6317  *              into pointers.
6318  *
6319  *       Ret:   On Success - Pointer to converted buffer
6320  *              On Failuer - Returns NULL
6321  *
6322  *       Notes: Function to Convert the Physical address to Pointer 
6323  *              (Virtual address) This will take work buffer as input
6324  *              and will convert all address associated with that to 
6325  *              Pointers.
6326  *
6327  *       File:  ss_msg.c
6328  *
6329  */
6330 S16 SConvPhyPtr
6331 (
6332 Buffer  **workPtr
6333 )
6334 {
6335
6336   Buffer      *curPtr;
6337   Buffer      *mBuf;
6338   SsMsgInfo   *minfoPtr;
6339   SsDblk      *dblkPtr;
6340   SsFrtn      *frtnPtr;
6341
6342
6343   /* check workPtr for NULLP */
6344   if ( (workPtr == NULLP) || (*workPtr == NULLP) )
6345   {
6346          return RFAILED;
6347   }
6348
6349   /* Convert the buffer address to pointer */
6350   mBuf = (Buffer*)cvmx_phys_to_ptr ((uint64_t)(*workPtr));
6351
6352   curPtr = mBuf;
6353
6354   /* first block is mblk */
6355   if (curPtr->b_next)
6356   {
6357          curPtr->b_next = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_next);
6358   }
6359
6360   if (curPtr->b_prev)
6361   {
6362          curPtr->b_prev = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_prev);
6363   }
6364
6365   if(curPtr->b_cont)
6366   {
6367          curPtr->b_cont = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_cont);
6368   }
6369
6370   curPtr->b_rptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_rptr);
6371
6372   /* Get the pointer for minfo */
6373   minfoPtr = (SsMsgInfo*)curPtr->b_rptr;
6374
6375   if (minfoPtr->endptr)
6376   {
6377          minfoPtr->endptr = (Buffer*)cvmx_phys_to_ptr ((uint64_t)minfoPtr->endptr);
6378   }
6379   if (minfoPtr->next)
6380   {
6381          minfoPtr->next = (Buffer*)cvmx_phys_to_ptr ((uint64_t)minfoPtr->next);
6382   }
6383
6384   curPtr->b_wptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_wptr);
6385
6386   curPtr->b_datap = (SsDblk*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_datap);
6387
6388   /* Get the Dblock pointers */
6389   dblkPtr = (SsDblk*)curPtr->b_datap;
6390
6391 /* ss008.301 */
6392 #ifdef SS_DBLK_FREE_RTN 
6393   if (dblkPtr->db_frtnp)
6394   {
6395          dblkPtr->db_frtnp = (SsFrtn*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_frtnp);
6396     frtnPtr = dblkPtr->db_frtnp;
6397
6398          if (frtnPtr->free_func)
6399          {
6400                 frtnPtr->free_func = cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_func);
6401          }
6402          if (frtnPtr->free_arg)
6403          {
6404                 frtnPtr->free_arg = (S8*)cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_arg);
6405          }
6406   }
6407 #endif  
6408
6409   dblkPtr->db_base = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_base);
6410   dblkPtr->db_lim = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_lim);
6411
6412   curPtr = curPtr->b_cont;
6413
6414   /* after the first block is dblk */
6415   while(curPtr)
6416   {
6417          if(curPtr->b_cont)
6418          {
6419                 curPtr->b_cont = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_cont);
6420          }
6421
6422          curPtr->b_rptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_rptr);
6423
6424          curPtr->b_wptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_wptr);
6425
6426          curPtr->b_datap = (SsDblk*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_datap);
6427
6428          /* Get the Dblock pointers */
6429          dblkPtr = (SsDblk*)curPtr->b_datap;
6430
6431 /* ss008.301 */
6432 #ifdef SS_DBLK_FREE_RTN 
6433          if (dblkPtr->db_frtnp)
6434          {
6435                 dblkPtr->db_frtnp = (SsFrtn*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_frtnp);
6436
6437       frtnPtr = dblkPtr->db_frtnp;
6438
6439                 if (frtnPtr->free_func)
6440                 {
6441                   frtnPtr->free_func = cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_func);
6442                 }
6443                 if (frtnPtr->free_arg)
6444                 {
6445                   frtnPtr->free_arg = (S8*)cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_arg);
6446                 }
6447          }
6448 #endif    
6449
6450          dblkPtr->db_base = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_base);
6451          dblkPtr->db_lim = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_lim);
6452
6453          curPtr = curPtr->b_cont;
6454   }
6455
6456   /* Place the converted buffer */
6457   *workPtr = mBuf;
6458
6459   return ROK;
6460
6461 } /* SConvPhyPtr */
6462
6463 /*
6464  *
6465  *       Fun:   SCpyFpaMsg
6466  *
6467  *       Desc:  This function will copy the message from FPA region
6468  *              to other region
6469  *
6470  *       Ret:   On Success - ROK
6471  *              On Failuer - RFAILED
6472  *
6473  *       Notes: Function to copy the message from FPA region to
6474  *              other region. This will internally allocates the
6475  *              memory for the destination buffer and copies the
6476  *              message in the same chain list 
6477  *
6478  *       File:
6479  *
6480  */
6481 S16 SCpyFpaMsg
6482 (
6483 Buffer *srcBuf,
6484 Region dstRegion,
6485 Pool dstPool,
6486 Buffer **dstBuf
6487 )
6488 {
6489
6490   Size        numBytes;
6491   Size        size;
6492   S16         ret;
6493   Buffer     *curPtr = NULLP;
6494   Data       *dat = NULLP;
6495   Buffer     *tmpblk = NULLP;
6496   Buffer     *newblk = NULLP;
6497   Buffer     *prevblk = NULLP;
6498   SsMsgInfo  *minfoSrc = NULLP;
6499   SsMsgInfo  *minfoDst = NULLP;
6500   SsDblk     *dblkPtr = NULLP;
6501   SsDblk     *dptr = NULLP;
6502
6503
6504   if ( srcBuf == (Buffer*)NULLP )
6505   {
6506          return RFAILED;
6507   }
6508
6509   if ((dstRegion >= SS_MAX_REGS) || (dstPool >= SS_MAX_POOLS_PER_REG))
6510   {
6511          /* Free the source buffer and return failure */
6512          SPutFpaMsg(srcBuf);
6513          return RFAILED;
6514   }
6515
6516   /* Allocate memory for destination buffer */
6517   if(SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
6518   {
6519          /* Free the source buffer and return failure */
6520          SPutFpaMsg(srcBuf);
6521          return (ROUTRES);
6522   }
6523
6524   /* get the minfo of dest and src buffers */
6525   minfoSrc = (SsMsgInfo*) srcBuf->b_rptr;
6526   minfoDst = (SsMsgInfo*) (*dstBuf)->b_rptr;
6527
6528   curPtr = srcBuf->b_cont;
6529
6530   /* Copy all the blocks associated with this Buffer */
6531   while(curPtr)
6532   {
6533          /* Allocate the memeory for dblock */
6534
6535          dblkPtr = (SsDblk*)curPtr->b_datap;
6536          numBytes = dblkPtr->db_lim - dblkPtr->db_base;
6537          size = numBytes + MDBSIZE;
6538
6539          ret = SAlloc( dstRegion, &size, 0, (Data**)&tmpblk);
6540
6541          if(ret != ROK)
6542          {
6543                 /* Free all allocated buffers before returning */
6544                 while (newblk)
6545                 {
6546                   tmpblk = newblk->b_cont;
6547                   (Void) SPutDBuf(dstRegion, dstPool, newblk);
6548                   newblk = tmpblk;
6549                 }
6550                 (Void) SPutMsg(*dstBuf);
6551                 /* Free the source buffer and return failure */
6552                 SPutFpaMsg(srcBuf);
6553                 return (ROUTRES);
6554          }
6555
6556          dat = (Data *)tmpblk + MDBSIZE;
6557          dptr = (SsDblk*) (((Data *) tmpblk) + MBSIZE);
6558          size -= MDBSIZE;
6559
6560          /* Initialize the pointer and copy the data */
6561          INITB( tmpblk, dptr, dat, size, NULLP );
6562 #ifndef SS_DBUF_REFLOCK_DISABLE
6563          SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX);
6564 #endif
6565          numBytes = curPtr->b_wptr - curPtr->b_rptr;
6566          /* Copy the data part if its present */
6567          if (numBytes > 0 )
6568          {
6569                 SMemCpy( (Void *) tmpblk->b_wptr, (Void *) curPtr->b_rptr, numBytes);
6570                 tmpblk->b_wptr += numBytes;
6571          }
6572
6573          if (!prevblk)
6574          {
6575                 newblk = tmpblk;
6576          }
6577          else
6578          {
6579                 prevblk->b_cont = tmpblk;
6580          }
6581
6582          prevblk = tmpblk;
6583          curPtr = curPtr->b_cont;
6584   }
6585
6586   if (tmpblk)
6587   {
6588          tmpblk->b_cont = NULLP;
6589   }
6590
6591   *minfoDst = *minfoSrc;
6592   minfoDst->region = 0;
6593   minfoDst->pool = 0;
6594   minfoDst->len  = minfoSrc->len;
6595   minfoDst->endptr  = tmpblk;
6596   minfoDst->next = NULLP;
6597
6598   (*dstBuf)->b_cont = newblk;
6599
6600   /* Free the source buffer after copying it */
6601   SPutFpaMsg(srcBuf);
6602
6603   return ROK;
6604
6605 } /* SCpyFpaMsg */
6606
6607 /*
6608  *
6609  *       Fun:   SCpyMsgFpa
6610  *
6611  *       Desc:  This function will copy the message from any region
6612  *              to FPA region
6613  *
6614  *       Ret:   On Success - ROK
6615  *              On Failuer - RFAILED
6616  *
6617  *       Notes: Function will copy the mbuf from msg to FPA.
6618  *              This function allocates the memory internally
6619  *              and copies the message to newly allocated mBuf.
6620  *              The size of mBuf should be either of one
6621  *              pre-difined sizes otherwise mBuf is dopped.
6622  *
6623  *       File:
6624  *
6625  */
6626 S16 SCpyMsgFpa
6627 (
6628 Buffer *srcBuf,
6629 Buffer **dstBuf
6630 )
6631 {
6632
6633   Buffer     *curPtr = NULLP;
6634   Data       *dat = NULLP;
6635   Buffer     *tmpblk = NULLP;
6636   Buffer     *prevblk = NULLP;
6637   Buffer     *newblk = NULLP;
6638   SsMsgInfo  *minfoSrc = NULLP;
6639   SsMsgInfo  *minfoDst = NULLP;
6640   SsDblk     *dblkPtr = NULLP;
6641   SsDblk     *dptr = NULLP;
6642   uint32_t         numBytes;
6643   Pool        pool;
6644
6645
6646   if (srcBuf == (Buffer*)NULLP)
6647   {
6648          return RFAILED;
6649   }
6650
6651   *dstBuf = (Buffer*)cvmx_fpa_alloc(SS_CVMX_POOL_0);
6652
6653   if ( *dstBuf == NULLP )
6654   {
6655          /* Free the source buffer before returning  */
6656          SPutMsg(srcBuf);
6657          return (ROUTRES);
6658   }
6659
6660   dat = (Data *)(*dstBuf) + MDBSIZE;
6661   dptr = (SsDblk*) ((Data *)(*dstBuf) + MBSIZE);
6662   numBytes = SS_CVMX_POOL_0_SIZE - MDBSIZE;
6663
6664   /* Initialize the pointers of new block */
6665   INITB((*dstBuf), dptr, dat, numBytes, NULLP);
6666
6667   (*dstBuf)->b_datap->db_type = SS_M_PROTO;
6668   (*dstBuf)->b_wptr = (*dstBuf)->b_rptr + sizeof(SsMsgInfo);
6669
6670   minfoSrc = (SsMsgInfo*) srcBuf->b_rptr;
6671   minfoDst = (SsMsgInfo*) (*dstBuf)->b_rptr;
6672   curPtr = srcBuf->b_cont;
6673
6674   while(curPtr)
6675   {
6676          dblkPtr = (SsDblk*)curPtr->b_datap;
6677
6678          /* Get the size required which is to be allocated */
6679          numBytes = dblkPtr->db_lim - dblkPtr->db_base;
6680          numBytes += MDBSIZE;
6681
6682          /* get the pool depending on the size need to be allocated */
6683          switch(numBytes)
6684          {
6685                 case SS_CVMX_POOL_0_SIZE:
6686
6687                   pool = SS_CVMX_POOL_0;
6688                   break;
6689
6690                 case SS_CVMX_POOL_1_SIZE:
6691
6692                   pool = SS_CVMX_POOL_1;
6693                   break;
6694
6695                 case SS_CVMX_POOL_2_SIZE:
6696
6697                   pool = SS_CVMX_POOL_2;
6698                   break;
6699
6700                 case SS_CVMX_POOL_3_SIZE:
6701
6702                   pool = SS_CVMX_POOL_3;
6703                   break;
6704
6705                 default:
6706                   /* size doesn't match, drop the mBuf and returning 
6707                         * RFAILED */
6708                   SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "SCpyMsgFpa: Invalid\
6709                                 buffer size, dropping the message");
6710
6711                   (*dstBuf)->b_cont = newblk;
6712                   SPutFpaMsg(*dstBuf);
6713                   /* Free the source buffer before returning  */
6714                   SPutMsg(srcBuf);
6715                   return RFAILED;
6716          }
6717
6718          /* Allocate for mBuf and copy both the header and contents */
6719          tmpblk = (Buffer*)cvmx_fpa_alloc(pool);
6720
6721          if ( tmpblk == NULLP )
6722          {
6723                 /* Return error if fails to allocate memory */
6724
6725                 (*dstBuf)->b_cont = newblk;
6726                 SPutFpaMsg(*dstBuf);
6727                 /* Free the source buffer before returning  */
6728                 SPutMsg(srcBuf);
6729                 return (ROUTRES);
6730          }
6731
6732          dat = (Data *)tmpblk + MDBSIZE;
6733          dptr = (SsDblk*) (((Data *) tmpblk) + MBSIZE);
6734          numBytes -= MDBSIZE;
6735
6736          /* Initialize the pointers of new block */
6737          INITB( tmpblk, dptr, dat, numBytes, NULLP );
6738
6739          numBytes = curPtr->b_wptr - curPtr->b_rptr;
6740          /* Copy the message contents */
6741          if (numBytes > 0 )
6742          {
6743                 SMemCpy( (Void *) tmpblk->b_wptr, (Void *) curPtr->b_rptr, numBytes);
6744                 tmpblk->b_wptr += numBytes;
6745          }
6746
6747          /* Add the mew mBuf to the Buffer chain */
6748          if (!prevblk)
6749          {
6750                 newblk = tmpblk;
6751          }
6752          else
6753          {
6754                 prevblk->b_cont = tmpblk;
6755          }
6756
6757          prevblk = tmpblk;
6758          /* Get the next block */
6759          curPtr = curPtr->b_cont;
6760   }
6761
6762   /* Initialize the last mBuf */
6763   if (tmpblk)
6764   {
6765          tmpblk->b_cont = NULLP;
6766   }
6767
6768   *minfoDst = *minfoSrc;
6769   minfoDst->region = 0;
6770   minfoDst->pool = 0;
6771   minfoDst->len  = minfoSrc->len;
6772   minfoDst->endptr  = tmpblk;
6773   minfoDst->next = NULLP;
6774
6775   (*dstBuf)->b_cont = newblk;
6776
6777   /* Free the source buffer after copying it */
6778   SPutMsg(srcBuf);
6779
6780   return ROK;
6781
6782 } /* SCpyMsgFpa */
6783
6784 /*
6785  *
6786  *       Fun:   SPutFpaMsg
6787  *
6788  *       Desc:  This function will free the Buffer associated with
6789  *              FPA pool.
6790  *
6791  *       Ret:   On Success - ROK
6792  *              On Failuer - RFAILED
6793  *
6794  *       Notes: Function free the all buffer associated with wqBuf
6795  *              This will get the pool id by the size of the buffer
6796  *              and same will be used to free the buffer.
6797  *
6798  *       File:
6799  *
6800  */
6801 S16 SPutFpaMsg
6802 (
6803 Buffer *fpaBuf
6804 )
6805 {
6806   uint16_t      size;
6807   Buffer   *curBlk;
6808   Buffer   *nextBlk;
6809   SsDblk   *dblkPtr;
6810
6811
6812   if( fpaBuf == NULLP )
6813   {
6814          return ROK;
6815   }
6816
6817   curBlk = fpaBuf->b_cont;
6818
6819   while(curBlk)
6820   {
6821          nextBlk = curBlk->b_cont;
6822
6823          dblkPtr = (SsDblk*)curBlk->b_datap;
6824
6825          size = dblkPtr->db_lim - dblkPtr->db_base + MDBSIZE;
6826
6827          /* Free the dblock according to its size */
6828          switch(size)
6829          {
6830                 case SS_CVMX_POOL_0_SIZE:
6831                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_0, 0);
6832                   break;
6833
6834                 case SS_CVMX_POOL_1_SIZE:
6835                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_1, 0);
6836                   break;
6837
6838                 case SS_CVMX_POOL_2_SIZE:
6839                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_2, 0);
6840                   break;
6841
6842                 case SS_CVMX_POOL_3_SIZE:
6843                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_3, 0);
6844                   break;
6845
6846                 default:
6847                   SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "SPutFpaMsg: Invalid\
6848                                 buffer size, dropping the message");
6849                   break;
6850          }
6851
6852          curBlk = nextBlk;
6853   }
6854
6855   cvmx_fpa_free(fpaBuf, SS_CVMX_POOL_0, 0);
6856
6857   return ROK;
6858
6859 } /* SPutFpaMsg */
6860
6861 #endif /* SS_SEUM_CAVIUM */
6862
6863 /* ss006.301 : new buffer management APIs, start */
6864 /*
6865 *
6866 *       Fun:   SCpyPartMsg
6867 *
6868 *       Desc:  This function is used to copy a portion of message(srcBuf) into
6869 *              another msg(dstBuf)
6870 *
6871 *       Ret:   ROK      - ok
6872 *              RFAILED  - failed, general (optional)
6873 *              ROUTRES  - failed, out of resources (optional) - In this case
6874 *                         caller shall reclaim the resources allocated for dstBuf.
6875 *
6876 *       Notes: None
6877 *
6878 *       File:  ss_msg.c
6879 *
6880 */
6881
6882 S16 SCpyPartMsg
6883 (
6884 Buffer *srcBuf,
6885 MsgLen idx,
6886 MsgLen cnt,
6887 Buffer *dstBuf
6888 )
6889 {
6890    SsMsgInfo *sMinfo;
6891    SsMsgInfo *dMinfo;
6892    Buffer    *dBuf;
6893    Buffer    *sBuf;
6894    Buffer    *dPrev;
6895    MsgLen    sCnt;
6896    MsgLen    dCnt;
6897    MsgLen    numCpd;
6898  
6899
6900 #if (ERRCLASS & ERRCLS_INT_PAR)
6901    if (!srcBuf)
6902    {
6903       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Null src Buffer");
6904       return RFAILED;
6905    }
6906    if (srcBuf->b_datap->db_type != SS_M_PROTO)
6907    {
6908       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
6909                                                    src buffer type");
6910       return RFAILED;
6911    }
6912    if(!dstBuf)
6913    {
6914       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Null dst Buffer");
6915       return RFAILED;
6916    }
6917    if (dstBuf->b_datap->db_type != SS_M_PROTO)
6918    {
6919       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
6920                                                    dst buffer type");
6921       return RFAILED;
6922    }
6923 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
6924
6925    /* if index > length of mBuf, return */
6926    sMinfo = (SsMsgInfo*)srcBuf->b_rptr;
6927    dMinfo = (SsMsgInfo*)dstBuf->b_rptr;
6928
6929    if (idx >= sMinfo->len)
6930    {
6931       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
6932                                                    idx value ");
6933       return RFAILED;
6934    }
6935
6936    sBuf = srcBuf->b_cont;
6937    FIND_OFFSET(sBuf, idx)
6938
6939    dPrev = dstBuf;
6940 /*-- ss008.301 */
6941    dBuf = (dMinfo->endptr) ? (dMinfo->endptr) : (dstBuf->b_cont);
6942    if(dMinfo->endptr)
6943    {
6944       Buffer *crnt;
6945       crnt = dPrev->b_cont;
6946       while(crnt)
6947       {
6948          dPrev = crnt;
6949          crnt = crnt->b_cont;
6950       }
6951    }
6952    dCnt = 0;
6953    numCpd = 0;
6954    while(cnt && sBuf)
6955    {
6956       sCnt = sBuf->b_wptr - (sBuf->b_rptr + idx);
6957       if(dBuf)
6958       {
6959          dCnt = dBuf->b_datap->db_lim - dBuf->b_wptr;
6960       }
6961       else
6962       {
6963          /* allocate a data buffer */
6964          if (SGetDBuf(dMinfo->region, dMinfo->pool, &dBuf) != ROK)
6965          {
6966             SSLOGERROR(ERRCLS_DEBUG, ESS125, ERRZERO, "SCpyPartMsg : SGetDBuf\
6967                  failed");
6968             return (ROUTRES);
6969          }
6970          dCnt = MIN(cnt, (dBuf->b_datap->db_lim - dBuf->b_datap->db_base));
6971          dPrev->b_cont = dBuf;
6972          dPrev = dBuf;
6973       }
6974       if(sCnt > cnt) /* src Dblk has enough data to copy */
6975       {
6976          if(dCnt < cnt)
6977          {
6978             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), dCnt);
6979             cnt -= dCnt;
6980             dBuf->b_wptr += dCnt;
6981             dBuf = dBuf->b_cont;
6982             idx += dCnt;
6983             numCpd += dCnt;
6984          }
6985          else
6986          {
6987             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), cnt);
6988             dBuf->b_wptr += cnt;
6989             dBuf->b_cont = NULLP;
6990             numCpd += cnt;
6991             cnt = 0;
6992             break;
6993          }
6994       }
6995       else /* src dBlk has partial data to be copied */
6996       {
6997          if(dCnt < sCnt)
6998          {
6999             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), dCnt);
7000             dBuf->b_wptr += dCnt;
7001             dBuf = dBuf->b_cont;
7002             cnt -= dCnt;
7003             idx += dCnt;
7004             numCpd += dCnt;
7005          }
7006          else
7007          {
7008             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), sCnt);
7009             dBuf->b_wptr += sCnt;
7010             cnt -= sCnt;
7011             sBuf = sBuf->b_cont;
7012             numCpd += sCnt;
7013             idx = 0;
7014          }
7015       }
7016    }
7017    /* update the msgLen in dstBuf */
7018    dMinfo->len += numCpd;
7019    dMinfo->endptr = dBuf; 
7020
7021    return ROK;
7022 }
7023
7024 /*
7025 *
7026 *       Fun:   SRepPartMsg
7027 *
7028 *       Desc:  This function is used to replace a portion of message(mBuf) with the
7029 *              given data
7030 *
7031 *       Ret:   ROK      - ok
7032 *              RFAILED  - failed, general (optional)
7033 *
7034 *       Notes: None
7035 *
7036 *       File:  ss_msg.c
7037 *
7038 */
7039
7040 S16 SRepPartMsg
7041 (
7042 Buffer *srcBuf,
7043 MsgLen idx,
7044 MsgLen cnt,
7045 Buffer *dstBuf
7046 )
7047 {
7048    SsMsgInfo *sMinfo;
7049    SsMsgInfo *dMinfo;
7050    Buffer    *dBuf;
7051    Buffer    *sBuf;
7052    MsgLen     dCnt;
7053    MsgLen     sCnt;
7054    MsgLen     sIdx;
7055    MsgLen     cpBytes;
7056
7057
7058 #if (ERRCLASS & ERRCLS_INT_PAR)
7059    if (!dstBuf)
7060    {
7061       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Null dstBuf");
7062       return RFAILED;
7063    }
7064    if (dstBuf->b_datap->db_type != SS_M_PROTO)
7065    {
7066       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7067                                                    dstBuf buffer type");
7068       return RFAILED;
7069    }
7070    if(!srcBuf)
7071    {
7072       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Null Src Buffer ");
7073       return RFAILED;
7074    }
7075    if (srcBuf->b_datap->db_type != SS_M_PROTO)
7076    {
7077       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7078                                                    sBuf buffer type");
7079       return RFAILED;
7080    }
7081 #endif  /* (ERRCLASS & ERRCLS_INT_PAR) */
7082
7083    dMinfo = (SsMsgInfo*)dstBuf->b_rptr;
7084    sMinfo = (SsMsgInfo*)srcBuf->b_rptr;
7085
7086    if(((idx + cnt) > dMinfo->len) || (cnt < sMinfo->len))
7087    {
7088       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7089                                                    cnt value ");
7090       return RFAILED;
7091    }
7092
7093    dBuf = dstBuf->b_cont;
7094    FIND_OFFSET(dBuf, idx)
7095    sBuf = srcBuf->b_cont;
7096    sIdx = 0;
7097    while(cnt && dBuf && sBuf)
7098    {
7099       dCnt = (dBuf->b_wptr - (dBuf->b_rptr + idx));
7100       sCnt = sBuf->b_wptr - (sBuf->b_rptr + sIdx);
7101       cpBytes = MIN(cnt, sCnt);
7102       cpBytes = MIN(cpBytes, dCnt);
7103       SMemCpy((void*)(dBuf->b_rptr + idx), (void*)(sBuf->b_rptr + sIdx), cpBytes);
7104       
7105       if(cpBytes < cnt)
7106       {
7107          if(cpBytes == sCnt)
7108          {
7109             /* move to next DBlk in srcBuf */
7110             sBuf = sBuf->b_cont;
7111             idx += cpBytes;
7112             sIdx = 0;
7113          }
7114          else /* cpBytes equals dCnt */
7115          {
7116             /* move to the next DBlk in dstBuf */
7117             dBuf = dBuf->b_cont;
7118             idx = 0;
7119             sIdx += cpBytes;
7120          }
7121          cnt -= cpBytes;
7122       }
7123       else
7124       {
7125          cnt = 0;
7126          break;
7127       }
7128    }
7129    if(cnt)
7130    {
7131       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : unable to replace\
7132                                                     some bytes ");
7133       return RFAILED;
7134    }
7135
7136    return ROK;
7137 }
7138
7139 /*
7140 *
7141 *       Fun:   SMovPartMsg
7142 *
7143 *       Desc:  This function will move a portion of the first msg to second msg
7144 *
7145 *       Ret:   ROK     - ok
7146 *              ROKDNA  - ok, data not available
7147 *              RFAILED - failed, general (optional)
7148 *              ROUTRES - failed, out of resources (optional)
7149 *
7150 *       Notes: message 1 is the message from which the segment will be copied 
7151 *
7152 *              message 2 is the updated message.
7153 *
7154 *              index is 0 based and indicates location in message 1
7155 *              up to which the data will be copied to message 2 
7156 *
7157 *              if index is equal to 0, message 1 will not be changed and no data 
7158                shall be copied to message 2.
7159 *              message 1 is not returned to memory. return is ok.
7160 *
7161 *              if index is not equal to 0 and less than the length of
7162 *              the message minus 1: data upto index, shall be copied to message 2 
7163 *              and read/write pointers of message 1 will be updated accordingly              
7164 *
7165 *              if index is not equal to 0 and greater than or equal to
7166 *              the length of the message minus 1: all of the message 1 data.
7167 *              shall be copied to message 2. return is ok.
7168 *
7169 *       File:  ss_msg.c
7170 *
7171 */
7172
7173
7174 S16 SMovPartMsg
7175 (
7176 Buffer *srcBuf,              /* message 1 */
7177 MsgLen idx,                 /* index */
7178 Buffer *dstBuf              /* message 2 */
7179 )
7180 {
7181    MsgLen     dCnt;
7182    MsgLen     sCnt;
7183    Buffer     *dPrev;
7184    Buffer     *sBuf;
7185    Buffer     *dBuf;
7186    SsMsgInfo  *sMinfo; 
7187    SsMsgInfo  *dMinfo;
7188 #ifdef T2K_MEM_LEAK_DBG
7189    char* file = __FILE__;
7190    uint32_t line = __LINE__;
7191 #endif
7192
7193
7194 #if (ERRCLASS & ERRCLS_INT_PAR)
7195    /* check message buffer 1 */
7196    if ((!srcBuf) || (!dstBuf ))
7197    {
7198       SSLOGERROR(ERRCLS_INT_PAR, ESS097, ERRZERO, "SMovPartMsg : Null Buffer (src or Dst)");
7199       return RFAILED;
7200    }
7201    if (idx < 0)
7202    {
7203       SSLOGERROR(ERRCLS_INT_PAR, ESS099, ERRZERO, "SMovPartMsg : Invalid index");
7204       return RFAILED;
7205    }
7206    if ((srcBuf->b_datap->db_type != SS_M_PROTO) || (dstBuf->b_datap->db_type != SS_M_PROTO)) 
7207    {
7208       SSLOGERROR(ERRCLS_INT_PAR, ESS100, ERRZERO, "SMovPartMsg : Incorrect buffer\
7209                                                    type");
7210       return RFAILED;
7211    }
7212 #endif
7213
7214    /* get the SsMsgInfo of srcBuf */
7215    sMinfo = (SsMsgInfo*) srcBuf->b_rptr;
7216
7217    /* get the SsMsgInfo of dstBuf */
7218    dMinfo = (SsMsgInfo*) dstBuf->b_rptr;
7219
7220    /* if index > length of mBuf, return */
7221    /*-- ss008.301 */
7222    if (idx > sMinfo->len)
7223    {
7224       return (ROKDNA);
7225    }
7226
7227    /* one block might not sufficient - Check for generic implementation */
7228    sBuf = srcBuf->b_cont;
7229    /* dBuf = dMinfo->endptr; */
7230    dPrev =  (dBuf = dMinfo->endptr) ?  dBuf : dstBuf;
7231
7232    /* adjust the lengths of srcBuf, dstBuf */
7233    sMinfo->len -= idx;
7234    dMinfo->len += idx;
7235
7236    while(idx && sBuf) 
7237    {
7238       sCnt = sBuf->b_wptr - sBuf->b_rptr;
7239       if(!dBuf)
7240       {
7241          /* allocate a data buffer */
7242          if (SGetDBuf(dMinfo->region, dMinfo->pool, &dBuf) != ROK)
7243          {
7244             SSLOGERROR(ERRCLS_DEBUG, ESS125, ERRZERO, "SCpyMsgPartMsg : SGetDBuf\
7245                  failed");
7246             return (ROUTRES);
7247          }
7248          dCnt = MIN(idx, (dBuf->b_datap->db_lim - dBuf->b_datap->db_base));
7249          dPrev->b_cont = dBuf;
7250          dPrev = dBuf;
7251       }
7252       else
7253       {
7254          dCnt = dBuf->b_datap->db_lim - dBuf->b_wptr;
7255       }
7256       if(sCnt > idx) /* src Dblk has enough data to copy */
7257       {
7258          if(dCnt < idx)
7259          {
7260             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), dCnt);
7261             dBuf->b_wptr += dCnt;
7262             dBuf = dBuf->b_cont;
7263             idx -= dCnt;
7264             sBuf->b_rptr += dCnt;
7265          }
7266          else
7267          {
7268             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), idx);
7269             dBuf->b_wptr += idx;
7270             dBuf->b_cont = NULLP;
7271             sBuf->b_rptr += idx;
7272             idx = 0;
7273             break;
7274          }
7275       }
7276       else /* src dBlk has partial data to be copied */
7277       {
7278          if(dCnt < sCnt)
7279          {
7280             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), dCnt);
7281             dBuf->b_wptr += dCnt;
7282             dBuf = dBuf->b_cont;
7283             idx -= dCnt;
7284             sBuf->b_rptr += dCnt;
7285          }
7286          else
7287          {
7288             Buffer *tmp;
7289             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), sCnt);
7290             dBuf->b_wptr += sCnt;
7291             idx -= sCnt;
7292             /* deallocate the sBuf here */
7293             tmp = sBuf->b_cont;
7294             /* printf("\nSMovPartMsg() sBuf is completely copied sBuf->b_cont(%p)\n", sBuf->b_cont); */
7295             (Void)SPutDBuf(sMinfo->region, sMinfo->pool, sBuf);
7296             srcBuf->b_cont = sBuf = tmp;
7297          }
7298       }
7299    }
7300    dMinfo->endptr = dBuf;
7301    if(idx)
7302    {
7303       sMinfo->len += idx;
7304       dMinfo->len -= idx;
7305       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SMovPartMsg : unable to copy\
7306                                                     some bytes ");
7307       return RFAILED;
7308    }
7309
7310    return ROK;
7311 }
7312
7313 /*
7314 *
7315 *       Fun:   SPkMsgMult
7316 *
7317 *       Desc:  This function copies consecutive bytes of data to the
7318 *              beginning of a message.
7319 *
7320 *       Ret:   ROK      - ok
7321 *              RFAILED  - failed, general (optional)
7322 *              ROUTRES  - failed, out of resources (optional)
7323 *
7324 *       Notes: if message is empty: data is placed in the message. message
7325 *              length is incremented. return is ok.
7326 *
7327 *              if message is not empty: data is read by source pointer,
7328 *              data is placed in front of all other data in message.
7329 *              message length is incremented. return is ok.
7330 *
7331 *              the first byte of data pointed to by the source pointer 
7332 *              will be placed at the front of the message first (i.e. it 
7333 *              will become the first byte of the message) and the last 
7334 *              byte will be placed in front of the existing msg contents,
7335 *              i.e. order of the source is preserved.
7336 *
7337 *       File:  ss_msg.c
7338 *
7339 */
7340  
7341 S16 SPkMsgMult
7342 (
7343 Data *src,
7344 MsgLen cnt,
7345 Buffer *mBuf
7346 )
7347 {
7348    SsMsgInfo *minfo;   /* Message info */
7349    Buffer *tmp;
7350    Buffer *newblk;
7351    MsgLen numBytes;  /* no. of bytes to be copied */
7352    MsgLen offset;
7353
7354
7355 #if (ERRCLASS & ERRCLS_INT_PAR)
7356    /* check message buffer */
7357    if (mBuf == NULLP)
7358    {
7359       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SPkMsgMult:Null Buffer");
7360       return RFAILED;
7361    }
7362    /* check source */
7363    if (src == NULLP)
7364    {
7365       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SPkMsgMult:Null Buffer");
7366       return RFAILED;
7367    }
7368    /* check count */
7369    if (cnt <= 0)
7370    {
7371       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SPkMsgMult: Invalid\
7372                                                    count");
7373       return RFAILED;
7374    }
7375    if (mBuf->b_datap->db_type != SS_M_PROTO)
7376    {
7377       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SPkMsgMult: Incorrect\
7378                                                    buffer type");
7379       return RFAILED;
7380    }
7381 #endif
7382
7383    /* get the SsMsgInfo of mBuf */
7384    minfo = (SsMsgInfo*) mBuf->b_rptr;
7385
7386 /* ss014.13: Addition */
7387    offset = 0;
7388
7389    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
7390        (tmp->b_datap->db_base < tmp->b_rptr))
7391    {
7392       /* store the offset of the read pointer of tmp */
7393       offset = tmp->b_rptr - tmp->b_datap->db_base;
7394
7395       /* determine the number of bytes to copy */
7396       numBytes = MIN(cnt, offset);
7397
7398       /* decrement cnt */
7399       cnt -= numBytes;
7400
7401       /* update the read ptr */
7402       tmp->b_rptr -= numBytes;
7403       /* copy data */
7404       memcpy(tmp->b_rptr, (src + cnt), numBytes);
7405       minfo->len += numBytes;
7406       if (!cnt)
7407       {
7408          return ROK;
7409       }
7410    }
7411
7412    /* allocate a DBlk minimum of size Cnt to pack the data */
7413    newblk = NULLP;
7414    if (ssGetDBufOfSize(minfo->region, cnt, &newblk) != ROK)
7415    {
7416        return (ROUTRES);
7417    }
7418    newblk->b_datap->db_type = SS_M_DATA;
7419    newblk->b_rptr = newblk->b_datap->db_lim - cnt;
7420    newblk->b_wptr = newblk->b_datap->db_lim;
7421    memcpy(newblk->b_rptr, src, cnt);
7422    /* attach the newblk chain into mBuf */
7423    newblk->b_cont = tmp;
7424    mBuf->b_cont = newblk;
7425    minfo->len += cnt;
7426    minfo->endptr = newblk;
7427
7428    return ROK;
7429 }
7430 /* ss006.301 : new buffer management APIs, end */
7431 /*
7432 *
7433 *       Fun:   SGetReadPtr 
7434 *
7435 *       Desc:  This function retunrs the pointer to the read the message from mBuf
7436 *              
7437 *       Ret:   ROK      - ok
7438 *              RFAILED  - failed, general (optional)
7439 *
7440 *       Notes: 
7441 *
7442 *       File:  ss_msg.c
7443 *
7444 */
7445 S16 SGetReadPtr
7446 (
7447 Buffer *mBuf, 
7448 uint8_t** data, 
7449 MsgLen *len
7450 )
7451 {
7452 /*   Buffer       *tmp; */
7453
7454    if (mBuf && mBuf->b_cont)
7455    {
7456       *data = mBuf->b_cont->b_rptr;
7457       *len = ((SsMsgInfo*)(mBuf->b_rptr))->len;
7458    }
7459    else
7460    {
7461       *data = NULLP;
7462       *len = 0;
7463    }
7464
7465    return ROK;
7466 }
7467 #ifdef SS_USE_ZBC_MEMORY
7468 /*
7469 *
7470 *       Fun:   SAttachPtrToBuf 
7471 *
7472 *       Desc:  This function attaches the Pointer provided into a new
7473 *              message buffer after allocating the same. It allocates
7474 *              header (M-Block) and an additional dBuf (D-Block) and attaches
7475 *              the provided pointer to it.
7476 *
7477 *       Ret:   ROK      - ok
7478 *              RFAILED  - failed, general (optional)
7479 *              ROUTRES  - failed, out of resources (optional)
7480 *
7481 *       Notes: 
7482 *
7483 *       File:  ss_msg.c
7484 *
7485 */
7486
7487 #ifdef T2K_MEM_LEAK_DBG
7488 S16 SAttachPtrToBufNew
7489 (
7490 Region   region,
7491 Pool     pool,
7492 Data    *ptr,
7493 MsgLen   totalLen,
7494 Buffer** mBuf,
7495 char* file,
7496 uint32_t line
7497 )
7498 #else
7499 S16 SAttachPtrToBuf
7500 (
7501 Region   region,
7502 Pool     pool,
7503 Data    *ptr,
7504 MsgLen   totalLen,
7505 Buffer** mBuf
7506 )
7507 #endif
7508 {
7509    SsMsgInfo *minfo;   /* Message info */
7510    Size       mdsize;
7511    SsDblk    *dptr;
7512    Buffer    *newblk;
7513    /*   Void      *iccHdlr; */
7514
7515
7516 #if (ERRCLASS & ERRCLS_INT_PAR)
7517    if (ptr == NULLP)
7518    {
7519       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
7520       return RFAILED;
7521    }
7522 #endif
7523
7524 #ifdef XEON_SPECIFIC_CHANGES
7525    region = 0;
7526 #endif   
7527    if(SGetMsg(region, pool, mBuf) != ROK)
7528    {
7529       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
7530       return (ROUTRES);
7531    }
7532
7533    mdsize = MDBSIZE;
7534
7535 #ifdef SS_HISTOGRAM_SUPPORT 
7536    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
7537 #else
7538    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
7539 #endif /* SS_HISTOGRAM_SUPPORT */
7540    {
7541        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
7542        SPutMsg(*mBuf);
7543        return (ROUTRES);
7544    }
7545
7546    (*mBuf)->b_cont = newblk;
7547
7548    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
7549
7550    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
7551
7552
7553    newblk->b_datap->db_base = ptr;
7554    newblk->b_datap->db_lim = ptr + totalLen;
7555    newblk->b_rptr = newblk->b_datap->db_base;
7556    newblk->b_wptr = newblk->b_rptr + totalLen;
7557
7558 #ifndef SS_DBUF_REFLOCK_DISABLE
7559    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
7560    {
7561       printf("\nFalied to destroy lock\n");
7562    }
7563 #endif
7564
7565    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
7566
7567    /* get the SsMsgInfo of mBuf */
7568    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
7569    minfo->len = totalLen;
7570    minfo->endptr = newblk;
7571
7572    return ROK;
7573 }
7574
7575 /*
7576 *
7577 *       Fun:   SPutZbcDBuf
7578 *
7579 *       Desc:  This function deallocates a buffer back to the
7580 *              dynamic memory pool which is allocated for ZBC
7581 *
7582 *       Ret:   ROK      - ok
7583 *              RFAILED  - failed, general (optional)
7584 *
7585 *       Notes:
7586 *
7587 *       File:  ss_msg.c
7588 *
7589 */
7590
7591 #ifdef T2K_MEM_LEAK_DBG
7592 static S16 SPutZbcDBufNew
7593 (
7594 Region region,
7595 Buffer *buf,
7596 char* file,
7597 uint32_t line
7598 )
7599 #else
7600 static S16 SPutZbcDBuf
7601 (
7602 Region region,
7603 Buffer *buf
7604 )
7605 #endif
7606 {
7607    register SsDblk *dptr;
7608    MsgLen          bufLen;
7609    S16             ret = ROK;
7610  
7611  
7612    dptr = buf->b_datap; 
7613    /* Get the length of the buffer */
7614    bufLen = buf->b_datap->db_lim - buf->b_datap->db_base;
7615
7616    /* If the buffer is not shared, free the buffer */
7617    if(!dptr->shared)
7618    {
7619 #ifndef SS_DBUF_REFLOCK_DISABLE
7620        SDestroyLock(&dptr->dBufLock);
7621 #endif
7622
7623        /* Free the ZBC buffer first and then free the block allocated for the 
7624         * D-Block */
7625
7626 #ifdef SS_HISTOGRAM_SUPPORT 
7627       ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen, __LINE__, 
7628                   (uint8_t*) __FILE__, ENTNC);
7629 #else
7630       ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen);
7631 #endif /* SS_HISTOGRAM_SUPPORT */
7632
7633       /* if the data block is not shared, free the buffer, checks not reqd */
7634 #ifdef SS_HISTOGRAM_SUPPORT 
7635       ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, 
7636                   ENTNC);
7637 #else
7638       ret =  SFree(region, (Data *) buf, MDBSIZE);
7639 #endif /* SS_HISTOGRAM_SUPPORT */
7640    }
7641    /* If the buffer is shared, reduce the refernce count and free the buffer
7642     * if reference count falls to zero */
7643    else
7644    {
7645 #ifndef SS_DBUF_REFLOCK_DISABLE
7646         if((ret=SLock(&dptr->dBufLock)))
7647         {
7648            SSLOGERROR(ERRCLS_DEBUG, ESSXXX, ERRZERO,
7649                 "Could not lock the mBuf Ref Lock");
7650            return RFAILED;
7651         }
7652 #endif
7653       --dptr->db_ref;
7654       /* if buffer's message blk is obtained during dupb */
7655       if (buf != (SsMblk *) (((Data*) dptr) - MBSIZE))
7656       {
7657 #ifdef SS_HISTOGRAM_SUPPORT 
7658          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, ENTNC);
7659 #else
7660          ret = SFree(region, (Data *) buf, MDBSIZE);
7661 #endif /* SS_HISTOGRAM_SUPPORT */
7662          buf = (SsMblk *) (((Data*) dptr) - MBSIZE);
7663       }
7664       /* if reference count falls to zero */
7665       if (!dptr->db_ref)
7666       {
7667 #ifndef SS_DBUF_REFLOCK_DISABLE
7668          ret = SUnlock(&dptr->dBufLock) ;
7669          if((SDestroyLock(&dptr->dBufLock)) != 0)
7670          {
7671              printf("\nFalied to destroy lock\n");
7672          }
7673 #endif
7674 #ifdef SS_HISTOGRAM_SUPPORT 
7675          ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen, __LINE__, 
7676                    (uint8_t*) __FILE__, ENTNC);
7677 #else
7678          ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen);
7679 #endif /* SS_HISTOGRAM_SUPPORT */
7680
7681 #ifdef SS_HISTOGRAM_SUPPORT 
7682          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, 
7683                   ENTNC);
7684 #else
7685          ret =  SFree(region, (Data *) buf, MDBSIZE);
7686 #endif /* SS_HISTOGRAM_SUPPORT */
7687           return (ret);
7688       }
7689 #ifndef SS_DBUF_REFLOCK_DISABLE
7690       ret = SUnlock(&(dptr->dBufLock));
7691 #endif
7692    }
7693
7694    return (ret);
7695 }
7696 #endif /* SS_USE_ZBC_MEMORY */
7697
7698 #ifdef INTEL_WLS
7699
7700 /*
7701 *
7702 *       Fun:   SAttachPtrToMBuf 
7703 *
7704 *       Desc:  This function attaches the Pointer provided into a new
7705 *              message buffer after allocating the same. It allocates
7706 *              header (M-Block) and an additional dBuf (D-Block) and attaches
7707 *              the provided pointer to it.
7708 *
7709 *       Ret:   ROK      - ok
7710 *              RFAILED  - failed, general (optional)
7711 *              ROUTRES  - failed, out of resources (optional)
7712 *
7713 *       Notes: 
7714 *
7715 *       File:  ss_msg.c
7716 *
7717 */
7718 #ifdef T2K_MEM_LEAK_DBG
7719 S16 SAttachPtrToMBuf1
7720 (
7721 Region   region,
7722 Pool     pool,
7723 Data    *ptr,
7724 MsgLen   totalLen,
7725 MsgLen   ptrLen,
7726 Buffer** mBuf,
7727 char* file,
7728 uint32_t line
7729 )
7730 #else
7731 S16 SAttachPtrToMBuf
7732 (
7733 Region   region,
7734 Pool     pool,
7735 Data    *ptr,
7736 MsgLen   totalLen,
7737 MsgLen   ptrLen,
7738 Buffer** mBuf
7739 )
7740 #endif
7741 {
7742    SsMsgInfo *minfo;   /* Message info */
7743    Size       mdsize;
7744    SsDblk    *dptr;
7745    Buffer    *newblk;
7746    /*   Void      *iccHdlr; */
7747
7748
7749 #if (ERRCLASS & ERRCLS_INT_PAR)
7750    if (ptr == NULLP)
7751    {
7752       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
7753       return RFAILED;
7754    }
7755 #endif
7756
7757    if(SGetMsg(region, pool, mBuf) != ROK)
7758    {
7759       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
7760       return (ROUTRES);
7761    }
7762
7763    region = 0;
7764    mdsize = MDBSIZE;
7765
7766 #ifdef SS_HISTOGRAM_SUPPORT 
7767    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
7768 #else
7769    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
7770 #endif /* SS_HISTOGRAM_SUPPORT */
7771    {
7772        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
7773        SPutMsg(*mBuf);
7774        return (ROUTRES);
7775    }
7776
7777    (*mBuf)->b_cont = newblk;
7778
7779    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
7780
7781    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
7782
7783
7784    newblk->b_datap->db_base = ptr;
7785    newblk->b_datap->db_lim = ptr + ptrLen;
7786    newblk->b_rptr = newblk->b_datap->db_base;
7787    newblk->b_wptr = newblk->b_rptr + totalLen;
7788
7789 #ifndef SS_DBUF_REFLOCK_DISABLE
7790    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
7791    {
7792       printf("\nFalied to destroy lock\n");
7793    }
7794 #endif
7795
7796    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
7797
7798    /* get the SsMsgInfo of mBuf */
7799    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
7800    minfo->len    = totalLen;
7801    minfo->endptr = newblk;
7802
7803    //printf("\nMbuf PTR = %x, DBlk PTR = %x, PTR = %x\n", *mBuf, newblk, ptr);
7804    return ROK;
7805 }
7806 #endif /* INTEL_WLS */
7807 S16 SIncMsgRef(Buffer *srcBuf,Region dstRegion, Pool dstPool,Buffer **dstBuf)
7808 {
7809 #ifndef L2_OPTMZ 
7810   return (SAddMsgRef(srcBuf,dstRegion, dstPool,dstBuf));
7811 #else 
7812  *dstBuf = srcBuf;
7813 #endif 
7814   return ROK;
7815 }
7816 #ifdef L2_OPTMZ
7817 Void SResetMBuf(Buffer *mbuf)
7818 {
7819    SsMsgInfo *minfo;
7820    Buffer *tmp;
7821 #if 1
7822
7823    minfo = (SsMsgInfo *) mbuf->b_rptr;
7824    //tmp   = minfo->endptr;
7825    tmp   = mbuf->b_cont;
7826
7827    minfo->len  = 0;
7828    if(tmp)
7829    {
7830
7831     //    printf("\nSResetMBuf The write & read ptr is %p %p %p %p \n", tmp->b_wptr, tmp->b_rptr, tmp->b_datap->db_base, tmp->b_datap->db_lim );
7832 //      tmp->b_wptr = tmp->b_rptr = tmp->b_datap->db_base;
7833       tmp->b_wptr = tmp->b_rptr = (tmp->b_datap->db_base + 5);
7834       tmp->b_datap->db_ref = 1;
7835       tmp->b_datap->shared = 0;
7836    }
7837 #endif
7838
7839
7840    return;
7841 }
7842 #endif
7843
7844 #ifdef INTEL_WLS
7845 /*
7846 *
7847 *       Fun:   SAttachWlsPtrToMBuf 
7848 *
7849 *       Desc:  This function attaches the Pointer provided into a new
7850 *              message buffer after allocating the same. It allocates
7851 *              header (M-Block) and an additional dBuf (D-Block) and attaches
7852 *              the provided pointer to it.
7853 *
7854 *       Ret:   ROK      - ok
7855 *              RFAILED  - failed, general (optional)
7856 *              ROUTRES  - failed, out of resources (optional)
7857 *
7858 *       Notes: 
7859 *
7860 *       File:  ss_msg.c
7861 *
7862 */
7863 #ifdef T2K_MEM_LEAK_DBG
7864 S16 SAttachWlsPtrToMBuf1
7865 (
7866 Region   region,
7867 Pool     pool,
7868 Data    *ptr,
7869 Data    *readPtr,
7870 MsgLen   totalLen,
7871 MsgLen   ptrLen,
7872 Buffer** mBuf,
7873 char* file,
7874 uint32_t line
7875 )
7876 #else
7877 S16 SAttachWlsPtrToMBuf
7878 (
7879 Region   region,
7880 Pool     pool,
7881 Data    *ptr,
7882 Data    *readPtr,
7883 MsgLen   totalLen,
7884 MsgLen   ptrLen,
7885 Buffer** mBuf
7886 )
7887 #endif
7888 {
7889    SsMsgInfo *minfo;   /* Message info */
7890    Size       mdsize;
7891    SsDblk    *dptr;
7892    Buffer    *newblk;
7893    /*   Void      *iccHdlr; */
7894
7895
7896 #if (ERRCLASS & ERRCLS_INT_PAR)
7897    if (ptr == NULLP)
7898    {
7899       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
7900       return RFAILED;
7901    }
7902 #endif
7903
7904    region = 0;
7905    if(SGetMsg(region, pool, mBuf) != ROK)
7906    {
7907       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
7908       return (ROUTRES);
7909    }
7910
7911    mdsize = MDBSIZE;
7912
7913 #ifdef SS_HISTOGRAM_SUPPORT 
7914    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
7915 #else
7916    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
7917 #endif /* SS_HISTOGRAM_SUPPORT */
7918    {
7919        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
7920        SPutMsg(*mBuf);
7921        return (ROUTRES);
7922    }
7923
7924    (*mBuf)->b_cont = newblk;
7925
7926    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
7927
7928    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
7929
7930
7931    newblk->b_datap->db_base = ptr;
7932    newblk->b_datap->db_lim  = ptr + ptrLen;
7933    newblk->b_rptr           = readPtr;
7934    newblk->b_wptr           = newblk->b_rptr + totalLen;
7935
7936 #ifndef SS_DBUF_REFLOCK_DISABLE
7937    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
7938    {
7939       printf("\nFalied to destroy lock\n");
7940    }
7941 #endif
7942
7943    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
7944
7945    /* get the SsMsgInfo of mBuf */
7946    minfo         = (SsMsgInfo*) (*mBuf)->b_rptr;
7947    minfo->len    = totalLen;
7948    minfo->endptr = newblk;
7949
7950    //printf("\nMbuf PTR = %x, DBlk PTR = %x, PTR = %x\n", *mBuf, newblk, ptr);
7951    return ROK;
7952 }
7953
7954 #ifdef TENB_DPDK_BUF
7955
7956 uint32_t numeTti;
7957
7958 S16 SGetSBufDpdk
7959 (
7960 Data **ptr,                     /* pointer to buffer */
7961 Size size                       /* size requested */
7962 )
7963 {
7964     S16   ret=ROK;
7965     
7966     *ptr = ntl_alloc(mtGetNtlHdl(), size);
7967
7968     return (ret);
7969 }
7970
7971 S16 SPutSBufDpdk
7972 (
7973 Data *ptr                     /* pointer to buffer */
7974 )
7975 {
7976     S16   ret = ROK;
7977     uint32_t   flags = 0;
7978
7979     ntl_free(mtGetNtlHdl(), ptr);
7980
7981     return (ret);
7982 }
7983
7984 S16 SDetachDpdkPtrFrmDBuf
7985 (
7986 Buffer     *mBuf,
7987 Data       **ptr
7988 )
7989 {
7990
7991     Buffer       *msgBlk;
7992     SsMsgInfo    *minfo;
7993
7994     //msgBlk = mBuf->b_cont;
7995     //*ptr   = msgBlk->b_rptr;
7996     *ptr   = mBuf->b_datap->db_base;
7997
7998     mBuf->b_cont = NULL;
7999
8000     //minfo         = (SsMsgInfo*) mBuf->b_rptr;
8001     //minfo->len    = 0;
8002
8003
8004     return ROK;
8005 }
8006
8007
8008 S16 SDetachDpdkPtrFrmMBuf
8009 (
8010  Buffer     *mBuf,
8011  Data       **ptr
8012  )
8013 {
8014
8015    Buffer       *msgBlk;
8016    SsMsgInfo    *minfo;
8017    register     SsDblk *dptr;
8018
8019
8020
8021    msgBlk = mBuf->b_cont;
8022    if(msgBlk == NULLP)
8023    {
8024       *ptr = NULLP;
8025        return RFAILED;
8026    }
8027       
8028    dptr = msgBlk->b_datap;
8029    if(dptr->db_type != SS_MEM_TYPE_DPDK_ZBC)
8030    {
8031       *ptr = NULLP;
8032       return RFAILED;
8033    }   
8034
8035    *ptr   = msgBlk->b_rptr;
8036
8037    mBuf->b_cont = NULL;
8038
8039    minfo         = (SsMsgInfo*) mBuf->b_rptr;
8040    minfo->len    = 0;
8041
8042    return ROK;
8043 }
8044
8045
8046 S16 SAttachDpdkPtrToMBuf
8047 (
8048 Region   region,
8049 Pool     pool,
8050 Data    *ptr,
8051 Data    *readPtr,
8052 MsgLen   msgLen,
8053 MsgLen   totalLen,
8054 Buffer** mBuf
8055 )
8056 {
8057
8058    SsDblk    *dptr;
8059
8060    if(0 == msgLen)
8061       return RFAILED;
8062
8063    SAttachWlsPtrToMBuf(region, pool, ptr, readPtr, msgLen, totalLen, mBuf);
8064    dptr = (SsDblk*) (((Data *) ((*mBuf)->b_cont)) + MBSIZE);
8065    
8066    dptr->db_type = SS_MEM_TYPE_DPDK_ZBC;
8067
8068    return ROK;
8069 }
8070
8071 #endif /* TENB_DPDK_BUF */
8072
8073 #endif /* INTEL_WLS */
8074 \f
8075 /**********************************************************************
8076          End of file
8077 **********************************************************************/
8078