OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [mcapi/] [suspend.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
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_endpoint_t     MCAPI_CTRL_TX_Endp;
35
extern MCAPI_BUF_QUEUE      MCAPI_Buf_Wait_List;
36
extern mcapi_endpoint_t     MCAPI_CTRL_RX_Endp;
37
 
38
/*************************************************************************
39
*
40
*   FUNCTION
41
*
42
*       mcapi_check_resume
43
*
44
*   DESCRIPTION
45
*
46
*       Checks if any pending requests in the system should be resumed.
47
*
48
*   INPUTS
49
*
50
*       type                    The type of request to check.
51
*       endpoint                The endpoint for which the request is
52
*                               suspended on some action.
53
*       *endp_ptr               If data was received, a pointer to the
54
*                               endpoint for which data was received.
55
*       byte_count              If the type is MCAPI_REQ_TX_FIN or
56
*                               MCAPI_REQ_RX_FIN, the number of bytes
57
*                               pending on the endpoint.
58
*       status                  The status to set in the request structure.
59
*
60
*   OUTPUTS
61
*
62
*       None.
63
*
64
*************************************************************************/
65
void mcapi_check_resume(int type, mcapi_endpoint_t endpoint,
66
                        MCAPI_ENDPOINT *endp_ptr, size_t byte_count,
67
                        mcapi_status_t status)
68
{
69
    mcapi_request_t     *request, *next_ptr;
70
    MCAPI_GLOBAL_DATA   *node_data;
71
    MCAPI_BUFFER        *rx_buf;
72
    mcapi_boolean_t     data_copied = 0;
73
 
74
    /* Get a pointer to the global node list. */
75
    node_data = mcapi_get_node_data();
76
 
77
    /* Get a pointer to the first entry in the request queue. */
78
    request = node_data->mcapi_local_req_queue.flink;
79
 
80
    /* Check each request to see if the operation has been completed. */
81
    while (request)
82
    {
83
        /* Get a pointer to the next entry. */
84
        next_ptr = request->mcapi_next;
85
 
86
        switch (type)
87
        {
88
            /* An endpoint associated with a request structure has been
89
             * deleted.
90
             */
91
            case MCAPI_REQ_DELETED:
92
 
93
                /* If the operation is waiting for something to happen
94
                 * on the send side, and the endpoint matches the send
95
                 * endpoint.
96
                 */
97
                if ( ((request->mcapi_type == MCAPI_REQ_TX_FIN) ||
98
                      (request->mcapi_type == MCAPI_REQ_TX_OPEN) ||
99
                      (request->mcapi_type == MCAPI_REQ_CONNECTED)) &&
100
                      (request->mcapi_target_endp == endpoint) )
101
                {
102
                    mcapi_resume(node_data, request, status);
103
                }
104
 
105
                /* If the operation is waiting for something to happen
106
                 * on the receive side, and the endpoint matches the receive
107
                 * endpoint.
108
                 */
109
                else if ( ((request->mcapi_type == MCAPI_REQ_RX_FIN) ||
110
                           (request->mcapi_type == MCAPI_REQ_RX_OPEN) ||
111
                           (request->mcapi_type == MCAPI_REQ_CONNECTED)) &&
112
                          (request->mcapi_target_endp == endpoint) )
113
                {
114
                    mcapi_resume(node_data, request, status);
115
                }
116
 
117
                break;
118
 
119
            /* The connection has been closed. */
120
            case MCAPI_REQ_CLOSED:
121
 
122
                /* No matter what the reason the thread is suspended, if the
123
                 * connection has been closed, resume the thread.
124
                 */
125
                if (request->mcapi_target_endp == endpoint)
126
                {
127
                    /* Resume the task. */
128
                    mcapi_resume(node_data, request, status);
129
                }
130
 
131
                break;
132
 
133
            /* The send side of an endpoint has been opened. */
134
            case MCAPI_REQ_TX_OPEN:
135
 
136
                /* If the request structure is waiting for the send side
137
                 * of the connection to open.
138
                 */
139
                if (request->mcapi_target_endp == endpoint)
140
                {
141
                    /* Resume the task. */
142
                    mcapi_resume(node_data, request, status);
143
                }
144
 
145
                break;
146
 
147
            /* Data associated with a request structure has been transmitted
148
             * successfully, or the transmit has been canceled.
149
             */
150
            case MCAPI_REQ_TX_FIN:
151
 
152
                /* If the request structure is waiting for outgoing data to be
153
                 * successfully transmitted.
154
                 */
155
                if ( (request->mcapi_type == MCAPI_REQ_TX_FIN) &&
156
                     (request->mcapi_target_endp == endpoint) )
157
                {
158
                    /* Indicate the number of bytes transmitted. */
159
                    request->mcapi_byte_count = byte_count;
160
 
161
                    /* Resume the task. */
162
                    mcapi_resume(node_data, request, status);
163
                }
164
 
165
                break;
166
 
167
            /* The receive side of an endpoint has been opened. */
168
            case MCAPI_REQ_RX_OPEN:
169
 
170
                /* If the request structure is waiting for the receive side
171
                 * of the connection to open.
172
                 */
173
                if (request->mcapi_target_endp == endpoint)
174
                {
175
                    /* Resume the task. */
176
                    mcapi_resume(node_data, request, status);
177
                }
178
 
179
                break;
180
 
181
            /* Data associated with a request structure has been received
182
             * successfully, or the receive has been canceled.
183
             */
184
            case MCAPI_REQ_RX_FIN:
185
 
186
                /* If the request structure is waiting for data to be
187
                 * received on the endpoint.
188
                 */
189
                if ( (request->mcapi_type == MCAPI_REQ_RX_FIN) &&
190
                     (request->mcapi_target_endp == endpoint) )
191
                {
192
                    switch (request->mcapi_chan_type)
193
                    {
194
                        case MCAPI_MSG_TYPE:
195
 
196
                            /* Ensure the data will fit in the buffer. */
197
                            if (byte_count <= request->mcapi_buf_size)
198
                            {
199
                                /* Copy the data into the user buffer. */
200
                                request->mcapi_byte_count =
201
                                    msg_recv_copy_data(endp_ptr, request->mcapi_buffer);
202
 
203
                                /* Data has been successfully copied. */
204
                                data_copied = MCAPI_TRUE;
205
                            }
206
 
207
                            /* The data is too big for the user buffer.  Leave
208
                             * the data on the receive queue for a subsequent
209
                             * receive call.
210
                             */
211
                            else
212
                            {
213
                                status = MCAPI_ERR_MSG_TRUNCATED;
214
                            }
215
 
216
                            break;
217
 
218
                        case MCAPI_CHAN_PKT_TYPE:
219
 
220
                            /* Get a pointer to the head buffer. */
221
                            rx_buf = endp_ptr->mcapi_rx_queue.head;
222
 
223
                            /* Set the user buffer to the incoming buffer. */
224
                            *(request->mcapi_pkt) =
225
                                &rx_buf->buf_ptr[MCAPI_HEADER_LEN];
226
 
227
                            /* Set the size of the incoming buffer. */
228
                            request->mcapi_byte_count =
229
                                rx_buf->buf_size - MCAPI_HEADER_LEN;
230
 
231
                            /* Data has been successfully copied. */
232
                            data_copied = MCAPI_TRUE;
233
 
234
                            break;
235
 
236
                        default:
237
 
238
                            break;
239
                    }
240
 
241
                    /* Resume the task. */
242
                    mcapi_resume(node_data, request, status);
243
                }
244
 
245
                break;
246
 
247
            /* An endpoint associated with a pending request has been
248
             * created.
249
             */
250
            case MCAPI_REQ_CREATED:
251
 
252
                /* If the request structure is waiting for an endpoint to be
253
                 * created.
254
                 */
255
                if ( (request->mcapi_type == MCAPI_REQ_CREATED) &&
256
                     (mcapi_encode_endpoint(request->mcapi_target_node_id,
257
                                            request->mcapi_target_port_id) == endpoint) )
258
                {
259
                    /* If this is a local call, fill in the endpoint pointer. */
260
                    if (request->mcapi_endp_ptr)
261
                    {
262
                        *(request->mcapi_endp_ptr) = endpoint;
263
                    }
264
 
265
                    mcapi_resume(node_data, request, status);
266
                }
267
 
268
                break;
269
 
270
            /* A connection between two endpoints has been made. */
271
            case MCAPI_REQ_CONNECTED:
272
 
273
                /* If the request structure is waiting for a connection
274
                 * between two endpoints to be made.
275
                 */
276
                if ( (request->mcapi_type == MCAPI_REQ_CONNECTED) &&
277
                     (request->mcapi_target_endp == endpoint) )
278
                {
279
                    /* Resume the task. */
280
                    mcapi_resume(node_data, request, status);
281
                }
282
 
283
                break;
284
 
285
            default:
286
 
287
                break;
288
        }
289
 
290
        /* Get the next request entry in the list. */
291
        request = next_ptr;
292
    }
293
 
294
    /* Multiple request structures could have been suspended on the same
295
     * receive operation; therefore, the data must not be removed from
296
     * the queue until all request structures have been serviced.
297
     */
298
    if (data_copied)
299
    {
300
        /* Remove the buffer from the receive queue. */
301
        rx_buf = mcapi_dequeue(&endp_ptr->mcapi_rx_queue);
302
 
303
        if (endp_ptr->mcapi_chan_type == MCAPI_CHAN_PKT_TYPE)
304
        {
305
            /* Add the receive buffer structure to the list of
306
             * buffers waiting to be freed by the application.
307
             */
308
            mcapi_enqueue(&MCAPI_Buf_Wait_List, rx_buf);
309
        }
310
 
311
        else
312
        {
313
            /* Return the message buffer to the pool of available buffers. */
314
            ((MCAPI_INTERFACE*)(rx_buf->mcapi_dev_ptr))->
315
                mcapi_recover_buffer(rx_buf);
316
        }
317
    }
318
 
319
}
320
 
321
/*************************************************************************
322
*
323
*   FUNCTION
324
*
325
*       mcapi_resume
326
*
327
*   DESCRIPTION
328
*
329
*       Resume a specific task.
330
*
331
*   INPUTS
332
*
333
*       *node_data              A pointer to the global MCAPI node data
334
*                               structure.
335
*       *request                The request structure associated with the
336
*                               task to resume.
337
*       status                  The status to set in the request structure.
338
*
339
*   OUTPUTS
340
*
341
*       None.
342
*
343
*************************************************************************/
344
void mcapi_resume(MCAPI_GLOBAL_DATA *node_data, mcapi_request_t *request,
345
                  mcapi_status_t status)
346
{
347
    /* Set the status so the blocking operation knows if the operation
348
     * completed successfully.
349
     */
350
    request->mcapi_status = status;
351
 
352
    /* Remove this request from the list. */
353
    mcapi_remove(&node_data->mcapi_local_req_queue, request);
354
 
355
    /* Resume the task. */
356
    MCAPI_Resume_Task(request);
357
 
358
}
359
 
360
/*************************************************************************
361
*
362
*   FUNCTION
363
*
364
*       mcapi_find_request
365
*
366
*   DESCRIPTION
367
*
368
*       Find a matching request structure on the global list.
369
*
370
*   INPUTS
371
*
372
*       *target_request         The target request to find.
373
*       *node_data              A pointer to the global node structure
374
*                               for the system.
375
*
376
*   OUTPUTS
377
*
378
*       A pointer to the request structure or MCAPI_NULL if the structure
379
*       could not be found.
380
*
381
*************************************************************************/
382
mcapi_request_t *mcapi_find_request(mcapi_request_t *target_request,
383
                                    MCAPI_REQ_QUEUE *req_queue)
384
{
385
    mcapi_request_t     *req_ptr;
386
 
387
    /* Get a pointer to the first entry in the request queue. */
388
    req_ptr = req_queue->flink;
389
 
390
    /* Search the list looking for the target entry. */
391
    while (req_ptr)
392
    {
393
        if ( (req_ptr->mcapi_requesting_node_id == target_request->mcapi_requesting_node_id) &&
394
             (req_ptr->mcapi_type == target_request->mcapi_type) &&
395
             (req_ptr->mcapi_target_endp == target_request->mcapi_target_endp) &&
396
             (req_ptr->mcapi_target_node_id == target_request->mcapi_target_node_id) &&
397
             (req_ptr->mcapi_target_port_id == target_request->mcapi_target_port_id) )
398
        {
399
            break;
400
        }
401
 
402
        /* Get a pointer to the next request structure. */
403
        req_ptr = req_ptr->mcapi_next;
404
    }
405
 
406
    return (req_ptr);
407
 
408
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.