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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <cam/cam_ccb.h>
40 #include <cam/cam_queue.h>
41 #include <cam/cam_debug.h>
43 MALLOC_DEFINE(M_CAMQ, "CAM queue", "CAM queue buffers");
44 MALLOC_DEFINE(M_CAMDEVQ, "CAM dev queue", "CAM dev queue buffers");
45 MALLOC_DEFINE(M_CAMCCBQ, "CAM ccb queue", "CAM ccb queue buffers");
48 queue_cmp(cam_pinfo **queue_array, int i, int j);
50 swap(cam_pinfo **queue_array, int i, int j);
51 static void heap_up(cam_pinfo **queue_array, int new_index);
52 static void heap_down(cam_pinfo **queue_array, int index,
60 camq = (struct camq *)malloc(sizeof(*camq), M_CAMQ, M_NOWAIT);
62 if (camq_init(camq, size) != 0) {
71 camq_init(struct camq *camq, int size)
73 bzero(camq, sizeof(*camq));
74 camq->array_size = size;
75 if (camq->array_size != 0) {
76 camq->queue_array = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*),
78 if (camq->queue_array == NULL) {
79 printf("camq_init: - cannot malloc array!\n");
83 * Heap algorithms like everything numbered from 1, so
84 * offset our pointer into the heap array by one element.
92 * Free a camq structure. This should only be called if a controller
93 * driver failes somehow during its attach routine or is unloaded and has
94 * obtained a camq structure. The XPT should ensure that the queue
95 * is empty before calling this routine.
98 camq_free(struct camq *queue)
107 camq_fini(struct camq *queue)
109 if (queue->queue_array != NULL) {
111 * Heap algorithms like everything numbered from 1, so
112 * our pointer into the heap array is offset by one element.
114 queue->queue_array++;
115 free(queue->queue_array, M_CAMQ);
120 camq_resize(struct camq *queue, int new_size)
122 cam_pinfo **new_array;
125 if (new_size < queue->entries)
126 panic("camq_resize: New queue size can't accomodate "
129 new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
131 if (new_array == NULL) {
132 /* Couldn't satisfy request */
133 return (CAM_RESRC_UNAVAIL);
136 * Heap algorithms like everything numbered from 1, so
137 * remember that our pointer into the heap array is offset
140 if (queue->queue_array != NULL) {
141 queue->queue_array++;
142 bcopy(queue->queue_array, new_array,
143 queue->entries * sizeof(cam_pinfo *));
144 free(queue->queue_array, M_CAMQ);
146 queue->queue_array = new_array-1;
147 queue->array_size = new_size;
148 return (CAM_REQ_CMP);
152 * camq_insert: Given an array of cam_pinfo* elememnts with
153 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
154 * output Heap(1, num_elements+1) including new_entry in the array.
157 camq_insert(struct camq *queue, cam_pinfo *new_entry)
160 if (queue->entries >= queue->array_size)
161 panic("camq_insert: Attempt to insert into a full queue");
164 queue->queue_array[queue->entries] = new_entry;
165 new_entry->index = queue->entries;
166 if (queue->entries != 0)
167 heap_up(queue->queue_array, queue->entries);
171 * camq_remove: Given an array of cam_pinfo* elevements with the
172 * Heap(1, num_elements) property and an index such that 1 <= index <=
173 * num_elements, remove that entry and restore the Heap(1, num_elements-1)
177 camq_remove(struct camq *queue, int index)
179 cam_pinfo *removed_entry;
181 if (index == 0 || index > queue->entries)
183 removed_entry = queue->queue_array[index];
184 if (queue->entries != index) {
185 queue->queue_array[index] = queue->queue_array[queue->entries];
186 queue->queue_array[index]->index = index;
187 heap_down(queue->queue_array, index, queue->entries - 1);
189 removed_entry->index = CAM_UNQUEUED_INDEX;
191 return (removed_entry);
195 * camq_change_priority: Given an array of cam_pinfo* elements with the
196 * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
197 * and a new priority for the element at index, change the priority of
198 * element index and restore the Heap(0, num_elements) property.
201 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
203 if (new_priority > queue->queue_array[index]->priority) {
204 queue->queue_array[index]->priority = new_priority;
205 heap_down(queue->queue_array, index, queue->entries);
207 /* new_priority <= old_priority */
208 queue->queue_array[index]->priority = new_priority;
209 heap_up(queue->queue_array, index);
214 cam_devq_alloc(int devices, int openings)
216 struct cam_devq *devq;
218 devq = (struct cam_devq *)malloc(sizeof(*devq), M_CAMDEVQ, M_NOWAIT);
220 printf("cam_devq_alloc: - cannot malloc!\n");
223 if (cam_devq_init(devq, devices, openings) != 0) {
224 free(devq, M_CAMDEVQ);
232 cam_devq_init(struct cam_devq *devq, int devices, int openings)
234 bzero(devq, sizeof(*devq));
235 if (camq_init(&devq->alloc_queue, devices) != 0) {
238 if (camq_init(&devq->send_queue, devices) != 0) {
239 camq_fini(&devq->alloc_queue);
242 devq->alloc_openings = openings;
243 devq->alloc_active = 0;
244 devq->send_openings = openings;
245 devq->send_active = 0;
250 cam_devq_free(struct cam_devq *devq)
252 camq_fini(&devq->alloc_queue);
253 camq_fini(&devq->send_queue);
254 free(devq, M_CAMDEVQ);
258 cam_devq_resize(struct cam_devq *camq, int devices)
262 retval = camq_resize(&camq->alloc_queue, devices);
264 if (retval == CAM_REQ_CMP)
265 retval = camq_resize(&camq->send_queue, devices);
271 cam_ccbq_alloc(int openings)
273 struct cam_ccbq *ccbq;
275 ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_CAMCCBQ, M_NOWAIT);
277 printf("cam_ccbq_alloc: - cannot malloc!\n");
280 if (cam_ccbq_init(ccbq, openings) != 0) {
281 free(ccbq, M_CAMCCBQ);
289 cam_ccbq_free(struct cam_ccbq *ccbq)
292 camq_fini(&ccbq->queue);
293 free(ccbq, M_CAMCCBQ);
298 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
303 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
304 space_left = new_size
305 - ccbq->queue.entries
310 * Only attempt to change the underlying queue size if we are
311 * shrinking it and there is space for all outstanding entries
312 * in the new array or we have been requested to grow the array.
313 * We don't fail in the case where we can't reduce the array size,
314 * but clients that care that the queue be "garbage collected"
315 * should detect this condition and call us again with the
316 * same size once the outstanding entries have been processed.
319 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
320 ccbq->devq_openings += delta;
321 ccbq->dev_openings += delta;
322 return (CAM_REQ_CMP);
324 return (CAM_RESRC_UNAVAIL);
329 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
331 bzero(ccbq, sizeof(*ccbq));
332 if (camq_init(&ccbq->queue, openings) != 0) {
335 ccbq->devq_openings = openings;
336 ccbq->dev_openings = openings;
337 TAILQ_INIT(&ccbq->active_ccbs);
342 * Heap routines for manipulating CAM queues.
345 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
346 * and j, return less than 0, 0, or greater than 0 if i is less than,
347 * equal too, or greater than j respectively.
350 queue_cmp(cam_pinfo **queue_array, int i, int j)
352 if (queue_array[i]->priority == queue_array[j]->priority)
353 return ( queue_array[i]->generation
354 - queue_array[j]->generation );
356 return ( queue_array[i]->priority
357 - queue_array[j]->priority );
361 * swap: Given an array of cam_pinfo* elements and indexes i and j,
362 * exchange elements i and j.
365 swap(cam_pinfo **queue_array, int i, int j)
367 cam_pinfo *temp_qentry;
369 temp_qentry = queue_array[j];
370 queue_array[j] = queue_array[i];
371 queue_array[i] = temp_qentry;
372 queue_array[j]->index = j;
373 queue_array[i]->index = i;
377 * heap_up: Given an array of cam_pinfo* elements with the
378 * Heap(1, new_index-1) property and a new element in location
379 * new_index, output Heap(1, new_index).
382 heap_up(cam_pinfo **queue_array, int new_index)
392 if (queue_cmp(queue_array, parent, child) <= 0)
394 swap(queue_array, parent, child);
400 * heap_down: Given an array of cam_pinfo* elements with the
401 * Heap(index + 1, num_entries) property with index containing
402 * an unsorted entry, output Heap(index, num_entries).
405 heap_down(cam_pinfo **queue_array, int index, int num_entries)
412 for (; child <= num_entries; child = parent << 1) {
414 if (child < num_entries) {
415 /* child+1 is the right child of parent */
416 if (queue_cmp(queue_array, child + 1, child) < 0)
419 /* child is now the least child of parent */
420 if (queue_cmp(queue_array, parent, child) <= 0)
422 swap(queue_array, child, parent);