Fix numerous compiler warnings and format conversion specifiers.
[dragonfly.git] / sys / bus / cam / cam_queue.c
blobc2369f4e6d23b266bc3fe1f7d63463825743bbf1
1 /*-
2 * CAM request queue management functions.
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
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.
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.
28 * $FreeBSD: src/sys/cam/cam_queue.c,v 1.9 2005/07/01 15:21:29 avatar Exp $
29 * $DragonFly: src/sys/bus/cam/cam_queue.c,v 1.12 2008/05/18 20:30:19 pavalos Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
37 #include "cam.h"
38 #include "cam_ccb.h"
39 #include "cam_queue.h"
40 #include "cam_debug.h"
42 MALLOC_DEFINE(M_CAMQ, "CAM queue", "CAM queue buffers");
43 MALLOC_DEFINE(M_CAMDEVQ, "CAM dev queue", "CAM dev queue buffers");
44 MALLOC_DEFINE(M_CAMCCBQ, "CAM ccb queue", "CAM ccb queue buffers");
46 static __inline int
47 queue_cmp(cam_pinfo **queue_array, int i, int j);
48 static __inline void
49 swap(cam_pinfo **queue_array, int i, int j);
50 static void heap_up(cam_pinfo **queue_array, int new_index);
51 static void heap_down(cam_pinfo **queue_array, int index,
52 int last_index);
54 struct camq *
55 camq_alloc(int size)
57 struct camq *camq;
59 camq = kmalloc(sizeof(*camq), M_CAMQ, M_INTWAIT);
60 camq_init(camq, size);
61 return (camq);
64 int
65 camq_init(struct camq *camq, int size)
67 bzero(camq, sizeof(*camq));
68 camq->array_size = size;
69 if (camq->array_size != 0) {
70 camq->queue_array = kmalloc(size * sizeof(cam_pinfo *),
71 M_CAMQ, M_INTWAIT | M_ZERO);
73 * Heap algorithms like everything numbered from 1, so
74 * offset our pointer into the heap array by one element.
76 * XXX this is a really dumb idea.
78 camq->queue_array--;
80 return (0);
84 * Free a camq structure. This should only be called if a controller
85 * driver failes somehow during its attach routine or is unloaded and has
86 * obtained a camq structure. The XPT should ensure that the queue
87 * is empty before calling this routine.
89 void
90 camq_free(struct camq *queue)
92 if (queue != NULL) {
93 camq_fini(queue);
94 kfree(queue, M_CAMQ);
98 void
99 camq_fini(struct camq *queue)
101 if (queue->queue_array != NULL) {
103 * Heap algorithms like everything numbered from 1, so
104 * our pointer into the heap array is offset by one element.
106 queue->queue_array++;
107 kfree(queue->queue_array, M_CAMQ);
111 u_int32_t
112 camq_resize(struct camq *queue, int new_size)
114 cam_pinfo **new_array;
116 #ifdef DIAGNOSTIC
117 if (new_size < queue->entries)
118 panic("camq_resize: New queue size can't accommodate "
119 "queued entries.");
120 #endif
121 new_array = kmalloc(new_size * sizeof(cam_pinfo *), M_CAMQ,
122 M_INTWAIT | M_ZERO);
125 * Heap algorithms like everything numbered from 1, so
126 * remember that our pointer into the heap array is offset
127 * by one element.
129 if (queue->queue_array != NULL) {
130 queue->queue_array++;
131 bcopy(queue->queue_array, new_array,
132 queue->entries * sizeof(cam_pinfo *));
133 kfree(queue->queue_array, M_CAMQ);
135 queue->queue_array = new_array-1;
136 queue->array_size = new_size;
137 return (CAM_REQ_CMP);
141 * camq_insert: Given an array of cam_pinfo* elememnts with
142 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
143 * output Heap(1, num_elements+1) including new_entry in the array.
145 void
146 camq_insert(struct camq *queue, cam_pinfo *new_entry)
148 #ifdef DIAGNOSTIC
149 if (queue->entries >= queue->array_size)
150 panic("camq_insert: Attempt to insert into a full queue");
151 #endif
152 queue->entries++;
153 queue->queue_array[queue->entries] = new_entry;
154 new_entry->index = queue->entries;
155 if (queue->entries != 0)
156 heap_up(queue->queue_array, queue->entries);
160 * camq_remove: Given an array of cam_pinfo* elevements with the
161 * Heap(1, num_elements) property and an index such that 1 <= index <=
162 * num_elements, remove that entry and restore the Heap(1, num_elements-1)
163 * property.
165 * When removing do not leave any junk pointers around in the array.
166 * This also ensures that CAMQ_GET_HEAD() returns NULL if the queue is
167 * empty.
169 cam_pinfo *
170 camq_remove(struct camq *queue, int index)
172 cam_pinfo *removed_entry;
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);
182 queue->queue_array[queue->entries] = NULL;
183 removed_entry->index = CAM_UNQUEUED_INDEX;
184 queue->entries--;
185 return (removed_entry);
189 * camq_change_priority: Given an array of cam_pinfo* elements with the
190 * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
191 * and a new priority for the element at index, change the priority of
192 * element index and restore the Heap(0, num_elements) property.
194 void
195 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
197 if (new_priority > queue->queue_array[index]->priority) {
198 queue->queue_array[index]->priority = new_priority;
199 heap_down(queue->queue_array, index, queue->entries);
200 } else {
201 /* new_priority <= old_priority */
202 queue->queue_array[index]->priority = new_priority;
203 heap_up(queue->queue_array, index);
207 struct cam_devq *
208 cam_devq_alloc(int devices, int openings)
210 struct cam_devq *devq;
212 devq = kmalloc(sizeof(*devq), M_CAMDEVQ, M_INTWAIT);
213 cam_devq_init(devq, devices, openings);
214 return (devq);
218 cam_devq_init(struct cam_devq *devq, int devices, int openings)
220 bzero(devq, sizeof(*devq));
221 camq_init(&devq->alloc_queue, devices);
222 camq_init(&devq->send_queue, devices);
223 devq->alloc_openings = openings;
224 devq->alloc_active = 0;
225 devq->send_openings = openings;
226 devq->send_active = 0;
227 devq->refcount = 1;
228 return (0);
231 void
232 cam_devq_reference(struct cam_devq *devq)
234 ++devq->refcount;
237 void
238 cam_devq_release(struct cam_devq *devq)
240 if (--devq->refcount == 0) {
241 if (devq->alloc_active || devq->send_active)
242 kprintf("cam_devq_release: WARNING active allocations %d active send %d!\n", devq->alloc_active, devq->send_active);
243 camq_fini(&devq->alloc_queue);
244 camq_fini(&devq->send_queue);
245 kfree(devq, M_CAMDEVQ);
249 u_int32_t
250 cam_devq_resize(struct cam_devq *camq, int devices)
252 u_int32_t retval;
254 retval = camq_resize(&camq->alloc_queue, devices);
256 if (retval == CAM_REQ_CMP)
257 retval = camq_resize(&camq->send_queue, devices);
259 return (retval);
262 struct cam_ccbq *
263 cam_ccbq_alloc(int openings)
265 struct cam_ccbq *ccbq;
267 ccbq = kmalloc(sizeof(*ccbq), M_CAMCCBQ, M_INTWAIT);
268 cam_ccbq_init(ccbq, openings);
269 return (ccbq);
272 void
273 cam_ccbq_free(struct cam_ccbq *ccbq)
275 if (ccbq) {
276 camq_fini(&ccbq->queue);
277 kfree(ccbq, M_CAMCCBQ);
281 u_int32_t
282 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
284 int delta;
285 int space_left;
287 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
288 space_left = new_size
289 - ccbq->queue.entries
290 - ccbq->held
291 - ccbq->dev_active;
294 * Only attempt to change the underlying queue size if we are
295 * shrinking it and there is space for all outstanding entries
296 * in the new array or we have been requested to grow the array.
297 * We don't fail in the case where we can't reduce the array size,
298 * but clients that care that the queue be "garbage collected"
299 * should detect this condition and call us again with the
300 * same size once the outstanding entries have been processed.
302 if (space_left < 0
303 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
304 ccbq->devq_openings += delta;
305 ccbq->dev_openings += delta;
306 return (CAM_REQ_CMP);
307 } else {
308 return (CAM_RESRC_UNAVAIL);
313 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
315 bzero(ccbq, sizeof(*ccbq));
316 camq_init(&ccbq->queue, openings);
317 ccbq->devq_openings = openings;
318 ccbq->dev_openings = openings;
319 TAILQ_INIT(&ccbq->active_ccbs);
320 return (0);
324 * Heap routines for manipulating CAM queues.
327 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
328 * and j, return less than 0, 0, or greater than 0 if i is less than,
329 * equal too, or greater than j respectively.
331 static __inline int
332 queue_cmp(cam_pinfo **queue_array, int i, int j)
334 if (queue_array[i]->priority == queue_array[j]->priority)
335 return ( queue_array[i]->generation
336 - queue_array[j]->generation );
337 else
338 return ( queue_array[i]->priority
339 - queue_array[j]->priority );
343 * swap: Given an array of cam_pinfo* elements and indexes i and j,
344 * exchange elements i and j.
346 static __inline void
347 swap(cam_pinfo **queue_array, int i, int j)
349 cam_pinfo *temp_qentry;
351 temp_qentry = queue_array[j];
352 queue_array[j] = queue_array[i];
353 queue_array[i] = temp_qentry;
354 queue_array[j]->index = j;
355 queue_array[i]->index = i;
359 * heap_up: Given an array of cam_pinfo* elements with the
360 * Heap(1, new_index-1) property and a new element in location
361 * new_index, output Heap(1, new_index).
363 static void
364 heap_up(cam_pinfo **queue_array, int new_index)
366 int child;
367 int parent;
369 child = new_index;
371 while (child != 1) {
373 parent = child >> 1;
374 if (queue_cmp(queue_array, parent, child) <= 0)
375 break;
376 swap(queue_array, parent, child);
377 child = parent;
382 * heap_down: Given an array of cam_pinfo* elements with the
383 * Heap(index + 1, num_entries) property with index containing
384 * an unsorted entry, output Heap(index, num_entries).
386 static void
387 heap_down(cam_pinfo **queue_array, int index, int num_entries)
389 int child;
390 int parent;
392 parent = index;
393 child = parent << 1;
394 for (; child <= num_entries; child = parent << 1) {
396 if (child < num_entries) {
397 /* child+1 is the right child of parent */
398 if (queue_cmp(queue_array, child + 1, child) < 0)
399 child++;
401 /* child is now the least child of parent */
402 if (queue_cmp(queue_array, parent, child) <= 0)
403 break;
404 swap(queue_array, child, parent);
405 parent = child;