Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*-
 * Copyright(c) 2002-2011 Exar Corp.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification are permitted provided the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    3. Neither the name of the Exar Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*$FreeBSD$*/

#include <dev/vxge/vxgehal/vxgehal.h>

/*
 * vxge_queue_item_data - Get item's data.
 * @item: Queue item.
 *
 * Returns:  item data(variable size). Note that vxge_queue_t
 * contains items comprized of a fixed vxge_queue_item_t "header"
 * and a variable size data. This function returns the variable
 * user-defined portion of the queue item.
 */
void *
vxge_queue_item_data(vxge_queue_item_t *item)
{
	return (char *) item + sizeof(vxge_queue_item_t);
}

/*
 * __queue_consume - (Lockless) dequeue an item from the specified queue.
 *
 * @queue: Event queue.
 * @data_max_size: Maximum size of the data
 * @item: Queue item
 * See vxge_queue_consume().
 */
static vxge_queue_status_e
__queue_consume(vxge_queue_t *queue,
    u32 data_max_size,
    vxge_queue_item_t *item)
{
	int real_size;
	vxge_queue_item_t *elem;
	__hal_device_t *hldev;

	vxge_assert(queue != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue(
	    "queue = 0x"VXGE_OS_STXFMT", size = %d, item = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queue, data_max_size, (ptr_t) item);

	if (vxge_list_is_empty(&queue->list_head)) {
		vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
		    __FILE__, __func__, __LINE__, VXGE_QUEUE_IS_EMPTY);
		return (VXGE_QUEUE_IS_EMPTY);
	}

	elem = (vxge_queue_item_t *) queue->list_head.next;
	if (elem->data_size > data_max_size) {
		vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
		    __FILE__, __func__, __LINE__, VXGE_QUEUE_NOT_ENOUGH_SPACE);
		return (VXGE_QUEUE_NOT_ENOUGH_SPACE);
	}

	vxge_list_remove(&elem->item);
	real_size = elem->data_size + sizeof(vxge_queue_item_t);
	if (queue->head_ptr == elem) {
		queue->head_ptr = (char *) queue->head_ptr + real_size;
		vxge_hal_info_log_queue("event_type: %d \
		    removing from the head: "
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT
		    ":0x0x"VXGE_OS_STXFMT" elem 0x0x"VXGE_OS_STXFMT" length %d",
		    elem->event_type, (ptr_t) queue->start_ptr,
		    (ptr_t) queue->head_ptr, (ptr_t) queue->tail_ptr,
		    (ptr_t) queue->end_ptr, (ptr_t) elem, real_size);
	} else if ((char *) queue->tail_ptr - real_size == (char *) elem) {
		queue->tail_ptr = (char *) queue->tail_ptr - real_size;
		vxge_hal_info_log_queue("event_type: %d \
		    removing from the tail: "
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT
		    ":0x"VXGE_OS_STXFMT" elem 0x"VXGE_OS_STXFMT" length %d",
		    elem->event_type, (ptr_t) queue->start_ptr,
		    (ptr_t) queue->head_ptr, (ptr_t) queue->tail_ptr,
		    (ptr_t) queue->end_ptr, (ptr_t) elem, real_size);
	} else {
		vxge_hal_info_log_queue("event_type: %d \
		    removing from the list: "
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT
		    ":0x"VXGE_OS_STXFMT" elem 0x"VXGE_OS_STXFMT" length %d",
		    elem->event_type, (ptr_t) queue->start_ptr,
		    (ptr_t) queue->head_ptr, (ptr_t) queue->tail_ptr,
		    (ptr_t) queue->end_ptr, (ptr_t) elem, real_size);
	}
	vxge_assert(queue->tail_ptr >= queue->head_ptr);
	vxge_assert(queue->tail_ptr >= queue->start_ptr &&
	    queue->tail_ptr <= queue->end_ptr);
	vxge_assert(queue->head_ptr >= queue->start_ptr &&
	    queue->head_ptr < queue->end_ptr);
	vxge_os_memcpy(item, elem, sizeof(vxge_queue_item_t));
	vxge_os_memcpy(vxge_queue_item_data(item), vxge_queue_item_data(elem),
	    elem->data_size);

	if (vxge_list_is_empty(&queue->list_head)) {
		/* reset buffer pointers just to be clean */
		queue->head_ptr = queue->tail_ptr = queue->start_ptr;
	}

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);

	return (VXGE_QUEUE_OK);
}

/*
 * vxge_queue_produce - Enqueue an item (see vxge_queue_item_t {})
 *			 into the specified queue.
 * @queueh: Queue handle.
 * @event_type: Event type. One of the enumerated event types
 *		 that both consumer and producer "understand".
 *		 For an example, please refer to vxge_hal_event_e.
 * @context: Opaque (void *) "context", for instance event producer object.
 * @is_critical: For critical event, e.g. ECC.
 * @data_size: Size of the @data.
 * @data: User data of variable @data_size that is _copied_ into
 *	the new queue item (see vxge_queue_item_t {}). Upon return
 *	from the call the @data memory can be re-used or released.
 *
 * Enqueue a new item.
 *
 * Returns: VXGE_QUEUE_OK - success.
 * VXGE_QUEUE_IS_FULL - Queue is full.
 * VXGE_QUEUE_OUT_OF_MEMORY - Memory allocation failed.
 *
 * See also: vxge_queue_item_t {}, vxge_queue_consume().
 */
vxge_queue_status_e
vxge_queue_produce(vxge_queue_h queueh,
    u32 event_type,
    void *context,
    u32 is_critical,
    const u32 data_size,
    void *data)
{
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	int real_size = data_size + sizeof(vxge_queue_item_t);
	__hal_device_t *hldev;
	vxge_queue_item_t *elem;
	unsigned long flags = 0;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue(
	    "queueh = 0x"VXGE_OS_STXFMT", event_type = %d, "
	    "context = 0x"VXGE_OS_STXFMT", is_critical = %d, "
	    "data_size = %d, data = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queueh, event_type, (ptr_t) context,
	    is_critical, data_size, (ptr_t) data);

	vxge_assert(real_size <= VXGE_QUEUE_BUF_SIZE);

	vxge_os_spin_lock_irq(&queue->lock, flags);

	if (is_critical && !queue->has_critical_event) {
		unsigned char item_buf[sizeof(vxge_queue_item_t) +
		    VXGE_DEFAULT_EVENT_MAX_DATA_SIZE];
		vxge_queue_item_t *item =
		    (vxge_queue_item_t *) (void *)item_buf;

		while (__queue_consume(queue, VXGE_DEFAULT_EVENT_MAX_DATA_SIZE,
		    item) != VXGE_QUEUE_IS_EMPTY) {
		}		/* do nothing */
	}

try_again:
	if ((char *) queue->tail_ptr + real_size <= (char *) queue->end_ptr) {
		elem = (vxge_queue_item_t *) queue->tail_ptr;
		queue->tail_ptr = (void *)((char *) queue->tail_ptr + real_size);
		vxge_hal_info_log_queue("event_type: %d adding to the tail: "
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT
		    ":0x"VXGE_OS_STXFMT" elem 0x"VXGE_OS_STXFMT" length %d",
		    event_type, (ptr_t) queue->start_ptr,
		    (ptr_t) queue->head_ptr, (ptr_t) queue->tail_ptr,
		    (ptr_t) queue->end_ptr, (ptr_t) elem, real_size);
	} else if ((char *) queue->head_ptr - real_size >=
	    (char *) queue->start_ptr) {
		elem = (vxge_queue_item_t *)
		    ((void *)((char *) queue->head_ptr - real_size));
		queue->head_ptr = elem;
		vxge_hal_info_log_queue("event_type: %d adding to the head: "
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT":"
		    "0x"VXGE_OS_STXFMT":0x"VXGE_OS_STXFMT" length %d",
		    event_type, (ptr_t) queue->start_ptr,
		    (ptr_t) queue->head_ptr, (ptr_t) queue->tail_ptr,
		    (ptr_t) queue->end_ptr, real_size);
	} else {
		vxge_queue_status_e status;

		if (queue->pages_current >= queue->pages_max) {
			vxge_os_spin_unlock_irq(&queue->lock, flags);
			vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
			    __FILE__, __func__, __LINE__, VXGE_QUEUE_IS_FULL);
			return (VXGE_QUEUE_IS_FULL);
		}

		if (queue->has_critical_event) {
			vxge_os_spin_unlock_irq(&queue->lock, flags);
			vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
			    __FILE__, __func__, __LINE__, VXGE_QUEUE_IS_FULL);
			return (VXGE_QUEUE_IS_FULL);
		}

		/* grow */
		status = vxge_io_queue_grow(queueh);
		if (status != VXGE_QUEUE_OK) {
			vxge_os_spin_unlock_irq(&queue->lock, flags);
			vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
			    __FILE__, __func__, __LINE__, status);
			return (status);
		}

		goto try_again;
	}
	vxge_assert(queue->tail_ptr >= queue->head_ptr);
	vxge_assert(queue->tail_ptr >= queue->start_ptr &&
	    queue->tail_ptr <= queue->end_ptr);
	vxge_assert(queue->head_ptr >= queue->start_ptr &&
	    queue->head_ptr < queue->end_ptr);
	elem->data_size = data_size;
	elem->event_type = (vxge_hal_event_e) event_type;
	elem->is_critical = is_critical;
	if (is_critical)
		queue->has_critical_event = 1;
	elem->context = context;
	vxge_os_memcpy(vxge_queue_item_data(elem), data, data_size);
	vxge_list_insert_before(&elem->item, &queue->list_head);
	vxge_os_spin_unlock_irq(&queue->lock, flags);

	/* no lock taken! */
	queue->queued_func(queue->queued_data, event_type);

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);

	return (VXGE_QUEUE_OK);
}


/*
 * vxge_queue_create - Create protected first-in-first-out queue.
 * @devh: HAL device handle.
 * @pages_initial: Number of pages to be initially allocated at the
 * time of queue creation.
 * @pages_max: Max number of pages that can be allocated in the queue.
 * @queued_func: Optional callback function to be called each time a new item is
 * added to the queue.
 * @queued_data: Argument to the callback function.
 *
 * Create protected (fifo) queue.
 *
 * Returns: Pointer to vxge_queue_t structure,
 * NULL - on failure.
 *
 * See also: vxge_queue_item_t {}, vxge_queue_destroy().
 */
vxge_queue_h
vxge_queue_create(vxge_hal_device_h devh,
    u32 pages_initial,
    u32 pages_max,
    vxge_queued_f queued_func,
    void *queued_data)
{
	vxge_queue_t *queue;
	__hal_device_t *hldev = (__hal_device_t *) devh;

	vxge_assert(devh != NULL);

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue(
	    "devh = 0x"VXGE_OS_STXFMT", pages_initial = %d, "
	    "pages_max = %d, queued_func = 0x"VXGE_OS_STXFMT", "
	    "queued_data = 0x"VXGE_OS_STXFMT, (ptr_t) devh, pages_initial,
	    pages_max, (ptr_t) queued_func, (ptr_t) queued_data);

	if ((queue = (vxge_queue_t *) vxge_os_malloc(hldev->header.pdev,
	    sizeof(vxge_queue_t))) == NULL) {
		vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
		    __FILE__, __func__, __LINE__, VXGE_QUEUE_OUT_OF_MEMORY);
		return (NULL);
	}

	queue->queued_func = queued_func;
	queue->queued_data = queued_data;
	queue->hldev = devh;
	queue->pdev = hldev->header.pdev;
	queue->irqh = hldev->header.irqh;
	queue->pages_current = pages_initial;
	queue->start_ptr = vxge_os_malloc(hldev->header.pdev,
	    queue->pages_current * VXGE_QUEUE_BUF_SIZE);
	if (queue->start_ptr == NULL) {
		vxge_os_free(hldev->header.pdev, queue, sizeof(vxge_queue_t));
		vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
		    __FILE__, __func__, __LINE__, VXGE_QUEUE_OUT_OF_MEMORY);
		return (NULL);
	}
	queue->head_ptr = queue->tail_ptr = queue->start_ptr;
	queue->end_ptr = (char *) queue->start_ptr +
	    queue->pages_current * VXGE_QUEUE_BUF_SIZE;
	vxge_os_spin_lock_init_irq(&queue->lock, queue->irqh);
	queue->pages_initial = pages_initial;
	queue->pages_max = pages_max;
	vxge_list_init(&queue->list_head);

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);

	return (queue);
}

/*
 * vxge_queue_destroy - Destroy vxge_queue_t object.
 * @queueh: Queue handle.
 *
 * Destroy the specified vxge_queue_t object.
 *
 * See also: vxge_queue_item_t {}, vxge_queue_create().
 */
void
vxge_queue_destroy(vxge_queue_h queueh)
{
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	__hal_device_t *hldev;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue("queueh = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queueh);

	vxge_os_spin_lock_destroy_irq(&queue->lock, queue->irqh);
	if (!vxge_list_is_empty(&queue->list_head)) {
		vxge_hal_trace_log_queue("destroying non-empty queue 0x"
		    VXGE_OS_STXFMT, (ptr_t) queue);
	}
	vxge_os_free(queue->pdev, queue->start_ptr, queue->pages_current *
	    VXGE_QUEUE_BUF_SIZE);

	vxge_os_free(queue->pdev, queue, sizeof(vxge_queue_t));

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);
}

/*
 * vxge_io_queue_grow - Dynamically increases the size of the queue.
 * @queueh: Queue handle.
 *
 * This function is called in the case of no slot avaialble in the queue
 * to accommodate the newly received event.
 * Note that queue cannot grow beyond the max size specified for the
 * queue.
 *
 * Returns VXGE_QUEUE_OK: On success.
 * VXGE_QUEUE_OUT_OF_MEMORY : No memory is available.
 */
vxge_queue_status_e
vxge_io_queue_grow(vxge_queue_h queueh)
{
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	__hal_device_t *hldev;
	void *newbuf, *oldbuf;
	vxge_list_t *item;
	vxge_queue_item_t *elem;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue("queueh = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queueh);

	vxge_hal_info_log_queue("queue 0x"VXGE_OS_STXFMT":%d is growing",
	    (ptr_t) queue, queue->pages_current);

	newbuf = vxge_os_malloc(queue->pdev,
	    (queue->pages_current + 1) * VXGE_QUEUE_BUF_SIZE);
	if (newbuf == NULL) {
		vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
		    __FILE__, __func__, __LINE__, VXGE_QUEUE_OUT_OF_MEMORY);
		return (VXGE_QUEUE_OUT_OF_MEMORY);
	}

	vxge_os_memcpy(newbuf, queue->start_ptr,
	    queue->pages_current * VXGE_QUEUE_BUF_SIZE);
	oldbuf = queue->start_ptr;

	/* adjust queue sizes */
	queue->start_ptr = newbuf;
	queue->end_ptr = (char *) newbuf +
	    (queue->pages_current + 1) * VXGE_QUEUE_BUF_SIZE;
	queue->tail_ptr = (char *) newbuf +
	/* LINTED */
	    ((char *) queue->tail_ptr - (char *) oldbuf);
	queue->head_ptr = (char *) newbuf +
	/* LINTED */
	    ((char *) queue->head_ptr - (char *) oldbuf);
	vxge_assert(!vxge_list_is_empty(&queue->list_head));
	queue->list_head.next = (vxge_list_t *) (void *)((char *) newbuf +
	/* LINTED */
	    ((char *) queue->list_head.next - (char *) oldbuf));
	queue->list_head.prev = (vxge_list_t *) (void *)((char *) newbuf +
	/* LINTED */
	    ((char *) queue->list_head.prev - (char *) oldbuf));
	/* adjust queue list */
	vxge_list_for_each(item, &queue->list_head) {
		elem = vxge_container_of(item, vxge_queue_item_t, item);
		if (elem->item.next != &queue->list_head) {
			elem->item.next =
			    (vxge_list_t *) (void *)((char *) newbuf +
			/* LINTED */
			    ((char *) elem->item.next - (char *) oldbuf));
		}
		if (elem->item.prev != &queue->list_head) {
			elem->item.prev =
			    (vxge_list_t *) (void *)((char *) newbuf +
			/* LINTED */
			    ((char *) elem->item.prev - (char *) oldbuf));
		}
	}
	vxge_os_free(queue->pdev, oldbuf,
	    queue->pages_current * VXGE_QUEUE_BUF_SIZE);
	queue->pages_current++;

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);
	return (VXGE_QUEUE_OK);
}

/*
 * vxge_queue_consume - Dequeue an item from the specified queue.
 * @queueh: Queue handle.
 * @data_max_size: Maximum expected size of the item.
 * @item: Memory area into which the item is _copied_ upon return
 *	from the function.
 *
 * Dequeue an item from the queue. The caller is required to provide
 * enough space for the item.
 *
 * Returns: VXGE_QUEUE_OK - success.
 * VXGE_QUEUE_IS_EMPTY - Queue is empty.
 * VXGE_QUEUE_NOT_ENOUGH_SPACE - Requested item size(@data_max_size)
 * is too small to accommodate an item from the queue.
 *
 * See also: vxge_queue_item_t {}, vxge_queue_produce().
 */
vxge_queue_status_e
vxge_queue_consume(vxge_queue_h queueh,
    u32 data_max_size,
    vxge_queue_item_t *item)
{
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	__hal_device_t *hldev;
	unsigned long flags = 0;
	vxge_queue_status_e status;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue(
	    "queueh = 0x"VXGE_OS_STXFMT", data_max_size = %d, "
	    "item = 0x"VXGE_OS_STXFMT, (ptr_t) queueh,
	    data_max_size, (ptr_t) item);

	vxge_os_spin_lock_irq(&queue->lock, flags);
	status = __queue_consume(queue, data_max_size, item);
	vxge_os_spin_unlock_irq(&queue->lock, flags);

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = %d",
	    __FILE__, __func__, __LINE__, status);
	return (status);
}


/*
 * vxge_queue_flush - Flush, or empty, the queue.
 * @queueh: Queue handle.
 *
 * Flush the queue, i.e. make it empty by consuming all events
 * without invoking the event processing logic (callbacks, etc.)
 */
void
vxge_queue_flush(vxge_queue_h queueh)
{
	unsigned char item_buf[sizeof(vxge_queue_item_t) +
	    VXGE_DEFAULT_EVENT_MAX_DATA_SIZE];
	vxge_queue_item_t *item = (vxge_queue_item_t *) (void *)item_buf;
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	__hal_device_t *hldev;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue("queueh = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queueh);

	/* flush queue by consuming all enqueued items */
	while (vxge_queue_consume(queueh, VXGE_DEFAULT_EVENT_MAX_DATA_SIZE,
	    item) != VXGE_QUEUE_IS_EMPTY) {
		/* do nothing */
		vxge_hal_trace_log_queue("item 0x"VXGE_OS_STXFMT"(%d) flushed",
		    (ptr_t) item, item->event_type);
	}

	(void) vxge_queue_get_reset_critical(queueh);

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);
}

/*
 * vxge_queue_get_reset_critical - Check for critical events in the queue,
 * @queueh: Queue handle.
 *
 * Check for critical event(s) in the queue, and reset the
 * "has-critical-event" flag upon return.
 * Returns: 1 - if the queue contains atleast one critical event.
 * 0 - If there are no critical events in the queue.
 */
u32
vxge_queue_get_reset_critical(vxge_queue_h queueh)
{
	vxge_queue_t *queue = (vxge_queue_t *) queueh;
	int c = queue->has_critical_event;
	__hal_device_t *hldev;

	vxge_assert(queueh != NULL);

	hldev = (__hal_device_t *) queue->hldev;

	vxge_hal_trace_log_queue("==> %s:%s:%d",
	    __FILE__, __func__, __LINE__);

	vxge_hal_trace_log_queue("queueh = 0x"VXGE_OS_STXFMT,
	    (ptr_t) queueh);

	queue->has_critical_event = 0;

	vxge_hal_trace_log_queue("<== %s:%s:%d Result = 0",
	    __FILE__, __func__, __LINE__);
	return (c);
}