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#ifndef XGE_QUEUE_H
30171095Ssam#define XGE_QUEUE_H
31171095Ssam
32171095Ssam#include <dev/nxge/include/xge-os-pal.h>
33171095Ssam#include <dev/nxge/include/xge-defs.h>
34171095Ssam#include <dev/nxge/include/xge-list.h>
35171095Ssam#include <dev/nxge/include/xgehal-event.h>
36171095Ssam
37171095Ssam__EXTERN_BEGIN_DECLS
38171095Ssam
39173139Srwatson#define XGE_QUEUE_BUF_SIZE      0x1000
40173139Srwatson#define XGE_DEFAULT_EVENT_MAX_DATA_SIZE 16
41171095Ssam
42171095Ssam/**
43171095Ssam * enum xge_queue_status_e - Enumerates return codes of the xge_queue
44171095Ssam * manipulation APIs.
45171095Ssam * @XGE_QUEUE_IS_FULL: Queue is full, need to grow.
46171095Ssam * @XGE_QUEUE_IS_EMPTY: Queue is empty.
47171095Ssam * @XGE_QUEUE_OUT_OF_MEMORY: Out of memory.
48171095Ssam * @XGE_QUEUE_NOT_ENOUGH_SPACE: Exceeded specified event size,
49171095Ssam * see xge_queue_consume().
50171095Ssam * @XGE_QUEUE_OK: Neither one of the codes listed above.
51171095Ssam *
52171095Ssam * Enumerates return codes of xge_queue_consume()
53171095Ssam * and xge_queue_produce() APIs.
54171095Ssam */
55171095Ssamtypedef enum xge_queue_status_e {
56173139Srwatson	XGE_QUEUE_OK            = 0,
57173139Srwatson	XGE_QUEUE_IS_FULL       = 1,
58173139Srwatson	XGE_QUEUE_IS_EMPTY      = 2,
59173139Srwatson	XGE_QUEUE_OUT_OF_MEMORY         = 3,
60173139Srwatson	XGE_QUEUE_NOT_ENOUGH_SPACE  = 4
61171095Ssam} xge_queue_status_e;
62171095Ssam
63171095Ssamtypedef void* xge_queue_h;
64171095Ssam
65171095Ssam/**
66171095Ssam * struct xge_queue_item_t - Queue item.
67171095Ssam * @item: List item. Note that the queue is "built" on top of
68171095Ssam *        the bi-directional linked list.
69171095Ssam * @event_type: Event type. Includes (but is not restricted to)
70171095Ssam * one of the xge_hal_event_e{} enumerated types.
71171095Ssam * @data_size: Size of the enqueued user data. Note that xge_queue_t
72171095Ssam * items are allowed to have variable sizes.
73171095Ssam * @is_critical: For critical events, e.g. ECC.
74171095Ssam * @context: Opaque (void*) "context", for instance event producer object.
75171095Ssam *
76171095Ssam * Item of the xge_queue_t{}. The queue is protected
77171095Ssam * in terms of multi-threaded concurrent access.
78171095Ssam * See also: xge_queue_t{}.
79171095Ssam */
80171095Ssamtypedef struct xge_queue_item_t {
81173139Srwatson	xge_list_t          item;
82173139Srwatson	xge_hal_event_e     event_type;
83173139Srwatson	int                 data_size;
84173139Srwatson	int                 is_critical;
85173139Srwatson	void                *context;
86171095Ssam} xge_queue_item_t;
87171095Ssam
88171095Ssam/**
89171095Ssam * function xge_queued_f - Item-enqueued callback.
90171095Ssam * @data: Per-queue context independent of the event. E.g., device handle.
91171095Ssam * @event_type: HAL or ULD-defined event type. Note that HAL own
92171095Ssam *        events are enumerated by xge_hal_event_e{}.
93171095Ssam *
94171095Ssam * Per-queue optional callback. If not NULL, called by HAL each
95171095Ssam * time an event gets added to the queue.
96171095Ssam */
97171095Ssamtypedef void (*xge_queued_f) (void *data, int event_type);
98171095Ssam
99171095Ssam/**
100171095Ssam * struct xge_queue_t - Protected dynamic queue of variable-size items.
101171095Ssam * @start_ptr: Points to the start of the queue.
102171095Ssam * @end_ptr: Points to the end of the queue.
103171095Ssam * @head_ptr: Points to the head of the queue. It gets changed during queue
104171095Ssam *            produce/consume operations.
105171095Ssam * @tail_ptr: Points to the tail of the queue. It gets changed during queue
106171095Ssam *            produce/consume operations.
107171095Ssam * @lock: Lock for queue operations(syncronization purpose).
108171095Ssam * @pages_initial:Number of pages to be initially allocated at the time
109173139Srwatson *        of queue creation.
110171095Ssam * @pages_max: Max number of pages that can be allocated in the queue.
111171095Ssam * @pages_current: Number of pages currently allocated
112171095Ssam * @list_head: Points to the list of queue elements that are produced, but yet
113171095Ssam *             to be consumed.
114171095Ssam * @signal_callback: (TODO)
115171095Ssam * @pdev: PCI device handle
116171095Ssam * @irqh: PCI device IRQ handle.
117171095Ssam * @queued_func: Optional callback function to be called each time a new
118171095Ssam * item is added to the queue.
119171095Ssam * @queued_data: Arguments to the callback function.
120171095Ssam * @has_critical_event: Non-zero, if the queue contains a critical event,
121171095Ssam * see xge_hal_event_e{}.
122171095Ssam * Protected dynamically growing queue. The queue is used to support multiple
123171095Ssam * producer/consumer type scenarios. The queue is a strict FIFO: first come
124171095Ssam * first served.
125171095Ssam * Queue users may "produce" (see xge_queue_produce()) and "consume"
126171095Ssam * (see xge_queue_consume()) items (a.k.a. events) variable sizes.
127171095Ssam * See also: xge_queue_item_t{}.
128171095Ssam */
129171095Ssamtypedef struct xge_queue_t {
130173139Srwatson	void                *start_ptr;
131173139Srwatson	void                *end_ptr;
132173139Srwatson	void                *head_ptr;
133173139Srwatson	void                *tail_ptr;
134173139Srwatson	spinlock_t          lock;
135173139Srwatson	unsigned int        pages_initial;
136173139Srwatson	unsigned int        pages_max;
137173139Srwatson	unsigned int        pages_current;
138173139Srwatson	xge_list_t          list_head;
139173139Srwatson	pci_dev_h           pdev;
140173139Srwatson	pci_irq_h           irqh;
141173139Srwatson	xge_queued_f        queued_func;
142173139Srwatson	void                *queued_data;
143173139Srwatson	int             has_critical_event;
144171095Ssam} xge_queue_t;
145171095Ssam
146171095Ssam/* ========================== PUBLIC API ================================= */
147171095Ssam
148171095Ssamxge_queue_h xge_queue_create(pci_dev_h pdev, pci_irq_h irqh, int pages_initial,
149173139Srwatson	    int pages_max, xge_queued_f queued_func, void *queued_data);
150171095Ssam
151171095Ssamvoid xge_queue_destroy(xge_queue_h queueh);
152171095Ssam
153171095Ssamvoid* xge_queue_item_data(xge_queue_item_t *item);
154171095Ssam
155171095Ssamxge_queue_status_e
156171095Ssamxge_queue_produce(xge_queue_h queueh, int event_type, void *context,
157173139Srwatson	    int is_critical, const int data_size, void *data);
158171095Ssam
159171095Ssamstatic inline xge_queue_status_e
160171095Ssamxge_queue_produce_context(xge_queue_h queueh, int event_type, void *context) {
161171095Ssam	return xge_queue_produce(queueh, event_type, context, 0, 0, 0);
162171095Ssam}
163171095Ssam
164171095Ssamxge_queue_status_e xge_queue_consume(xge_queue_h queueh, int data_max_size,
165173139Srwatson	    xge_queue_item_t *item);
166171095Ssam
167171095Ssamvoid xge_queue_flush(xge_queue_h queueh);
168171095Ssam
169171095Ssam/* ========================== PRIVATE API ================================= */
170171095Ssam
171171095Ssamxge_queue_status_e __io_queue_grow(xge_queue_h qh);
172171095Ssam
173171095Ssamint __queue_get_reset_critical (xge_queue_h qh);
174171095Ssam
175171095Ssam__EXTERN_END_DECLS
176171095Ssam
177171095Ssam#endif /* XGE_QUEUE_H */
178