Made requesting instance's allocation count for resource available via the service...
[keystone-rtos/rm-lld.git] / src / rm_transport.c
1 /**\r
2  *   @file  rm_transport.c\r
3  *\r
4  *   @brief   \r
5  *      This is the Resource Manager transport source.\r
6  *\r
7  *  \par\r
8  *  ============================================================================\r
9  *  @n   (C) Copyright 2012-2013, Texas Instruments, Inc.\r
10  * \r
11  *  Redistribution and use in source and binary forms, with or without \r
12  *  modification, are permitted provided that the following conditions \r
13  *  are met:\r
14  *\r
15  *    Redistributions of source code must retain the above copyright \r
16  *    notice, this list of conditions and the following disclaimer.\r
17  *\r
18  *    Redistributions in binary form must reproduce the above copyright\r
19  *    notice, this list of conditions and the following disclaimer in the \r
20  *    documentation and/or other materials provided with the   \r
21  *    distribution.\r
22  *\r
23  *    Neither the name of Texas Instruments Incorporated nor the names of\r
24  *    its contributors may be used to endorse or promote products derived\r
25  *    from this software without specific prior written permission.\r
26  *\r
27  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
28  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
29  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
30  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
31  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
32  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
33  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
34  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
35  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
36  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
37  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
38  *\r
39  *  \par\r
40 */\r
41 \r
42 /* Standard includes */\r
43 #include <string.h>\r
44 \r
45 /* RM external includes */\r
46 #include <ti/drv/rm/rm.h>\r
47 #include <ti/drv/rm/rm_transport.h>\r
48 \r
49 /* RM internal includes */\r
50 #include <ti/drv/rm/include/rm_loc.h>\r
51 #include <ti/drv/rm/include/rm_transportloc.h>\r
52 \r
53 /* RM OSAL layer */\r
54 #include <rm_osal.h>\r
55 \r
56 /**********************************************************************\r
57  ************************ Local Functions *****************************\r
58  **********************************************************************/\r
59 \r
60 /* FUNCTION PURPOSE: Adds a transport\r
61  ***********************************************************************\r
62  * DESCRIPTION: Returns a pointer to a transport structure that\r
63  *              was created, initialized, and added to the \r
64  *              instance transport list.\r
65  */\r
66 static Rm_Transport *transportAdd(const Rm_TransportCfg *transportCfg)\r
67 {\r
68     Rm_Inst      *rmInst = (Rm_Inst *) transportCfg->rmHandle;\r
69     Rm_Transport *transports = rmInst->transports;\r
70     Rm_Transport *newTransport = NULL;\r
71 \r
72     newTransport = Rm_osalMalloc (sizeof(Rm_Transport));\r
73     memset((void *)newTransport, 0, sizeof(Rm_Transport));\r
74 \r
75     if (newTransport) {\r
76         newTransport->rmHandle = transportCfg->rmHandle;\r
77         newTransport->appTransportHandle = transportCfg->appTransportHandle;\r
78         newTransport->remoteInstType = transportCfg->remoteInstType;\r
79         newTransport->callouts.rmAllocPkt = transportCfg->transportCallouts.rmAllocPkt;\r
80         newTransport->callouts.rmSendPkt = transportCfg->transportCallouts.rmSendPkt;\r
81         newTransport->nextTransport = NULL;\r
82 \r
83         if (transports) {\r
84             while (transports->nextTransport) {\r
85                 transports = transports->nextTransport;\r
86             }\r
87             transports->nextTransport = newTransport;\r
88         }\r
89         else {\r
90             rmInst->transports = newTransport;\r
91         }\r
92     }\r
93     return (newTransport);\r
94 }\r
95 \r
96 /* FUNCTION PURPOSE: Tests if a transport is registered to an instance\r
97  ***********************************************************************\r
98  * DESCRIPTION: Returns TRUE if the supplied transport is found in\r
99  *              the instance transport list.  Otherwise, returns FALSE\r
100  */\r
101 static int transportIsRegistered(Rm_Handle rmHandle, Rm_Transport *transport)\r
102 {\r
103     Rm_Inst      *rmInst = (Rm_Inst *)rmHandle;\r
104     Rm_Transport *transportList = (Rm_Transport *)rmInst->transports;\r
105     \r
106     while (transportList) {\r
107         if (transportList == transport) {\r
108             return(RM_TRUE);             \r
109         }\r
110         transportList = transportList->nextTransport;\r
111     }\r
112     return (RM_FALSE);\r
113 }\r
114 \r
115 /* FUNCTION PURPOSE: Deletes a transport\r
116  ***********************************************************************\r
117  * DESCRIPTION: Removes a transport from an instance transport list\r
118  *              and then frees the memory associated with the transport\r
119  *              data structure\r
120  */\r
121 static void transportDelete(Rm_Transport *transport)\r
122 {\r
123     Rm_Inst      *rmInst = (Rm_Inst *)transport->rmHandle;\r
124     Rm_Transport *transportList = (Rm_Transport *)rmInst->transports;\r
125     Rm_Transport *prevTransport = NULL;\r
126 \r
127     /* Get previous transport in list */\r
128     while (transportList) {\r
129         if (transportList == transport) {\r
130             break;             \r
131         }\r
132         prevTransport = transportList;\r
133         transportList = transportList->nextTransport;\r
134     }\r
135 \r
136     if (prevTransport == NULL) {\r
137          rmInst->transports = transport->nextTransport;\r
138     }\r
139     else {\r
140          prevTransport->nextTransport = transport->nextTransport;\r
141     }\r
142     Rm_osalFree((void *)transport, sizeof(Rm_Transport));\r
143 }\r
144 \r
145 /**********************************************************************\r
146  ********************** Internal Functions ****************************\r
147  **********************************************************************/\r
148 \r
149 /* FUNCTION PURPOSE: Finds a transport based on remote inst type\r
150  ***********************************************************************\r
151  * DESCRIPTION: Returns a pointer to the transport within an instance's\r
152  *              transport list that matches the provided remote\r
153  *              instance type.  NULL is returned if no transports in \r
154  *              the list match the remote instance type.\r
155  */\r
156 Rm_Transport *rmTransportFindRemoteInstType(Rm_Transport *transports, Rm_InstType remoteInstType)\r
157 {\r
158     while (transports) {\r
159         if (transports->remoteInstType == remoteInstType) {\r
160             break;             \r
161         }\r
162         transports = transports->nextTransport;\r
163     }\r
164     return (transports);\r
165 }\r
166 \r
167 /**********************************************************************\r
168  ********************* Application visible APIs ***********************\r
169  **********************************************************************/\r
170 \r
171 /* FUNCTION PURPOSE: Registers an app transport with a RM instance\r
172  ***********************************************************************\r
173  * DESCRIPTION: Returns a transport handle to the application that\r
174  *              has been registered with an RM instance.  The handle\r
175  *              is used by the application to route packets to the\r
176  *              proper RM instance's based on the application\r
177  *              transport receive code.  The handle is also used\r
178  *              internally by RM to route request and response \r
179  *              packets to the correct application transports. NULL\r
180  *              is returned for the transport handle if any errors\r
181  *              are encountered.\r
182  */\r
183 Rm_TransportHandle Rm_transportRegister (const Rm_TransportCfg *transportCfg, int32_t *result)\r
184 {\r
185     Rm_Inst      *rmInst = (Rm_Inst *) transportCfg->rmHandle;\r
186     Rm_Transport *transport = NULL;\r
187     void         *key;\r
188 \r
189     *result = RM_OK;\r
190 \r
191     RM_SS_INST_INV_ENTER_CS(key);\r
192 \r
193     /* Shared servers and clients cannot connect to anyone */\r
194     if ((rmInst->instType == Rm_instType_SHARED_SERVER) ||\r
195         (rmInst->instType == Rm_instType_SHARED_CLIENT)){\r
196         *result = RM_ERROR_SHARED_INSTANCE_CANNOT_REG_TRANS;\r
197         goto errorExit;\r
198     }\r
199 \r
200     /* No one can connect to a shared server\r
201      * RM Servers cannot connect to other Servers.  \r
202      * RM Client Delegates cannot connect to other Client Delegates.\r
203      * RM Clients cannot connect to other Clients */\r
204     if ((transportCfg->remoteInstType == Rm_instType_SHARED_SERVER) ||\r
205         ((rmInst->instType == Rm_instType_SERVER) &&\r
206          (transportCfg->remoteInstType == Rm_instType_SERVER)) ||\r
207         ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
208          (transportCfg->remoteInstType == Rm_instType_CLIENT_DELEGATE)) ||\r
209         ((rmInst->instType == Rm_instType_CLIENT) &&\r
210          (transportCfg->remoteInstType == Rm_instType_CLIENT))) {\r
211         *result = RM_ERROR_INVALID_REMOTE_INST_TYPE;\r
212         goto errorExit;\r
213     }\r
214 \r
215     /* Verify Clients are not registering with more than one Client Delegate or Server. And\r
216      * that Client Delegate is not registering with more than one Server. */\r
217     if (rmInst->registeredWithDelegateOrServer &&\r
218         (((rmInst->instType == Rm_instType_CLIENT) &&\r
219           (transportCfg->remoteInstType == Rm_instType_CLIENT_DELEGATE)) || \r
220          ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
221           (transportCfg->remoteInstType == Rm_instType_SERVER)))) {\r
222         *result = RM_ERROR_ALREADY_REGD_SERVER_OR_CD;\r
223         goto errorExit;\r
224     }         \r
225     \r
226     if (!transportCfg->transportCallouts.rmAllocPkt) {\r
227         *result = RM_ERROR_TRANSPORT_ALLOC_PKT_NOT_REGD;\r
228         goto errorExit;\r
229     }\r
230     else if (!transportCfg->transportCallouts.rmSendPkt) {\r
231         *result = RM_ERROR_TRANSPORT_SEND_NOT_REGD;\r
232         goto errorExit;\r
233     }\r
234 \r
235     transport = transportAdd(transportCfg);\r
236     if ((transport->remoteInstType == Rm_instType_CLIENT_DELEGATE) ||\r
237         (transport->remoteInstType == Rm_instType_SERVER)) {\r
238         rmInst->registeredWithDelegateOrServer = RM_TRUE;\r
239     }\r
240 errorExit:\r
241     RM_SS_INST_WB_EXIT_CS(key);    \r
242     return ((Rm_TransportHandle) transport);\r
243 }\r
244 \r
245 /* FUNCTION PURPOSE: Reconfigures an instance's transport handle\r
246  ***********************************************************************\r
247  * DESCRIPTION: Reconfigures a transport handle based on the provided\r
248  *              configuration parameters if it exists within the \r
249  *              instance.\r
250  */\r
251 int32_t Rm_transportReconfig (Rm_TransportHandle transportHandle, const Rm_TransportReCfg *reCfg)\r
252 {\r
253     Rm_Transport *transport = (Rm_Transport *)transportHandle;\r
254     Rm_Inst      *rmInst = (Rm_Inst *)transport->rmHandle;\r
255     void         *key;\r
256     int32_t       retVal = RM_OK;\r
257 \r
258     RM_SS_INST_INV_ENTER_CS(key);\r
259 \r
260     if (transportIsRegistered(transport->rmHandle, transport)) {\r
261         /* Reconfigure existing transport's appTransportHandle.  Used in cases where instances\r
262          * running on same core connected by function call. */\r
263         transport->appTransportHandle = reCfg->appTransportHandle;\r
264 \r
265         if (reCfg->transportCallouts.rmAllocPkt) {\r
266             transport->callouts.rmAllocPkt = reCfg->transportCallouts.rmAllocPkt;\r
267         }\r
268         if (reCfg->transportCallouts.rmSendPkt) {\r
269             transport->callouts.rmSendPkt = reCfg->transportCallouts.rmSendPkt;\r
270         }\r
271     }\r
272     else {\r
273         retVal = RM_ERROR_TRANSPORT_HANDLE_DOES_NOT_EXIST;\r
274     }        \r
275 \r
276     RM_SS_INST_WB_EXIT_CS(key);    \r
277     return (retVal);\r
278 }\r
279 \r
280 /* FUNCTION PURPOSE: Unregisters an app transport from a RM instance\r
281  ***********************************************************************\r
282  * DESCRIPTION: Deletes a registered transport from the transport\r
283  *              list and cleans up the memory associated with the\r
284  *              transport data structure.\r
285  */\r
286 int32_t Rm_transportUnregister(Rm_TransportHandle transportHandle)\r
287 {\r
288     Rm_Transport *transport = (Rm_Transport *)transportHandle;\r
289     Rm_Inst      *rmInst = (Rm_Inst *)transport->rmHandle;\r
290     int32_t       retVal = RM_OK;\r
291 \r
292     if (transportIsRegistered(transport->rmHandle, transport)) {\r
293         if ((transport->remoteInstType == Rm_instType_CLIENT_DELEGATE) ||\r
294             (transport->remoteInstType == Rm_instType_SERVER)) {\r
295             rmInst->registeredWithDelegateOrServer = RM_FALSE;\r
296         }\r
297         transportDelete(transport);\r
298     }\r
299     else {\r
300         retVal = RM_ERROR_TRANSPORT_HANDLE_DOES_NOT_EXIST;\r
301     }\r
302     return (retVal);\r
303 }\r
304 \r
305 /* FUNCTION PURPOSE: Receives RM packets\r
306  ***********************************************************************\r
307  * DESCRIPTION: The application provides RM packets received on the\r
308  *              application transports to RM via this API.  Function\r
309  *              can be called from polling or ISR contexts.  Assume \r
310  *              invoking application will free packet after this\r
311  *              function returns.\r
312  */\r
313 int32_t Rm_receivePacket(Rm_TransportHandle transportHandle, const Rm_Packet *pkt)\r
314 {\r\r\r
315     Rm_Transport   *transport = (Rm_Transport *)transportHandle;\r
316     Rm_Inst        *rmInst = (Rm_Inst *)transport->rmHandle;\r
317     Rm_Transaction *transaction;\r
318     int32_t         retVal = RM_OK;\r
319 \r
320     if (!transportIsRegistered(transport->rmHandle, transport)) {\r
321         retVal = RM_ERROR_TRANSPORT_HANDLE_DOES_NOT_EXIST;\r
322         goto errorExit;\r
323     }\r
324 \r
325     switch (pkt->pktType) {\r
326         case Rm_pktType_RESOURCE_REQUEST:\r
327         {\r
328             Rm_ResourceRequestPkt *resourceReqPkt = (Rm_ResourceRequestPkt *)pkt->data;\r
329 \r
330             transaction = rmTransactionQueueAdd(rmInst);\r
331             transaction->remoteOriginatingId = resourceReqPkt->requestId;\r
332             transaction->u.respTrans = transport;\r
333             if (resourceReqPkt->resourceReqType == Rm_resReqPktType_ALLOCATE_INIT) {\r
334                 transaction->type = Rm_service_RESOURCE_ALLOCATE_INIT;\r
335             }\r
336             else if (resourceReqPkt->resourceReqType == Rm_resReqPktType_ALLOCATE_USE) {\r
337                 transaction->type = Rm_service_RESOURCE_ALLOCATE_USE;\r
338             }\r
339             else if (resourceReqPkt->resourceReqType == Rm_resReqPktType_GET_STATUS) {\r
340                 transaction->type = Rm_service_RESOURCE_STATUS;\r
341             }\r
342             else if (resourceReqPkt->resourceReqType == Rm_resReqPktType_FREE) {\r
343                 transaction->type = Rm_service_RESOURCE_FREE;\r
344             }\r
345             else if (resourceReqPkt->resourceReqType == Rm_resReqPktType_GET_NAMED) {\r
346                 transaction->type = Rm_service_RESOURCE_GET_BY_NAME;\r
347             }            \r
348             strncpy(transaction->serviceSrcInstName, resourceReqPkt->serviceSrcInstName, RM_NAME_MAX_CHARS);\r
349             transaction->state = RM_SERVICE_PROCESSING;\r
350             memcpy ((void *)&(transaction->resourceInfo), (void *)&(resourceReqPkt->resourceInfo),\r
351                     sizeof(Rm_ResourceInfo));\r
352             break;\r
353         }\r
354         case Rm_pktType_RESOURCE_RESPONSE:\r
355         {\r
356             Rm_ResourceResponsePkt *resourceRespPkt = (Rm_ResourceResponsePkt *)pkt->data;\r
357 \r
358             if (transaction = rmTransactionQueueFind(rmInst,resourceRespPkt->responseId)) {\r
359                 if ((transaction->state == RM_SERVICE_APPROVED_STATIC) &&\r
360                     (resourceRespPkt->requestState != RM_SERVICE_APPROVED)) {\r
361                     /* Lock the RM instance since service validated against static policy failed against \r
362                      * Server's global policy */\r
363                     rmInst->isLocked = RM_TRUE;\r
364                 }\r
365                 transaction->state = resourceRespPkt->requestState;\r
366 \r
367                 if ((transaction->state == RM_SERVICE_APPROVED) &&\r
368                     ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||\r
369                      (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) ||\r
370                      (transaction->type == Rm_service_RESOURCE_STATUS) ||\r
371                      (transaction->type == Rm_service_RESOURCE_GET_BY_NAME))) {\r
372                     memcpy ((void *)&(transaction->resourceInfo), (void *)&(resourceRespPkt->resourceInfo),\r
373                             sizeof(Rm_ResourceInfo));\r
374                 }\r
375                 else {\r
376                     /* Always copy owner count and instance allocation count */\r
377                     transaction->resourceInfo.ownerCount = resourceRespPkt->resourceInfo.ownerCount;\r
378                     transaction->resourceInfo.instAllocCount = resourceRespPkt->resourceInfo.instAllocCount;\r
379                 }\r
380             }\r
381             else {\r
382                 retVal = RM_ERROR_PKT_RESP_DOES_NOT_MATCH_ANY_REQ;\r
383                 goto errorExit;\r
384             }\r
385             break;\r
386         }\r
387         case Rm_pktType_NAMESERVER_REQUEST:\r
388         {\r
389             Rm_NsRequestPkt *nsRequestPkt = (Rm_NsRequestPkt *)pkt->data;\r
390 \r
391             transaction = rmTransactionQueueAdd(rmInst);\r
392             transaction->remoteOriginatingId = nsRequestPkt->requestId;\r
393             transaction->u.respTrans = transport;\r
394 \r
395             if (nsRequestPkt->nsRequestType == Rm_nsReqPktType_MAP_RESOURCE) {\r
396                 transaction->type = Rm_service_RESOURCE_MAP_TO_NAME;\r
397             }\r
398             else if (nsRequestPkt->nsRequestType == Rm_nsReqPktType_UNMAP_RESOURCE) {\r
399                 transaction->type = Rm_service_RESOURCE_UNMAP_NAME;\r
400             }\r
401 \r
402             strncpy(transaction->serviceSrcInstName, nsRequestPkt->serviceSrcInstName, RM_NAME_MAX_CHARS);\r
403             transaction->state = RM_SERVICE_PROCESSING;\r
404             memcpy ((void *)&(transaction->resourceInfo), (void *)&(nsRequestPkt->resourceInfo),\r
405                     sizeof(Rm_ResourceInfo));           \r
406             break;\r
407         }\r
408         case Rm_pktType_NAMESERVER_RESPONSE:\r
409         {\r
410             Rm_NsResponsePkt *nsResponsePkt = (Rm_NsResponsePkt *)pkt->data;\r
411 \r
412             if (transaction = rmTransactionQueueFind(rmInst, nsResponsePkt->responseId)) {\r
413                 if ((transaction->state == RM_SERVICE_APPROVED_STATIC) &&\r
414                     (nsResponsePkt->requestState != RM_SERVICE_APPROVED)) {\r
415                     /* Lock the RM instance since service validated against static policy failed against \r
416                      * Server's global policy */                    \r
417                     rmInst->isLocked = RM_TRUE;\r
418                 }                \r
419                 transaction->state = nsResponsePkt->requestState;            \r
420             }\r
421             else {\r
422                 retVal = RM_ERROR_PKT_RESP_DOES_NOT_MATCH_ANY_REQ;\r
423                 goto errorExit;\r
424             }\r
425             break;\r
426         }\r
427         default:\r
428             retVal = RM_ERROR_RECEIVED_INVALID_PACKET_TYPE;\r
429             goto errorExit;\r
430     }\r
431 \r
432     /* Process received transaction */\r
433     rmProcessRouter(rmInst, transaction);   \r
434 errorExit:\r
435     return(retVal);\r
436 }\r
437 \r