1a23fd118Syl /*
2a23fd118Syl  * CDDL HEADER START
3a23fd118Syl  *
4a23fd118Syl  * The contents of this file are subject to the terms of the
5a23fd118Syl  * Common Development and Distribution License (the "License").
6a23fd118Syl  * You may not use this file except in compliance with the License.
7a23fd118Syl  *
8a23fd118Syl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a23fd118Syl  * or http://www.opensolaris.org/os/licensing.
10a23fd118Syl  * See the License for the specific language governing permissions
11a23fd118Syl  * and limitations under the License.
12a23fd118Syl  *
13a23fd118Syl  * When distributing Covered Code, include this CDDL HEADER in each
14a23fd118Syl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a23fd118Syl  * If applicable, add the following below this CDDL HEADER, with the
16a23fd118Syl  * fields enclosed by brackets "[]" replaced with your own identifying
17a23fd118Syl  * information: Portions Copyright [yyyy] [name of copyright owner]
18a23fd118Syl  *
19a23fd118Syl  * CDDL HEADER END
20a23fd118Syl  *
218347601bSyl  * Copyright (c) 2002-2006 Neterion, Inc.
22a23fd118Syl  */
23a23fd118Syl 
24a23fd118Syl #include "xge-queue.h"
25a23fd118Syl 
26a23fd118Syl /**
27a23fd118Syl  * xge_queue_item_data - Get item's data.
28a23fd118Syl  * @item: Queue item.
29a23fd118Syl  *
30a23fd118Syl  * Returns:  item data(variable size). Note that xge_queue_t
31a23fd118Syl  * contains items comprized of a fixed xge_queue_item_t "header"
32a23fd118Syl  * and a variable size data. This function returns the variable
33a23fd118Syl  * user-defined portion of the queue item.
34a23fd118Syl  */
xge_queue_item_data(xge_queue_item_t * item)35a23fd118Syl void* xge_queue_item_data(xge_queue_item_t *item)
36a23fd118Syl {
37a23fd118Syl 	return (char *)item + sizeof(xge_queue_item_t);
38a23fd118Syl }
39a23fd118Syl 
40a23fd118Syl /*
41a23fd118Syl  * __queue_consume - (Lockless) dequeue an item from the specified queue.
42a23fd118Syl  *
43a23fd118Syl  * @queue: Event queue.
44a23fd118Syl  * See xge_queue_consume().
45a23fd118Syl  */
46a23fd118Syl static xge_queue_status_e
__queue_consume(xge_queue_t * queue,int data_max_size,xge_queue_item_t * item)47a23fd118Syl __queue_consume(xge_queue_t *queue, int data_max_size, xge_queue_item_t *item)
48a23fd118Syl {
49a23fd118Syl 	int real_size;
50a23fd118Syl 	xge_queue_item_t *elem;
51a23fd118Syl 
52a23fd118Syl 	if (xge_list_is_empty(&queue->list_head))
53a23fd118Syl 		return XGE_QUEUE_IS_EMPTY;
54a23fd118Syl 
55a23fd118Syl 	elem = (xge_queue_item_t *)queue->list_head.next;
56a23fd118Syl 	if (elem->data_size > data_max_size)
57a23fd118Syl 		return XGE_QUEUE_NOT_ENOUGH_SPACE;
58a23fd118Syl 
59a23fd118Syl 	xge_list_remove(&elem->item);
60a23fd118Syl 	real_size = elem->data_size + sizeof(xge_queue_item_t);
61a23fd118Syl 	if (queue->head_ptr == elem) {
62a23fd118Syl 		queue->head_ptr = (char *)queue->head_ptr + real_size;
63a23fd118Syl 		xge_debug_queue(XGE_TRACE,
64a23fd118Syl 			"event_type: %d removing from the head: "
658347601bSyl 			"0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
668347601bSyl 			":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
67a23fd118Syl 			elem->event_type,
68a23fd118Syl 			(u64)(ulong_t)queue->start_ptr,
69a23fd118Syl 			(u64)(ulong_t)queue->head_ptr,
70a23fd118Syl 			(u64)(ulong_t)queue->tail_ptr,
71a23fd118Syl 			(u64)(ulong_t)queue->end_ptr,
72a23fd118Syl 			(u64)(ulong_t)elem,
73a23fd118Syl 			real_size);
74a23fd118Syl 	} else if ((char *)queue->tail_ptr - real_size == (char*)elem) {
75a23fd118Syl 		queue->tail_ptr = (char *)queue->tail_ptr - real_size;
76a23fd118Syl 		xge_debug_queue(XGE_TRACE,
77a23fd118Syl 			"event_type: %d removing from the tail: "
788347601bSyl 			"0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
798347601bSyl 			":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
80a23fd118Syl 			elem->event_type,
81a23fd118Syl 			(u64)(ulong_t)queue->start_ptr,
82a23fd118Syl 			(u64)(ulong_t)queue->head_ptr,
83a23fd118Syl 			(u64)(ulong_t)queue->tail_ptr,
84a23fd118Syl 			(u64)(ulong_t)queue->end_ptr,
85a23fd118Syl 			(u64)(ulong_t)elem,
86a23fd118Syl 			real_size);
87a23fd118Syl 	} else {
88a23fd118Syl 		xge_debug_queue(XGE_TRACE,
89a23fd118Syl 			"event_type: %d removing from the list: "
908347601bSyl 			"0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
918347601bSyl 			":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
92a23fd118Syl 			elem->event_type,
93a23fd118Syl 			(u64)(ulong_t)queue->start_ptr,
94a23fd118Syl 			(u64)(ulong_t)queue->head_ptr,
95a23fd118Syl 			(u64)(ulong_t)queue->tail_ptr,
96a23fd118Syl 			(u64)(ulong_t)queue->end_ptr,
97a23fd118Syl 			(u64)(ulong_t)elem,
98a23fd118Syl 			real_size);
99a23fd118Syl 	}
100a23fd118Syl 	xge_assert(queue->tail_ptr >= queue->head_ptr);
101a23fd118Syl 	xge_assert(queue->tail_ptr >= queue->start_ptr &&
102a23fd118Syl 		    queue->tail_ptr <= queue->end_ptr);
103a23fd118Syl 	xge_assert(queue->head_ptr >= queue->start_ptr &&
104a23fd118Syl 		    queue->head_ptr < queue->end_ptr);
105a23fd118Syl 	xge_os_memcpy(item, elem, sizeof(xge_queue_item_t));
106a23fd118Syl 	xge_os_memcpy(xge_queue_item_data(item), xge_queue_item_data(elem),
107a23fd118Syl 		    elem->data_size);
108a23fd118Syl 
109a23fd118Syl 	if (xge_list_is_empty(&queue->list_head)) {
110a23fd118Syl 		/* reset buffer pointers just to be clean */
111a23fd118Syl 		queue->head_ptr = queue->tail_ptr = queue->start_ptr;
112a23fd118Syl 	}
113a23fd118Syl 	return XGE_QUEUE_OK;
114a23fd118Syl }
115a23fd118Syl 
116a23fd118Syl /**
117a23fd118Syl  * xge_queue_produce - Enqueue an item (see xge_queue_item_t{})
118a23fd118Syl  *                      into the specified queue.
119a23fd118Syl  * @queueh: Queue handle.
120a23fd118Syl  * @event_type: Event type. One of the enumerated event types
121a23fd118Syl  *              that both consumer and producer "understand".
122a23fd118Syl  *              For an example, please refer to xge_hal_event_e.
123a23fd118Syl  * @context: Opaque (void*) "context", for instance event producer object.
124a23fd118Syl  * @is_critical: For critical event, e.g. ECC.
125a23fd118Syl  * @data_size: Size of the @data.
126a23fd118Syl  * @data: User data of variable @data_size that is _copied_ into
127a23fd118Syl  *        the new queue item (see xge_queue_item_t{}). Upon return
128a23fd118Syl  *        from the call the @data memory can be re-used or released.
129a23fd118Syl  *
130a23fd118Syl  * Enqueue a new item.
131a23fd118Syl  *
132a23fd118Syl  * Returns: XGE_QUEUE_OK - success.
133a23fd118Syl  * XGE_QUEUE_IS_FULL - Queue is full.
134a23fd118Syl  * XGE_QUEUE_OUT_OF_MEMORY - Memory allocation failed.
135a23fd118Syl  *
136a23fd118Syl  * See also: xge_queue_item_t{}, xge_queue_consume().
137a23fd118Syl  */
138a23fd118Syl xge_queue_status_e
xge_queue_produce(xge_queue_h queueh,int event_type,void * context,int is_critical,const int data_size,void * data)139a23fd118Syl xge_queue_produce(xge_queue_h queueh, int event_type, void *context,
140a23fd118Syl 		int is_critical, const int data_size, void *data)
141a23fd118Syl {
142a23fd118Syl 	xge_queue_t *queue = (xge_queue_t *)queueh;
143a23fd118Syl 	int real_size = data_size + sizeof(xge_queue_item_t);
144a23fd118Syl 	xge_queue_item_t *elem;
145a23fd118Syl 	unsigned long flags = 0;
146a23fd118Syl 
147a23fd118Syl 	xge_assert(real_size <= XGE_QUEUE_BUF_SIZE);
148a23fd118Syl 
149a23fd118Syl 	xge_os_spin_lock_irq(&queue->lock, flags);
150a23fd118Syl 
151a23fd118Syl 	if (is_critical && !queue->has_critical_event)  {
152a23fd118Syl 		unsigned char item_buf[sizeof(xge_queue_item_t) +
153a23fd118Syl 				XGE_DEFAULT_EVENT_MAX_DATA_SIZE];
154a23fd118Syl 		xge_queue_item_t *item = (xge_queue_item_t *)(void *)item_buf;
1557eced415Sxw     xge_os_memzero(item_buf, (sizeof(xge_queue_item_t) +
156*55fea89dSDan Cross                              XGE_DEFAULT_EVENT_MAX_DATA_SIZE));
157*55fea89dSDan Cross 
158a23fd118Syl 	        while (__queue_consume(queue,
159a23fd118Syl 				       XGE_DEFAULT_EVENT_MAX_DATA_SIZE,
160a23fd118Syl 				       item) != XGE_QUEUE_IS_EMPTY)
161a23fd118Syl 		        ; /* do nothing */
162a23fd118Syl 	}
163a23fd118Syl 
164a23fd118Syl try_again:
165a23fd118Syl 	if ((char *)queue->tail_ptr + real_size <= (char *)queue->end_ptr) {
1668347601bSyl         elem = (xge_queue_item_t *) queue->tail_ptr;
167a23fd118Syl 		queue->tail_ptr = (void *)((char *)queue->tail_ptr + real_size);
168a23fd118Syl 		xge_debug_queue(XGE_TRACE,
169a23fd118Syl 			"event_type: %d adding to the tail: "
1708347601bSyl 			"0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
1718347601bSyl 			":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
172a23fd118Syl 			event_type,
173a23fd118Syl 			(u64)(ulong_t)queue->start_ptr,
174a23fd118Syl 			(u64)(ulong_t)queue->head_ptr,
175a23fd118Syl 			(u64)(ulong_t)queue->tail_ptr,
176a23fd118Syl 			(u64)(ulong_t)queue->end_ptr,
177a23fd118Syl 			(u64)(ulong_t)elem,
178a23fd118Syl 			real_size);
179a23fd118Syl 	} else if ((char *)queue->head_ptr - real_size >=
180a23fd118Syl 					(char *)queue->start_ptr) {
1818347601bSyl         elem = (xge_queue_item_t *) ((char *)queue->head_ptr - real_size);
182a23fd118Syl 		queue->head_ptr = elem;
183a23fd118Syl 		xge_debug_queue(XGE_TRACE,
184a23fd118Syl 			"event_type: %d adding to the head: "
1858347601bSyl 			"0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
1868347601bSyl 			":0x"XGE_OS_LLXFMT" length %d",
187a23fd118Syl 			event_type,
188a23fd118Syl 			(u64)(ulong_t)queue->start_ptr,
189a23fd118Syl 			(u64)(ulong_t)queue->head_ptr,
190a23fd118Syl 			(u64)(ulong_t)queue->tail_ptr,
191a23fd118Syl 			(u64)(ulong_t)queue->end_ptr,
192a23fd118Syl 			real_size);
193a23fd118Syl 	} else {
194a23fd118Syl 		xge_queue_status_e status;
195a23fd118Syl 
196a23fd118Syl 		if (queue->pages_current >= queue->pages_max) {
197a23fd118Syl 			xge_os_spin_unlock_irq(&queue->lock, flags);
198a23fd118Syl 			return XGE_QUEUE_IS_FULL;
199a23fd118Syl 		}
200a23fd118Syl 
2017eced415Sxw 		if (queue->has_critical_event) {
2027eced415Sxw    		xge_os_spin_unlock_irq(&queue->lock, flags);
203a23fd118Syl 			return XGE_QUEUE_IS_FULL;
2047eced415Sxw     }
205a23fd118Syl 
206a23fd118Syl 		/* grow */
207a23fd118Syl 		status = __io_queue_grow(queueh);
208a23fd118Syl 		if (status != XGE_QUEUE_OK) {
209a23fd118Syl 			xge_os_spin_unlock_irq(&queue->lock, flags);
210a23fd118Syl 			return status;
211a23fd118Syl 		}
212a23fd118Syl 
213a23fd118Syl 		goto try_again;
214a23fd118Syl 	}
215a23fd118Syl 	xge_assert(queue->tail_ptr >= queue->head_ptr);
216a23fd118Syl 	xge_assert(queue->tail_ptr >= queue->start_ptr &&
217a23fd118Syl 		    queue->tail_ptr <= queue->end_ptr);
218a23fd118Syl 	xge_assert(queue->head_ptr >= queue->start_ptr &&
219a23fd118Syl 		    queue->head_ptr < queue->end_ptr);
220a23fd118Syl 	elem->data_size = data_size;
2218347601bSyl     elem->event_type = (xge_hal_event_e) event_type;
222a23fd118Syl 	elem->is_critical = is_critical;
223a23fd118Syl 	if (is_critical)
224a23fd118Syl 	        queue->has_critical_event = 1;
225a23fd118Syl 	elem->context = context;
226a23fd118Syl 	xge_os_memcpy(xge_queue_item_data(elem), data, data_size);
227a23fd118Syl 	xge_list_insert_before(&elem->item, &queue->list_head);
228a23fd118Syl 	xge_os_spin_unlock_irq(&queue->lock, flags);
229a23fd118Syl 
230a23fd118Syl 	/* no lock taken! */
231a23fd118Syl 	queue->queued_func(queue->queued_data, event_type);
232a23fd118Syl 
233a23fd118Syl 	return XGE_QUEUE_OK;
234a23fd118Syl }
235a23fd118Syl 
236a23fd118Syl 
237a23fd118Syl /**
238a23fd118Syl  * xge_queue_create - Create protected first-in-first-out queue.
239a23fd118Syl  * @pdev: PCI device handle.
240a23fd118Syl  * @irqh: PCI device IRQ handle.
241a23fd118Syl  * @pages_initial: Number of pages to be initially allocated at the
242a23fd118Syl  * time of queue creation.
243a23fd118Syl  * @pages_max: Max number of pages that can be allocated in the queue.
244a23fd118Syl  * @queued: Optional callback function to be called each time a new item is
245a23fd118Syl  * added to the queue.
246a23fd118Syl  * @queued_data: Argument to the callback function.
247a23fd118Syl  *
248a23fd118Syl  * Create protected (fifo) queue.
249a23fd118Syl  *
250a23fd118Syl  * Returns: Pointer to xge_queue_t structure,
251a23fd118Syl  * NULL - on failure.
252a23fd118Syl  *
253a23fd118Syl  * See also: xge_queue_item_t{}, xge_queue_destroy().
254a23fd118Syl  */
255a23fd118Syl xge_queue_h
xge_queue_create(pci_dev_h pdev,pci_irq_h irqh,int pages_initial,int pages_max,xge_queued_f queued,void * queued_data)256a23fd118Syl xge_queue_create(pci_dev_h pdev, pci_irq_h irqh, int pages_initial,
257a23fd118Syl 		int pages_max, xge_queued_f queued, void *queued_data)
258a23fd118Syl {
259a23fd118Syl 	xge_queue_t *queue;
260a23fd118Syl 
2618347601bSyl     if ((queue = (xge_queue_t *) xge_os_malloc(pdev, sizeof(xge_queue_t))) == NULL)
262a23fd118Syl 		return NULL;
263a23fd118Syl 
264a23fd118Syl 	queue->queued_func = queued;
265a23fd118Syl 	queue->queued_data = queued_data;
266a23fd118Syl 	queue->pdev = pdev;
267a23fd118Syl 	queue->irqh = irqh;
268a23fd118Syl 	queue->pages_current = pages_initial;
269a23fd118Syl 	queue->start_ptr = xge_os_malloc(pdev, queue->pages_current *
270a23fd118Syl 	                               XGE_QUEUE_BUF_SIZE);
271a23fd118Syl 	if (queue->start_ptr == NULL) {
272a23fd118Syl 		xge_os_free(pdev, queue, sizeof(xge_queue_t));
273a23fd118Syl 		return NULL;
274a23fd118Syl 	}
275a23fd118Syl 	queue->head_ptr = queue->tail_ptr = queue->start_ptr;
276a23fd118Syl 	queue->end_ptr = (char *)queue->start_ptr +
277a23fd118Syl 		queue->pages_current * XGE_QUEUE_BUF_SIZE;
278a23fd118Syl 	xge_os_spin_lock_init_irq(&queue->lock, irqh);
279a23fd118Syl 	queue->pages_initial = pages_initial;
280a23fd118Syl 	queue->pages_max = pages_max;
281a23fd118Syl 	xge_list_init(&queue->list_head);
282a23fd118Syl 
283a23fd118Syl 	return queue;
284a23fd118Syl }
285a23fd118Syl 
286a23fd118Syl /**
287a23fd118Syl  * xge_queue_destroy - Destroy xge_queue_t object.
288a23fd118Syl  * @queueh: Queue handle.
289a23fd118Syl  *
290a23fd118Syl  * Destroy the specified xge_queue_t object.
291a23fd118Syl  *
292a23fd118Syl  * See also: xge_queue_item_t{}, xge_queue_create().
293a23fd118Syl  */
xge_queue_destroy(xge_queue_h queueh)294a23fd118Syl void xge_queue_destroy(xge_queue_h queueh)
295a23fd118Syl {
296a23fd118Syl 	xge_queue_t *queue = (xge_queue_t *)queueh;
2977eced415Sxw 	xge_os_spin_lock_destroy_irq(&queue->lock, queue->irqh);
298a23fd118Syl 	if (!xge_list_is_empty(&queue->list_head)) {
2998347601bSyl 		xge_debug_queue(XGE_ERR, "destroying non-empty queue 0x"
3008347601bSyl 				XGE_OS_LLXFMT, (u64)(ulong_t)queue);
301a23fd118Syl 	}
302a23fd118Syl 	xge_os_free(queue->pdev, queue->start_ptr, queue->pages_current *
303a23fd118Syl 	          XGE_QUEUE_BUF_SIZE);
304a23fd118Syl 
305a23fd118Syl 	xge_os_free(queue->pdev, queue, sizeof(xge_queue_t));
306a23fd118Syl }
307a23fd118Syl 
308a23fd118Syl /*
309a23fd118Syl  * __io_queue_grow - Dynamically increases the size of the queue.
310a23fd118Syl  * @queueh: Queue handle.
311a23fd118Syl  *
312a23fd118Syl  * This function is called in the case of no slot avaialble in the queue
313a23fd118Syl  * to accomodate the newly received event.
314a23fd118Syl  * Note that queue cannot grow beyond the max size specified for the
315a23fd118Syl  * queue.
316a23fd118Syl  *
317a23fd118Syl  * Returns XGE_QUEUE_OK: On success.
318a23fd118Syl  * XGE_QUEUE_OUT_OF_MEMORY : No memory is available.
319a23fd118Syl  */
320a23fd118Syl xge_queue_status_e
__io_queue_grow(xge_queue_h queueh)321a23fd118Syl __io_queue_grow(xge_queue_h queueh)
322a23fd118Syl {
323a23fd118Syl 	xge_queue_t *queue = (xge_queue_t *)queueh;
324a23fd118Syl 	void *newbuf, *oldbuf;
325a23fd118Syl 	xge_list_t *item;
326a23fd118Syl 	xge_queue_item_t *elem;
327a23fd118Syl 
3288347601bSyl 	xge_debug_queue(XGE_TRACE, "queue 0x"XGE_OS_LLXFMT":%d is growing",
329a23fd118Syl 			 (u64)(ulong_t)queue, queue->pages_current);
330a23fd118Syl 
331a23fd118Syl 	newbuf = xge_os_malloc(queue->pdev,
332a23fd118Syl 	        (queue->pages_current + 1) * XGE_QUEUE_BUF_SIZE);
333a23fd118Syl 	if (newbuf == NULL)
334a23fd118Syl 		return XGE_QUEUE_OUT_OF_MEMORY;
335a23fd118Syl 
336a23fd118Syl 	xge_os_memcpy(newbuf, queue->start_ptr,
337a23fd118Syl 	       queue->pages_current * XGE_QUEUE_BUF_SIZE);
338a23fd118Syl 	oldbuf = queue->start_ptr;
339a23fd118Syl 
340a23fd118Syl 	/* adjust queue sizes */
341a23fd118Syl 	queue->start_ptr = newbuf;
342a23fd118Syl 	queue->end_ptr = (char *)newbuf +
343a23fd118Syl 			(queue->pages_current + 1) * XGE_QUEUE_BUF_SIZE;
344a23fd118Syl 	queue->tail_ptr = (char *)newbuf + ((char *)queue->tail_ptr -
345a23fd118Syl 					    (char *)oldbuf);
346a23fd118Syl 	queue->head_ptr = (char *)newbuf + ((char *)queue->head_ptr -
347a23fd118Syl 					    (char *)oldbuf);
348a23fd118Syl 	xge_assert(!xge_list_is_empty(&queue->list_head));
349a23fd118Syl 	queue->list_head.next = (xge_list_t *) (void *)((char *)newbuf +
350a23fd118Syl 			((char *)queue->list_head.next - (char *)oldbuf));
351a23fd118Syl 	queue->list_head.prev = (xge_list_t *) (void *)((char *)newbuf +
352a23fd118Syl 			((char *)queue->list_head.prev - (char *)oldbuf));
353a23fd118Syl 	/* adjust queue list */
354a23fd118Syl 	xge_list_for_each(item, &queue->list_head) {
355a23fd118Syl 		elem = xge_container_of(item, xge_queue_item_t, item);
356a23fd118Syl 		if (elem->item.next != &queue->list_head) {
357a23fd118Syl 			elem->item.next =
358a23fd118Syl 				(xge_list_t*)(void *)((char *)newbuf +
359a23fd118Syl 				 ((char *)elem->item.next - (char *)oldbuf));
360a23fd118Syl 		}
361a23fd118Syl 		if (elem->item.prev != &queue->list_head) {
362a23fd118Syl 			elem->item.prev =
363a23fd118Syl 				(xge_list_t*) (void *)((char *)newbuf +
364a23fd118Syl 				 ((char *)elem->item.prev - (char *)oldbuf));
365a23fd118Syl 		}
366a23fd118Syl 	}
367a23fd118Syl 	xge_os_free(queue->pdev, oldbuf,
368a23fd118Syl 		  queue->pages_current * XGE_QUEUE_BUF_SIZE);
369a23fd118Syl 	queue->pages_current++;
370a23fd118Syl 
371a23fd118Syl 	return XGE_QUEUE_OK;
372a23fd118Syl }
373a23fd118Syl 
374a23fd118Syl /**
375a23fd118Syl  * xge_queue_consume - Dequeue an item from the specified queue.
376a23fd118Syl  * @queueh: Queue handle.
377a23fd118Syl  * @data_max_size: Maximum expected size of the item.
378a23fd118Syl  * @item: Memory area into which the item is _copied_ upon return
379a23fd118Syl  *        from the function.
380a23fd118Syl  *
381a23fd118Syl  * Dequeue an item from the queue. The caller is required to provide
382a23fd118Syl  * enough space for the item.
383a23fd118Syl  *
384a23fd118Syl  * Returns: XGE_QUEUE_OK - success.
385a23fd118Syl  * XGE_QUEUE_IS_EMPTY - Queue is empty.
386a23fd118Syl  * XGE_QUEUE_NOT_ENOUGH_SPACE - Requested item size(@data_max_size)
387a23fd118Syl  * is too small to accomodate an item from the queue.
388a23fd118Syl  *
389a23fd118Syl  * See also: xge_queue_item_t{}, xge_queue_produce().
390a23fd118Syl  */
391a23fd118Syl xge_queue_status_e
xge_queue_consume(xge_queue_h queueh,int data_max_size,xge_queue_item_t * item)392a23fd118Syl xge_queue_consume(xge_queue_h queueh, int data_max_size, xge_queue_item_t *item)
393a23fd118Syl {
394a23fd118Syl 	xge_queue_t *queue = (xge_queue_t *)queueh;
395a23fd118Syl 	unsigned long flags = 0;
396a23fd118Syl 	xge_queue_status_e status;
397a23fd118Syl 
398a23fd118Syl 	xge_os_spin_lock_irq(&queue->lock, flags);
399a23fd118Syl 	status = __queue_consume(queue, data_max_size, item);
400a23fd118Syl 	xge_os_spin_unlock_irq(&queue->lock, flags);
401a23fd118Syl 
402a23fd118Syl 	return status;
403a23fd118Syl }
404a23fd118Syl 
405a23fd118Syl 
406a23fd118Syl /**
407a23fd118Syl  * xge_queue_flush - Flush, or empty, the queue.
408a23fd118Syl  * @queueh: Queue handle.
409a23fd118Syl  *
410a23fd118Syl  * Flush the queue, i.e. make it empty by consuming all events
411a23fd118Syl  * without invoking the event processing logic (callbacks, etc.)
412a23fd118Syl  */
xge_queue_flush(xge_queue_h queueh)413a23fd118Syl void xge_queue_flush(xge_queue_h queueh)
414a23fd118Syl {
415a23fd118Syl 	unsigned char item_buf[sizeof(xge_queue_item_t) +
416a23fd118Syl 				XGE_DEFAULT_EVENT_MAX_DATA_SIZE];
417a23fd118Syl 	xge_queue_item_t *item = (xge_queue_item_t *)(void *)item_buf;
4187eced415Sxw   xge_os_memzero(item_buf, (sizeof(xge_queue_item_t) +
419*55fea89dSDan Cross                              XGE_DEFAULT_EVENT_MAX_DATA_SIZE));
420*55fea89dSDan Cross 
421a23fd118Syl 	/* flush queue by consuming all enqueued items */
422a23fd118Syl 	while (xge_queue_consume(queueh,
423a23fd118Syl 				    XGE_DEFAULT_EVENT_MAX_DATA_SIZE,
424a23fd118Syl 				    item) != XGE_QUEUE_IS_EMPTY) {
425a23fd118Syl 		/* do nothing */
4268347601bSyl 		xge_debug_queue(XGE_TRACE, "item "XGE_OS_LLXFMT"(%d) flushed",
427a23fd118Syl 				 item, item->event_type);
428a23fd118Syl 	}
429a23fd118Syl 	(void) __queue_get_reset_critical (queueh);
430a23fd118Syl }
431a23fd118Syl 
432a23fd118Syl /*
433a23fd118Syl  * __queue_get_reset_critical - Check for critical events in the queue,
434a23fd118Syl  * @qh: Queue handle.
435a23fd118Syl  *
436a23fd118Syl  * Check for critical event(s) in the queue, and reset the
437a23fd118Syl  * "has-critical-event" flag upon return.
438a23fd118Syl  * Returns: 1 - if the queue contains atleast one critical event.
439a23fd118Syl  * 0 - If there are no critical events in the queue.
440a23fd118Syl  */
__queue_get_reset_critical(xge_queue_h qh)441a23fd118Syl int __queue_get_reset_critical (xge_queue_h qh) {
442a23fd118Syl 	xge_queue_t* queue = (xge_queue_t*)qh;
443a23fd118Syl 	int c = queue->has_critical_event;
444a23fd118Syl 
445a23fd118Syl 	queue->has_critical_event = 0;
446a23fd118Syl         return c;
447a23fd118Syl }
448