1 |
145 |
lanttu |
/*
|
2 |
|
|
* Copyright (c) 2010, Mentor Graphics Corporation
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
9 |
|
|
* this list of conditions and the following disclaimer.
|
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
11 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
12 |
|
|
* and/or other materials provided with the distribution.
|
13 |
|
|
* 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
|
14 |
|
|
* may be used to endorse or promote products derived from this software
|
15 |
|
|
* without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
18 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
21 |
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
22 |
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26 |
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
#include <openmcapi.h>
|
33 |
|
|
|
34 |
|
|
extern mcapi_uint8_t MCAPI_Init;
|
35 |
|
|
extern MCAPI_BUF_QUEUE MCAPI_RX_Queue;
|
36 |
|
|
extern MCAPI_INTERFACE MCAPI_Interface_List[];
|
37 |
|
|
extern MCAPI_MUTEX MCAPI_Mutex;
|
38 |
|
|
extern mcapi_endpoint_t MCAPI_CTRL_RX_Endp;
|
39 |
|
|
extern mcapi_endpoint_t MCAPI_CTRL_TX_Endp;
|
40 |
|
|
extern MCAPI_BUF_QUEUE MCAPI_Buf_Wait_List;
|
41 |
|
|
|
42 |
|
|
/*************************************************************************
|
43 |
|
|
*
|
44 |
|
|
* FUNCTION
|
45 |
|
|
*
|
46 |
|
|
* mcapi_finalize
|
47 |
|
|
*
|
48 |
|
|
* DESCRIPTION
|
49 |
|
|
*
|
50 |
|
|
* API routine to shut down the MCAPI module on a node.
|
51 |
|
|
*
|
52 |
|
|
* INPUTS
|
53 |
|
|
*
|
54 |
|
|
* *mcapi_status A pointer to memory that will be filled in
|
55 |
|
|
* with the status of the call.
|
56 |
|
|
*
|
57 |
|
|
* OUTPUTS
|
58 |
|
|
*
|
59 |
|
|
* None.
|
60 |
|
|
*
|
61 |
|
|
*************************************************************************/
|
62 |
|
|
void mcapi_finalize(mcapi_status_t *mcapi_status)
|
63 |
|
|
{
|
64 |
|
|
MCAPI_GLOBAL_DATA *node_data;
|
65 |
|
|
MCAPI_BUFFER *cur_buf;
|
66 |
|
|
MCAPI_ENDPOINT *endp_ptr;
|
67 |
|
|
int i, j;
|
68 |
|
|
mcapi_request_t *request;
|
69 |
|
|
|
70 |
|
|
/* Validate the status pointer. */
|
71 |
|
|
if (mcapi_status)
|
72 |
|
|
{
|
73 |
|
|
/* If the node is initialized. */
|
74 |
|
|
if (MCAPI_Init)
|
75 |
|
|
{
|
76 |
|
|
/* Delete the control endpoints. */
|
77 |
|
|
mcapi_delete_endpoint(MCAPI_CTRL_RX_Endp, mcapi_status);
|
78 |
|
|
mcapi_delete_endpoint(MCAPI_CTRL_TX_Endp, mcapi_status);
|
79 |
|
|
|
80 |
|
|
/* Get the lock. */
|
81 |
|
|
mcapi_lock_node_data();
|
82 |
|
|
|
83 |
|
|
/* Get a pointer to the global node list. */
|
84 |
|
|
node_data = mcapi_get_node_data();
|
85 |
|
|
|
86 |
|
|
/* Get the index of the node associated with the node ID. */
|
87 |
|
|
i = mcapi_find_node(MCAPI_Node_ID, node_data);
|
88 |
|
|
|
89 |
|
|
/* If the node was found. */
|
90 |
|
|
if (i != -1)
|
91 |
|
|
{
|
92 |
|
|
/* Close each open endpoint in the system. */
|
93 |
|
|
for (j = 0; j < MCAPI_MAX_ENDPOINTS; j++)
|
94 |
|
|
{
|
95 |
|
|
endp_ptr = &node_data->mcapi_node_list[i].mcapi_endpoint_list[j];
|
96 |
|
|
|
97 |
|
|
if (endp_ptr->mcapi_state != MCAPI_ENDP_CLOSED)
|
98 |
|
|
{
|
99 |
|
|
/* If this endpoint is part of an open connection. */
|
100 |
|
|
if ( (endp_ptr->mcapi_state & MCAPI_ENDP_TX) ||
|
101 |
|
|
(endp_ptr->mcapi_state & MCAPI_ENDP_RX) )
|
102 |
|
|
{
|
103 |
|
|
/* Close the connection. */
|
104 |
|
|
mcapi_tx_fin_msg(endp_ptr, mcapi_status);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/* Set the state to closed. */
|
108 |
|
|
endp_ptr->mcapi_state = MCAPI_ENDP_CLOSED;
|
109 |
|
|
|
110 |
|
|
/* Cancel any threads blocking on this endpoint and
|
111 |
|
|
* set an error in the request structure.
|
112 |
|
|
*/
|
113 |
|
|
mcapi_check_resume(MCAPI_REQ_DELETED,
|
114 |
|
|
endp_ptr->mcapi_endp_handle,
|
115 |
|
|
MCAPI_NULL, 0, MCAPI_ERR_REQUEST_CANCELLED);
|
116 |
|
|
|
117 |
|
|
/* Remove the first buffer from the receive queue. */
|
118 |
|
|
cur_buf = mcapi_dequeue(&endp_ptr->mcapi_rx_queue);
|
119 |
|
|
|
120 |
|
|
/* If there is data pending on the endpoint, free it. */
|
121 |
|
|
while (cur_buf)
|
122 |
|
|
{
|
123 |
|
|
/* Remove the buffer from the receive queue and place
|
124 |
|
|
* it on the free list.
|
125 |
|
|
*/
|
126 |
|
|
((MCAPI_INTERFACE*)(cur_buf->mcapi_dev_ptr))->
|
127 |
|
|
mcapi_recover_buffer(cur_buf);
|
128 |
|
|
|
129 |
|
|
/* Get the next buffer. */
|
130 |
|
|
cur_buf = mcapi_dequeue(&endp_ptr->mcapi_rx_queue);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/* Decrement the number of used endpoints on this node. */
|
134 |
|
|
node_data->mcapi_node_list[i].mcapi_endpoint_count --;
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/* Get a pointer to the first entry in the local request
|
139 |
|
|
* queue.
|
140 |
|
|
*/
|
141 |
|
|
request = mcapi_dequeue(&node_data->mcapi_local_req_queue);
|
142 |
|
|
|
143 |
|
|
/* Resume all tasks pending outstanding requests. */
|
144 |
|
|
while (request)
|
145 |
|
|
{
|
146 |
|
|
/* Resume this thread indicating that the request has
|
147 |
|
|
* been canceled.
|
148 |
|
|
*/
|
149 |
|
|
mcapi_resume(node_data, request, MCAPI_ERR_REQUEST_CANCELLED);
|
150 |
|
|
|
151 |
|
|
/* Get the next request structure. */
|
152 |
|
|
request = mcapi_dequeue(&node_data->mcapi_local_req_queue);
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
/* Get a pointer to the first entry in the foreign request
|
156 |
|
|
* queue.
|
157 |
|
|
*/
|
158 |
|
|
request = mcapi_dequeue(&node_data->mcapi_foreign_req_queue);
|
159 |
|
|
|
160 |
|
|
/* Inform all foreign nodes pending outstanding requests. */
|
161 |
|
|
while (request)
|
162 |
|
|
{
|
163 |
|
|
/* Indicate that the node has shut down. */
|
164 |
|
|
request->mcapi_status = MCAPI_ERR_NODE_INVALID;
|
165 |
|
|
|
166 |
|
|
/* Send the response to the foreign node. */
|
167 |
|
|
mcapi_tx_response(node_data, request);
|
168 |
|
|
|
169 |
|
|
/* Get the next request structure. */
|
170 |
|
|
request = mcapi_dequeue(&node_data->mcapi_foreign_req_queue);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/* Get a pointer to the first buffer on the wait list. */
|
174 |
|
|
cur_buf = mcapi_dequeue(&MCAPI_Buf_Wait_List);
|
175 |
|
|
|
176 |
|
|
/* Free each buffer on this list. */
|
177 |
|
|
while (cur_buf)
|
178 |
|
|
{
|
179 |
|
|
/* Return the buffer to the list of free buffers. */
|
180 |
|
|
((MCAPI_INTERFACE*)(cur_buf->mcapi_dev_ptr))->
|
181 |
|
|
mcapi_recover_buffer(cur_buf);
|
182 |
|
|
|
183 |
|
|
/* Get a pointer to the next buffer. */
|
184 |
|
|
cur_buf = mcapi_dequeue(&MCAPI_Buf_Wait_List);
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/* Shut down each interface. Drop the global lock first to
|
188 |
|
|
* avoid deadlocks with transport-level threads. */
|
189 |
|
|
mcapi_unlock_node_data();
|
190 |
|
|
for (j = 0; j < MCAPI_INTERFACE_COUNT; j++)
|
191 |
|
|
{
|
192 |
|
|
/* Shut down the interface. */
|
193 |
|
|
MCAPI_Interface_List[j].mcapi_ioctl(MCAPI_FINALIZE_DRIVER,
|
194 |
|
|
MCAPI_NULL, 0);
|
195 |
|
|
}
|
196 |
|
|
mcapi_lock_node_data();
|
197 |
|
|
|
198 |
|
|
/* Remove the first buffer from the pending RX list. */
|
199 |
|
|
cur_buf = mcapi_dequeue(&MCAPI_RX_Queue);
|
200 |
|
|
|
201 |
|
|
/* Free all buffers that are pending processing. */
|
202 |
|
|
while (cur_buf)
|
203 |
|
|
{
|
204 |
|
|
/* Put the buffer on the appropriate free list. */
|
205 |
|
|
((MCAPI_INTERFACE*)(cur_buf->mcapi_dev_ptr))->
|
206 |
|
|
mcapi_recover_buffer(cur_buf);
|
207 |
|
|
|
208 |
|
|
/* Get the next buffer. */
|
209 |
|
|
cur_buf = mcapi_dequeue(&MCAPI_RX_Queue);
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
/* Shut down the OS. */
|
213 |
|
|
MCAPI_Exit_OS();
|
214 |
|
|
|
215 |
|
|
/* Set the initialization variable to indicate that MCAPI
|
216 |
|
|
* is not initialized on this node.
|
217 |
|
|
*/
|
218 |
|
|
MCAPI_Init = 0;
|
219 |
|
|
|
220 |
|
|
/* Set the state to finalized. */
|
221 |
|
|
node_data->mcapi_node_list[i].mcapi_state = MCAPI_NODE_FINALIZED;
|
222 |
|
|
|
223 |
|
|
*mcapi_status = MCAPI_SUCCESS;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
else
|
227 |
|
|
{
|
228 |
|
|
*mcapi_status = MCAPI_ERR_NODE_FINALFAILED;
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/* Release the lock. */
|
232 |
|
|
mcapi_unlock_node_data();
|
233 |
|
|
|
234 |
|
|
if (*mcapi_status == MCAPI_SUCCESS)
|
235 |
|
|
{
|
236 |
|
|
/* Delete the MCAPI mutex. */
|
237 |
|
|
MCAPI_Delete_Mutex(&MCAPI_Mutex);
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
else
|
242 |
|
|
{
|
243 |
|
|
*mcapi_status = MCAPI_SUCCESS;
|
244 |
|
|
}
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
}
|