]> CyberLeo.Net >> Repos - FreeBSD/releng/10.1.git/blob - sys/cam/cam_queue.h
Improve patch for SA-15:04.igmp to solve a potential buffer overflow.
[FreeBSD/releng/10.1.git] / sys / cam / cam_queue.h
1 /*-
2  * CAM request queue management definitions.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #ifndef _CAM_CAM_QUEUE_H
32 #define _CAM_CAM_QUEUE_H 1
33
34 #ifdef _KERNEL
35
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/queue.h>
39 #include <cam/cam.h>
40
41 /*
42  * This structure implements a heap based priority queue.  The queue
43  * assumes that the objects stored in it begin with a cam_qentry
44  * structure holding the priority information used to sort the objects.
45  * This structure is opaque to clients (outside of the XPT layer) to allow
46  * the implementation to change without affecting them.
47  */
48 struct camq {
49         cam_pinfo **queue_array;
50         int        array_size;
51         int        entries;
52         u_int32_t  generation;
53         u_int32_t  qfrozen_cnt;
54 };
55
56 TAILQ_HEAD(ccb_hdr_tailq, ccb_hdr);
57 LIST_HEAD(ccb_hdr_list, ccb_hdr);
58 SLIST_HEAD(ccb_hdr_slist, ccb_hdr);
59
60 struct cam_ccbq {
61         struct  camq queue;
62         struct ccb_hdr_tailq    queue_extra_head;
63         int     queue_extra_entries;
64         int     total_openings;
65         int     devq_openings;
66         int     dev_openings;
67         int     dev_active;
68         int     held;
69 };
70
71 struct cam_ed;
72
73 struct cam_devq {
74         struct mtx       send_mtx;
75         struct camq      send_queue;
76         int              send_openings;
77         int              send_active;
78 };
79
80
81 struct cam_devq *cam_devq_alloc(int devices, int openings);
82
83 int              cam_devq_init(struct cam_devq *devq, int devices,
84                                int openings);
85
86 void             cam_devq_free(struct cam_devq *devq);
87
88 u_int32_t        cam_devq_resize(struct cam_devq *camq, int openings);
89         
90 /*
91  * Allocate a cam_ccb_queue structure and initialize it.
92  */
93 struct cam_ccbq *cam_ccbq_alloc(int openings);
94
95 u_int32_t       cam_ccbq_resize(struct cam_ccbq *ccbq, int devices);
96
97 int             cam_ccbq_init(struct cam_ccbq *ccbq, int openings);
98
99 void            cam_ccbq_free(struct cam_ccbq *ccbq);
100
101 void            cam_ccbq_fini(struct cam_ccbq *ccbq);
102
103 /*
104  * Allocate and initialize a cam_queue structure.
105  */
106 struct camq     *camq_alloc(int size);
107
108 /*
109  * Resize a cam queue
110  */
111 u_int32_t       camq_resize(struct camq *queue, int new_size);
112
113 /* 
114  * Initialize a camq structure.  Return 0 on success, 1 on failure.
115  */
116 int             camq_init(struct camq *camq, int size);
117
118 /*
119  * Free a cam_queue structure.  This should only be called if a controller
120  * driver failes somehow during its attach routine or is unloaded and has
121  * obtained a cam_queue structure.
122  */
123 void            camq_free(struct camq *queue);
124
125 /*
126  * Finialize any internal storage or state of a cam_queue.
127  */
128 void            camq_fini(struct camq *queue);
129
130 /*
131  * cam_queue_insert: Given a CAM queue with at least one open spot,
132  * insert the new entry maintaining order.
133  */
134 void            camq_insert(struct camq *queue, cam_pinfo *new_entry);
135
136 /*
137  * camq_remove: Remove and arbitrary entry from the queue maintaining
138  * queue order.
139  */
140 cam_pinfo       *camq_remove(struct camq *queue, int index);
141 #define CAMQ_HEAD 1     /* Head of queue index */
142
143 /* Index the first element in the heap */
144 #define CAMQ_GET_HEAD(camq) ((camq)->queue_array[CAMQ_HEAD])
145
146 /* Get the first element priority. */
147 #define CAMQ_GET_PRIO(camq) (((camq)->entries > 0) ?                    \
148                             ((camq)->queue_array[CAMQ_HEAD]->priority) : 0)
149
150 /*
151  * camq_change_priority: Raise or lower the priority of an entry
152  * maintaining queue order.
153  */
154 void            camq_change_priority(struct camq *queue, int index,
155                                      u_int32_t new_priority);
156
157 static __inline int
158 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq);
159
160 static __inline void
161 cam_ccbq_take_opening(struct cam_ccbq *ccbq);
162
163 static __inline void
164 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb);
165
166 static __inline void
167 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb);
168
169 static __inline union ccb *
170 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index);
171
172 static __inline void
173 cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb);
174
175 static __inline void
176 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb);
177
178 static __inline void
179 cam_ccbq_release_opening(struct cam_ccbq *ccbq);
180
181
182 static __inline int
183 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq)
184 {
185         return (ccbq->queue.entries + ccbq->queue_extra_entries);
186 }
187
188 static __inline void
189 cam_ccbq_take_opening(struct cam_ccbq *ccbq)
190 {
191         ccbq->devq_openings--;
192         ccbq->held++;
193 }
194
195 static __inline void
196 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb)
197 {
198         struct ccb_hdr *old_ccb;
199         struct camq *queue = &ccbq->queue;
200
201         ccbq->held--;
202
203         /*
204          * If queue is already full, try to resize.
205          * If resize fail, push CCB with lowest priority out to the TAILQ.
206          */
207         if (queue->entries == queue->array_size &&
208             camq_resize(&ccbq->queue, queue->array_size * 2) != CAM_REQ_CMP) {
209                 old_ccb = (struct ccb_hdr *)camq_remove(queue, queue->entries);
210                 TAILQ_INSERT_HEAD(&ccbq->queue_extra_head, old_ccb,
211                     xpt_links.tqe);
212                 old_ccb->pinfo.index = CAM_EXTRAQ_INDEX;
213                 ccbq->queue_extra_entries++;
214         }
215
216         camq_insert(queue, &new_ccb->ccb_h.pinfo);
217 }
218
219 static __inline void
220 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb)
221 {
222         struct ccb_hdr *cccb, *bccb;
223         struct camq *queue = &ccbq->queue;
224
225         /* If the CCB is on the TAILQ, remove it from there. */
226         if (ccb->ccb_h.pinfo.index == CAM_EXTRAQ_INDEX) {
227                 TAILQ_REMOVE(&ccbq->queue_extra_head, &ccb->ccb_h,
228                     xpt_links.tqe);
229                 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
230                 ccbq->queue_extra_entries--;
231                 return;
232         }
233
234         camq_remove(queue, ccb->ccb_h.pinfo.index);
235
236         /*
237          * If there are some CCBs on TAILQ, find the best one and move it
238          * to the emptied space in the queue.
239          */
240         bccb = TAILQ_FIRST(&ccbq->queue_extra_head);
241         if (bccb == NULL)
242                 return;
243         TAILQ_FOREACH(cccb, &ccbq->queue_extra_head, xpt_links.tqe) {
244                 if (bccb->pinfo.priority > cccb->pinfo.priority ||
245                     (bccb->pinfo.priority == cccb->pinfo.priority &&
246                      GENERATIONCMP(bccb->pinfo.generation, >,
247                       cccb->pinfo.generation)))
248                         bccb = cccb;
249         }
250         TAILQ_REMOVE(&ccbq->queue_extra_head, bccb, xpt_links.tqe);
251         ccbq->queue_extra_entries--;
252         camq_insert(queue, &bccb->pinfo);
253 }
254
255 static __inline union ccb *
256 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index)
257 {
258         return((union ccb *)ccbq->queue.queue_array[index]);
259 }
260
261 static __inline void
262 cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb)
263 {
264
265         send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX;
266         ccbq->dev_active++;
267         ccbq->dev_openings--;           
268 }
269
270 static __inline void
271 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb)
272 {
273
274         ccbq->dev_active--;
275         ccbq->dev_openings++;   
276         ccbq->held++;
277 }
278
279 static __inline void
280 cam_ccbq_release_opening(struct cam_ccbq *ccbq)
281 {
282         ccbq->held--;
283         ccbq->devq_openings++;
284 }
285
286 #endif /* _KERNEL */
287 #endif  /* _CAM_CAM_QUEUE_H */