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