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 $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33 #include <sys/malloc.h>
36 #include <cam/cam_ccb.h>
37 #include <cam/cam_queue.h>
38 #include <cam/cam_debug.h>
41 queue_cmp(cam_pinfo **queue_array, int i, int j);
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,
53 camq = (struct camq *)malloc(sizeof(*camq), M_DEVBUF, M_NOWAIT);
55 if (camq_init(camq, size) != 0) {
64 camq_init(struct camq *camq, int size)
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*),
71 if (camq->queue_array == NULL) {
72 printf("camq_init: - cannot malloc array!\n");
76 * Heap algorithms like everything numbered from 1, so
77 * offset our pointer into the heap array by one element.
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.
91 camq_free(struct camq *queue)
95 free(queue, M_DEVBUF);
100 camq_fini(struct camq *queue)
102 if (queue->queue_array != NULL) {
104 * Heap algorithms like everything numbered from 1, so
105 * our pointer into the heap array is offset by one element.
107 queue->queue_array++;
108 free(queue->queue_array, M_DEVBUF);
113 camq_resize(struct camq *queue, int new_size)
115 cam_pinfo **new_array;
118 if (new_size < queue->entries)
119 panic("camq_resize: New queue size can't accomodate "
122 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
124 if (new_array == NULL) {
125 /* Couldn't satisfy request */
126 return (CAM_RESRC_UNAVAIL);
129 * Heap algorithms like everything numbered from 1, so
130 * remember that our pointer into the heap array is offset
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);
139 queue->queue_array = new_array-1;
140 queue->array_size = new_size;
141 return (CAM_REQ_CMP);
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.
150 camq_insert(struct camq *queue, cam_pinfo *new_entry)
153 if (queue->entries >= queue->array_size)
154 panic("camq_insert: Attempt to insert into a full queue");
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);
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)
170 camq_remove(struct camq *queue, int index)
172 cam_pinfo *removed_entry;
174 if (index == 0 || index > queue->entries)
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 removed_entry->index = CAM_UNQUEUED_INDEX;
184 return (removed_entry);
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 an new priority for the element at index, change the priority of
191 * element index and restore the Heap(0, num_elements) property.
194 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
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);
200 /* new_priority <= old_priority */
201 queue->queue_array[index]->priority = new_priority;
202 heap_up(queue->queue_array, index);
207 cam_devq_alloc(int devices, int openings)
209 struct cam_devq *devq;
211 devq = (struct cam_devq *)malloc(sizeof(*devq), M_DEVBUF, M_NOWAIT);
213 printf("cam_devq_alloc: - cannot malloc!\n");
216 if (cam_devq_init(devq, devices, openings) != 0) {
217 free(devq, M_DEVBUF);
225 cam_devq_init(struct cam_devq *devq, int devices, int openings)
227 bzero(devq, sizeof(*devq));
228 if (camq_init(&devq->alloc_queue, devices) != 0) {
231 if (camq_init(&devq->send_queue, devices) != 0) {
232 camq_fini(&devq->alloc_queue);
235 devq->alloc_openings = openings;
236 devq->alloc_active = 0;
237 devq->send_openings = openings;
238 devq->send_active = 0;
243 cam_devq_free(struct cam_devq *devq)
245 camq_fini(&devq->alloc_queue);
246 camq_fini(&devq->send_queue);
247 free(devq, M_DEVBUF);
251 cam_devq_resize(struct cam_devq *camq, int devices)
255 retval = camq_resize(&camq->alloc_queue, devices);
257 if (retval == CAM_REQ_CMP)
258 retval = camq_resize(&camq->send_queue, devices);
264 cam_ccbq_alloc(int openings)
266 struct cam_ccbq *ccbq;
268 ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_DEVBUF, M_NOWAIT);
270 printf("cam_ccbq_alloc: - cannot malloc!\n");
273 if (cam_ccbq_init(ccbq, openings) != 0) {
274 free(ccbq, M_DEVBUF);
282 cam_ccbq_free(struct cam_ccbq *ccbq)
285 camq_fini(&ccbq->queue);
286 free(ccbq, M_DEVBUF);
291 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
296 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
297 space_left = new_size
298 - ccbq->queue.entries
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.
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);
317 return (CAM_RESRC_UNAVAIL);
322 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
324 bzero(ccbq, sizeof(*ccbq));
325 if (camq_init(&ccbq->queue, openings) != 0) {
328 ccbq->devq_openings = openings;
329 ccbq->dev_openings = openings;
330 TAILQ_INIT(&ccbq->active_ccbs);
335 * Heap routines for manipulating CAM queues.
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.
343 queue_cmp(cam_pinfo **queue_array, int i, int j)
345 if (queue_array[i]->priority == queue_array[j]->priority)
346 return ( queue_array[i]->generation
347 - queue_array[j]->generation );
349 return ( queue_array[i]->priority
350 - queue_array[j]->priority );
354 * swap: Given an array of cam_pinfo* elements and indexes i and j,
355 * exchange elements i and j.
358 swap(cam_pinfo **queue_array, int i, int j)
360 cam_pinfo *temp_qentry;
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;
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).
375 heap_up(cam_pinfo **queue_array, int new_index)
385 if (queue_cmp(queue_array, parent, child) <= 0)
387 swap(queue_array, parent, child);
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).
398 heap_down(cam_pinfo **queue_array, int index, int num_entries)
405 for (; child <= num_entries; child = parent << 1) {
407 if (child < num_entries) {
408 /* child+1 is the right child of parent */
409 if (queue_cmp(queue_array, child + 1, child) < 0)
412 /* child is now the least child of parent */
413 if (queue_cmp(queue_array, parent, child) <= 0)
415 swap(queue_array, child, parent);