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