2 * CAM request queue management functions.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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>
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");
47 queue_cmp(cam_pinfo **queue_array, int i, int j);
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,
59 camq = kmalloc(sizeof(*camq), M_CAMQ, M_INTWAIT);
60 camq_init(camq, size);
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 *),
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.
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.
90 camq_free(struct camq *queue)
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);
112 camq_resize(struct camq *queue, int new_size)
114 cam_pinfo **new_array;
117 if (new_size < queue->entries)
118 panic("camq_resize: New queue size can't accommodate "
121 new_array = kmalloc(new_size * sizeof(cam_pinfo *), M_CAMQ, M_INTWAIT);
124 * Heap algorithms like everything numbered from 1, so
125 * remember that our pointer into the heap array is offset
128 if (queue->queue_array != NULL) {
129 queue->queue_array++;
130 bcopy(queue->queue_array, new_array,
131 queue->entries * sizeof(cam_pinfo *));
132 kfree(queue->queue_array, M_CAMQ);
134 queue->queue_array = new_array-1;
135 queue->array_size = new_size;
136 return (CAM_REQ_CMP);
140 * camq_insert: Given an array of cam_pinfo* elememnts with
141 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
142 * output Heap(1, num_elements+1) including new_entry in the array.
145 camq_insert(struct camq *queue, cam_pinfo *new_entry)
148 if (queue->entries >= queue->array_size)
149 panic("camq_insert: Attempt to insert into a full queue");
152 queue->queue_array[queue->entries] = new_entry;
153 new_entry->index = queue->entries;
154 if (queue->entries != 0)
155 heap_up(queue->queue_array, queue->entries);
159 * camq_remove: Given an array of cam_pinfo* elevements with the
160 * Heap(1, num_elements) property and an index such that 1 <= index <=
161 * num_elements, remove that entry and restore the Heap(1, num_elements-1)
165 camq_remove(struct camq *queue, int index)
167 cam_pinfo *removed_entry;
169 if (index == 0 || index > queue->entries)
171 removed_entry = queue->queue_array[index];
172 if (queue->entries != index) {
173 queue->queue_array[index] = queue->queue_array[queue->entries];
174 queue->queue_array[index]->index = index;
175 heap_down(queue->queue_array, index, queue->entries - 1);
177 removed_entry->index = CAM_UNQUEUED_INDEX;
179 return (removed_entry);
183 * camq_change_priority: Given an array of cam_pinfo* elements with the
184 * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
185 * and a new priority for the element at index, change the priority of
186 * element index and restore the Heap(0, num_elements) property.
189 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
191 if (new_priority > queue->queue_array[index]->priority) {
192 queue->queue_array[index]->priority = new_priority;
193 heap_down(queue->queue_array, index, queue->entries);
195 /* new_priority <= old_priority */
196 queue->queue_array[index]->priority = new_priority;
197 heap_up(queue->queue_array, index);
202 cam_devq_alloc(int devices, int openings)
204 struct cam_devq *devq;
206 devq = kmalloc(sizeof(*devq), M_CAMDEVQ, M_INTWAIT);
207 cam_devq_init(devq, devices, openings);
212 cam_devq_init(struct cam_devq *devq, int devices, int openings)
214 bzero(devq, sizeof(*devq));
215 camq_init(&devq->alloc_queue, devices);
216 camq_init(&devq->send_queue, devices);
217 devq->alloc_openings = openings;
218 devq->alloc_active = 0;
219 devq->send_openings = openings;
220 devq->send_active = 0;
226 cam_devq_reference(struct cam_devq *devq)
232 cam_devq_release(struct cam_devq *devq)
234 if (--devq->refcount == 0) {
235 if (devq->alloc_active || devq->send_active)
236 kprintf("cam_devq_release: WARNING active allocations %d active send %d!\n", devq->alloc_active, devq->send_active);
237 camq_fini(&devq->alloc_queue);
238 camq_fini(&devq->send_queue);
239 kfree(devq, M_CAMDEVQ);
244 cam_devq_resize(struct cam_devq *camq, int devices)
248 retval = camq_resize(&camq->alloc_queue, devices);
250 if (retval == CAM_REQ_CMP)
251 retval = camq_resize(&camq->send_queue, devices);
257 cam_ccbq_alloc(int openings)
259 struct cam_ccbq *ccbq;
261 ccbq = kmalloc(sizeof(*ccbq), M_CAMCCBQ, M_INTWAIT);
262 cam_ccbq_init(ccbq, openings);
267 cam_ccbq_free(struct cam_ccbq *ccbq)
270 camq_fini(&ccbq->queue);
271 kfree(ccbq, M_CAMCCBQ);
276 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
281 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
282 space_left = new_size
283 - ccbq->queue.entries
288 * Only attempt to change the underlying queue size if we are
289 * shrinking it and there is space for all outstanding entries
290 * in the new array or we have been requested to grow the array.
291 * We don't fail in the case where we can't reduce the array size,
292 * but clients that care that the queue be "garbage collected"
293 * should detect this condition and call us again with the
294 * same size once the outstanding entries have been processed.
297 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
298 ccbq->devq_openings += delta;
299 ccbq->dev_openings += delta;
300 return (CAM_REQ_CMP);
302 return (CAM_RESRC_UNAVAIL);
307 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
309 bzero(ccbq, sizeof(*ccbq));
310 camq_init(&ccbq->queue, openings);
311 ccbq->devq_openings = openings;
312 ccbq->dev_openings = openings;
313 TAILQ_INIT(&ccbq->active_ccbs);
318 * Heap routines for manipulating CAM queues.
321 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
322 * and j, return less than 0, 0, or greater than 0 if i is less than,
323 * equal too, or greater than j respectively.
326 queue_cmp(cam_pinfo **queue_array, int i, int j)
328 if (queue_array[i]->priority == queue_array[j]->priority)
329 return ( queue_array[i]->generation
330 - queue_array[j]->generation );
332 return ( queue_array[i]->priority
333 - queue_array[j]->priority );
337 * swap: Given an array of cam_pinfo* elements and indexes i and j,
338 * exchange elements i and j.
341 swap(cam_pinfo **queue_array, int i, int j)
343 cam_pinfo *temp_qentry;
345 temp_qentry = queue_array[j];
346 queue_array[j] = queue_array[i];
347 queue_array[i] = temp_qentry;
348 queue_array[j]->index = j;
349 queue_array[i]->index = i;
353 * heap_up: Given an array of cam_pinfo* elements with the
354 * Heap(1, new_index-1) property and a new element in location
355 * new_index, output Heap(1, new_index).
358 heap_up(cam_pinfo **queue_array, int new_index)
368 if (queue_cmp(queue_array, parent, child) <= 0)
370 swap(queue_array, parent, child);
376 * heap_down: Given an array of cam_pinfo* elements with the
377 * Heap(index + 1, num_entries) property with index containing
378 * an unsorted entry, output Heap(index, num_entries).
381 heap_down(cam_pinfo **queue_array, int index, int num_entries)
388 for (; child <= num_entries; child = parent << 1) {
390 if (child < num_entries) {
391 /* child+1 is the right child of parent */
392 if (queue_cmp(queue_array, child + 1, child) < 0)
395 /* child is now the least child of parent */
396 if (queue_cmp(queue_array, parent, child) <= 0)
398 swap(queue_array, child, parent);