FreeBSD/Linux Kernel Cross Reference
sys/cam/cam_queue.c
1 /*
2 * CAM request queue management functions.
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: releng/5.1/sys/cam/cam_queue.c 108470 2002-12-30 21:18:15Z schweikh $
29 */
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33 #include <sys/malloc.h>
34
35 #include <cam/cam.h>
36 #include <cam/cam_ccb.h>
37 #include <cam/cam_queue.h>
38 #include <cam/cam_debug.h>
39
40 static __inline int
41 queue_cmp(cam_pinfo **queue_array, int i, int j);
42 static __inline void
43 swap(cam_pinfo **queue_array, int i, int j);
44 static void heap_up(cam_pinfo **queue_array, int new_index);
45 static void heap_down(cam_pinfo **queue_array, int index,
46 int last_index);
47
48 struct camq *
49 camq_alloc(int size)
50 {
51 struct camq *camq;
52
53 camq = (struct camq *)malloc(sizeof(*camq), M_DEVBUF, M_NOWAIT);
54 if (camq != NULL) {
55 if (camq_init(camq, size) != 0) {
56 free(camq, M_DEVBUF);
57 camq = NULL;
58 }
59 }
60 return (camq);
61 }
62
63 int
64 camq_init(struct camq *camq, int size)
65 {
66 bzero(camq, sizeof(*camq));
67 camq->array_size = size;
68 if (camq->array_size != 0) {
69 camq->queue_array = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*),
70 M_DEVBUF, M_NOWAIT);
71 if (camq->queue_array == NULL) {
72 printf("camq_init: - cannot malloc array!\n");
73 return (1);
74 }
75 /*
76 * Heap algorithms like everything numbered from 1, so
77 * offset our pointer into the heap array by one element.
78 */
79 camq->queue_array--;
80 }
81 return (0);
82 }
83
84 /*
85 * Free a camq structure. This should only be called if a controller
86 * driver failes somehow during its attach routine or is unloaded and has
87 * obtained a camq structure. The XPT should ensure that the queue
88 * is empty before calling this routine.
89 */
90 void
91 camq_free(struct camq *queue)
92 {
93 if (queue != NULL) {
94 camq_fini(queue);
95 free(queue, M_DEVBUF);
96 }
97 }
98
99 void
100 camq_fini(struct camq *queue)
101 {
102 if (queue->queue_array != NULL) {
103 /*
104 * Heap algorithms like everything numbered from 1, so
105 * our pointer into the heap array is offset by one element.
106 */
107 queue->queue_array++;
108 free(queue->queue_array, M_DEVBUF);
109 }
110 }
111
112 u_int32_t
113 camq_resize(struct camq *queue, int new_size)
114 {
115 cam_pinfo **new_array;
116
117 #ifdef DIAGNOSTIC
118 if (new_size < queue->entries)
119 panic("camq_resize: New queue size can't accomodate "
120 "queued entries.");
121 #endif
122 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
123 M_DEVBUF, M_NOWAIT);
124 if (new_array == NULL) {
125 /* Couldn't satisfy request */
126 return (CAM_RESRC_UNAVAIL);
127 }
128 /*
129 * Heap algorithms like everything numbered from 1, so
130 * remember that our pointer into the heap array is offset
131 * by one element.
132 */
133 if (queue->queue_array != NULL) {
134 queue->queue_array++;
135 bcopy(queue->queue_array, new_array,
136 queue->entries * sizeof(cam_pinfo *));
137 free(queue->queue_array, M_DEVBUF);
138 }
139 queue->queue_array = new_array-1;
140 queue->array_size = new_size;
141 return (CAM_REQ_CMP);
142 }
143
144 /*
145 * camq_insert: Given an array of cam_pinfo* elememnts with
146 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
147 * output Heap(1, num_elements+1) including new_entry in the array.
148 */
149 void
150 camq_insert(struct camq *queue, cam_pinfo *new_entry)
151 {
152 #ifdef DIAGNOSTIC
153 if (queue->entries >= queue->array_size)
154 panic("camq_insert: Attempt to insert into a full queue");
155 #endif
156 queue->entries++;
157 queue->queue_array[queue->entries] = new_entry;
158 new_entry->index = queue->entries;
159 if (queue->entries != 0)
160 heap_up(queue->queue_array, queue->entries);
161 }
162
163 /*
164 * camq_remove: Given an array of cam_pinfo* elevements with the
165 * Heap(1, num_elements) property and an index such that 1 <= index <=
166 * num_elements, remove that entry and restore the Heap(1, num_elements-1)
167 * property.
168 */
169 cam_pinfo *
170 camq_remove(struct camq *queue, int index)
171 {
172 cam_pinfo *removed_entry;
173
174 if (index == 0 || index > queue->entries)
175 return (NULL);
176 removed_entry = queue->queue_array[index];
177 if (queue->entries != index) {
178 queue->queue_array[index] = queue->queue_array[queue->entries];
179 queue->queue_array[index]->index = index;
180 heap_down(queue->queue_array, index, queue->entries - 1);
181 }
182 removed_entry->index = CAM_UNQUEUED_INDEX;
183 queue->entries--;
184 return (removed_entry);
185 }
186
187 /*
188 * camq_change_priority: Given an array of cam_pinfo* elements with the
189 * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
190 * and a new priority for the element at index, change the priority of
191 * element index and restore the Heap(0, num_elements) property.
192 */
193 void
194 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
195 {
196 if (new_priority > queue->queue_array[index]->priority) {
197 queue->queue_array[index]->priority = new_priority;
198 heap_down(queue->queue_array, index, queue->entries);
199 } else {
200 /* new_priority <= old_priority */
201 queue->queue_array[index]->priority = new_priority;
202 heap_up(queue->queue_array, index);
203 }
204 }
205
206 struct cam_devq *
207 cam_devq_alloc(int devices, int openings)
208 {
209 struct cam_devq *devq;
210
211 devq = (struct cam_devq *)malloc(sizeof(*devq), M_DEVBUF, M_NOWAIT);
212 if (devq == NULL) {
213 printf("cam_devq_alloc: - cannot malloc!\n");
214 return (NULL);
215 }
216 if (cam_devq_init(devq, devices, openings) != 0) {
217 free(devq, M_DEVBUF);
218 return (NULL);
219 }
220
221 return (devq);
222 }
223
224 int
225 cam_devq_init(struct cam_devq *devq, int devices, int openings)
226 {
227 bzero(devq, sizeof(*devq));
228 if (camq_init(&devq->alloc_queue, devices) != 0) {
229 return (1);
230 }
231 if (camq_init(&devq->send_queue, devices) != 0) {
232 camq_fini(&devq->alloc_queue);
233 return (1);
234 }
235 devq->alloc_openings = openings;
236 devq->alloc_active = 0;
237 devq->send_openings = openings;
238 devq->send_active = 0;
239 return (0);
240 }
241
242 void
243 cam_devq_free(struct cam_devq *devq)
244 {
245 camq_fini(&devq->alloc_queue);
246 camq_fini(&devq->send_queue);
247 free(devq, M_DEVBUF);
248 }
249
250 u_int32_t
251 cam_devq_resize(struct cam_devq *camq, int devices)
252 {
253 u_int32_t retval;
254
255 retval = camq_resize(&camq->alloc_queue, devices);
256
257 if (retval == CAM_REQ_CMP)
258 retval = camq_resize(&camq->send_queue, devices);
259
260 return (retval);
261 }
262
263 struct cam_ccbq *
264 cam_ccbq_alloc(int openings)
265 {
266 struct cam_ccbq *ccbq;
267
268 ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_DEVBUF, M_NOWAIT);
269 if (ccbq == NULL) {
270 printf("cam_ccbq_alloc: - cannot malloc!\n");
271 return (NULL);
272 }
273 if (cam_ccbq_init(ccbq, openings) != 0) {
274 free(ccbq, M_DEVBUF);
275 return (NULL);
276 }
277
278 return (ccbq);
279 }
280
281 void
282 cam_ccbq_free(struct cam_ccbq *ccbq)
283 {
284 if (ccbq) {
285 camq_fini(&ccbq->queue);
286 free(ccbq, M_DEVBUF);
287 }
288 }
289
290 u_int32_t
291 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
292 {
293 int delta;
294 int space_left;
295
296 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
297 space_left = new_size
298 - ccbq->queue.entries
299 - ccbq->held
300 - ccbq->dev_active;
301
302 /*
303 * Only attempt to change the underlying queue size if we are
304 * shrinking it and there is space for all outstanding entries
305 * in the new array or we have been requested to grow the array.
306 * We don't fail in the case where we can't reduce the array size,
307 * but clients that care that the queue be "garbage collected"
308 * should detect this condition and call us again with the
309 * same size once the outstanding entries have been processed.
310 */
311 if (space_left < 0
312 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
313 ccbq->devq_openings += delta;
314 ccbq->dev_openings += delta;
315 return (CAM_REQ_CMP);
316 } else {
317 return (CAM_RESRC_UNAVAIL);
318 }
319 }
320
321 int
322 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
323 {
324 bzero(ccbq, sizeof(*ccbq));
325 if (camq_init(&ccbq->queue, openings) != 0) {
326 return (1);
327 }
328 ccbq->devq_openings = openings;
329 ccbq->dev_openings = openings;
330 TAILQ_INIT(&ccbq->active_ccbs);
331 return (0);
332 }
333
334 /*
335 * Heap routines for manipulating CAM queues.
336 */
337 /*
338 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
339 * and j, return less than 0, 0, or greater than 0 if i is less than,
340 * equal too, or greater than j respectively.
341 */
342 static __inline int
343 queue_cmp(cam_pinfo **queue_array, int i, int j)
344 {
345 if (queue_array[i]->priority == queue_array[j]->priority)
346 return ( queue_array[i]->generation
347 - queue_array[j]->generation );
348 else
349 return ( queue_array[i]->priority
350 - queue_array[j]->priority );
351 }
352
353 /*
354 * swap: Given an array of cam_pinfo* elements and indexes i and j,
355 * exchange elements i and j.
356 */
357 static __inline void
358 swap(cam_pinfo **queue_array, int i, int j)
359 {
360 cam_pinfo *temp_qentry;
361
362 temp_qentry = queue_array[j];
363 queue_array[j] = queue_array[i];
364 queue_array[i] = temp_qentry;
365 queue_array[j]->index = j;
366 queue_array[i]->index = i;
367 }
368
369 /*
370 * heap_up: Given an array of cam_pinfo* elements with the
371 * Heap(1, new_index-1) property and a new element in location
372 * new_index, output Heap(1, new_index).
373 */
374 static void
375 heap_up(cam_pinfo **queue_array, int new_index)
376 {
377 int child;
378 int parent;
379
380 child = new_index;
381
382 while (child != 1) {
383
384 parent = child >> 1;
385 if (queue_cmp(queue_array, parent, child) <= 0)
386 break;
387 swap(queue_array, parent, child);
388 child = parent;
389 }
390 }
391
392 /*
393 * heap_down: Given an array of cam_pinfo* elements with the
394 * Heap(index + 1, num_entries) property with index containing
395 * an unsorted entry, output Heap(index, num_entries).
396 */
397 static void
398 heap_down(cam_pinfo **queue_array, int index, int num_entries)
399 {
400 int child;
401 int parent;
402
403 parent = index;
404 child = parent << 1;
405 for (; child <= num_entries; child = parent << 1) {
406
407 if (child < num_entries) {
408 /* child+1 is the right child of parent */
409 if (queue_cmp(queue_array, child + 1, child) < 0)
410 child++;
411 }
412 /* child is now the least child of parent */
413 if (queue_cmp(queue_array, parent, child) <= 0)
414 break;
415 swap(queue_array, child, parent);
416 parent = child;
417 }
418 }
Cache object: f9986d155f3eb3461a8cc17e9bb86da7
|