1171095Ssam/*-
2171095Ssam * Copyright (c) 2002-2007 Neterion, Inc.
3171095Ssam * All rights reserved.
4171095Ssam *
5171095Ssam * Redistribution and use in source and binary forms, with or without
6171095Ssam * modification, are permitted provided that the following conditions
7171095Ssam * are met:
8171095Ssam * 1. Redistributions of source code must retain the above copyright
9171095Ssam *    notice, this list of conditions and the following disclaimer.
10171095Ssam * 2. Redistributions in binary form must reproduce the above copyright
11171095Ssam *    notice, this list of conditions and the following disclaimer in the
12171095Ssam *    documentation and/or other materials provided with the distribution.
13171095Ssam *
14171095Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171095Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171095Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171095Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171095Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171095Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171095Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171095Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171095Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171095Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171095Ssam * SUCH DAMAGE.
25171095Ssam *
26171095Ssam * $FreeBSD$
27171095Ssam */
28171095Ssam
29171095Ssam#include <dev/nxge/include/xge-queue.h>
30171095Ssam
31171095Ssam/**
32171095Ssam * xge_queue_item_data - Get item's data.
33171095Ssam * @item: Queue item.
34171095Ssam *
35171095Ssam * Returns:  item data(variable size). Note that xge_queue_t
36171095Ssam * contains items comprized of a fixed xge_queue_item_t "header"
37171095Ssam * and a variable size data. This function returns the variable
38171095Ssam * user-defined portion of the queue item.
39171095Ssam */
40171095Ssamvoid* xge_queue_item_data(xge_queue_item_t *item)
41171095Ssam{
42171095Ssam	return (char *)item + sizeof(xge_queue_item_t);
43171095Ssam}
44171095Ssam
45171095Ssam/*
46171095Ssam * __queue_consume - (Lockless) dequeue an item from the specified queue.
47171095Ssam *
48171095Ssam * @queue: Event queue.
49171095Ssam * See xge_queue_consume().
50171095Ssam */
51171095Ssamstatic xge_queue_status_e
52171095Ssam__queue_consume(xge_queue_t *queue, int data_max_size, xge_queue_item_t *item)
53171095Ssam{
54171095Ssam	int real_size;
55171095Ssam	xge_queue_item_t *elem;
56171095Ssam
57171095Ssam	if (xge_list_is_empty(&queue->list_head))
58173139Srwatson	    return XGE_QUEUE_IS_EMPTY;
59171095Ssam
60171095Ssam	elem = (xge_queue_item_t *)queue->list_head.next;
61171095Ssam	if (elem->data_size > data_max_size)
62173139Srwatson	    return XGE_QUEUE_NOT_ENOUGH_SPACE;
63171095Ssam
64171095Ssam	xge_list_remove(&elem->item);
65171095Ssam	real_size = elem->data_size + sizeof(xge_queue_item_t);
66171095Ssam	if (queue->head_ptr == elem) {
67173139Srwatson	    queue->head_ptr = (char *)queue->head_ptr + real_size;
68173139Srwatson	    xge_debug_queue(XGE_TRACE,
69173139Srwatson	        "event_type: %d removing from the head: "
70173139Srwatson	        "0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
71173139Srwatson	        ":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
72173139Srwatson	        elem->event_type,
73173139Srwatson	        (u64)(ulong_t)queue->start_ptr,
74173139Srwatson	        (u64)(ulong_t)queue->head_ptr,
75173139Srwatson	        (u64)(ulong_t)queue->tail_ptr,
76173139Srwatson	        (u64)(ulong_t)queue->end_ptr,
77173139Srwatson	        (u64)(ulong_t)elem,
78173139Srwatson	        real_size);
79171095Ssam	} else if ((char *)queue->tail_ptr - real_size == (char*)elem) {
80173139Srwatson	    queue->tail_ptr = (char *)queue->tail_ptr - real_size;
81173139Srwatson	    xge_debug_queue(XGE_TRACE,
82173139Srwatson	        "event_type: %d removing from the tail: "
83173139Srwatson	        "0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
84173139Srwatson	        ":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
85173139Srwatson	        elem->event_type,
86173139Srwatson	        (u64)(ulong_t)queue->start_ptr,
87173139Srwatson	        (u64)(ulong_t)queue->head_ptr,
88173139Srwatson	        (u64)(ulong_t)queue->tail_ptr,
89173139Srwatson	        (u64)(ulong_t)queue->end_ptr,
90173139Srwatson	        (u64)(ulong_t)elem,
91173139Srwatson	        real_size);
92171095Ssam	} else {
93173139Srwatson	    xge_debug_queue(XGE_TRACE,
94173139Srwatson	        "event_type: %d removing from the list: "
95173139Srwatson	        "0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
96173139Srwatson	        ":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
97173139Srwatson	        elem->event_type,
98173139Srwatson	        (u64)(ulong_t)queue->start_ptr,
99173139Srwatson	        (u64)(ulong_t)queue->head_ptr,
100173139Srwatson	        (u64)(ulong_t)queue->tail_ptr,
101173139Srwatson	        (u64)(ulong_t)queue->end_ptr,
102173139Srwatson	        (u64)(ulong_t)elem,
103173139Srwatson	        real_size);
104171095Ssam	}
105171095Ssam	xge_assert(queue->tail_ptr >= queue->head_ptr);
106171095Ssam	xge_assert(queue->tail_ptr >= queue->start_ptr &&
107173139Srwatson	        queue->tail_ptr <= queue->end_ptr);
108171095Ssam	xge_assert(queue->head_ptr >= queue->start_ptr &&
109173139Srwatson	        queue->head_ptr < queue->end_ptr);
110171095Ssam	xge_os_memcpy(item, elem, sizeof(xge_queue_item_t));
111171095Ssam	xge_os_memcpy(xge_queue_item_data(item), xge_queue_item_data(elem),
112173139Srwatson	        elem->data_size);
113171095Ssam
114171095Ssam	if (xge_list_is_empty(&queue->list_head)) {
115173139Srwatson	    /* reset buffer pointers just to be clean */
116173139Srwatson	    queue->head_ptr = queue->tail_ptr = queue->start_ptr;
117171095Ssam	}
118171095Ssam	return XGE_QUEUE_OK;
119171095Ssam}
120171095Ssam
121171095Ssam/**
122171095Ssam * xge_queue_produce - Enqueue an item (see xge_queue_item_t{})
123171095Ssam *                      into the specified queue.
124171095Ssam * @queueh: Queue handle.
125171095Ssam * @event_type: Event type. One of the enumerated event types
126171095Ssam *              that both consumer and producer "understand".
127171095Ssam *              For an example, please refer to xge_hal_event_e.
128171095Ssam * @context: Opaque (void*) "context", for instance event producer object.
129171095Ssam * @is_critical: For critical event, e.g. ECC.
130171095Ssam * @data_size: Size of the @data.
131171095Ssam * @data: User data of variable @data_size that is _copied_ into
132171095Ssam *        the new queue item (see xge_queue_item_t{}). Upon return
133171095Ssam *        from the call the @data memory can be re-used or released.
134171095Ssam *
135171095Ssam * Enqueue a new item.
136171095Ssam *
137171095Ssam * Returns: XGE_QUEUE_OK - success.
138171095Ssam * XGE_QUEUE_IS_FULL - Queue is full.
139171095Ssam * XGE_QUEUE_OUT_OF_MEMORY - Memory allocation failed.
140171095Ssam *
141171095Ssam * See also: xge_queue_item_t{}, xge_queue_consume().
142171095Ssam */
143171095Ssamxge_queue_status_e
144171095Ssamxge_queue_produce(xge_queue_h queueh, int event_type, void *context,
145173139Srwatson	    int is_critical, const int data_size, void *data)
146171095Ssam{
147171095Ssam	xge_queue_t *queue = (xge_queue_t *)queueh;
148171095Ssam	int real_size = data_size + sizeof(xge_queue_item_t);
149171095Ssam	xge_queue_item_t *elem;
150171095Ssam	unsigned long flags = 0;
151171095Ssam
152171095Ssam	xge_assert(real_size <= XGE_QUEUE_BUF_SIZE);
153171095Ssam
154171095Ssam	xge_os_spin_lock_irq(&queue->lock, flags);
155171095Ssam
156171095Ssam	if (is_critical && !queue->has_critical_event)  {
157173139Srwatson	    unsigned char item_buf[sizeof(xge_queue_item_t) +
158173139Srwatson	            XGE_DEFAULT_EVENT_MAX_DATA_SIZE];
159173139Srwatson	    xge_queue_item_t *item = (xge_queue_item_t *)(void *)item_buf;
160173139Srwatson	xge_os_memzero(item_buf, (sizeof(xge_queue_item_t) +
161173139Srwatson	                         XGE_DEFAULT_EVENT_MAX_DATA_SIZE));
162171095Ssam
163171095Ssam	        while (__queue_consume(queue,
164173139Srwatson	                   XGE_DEFAULT_EVENT_MAX_DATA_SIZE,
165173139Srwatson	                   item) != XGE_QUEUE_IS_EMPTY)
166173139Srwatson	            ; /* do nothing */
167171095Ssam	}
168171095Ssam
169171095Ssamtry_again:
170171095Ssam	if ((char *)queue->tail_ptr + real_size <= (char *)queue->end_ptr) {
171173139Srwatson	    elem = (xge_queue_item_t *) queue->tail_ptr;
172173139Srwatson	    queue->tail_ptr = (void *)((char *)queue->tail_ptr + real_size);
173173139Srwatson	    xge_debug_queue(XGE_TRACE,
174173139Srwatson	        "event_type: %d adding to the tail: "
175173139Srwatson	        "0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
176173139Srwatson	        ":0x"XGE_OS_LLXFMT" elem 0x"XGE_OS_LLXFMT" length %d",
177173139Srwatson	        event_type,
178173139Srwatson	        (u64)(ulong_t)queue->start_ptr,
179173139Srwatson	        (u64)(ulong_t)queue->head_ptr,
180173139Srwatson	        (u64)(ulong_t)queue->tail_ptr,
181173139Srwatson	        (u64)(ulong_t)queue->end_ptr,
182173139Srwatson	        (u64)(ulong_t)elem,
183173139Srwatson	        real_size);
184171095Ssam	} else if ((char *)queue->head_ptr - real_size >=
185173139Srwatson	                (char *)queue->start_ptr) {
186173139Srwatson	    elem = (xge_queue_item_t *) ((char *)queue->head_ptr - real_size);
187173139Srwatson	    queue->head_ptr = elem;
188173139Srwatson	    xge_debug_queue(XGE_TRACE,
189173139Srwatson	        "event_type: %d adding to the head: "
190173139Srwatson	        "0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT":0x"XGE_OS_LLXFMT
191173139Srwatson	        ":0x"XGE_OS_LLXFMT" length %d",
192173139Srwatson	        event_type,
193173139Srwatson	        (u64)(ulong_t)queue->start_ptr,
194173139Srwatson	        (u64)(ulong_t)queue->head_ptr,
195173139Srwatson	        (u64)(ulong_t)queue->tail_ptr,
196173139Srwatson	        (u64)(ulong_t)queue->end_ptr,
197173139Srwatson	        real_size);
198171095Ssam	} else {
199173139Srwatson	    xge_queue_status_e status;
200171095Ssam
201173139Srwatson	    if (queue->pages_current >= queue->pages_max) {
202173139Srwatson	        xge_os_spin_unlock_irq(&queue->lock, flags);
203173139Srwatson	        return XGE_QUEUE_IS_FULL;
204173139Srwatson	    }
205171095Ssam
206173139Srwatson	    if (queue->has_critical_event) {
207173139Srwatson	    xge_os_spin_unlock_irq(&queue->lock, flags);
208173139Srwatson	        return XGE_QUEUE_IS_FULL;
209173139Srwatson	}
210171095Ssam
211173139Srwatson	    /* grow */
212173139Srwatson	    status = __io_queue_grow(queueh);
213173139Srwatson	    if (status != XGE_QUEUE_OK) {
214173139Srwatson	        xge_os_spin_unlock_irq(&queue->lock, flags);
215173139Srwatson	        return status;
216173139Srwatson	    }
217171095Ssam
218173139Srwatson	    goto try_again;
219171095Ssam	}
220171095Ssam	xge_assert(queue->tail_ptr >= queue->head_ptr);
221171095Ssam	xge_assert(queue->tail_ptr >= queue->start_ptr &&
222173139Srwatson	        queue->tail_ptr <= queue->end_ptr);
223171095Ssam	xge_assert(queue->head_ptr >= queue->start_ptr &&
224173139Srwatson	        queue->head_ptr < queue->end_ptr);
225171095Ssam	elem->data_size = data_size;
226173139Srwatson	elem->event_type = (xge_hal_event_e) event_type;
227171095Ssam	elem->is_critical = is_critical;
228171095Ssam	if (is_critical)
229171095Ssam	        queue->has_critical_event = 1;
230171095Ssam	elem->context = context;
231171095Ssam	xge_os_memcpy(xge_queue_item_data(elem), data, data_size);
232171095Ssam	xge_list_insert_before(&elem->item, &queue->list_head);
233171095Ssam	xge_os_spin_unlock_irq(&queue->lock, flags);
234171095Ssam
235171095Ssam	/* no lock taken! */
236171095Ssam	queue->queued_func(queue->queued_data, event_type);
237171095Ssam
238171095Ssam	return XGE_QUEUE_OK;
239171095Ssam}
240171095Ssam
241171095Ssam
242171095Ssam/**
243171095Ssam * xge_queue_create - Create protected first-in-first-out queue.
244171095Ssam * @pdev: PCI device handle.
245171095Ssam * @irqh: PCI device IRQ handle.
246171095Ssam * @pages_initial: Number of pages to be initially allocated at the
247171095Ssam * time of queue creation.
248171095Ssam * @pages_max: Max number of pages that can be allocated in the queue.
249171095Ssam * @queued: Optional callback function to be called each time a new item is
250171095Ssam * added to the queue.
251171095Ssam * @queued_data: Argument to the callback function.
252171095Ssam *
253171095Ssam * Create protected (fifo) queue.
254171095Ssam *
255171095Ssam * Returns: Pointer to xge_queue_t structure,
256171095Ssam * NULL - on failure.
257171095Ssam *
258171095Ssam * See also: xge_queue_item_t{}, xge_queue_destroy().
259171095Ssam */
260171095Ssamxge_queue_h
261171095Ssamxge_queue_create(pci_dev_h pdev, pci_irq_h irqh, int pages_initial,
262173139Srwatson	    int pages_max, xge_queued_f queued, void *queued_data)
263171095Ssam{
264171095Ssam	xge_queue_t *queue;
265171095Ssam
266173139Srwatson	if ((queue = (xge_queue_t *) xge_os_malloc(pdev, sizeof(xge_queue_t))) == NULL)
267173139Srwatson	    return NULL;
268171095Ssam
269171095Ssam	queue->queued_func = queued;
270171095Ssam	queue->queued_data = queued_data;
271171095Ssam	queue->pdev = pdev;
272171095Ssam	queue->irqh = irqh;
273171095Ssam	queue->pages_current = pages_initial;
274171095Ssam	queue->start_ptr = xge_os_malloc(pdev, queue->pages_current *
275171095Ssam	                               XGE_QUEUE_BUF_SIZE);
276171095Ssam	if (queue->start_ptr == NULL) {
277173139Srwatson	    xge_os_free(pdev, queue, sizeof(xge_queue_t));
278173139Srwatson	    return NULL;
279171095Ssam	}
280171095Ssam	queue->head_ptr = queue->tail_ptr = queue->start_ptr;
281171095Ssam	queue->end_ptr = (char *)queue->start_ptr +
282173139Srwatson	    queue->pages_current * XGE_QUEUE_BUF_SIZE;
283171095Ssam	xge_os_spin_lock_init_irq(&queue->lock, irqh);
284171095Ssam	queue->pages_initial = pages_initial;
285171095Ssam	queue->pages_max = pages_max;
286171095Ssam	xge_list_init(&queue->list_head);
287171095Ssam
288171095Ssam	return queue;
289171095Ssam}
290171095Ssam
291171095Ssam/**
292171095Ssam * xge_queue_destroy - Destroy xge_queue_t object.
293171095Ssam * @queueh: Queue handle.
294171095Ssam *
295171095Ssam * Destroy the specified xge_queue_t object.
296171095Ssam *
297171095Ssam * See also: xge_queue_item_t{}, xge_queue_create().
298171095Ssam */
299171095Ssamvoid xge_queue_destroy(xge_queue_h queueh)
300171095Ssam{
301171095Ssam	xge_queue_t *queue = (xge_queue_t *)queueh;
302171095Ssam	xge_os_spin_lock_destroy_irq(&queue->lock, queue->irqh);
303171095Ssam	if (!xge_list_is_empty(&queue->list_head)) {
304173139Srwatson	    xge_debug_queue(XGE_ERR, "destroying non-empty queue 0x"
305173139Srwatson	            XGE_OS_LLXFMT, (u64)(ulong_t)queue);
306171095Ssam	}
307171095Ssam	xge_os_free(queue->pdev, queue->start_ptr, queue->pages_current *
308171095Ssam	          XGE_QUEUE_BUF_SIZE);
309171095Ssam
310171095Ssam	xge_os_free(queue->pdev, queue, sizeof(xge_queue_t));
311171095Ssam}
312171095Ssam
313171095Ssam/*
314171095Ssam * __io_queue_grow - Dynamically increases the size of the queue.
315171095Ssam * @queueh: Queue handle.
316171095Ssam *
317171095Ssam * This function is called in the case of no slot avaialble in the queue
318171095Ssam * to accomodate the newly received event.
319171095Ssam * Note that queue cannot grow beyond the max size specified for the
320171095Ssam * queue.
321171095Ssam *
322171095Ssam * Returns XGE_QUEUE_OK: On success.
323171095Ssam * XGE_QUEUE_OUT_OF_MEMORY : No memory is available.
324171095Ssam */
325171095Ssamxge_queue_status_e
326171095Ssam__io_queue_grow(xge_queue_h queueh)
327171095Ssam{
328171095Ssam	xge_queue_t *queue = (xge_queue_t *)queueh;
329171095Ssam	void *newbuf, *oldbuf;
330171095Ssam	xge_list_t *item;
331171095Ssam	xge_queue_item_t *elem;
332171095Ssam
333171095Ssam	xge_debug_queue(XGE_TRACE, "queue 0x"XGE_OS_LLXFMT":%d is growing",
334173139Srwatson	         (u64)(ulong_t)queue, queue->pages_current);
335171095Ssam
336171095Ssam	newbuf = xge_os_malloc(queue->pdev,
337171095Ssam	        (queue->pages_current + 1) * XGE_QUEUE_BUF_SIZE);
338171095Ssam	if (newbuf == NULL)
339173139Srwatson	    return XGE_QUEUE_OUT_OF_MEMORY;
340171095Ssam
341171095Ssam	xge_os_memcpy(newbuf, queue->start_ptr,
342171095Ssam	       queue->pages_current * XGE_QUEUE_BUF_SIZE);
343171095Ssam	oldbuf = queue->start_ptr;
344171095Ssam
345171095Ssam	/* adjust queue sizes */
346171095Ssam	queue->start_ptr = newbuf;
347171095Ssam	queue->end_ptr = (char *)newbuf +
348173139Srwatson	        (queue->pages_current + 1) * XGE_QUEUE_BUF_SIZE;
349171095Ssam	queue->tail_ptr = (char *)newbuf + ((char *)queue->tail_ptr -
350173139Srwatson	                    (char *)oldbuf);
351171095Ssam	queue->head_ptr = (char *)newbuf + ((char *)queue->head_ptr -
352173139Srwatson	                    (char *)oldbuf);
353171095Ssam	xge_assert(!xge_list_is_empty(&queue->list_head));
354171095Ssam	queue->list_head.next = (xge_list_t *) (void *)((char *)newbuf +
355173139Srwatson	        ((char *)queue->list_head.next - (char *)oldbuf));
356171095Ssam	queue->list_head.prev = (xge_list_t *) (void *)((char *)newbuf +
357173139Srwatson	        ((char *)queue->list_head.prev - (char *)oldbuf));
358171095Ssam	/* adjust queue list */
359171095Ssam	xge_list_for_each(item, &queue->list_head) {
360173139Srwatson	    elem = xge_container_of(item, xge_queue_item_t, item);
361173139Srwatson	    if (elem->item.next != &queue->list_head) {
362173139Srwatson	        elem->item.next =
363173139Srwatson	            (xge_list_t*)(void *)((char *)newbuf +
364173139Srwatson	             ((char *)elem->item.next - (char *)oldbuf));
365173139Srwatson	    }
366173139Srwatson	    if (elem->item.prev != &queue->list_head) {
367173139Srwatson	        elem->item.prev =
368173139Srwatson	            (xge_list_t*) (void *)((char *)newbuf +
369173139Srwatson	             ((char *)elem->item.prev - (char *)oldbuf));
370173139Srwatson	    }
371171095Ssam	}
372171095Ssam	xge_os_free(queue->pdev, oldbuf,
373173139Srwatson	      queue->pages_current * XGE_QUEUE_BUF_SIZE);
374171095Ssam	queue->pages_current++;
375171095Ssam
376171095Ssam	return XGE_QUEUE_OK;
377171095Ssam}
378171095Ssam
379171095Ssam/**
380171095Ssam * xge_queue_consume - Dequeue an item from the specified queue.
381171095Ssam * @queueh: Queue handle.
382171095Ssam * @data_max_size: Maximum expected size of the item.
383171095Ssam * @item: Memory area into which the item is _copied_ upon return
384171095Ssam *        from the function.
385171095Ssam *
386171095Ssam * Dequeue an item from the queue. The caller is required to provide
387171095Ssam * enough space for the item.
388171095Ssam *
389171095Ssam * Returns: XGE_QUEUE_OK - success.
390171095Ssam * XGE_QUEUE_IS_EMPTY - Queue is empty.
391171095Ssam * XGE_QUEUE_NOT_ENOUGH_SPACE - Requested item size(@data_max_size)
392171095Ssam * is too small to accomodate an item from the queue.
393171095Ssam *
394171095Ssam * See also: xge_queue_item_t{}, xge_queue_produce().
395171095Ssam */
396171095Ssamxge_queue_status_e
397171095Ssamxge_queue_consume(xge_queue_h queueh, int data_max_size, xge_queue_item_t *item)
398171095Ssam{
399171095Ssam	xge_queue_t *queue = (xge_queue_t *)queueh;
400171095Ssam	unsigned long flags = 0;
401171095Ssam	xge_queue_status_e status;
402171095Ssam
403171095Ssam	xge_os_spin_lock_irq(&queue->lock, flags);
404171095Ssam	status = __queue_consume(queue, data_max_size, item);
405171095Ssam	xge_os_spin_unlock_irq(&queue->lock, flags);
406171095Ssam
407171095Ssam	return status;
408171095Ssam}
409171095Ssam
410171095Ssam
411171095Ssam/**
412171095Ssam * xge_queue_flush - Flush, or empty, the queue.
413171095Ssam * @queueh: Queue handle.
414171095Ssam *
415171095Ssam * Flush the queue, i.e. make it empty by consuming all events
416171095Ssam * without invoking the event processing logic (callbacks, etc.)
417171095Ssam */
418171095Ssamvoid xge_queue_flush(xge_queue_h queueh)
419171095Ssam{
420171095Ssam	unsigned char item_buf[sizeof(xge_queue_item_t) +
421173139Srwatson	            XGE_DEFAULT_EVENT_MAX_DATA_SIZE];
422171095Ssam	xge_queue_item_t *item = (xge_queue_item_t *)(void *)item_buf;
423171095Ssam  xge_os_memzero(item_buf, (sizeof(xge_queue_item_t) +
424173139Srwatson	                         XGE_DEFAULT_EVENT_MAX_DATA_SIZE));
425171095Ssam
426171095Ssam	/* flush queue by consuming all enqueued items */
427171095Ssam	while (xge_queue_consume(queueh,
428173139Srwatson	                XGE_DEFAULT_EVENT_MAX_DATA_SIZE,
429173139Srwatson	                item) != XGE_QUEUE_IS_EMPTY) {
430173139Srwatson	    /* do nothing */
431173139Srwatson	    xge_debug_queue(XGE_TRACE, "item "XGE_OS_LLXFMT"(%d) flushed",
432173139Srwatson	             item, item->event_type);
433171095Ssam	}
434171095Ssam	(void) __queue_get_reset_critical (queueh);
435171095Ssam}
436171095Ssam
437171095Ssam/*
438171095Ssam * __queue_get_reset_critical - Check for critical events in the queue,
439171095Ssam * @qh: Queue handle.
440171095Ssam *
441171095Ssam * Check for critical event(s) in the queue, and reset the
442171095Ssam * "has-critical-event" flag upon return.
443171095Ssam * Returns: 1 - if the queue contains atleast one critical event.
444171095Ssam * 0 - If there are no critical events in the queue.
445171095Ssam */
446171095Ssamint __queue_get_reset_critical (xge_queue_h qh) {
447171095Ssam	xge_queue_t* queue = (xge_queue_t*)qh;
448171095Ssam	int c = queue->has_critical_event;
449171095Ssam
450171095Ssam	queue->has_critical_event = 0;
451173139Srwatson	    return c;
452171095Ssam}
453