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.5 1999/08/28 00:40:41 peter Exp $
29 * $DragonFly: src/sys/bus/cam/cam_queue.c,v 1.3 2003/08/07 21:16:44 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/malloc.h>
38 #include "cam_queue.h"
39 #include "cam_debug.h"
42 queue_cmp(cam_pinfo **queue_array, int i, int j);
44 swap(cam_pinfo **queue_array, int i, int j);
45 static void heap_up(cam_pinfo **queue_array, int new_index);
46 static void heap_down(cam_pinfo **queue_array, int index,
54 camq = (struct camq *)malloc(sizeof(*camq), M_DEVBUF, M_NOWAIT);
56 if (camq_init(camq, size) != 0) {
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 = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*),
72 if (camq->queue_array == NULL) {
73 printf("camq_init: - cannot malloc array!\n");
77 * Heap algorithms like everything numbered from 1, so
78 * offset our pointer into the heap array by one element.
86 * Free a camq structure. This should only be called if a controller
87 * driver failes somehow during its attach routine or is unloaded and has
88 * obtained a camq structure. The XPT should ensure that the queue
89 * is empty before calling this routine.
92 camq_free(struct camq *queue)
96 free(queue, M_DEVBUF);
101 camq_fini(struct camq *queue)
103 if (queue->queue_array != NULL) {
105 * Heap algorithms like everything numbered from 1, so
106 * our pointer into the heap array is offset by one element.
108 queue->queue_array++;
109 free(queue->queue_array, M_DEVBUF);
114 camq_resize(struct camq *queue, int new_size)
116 cam_pinfo **new_array;
119 if (new_size < queue->entries)
120 panic("camq_resize: New queue size can't accomodate "
123 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
125 if (new_array == NULL) {
126 /* Couldn't satisfy request */
127 return (CAM_RESRC_UNAVAIL);
130 * Heap algorithms like everything numbered from 1, so
131 * remember that our pointer into the heap array is offset
134 if (queue->queue_array != NULL) {
135 queue->queue_array++;
136 bcopy(queue->queue_array, new_array,
137 queue->entries * sizeof(cam_pinfo *));
138 free(queue->queue_array, M_DEVBUF);
140 queue->queue_array = new_array-1;
141 queue->array_size = new_size;
142 return (CAM_REQ_CMP);
146 * camq_insert: Given an array of cam_pinfo* elememnts with
147 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
148 * output Heap(1, num_elements+1) including new_entry in the array.
151 camq_insert(struct camq *queue, cam_pinfo *new_entry)
154 if (queue->entries >= queue->array_size)
155 panic("camq_insert: Attempt to insert into a full queue");
158 queue->queue_array[queue->entries] = new_entry;
159 new_entry->index = queue->entries;
160 if (queue->entries != 0)
161 heap_up(queue->queue_array, queue->entries);
165 * camq_remove: Given an array of cam_pinfo* elevements with the
166 * Heap(1, num_elements) property and an index such that 1 <= index <=
167 * num_elements, remove that entry and restore the Heap(1, num_elements-1)
171 camq_remove(struct camq *queue, int index)
173 cam_pinfo *removed_entry;
175 if (index == 0 || index > queue->entries)
177 removed_entry = queue->queue_array[index];
178 if (queue->entries != index) {
179 queue->queue_array[index] = queue->queue_array[queue->entries];
180 queue->queue_array[index]->index = index;
181 heap_down(queue->queue_array, index, queue->entries - 1);
183 removed_entry->index = CAM_UNQUEUED_INDEX;
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 an new priority for the element at index, change the priority of
192 * element index and restore the Heap(0, num_elements) property.
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);
201 /* new_priority <= old_priority */
202 queue->queue_array[index]->priority = new_priority;
203 heap_up(queue->queue_array, index);
208 cam_devq_alloc(int devices, int openings)
210 struct cam_devq *devq;
212 devq = (struct cam_devq *)malloc(sizeof(*devq), M_DEVBUF, M_NOWAIT);
214 printf("cam_devq_alloc: - cannot malloc!\n");
217 if (cam_devq_init(devq, devices, openings) != 0) {
218 free(devq, M_DEVBUF);
226 cam_devq_init(struct cam_devq *devq, int devices, int openings)
228 bzero(devq, sizeof(*devq));
229 if (camq_init(&devq->alloc_queue, devices) != 0) {
232 if (camq_init(&devq->send_queue, devices) != 0) {
233 camq_fini(&devq->alloc_queue);
236 devq->alloc_openings = openings;
237 devq->alloc_active = 0;
238 devq->send_openings = openings;
239 devq->send_active = 0;
244 cam_devq_free(struct cam_devq *devq)
246 camq_fini(&devq->alloc_queue);
247 camq_fini(&devq->send_queue);
248 free(devq, M_DEVBUF);
252 cam_devq_resize(struct cam_devq *camq, int devices)
256 retval = camq_resize(&camq->alloc_queue, devices);
258 if (retval == CAM_REQ_CMP)
259 retval = camq_resize(&camq->send_queue, devices);
265 cam_ccbq_alloc(int openings)
267 struct cam_ccbq *ccbq;
269 ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_DEVBUF, M_NOWAIT);
271 printf("cam_ccbq_alloc: - cannot malloc!\n");
274 if (cam_ccbq_init(ccbq, openings) != 0) {
275 free(ccbq, M_DEVBUF);
283 cam_ccbq_free(struct cam_ccbq *ccbq)
286 camq_fini(&ccbq->queue);
287 free(ccbq, M_DEVBUF);
292 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
297 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
298 space_left = new_size
299 - ccbq->queue.entries
304 * Only attempt to change the underlying queue size if we are
305 * shrinking it and there is space for all outstanding entries
306 * in the new array or we have been requested to grow the array.
307 * We don't fail in the case where we can't reduce the array size,
308 * but clients that care that the queue be "garbage collected"
309 * should detect this condition and call us again with the
310 * same size once the outstanding entries have been processed.
313 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
314 ccbq->devq_openings += delta;
315 ccbq->dev_openings += delta;
316 return (CAM_REQ_CMP);
318 return (CAM_RESRC_UNAVAIL);
323 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
325 bzero(ccbq, sizeof(*ccbq));
326 if (camq_init(&ccbq->queue, openings) != 0) {
329 ccbq->devq_openings = openings;
330 ccbq->dev_openings = openings;
331 TAILQ_INIT(&ccbq->active_ccbs);
336 * Heap routines for manipulating CAM queues.
339 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
340 * and j, return less than 0, 0, or greater than 0 if i is less than,
341 * equal too, or greater than j respectively.
344 queue_cmp(cam_pinfo **queue_array, int i, int j)
346 if (queue_array[i]->priority == queue_array[j]->priority)
347 return ( queue_array[i]->generation
348 - queue_array[j]->generation );
350 return ( queue_array[i]->priority
351 - queue_array[j]->priority );
355 * swap: Given an array of cam_pinfo* elements and indexes i and j,
356 * exchange elements i and j.
359 swap(cam_pinfo **queue_array, int i, int j)
361 cam_pinfo *temp_qentry;
363 temp_qentry = queue_array[j];
364 queue_array[j] = queue_array[i];
365 queue_array[i] = temp_qentry;
366 queue_array[j]->index = j;
367 queue_array[i]->index = i;
371 * heap_up: Given an array of cam_pinfo* elements with the
372 * Heap(1, new_index-1) property and a new element in location
373 * new_index, output Heap(1, new_index).
376 heap_up(cam_pinfo **queue_array, int new_index)
386 if (queue_cmp(queue_array, parent, child) <= 0)
388 swap(queue_array, parent, child);
394 * heap_down: Given an array of cam_pinfo* elements with the
395 * Heap(index + 1, num_entries) property with index containing
396 * an unsorted entry, output Heap(index, num_entries).
399 heap_down(cam_pinfo **queue_array, int index, int num_entries)
406 for (; child <= num_entries; child = parent << 1) {
408 if (child < num_entries) {
409 /* child+1 is the right child of parent */
410 if (queue_cmp(queue_array, child + 1, child) < 0)
413 /* child is now the least child of parent */
414 if (queue_cmp(queue_array, parent, child) <= 0)
416 swap(queue_array, child, parent);