1 /*-
2 * CAM request queue management functions.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
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.
15 *
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.
27 *
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 $
30 */
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>
36
37 #include "cam.h"
38 #include "cam_ccb.h"
39 #include "cam_queue.h"
40 #include "cam_debug.h"
41
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");
45
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);
53
54 struct camq *
camq_alloc(int size)55 camq_alloc(int size)
56 {
57 struct camq *camq;
58
59 camq = kmalloc(sizeof(*camq), M_CAMQ, M_INTWAIT);
60 camq_init(camq, size);
61 return (camq);
62 }
63
64 int
camq_init(struct camq * camq,int size)65 camq_init(struct camq *camq, int size)
66 {
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);
72 /*
73 * Heap algorithms like everything numbered from 1, so
74 * offset our pointer into the heap array by one element.
75 *
76 * XXX this is a really dumb idea.
77 */
78 camq->queue_array--;
79 }
80 return (0);
81 }
82
83 /*
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.
88 */
89 void
camq_free(struct camq * queue)90 camq_free(struct camq *queue)
91 {
92 if (queue != NULL) {
93 camq_fini(queue);
94 kfree(queue, M_CAMQ);
95 }
96 }
97
98 void
camq_fini(struct camq * queue)99 camq_fini(struct camq *queue)
100 {
101 if (queue->queue_array != NULL) {
102 /*
103 * Heap algorithms like everything numbered from 1, so
104 * our pointer into the heap array is offset by one element.
105 */
106 queue->queue_array++;
107 kfree(queue->queue_array, M_CAMQ);
108 }
109 }
110
111 u_int32_t
camq_resize(struct camq * queue,int new_size)112 camq_resize(struct camq *queue, int new_size)
113 {
114 cam_pinfo **new_array;
115
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);
123
124 /*
125 * Heap algorithms like everything numbered from 1, so
126 * remember that our pointer into the heap array is offset
127 * by one element.
128 */
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);
134 }
135 queue->queue_array = new_array-1;
136 queue->array_size = new_size;
137 return (CAM_REQ_CMP);
138 }
139
140 /*
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.
144 */
145 void
camq_insert(struct camq * queue,cam_pinfo * new_entry)146 camq_insert(struct camq *queue, cam_pinfo *new_entry)
147 {
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);
157 }
158
159 /*
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.
164 *
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.
168 */
169 cam_pinfo *
camq_remove(struct camq * queue,int index)170 camq_remove(struct camq *queue, int index)
171 {
172 cam_pinfo *removed_entry;
173
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);
181 }
182 queue->queue_array[queue->entries] = NULL;
183 removed_entry->index = CAM_UNQUEUED_INDEX;
184 queue->entries--;
185 return (removed_entry);
186 }
187
188 /*
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.
193 */
194 void
camq_change_priority(struct camq * queue,int index,u_int32_t new_priority)195 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
196 {
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);
204 }
205 }
206
207 struct cam_devq *
cam_devq_alloc(int devices,int openings)208 cam_devq_alloc(int devices, int openings)
209 {
210 struct cam_devq *devq;
211
212 devq = kmalloc(sizeof(*devq), M_CAMDEVQ, M_INTWAIT);
213 cam_devq_init(devq, devices, openings);
214 return (devq);
215 }
216
217 int
cam_devq_init(struct cam_devq * devq,int devices,int openings)218 cam_devq_init(struct cam_devq *devq, int devices, int openings)
219 {
220 bzero(devq, sizeof(*devq));
221 camq_init(&devq->alloc_queue, devices);
222 camq_init(&devq->send_queue, devices);
223 devq->max_openings = openings;
224 devq->alloc_openings = openings;
225 devq->alloc_active = 0;
226 devq->send_openings = openings;
227 devq->send_active = 0;
228 devq->refcount = 1;
229 return (0);
230 }
231
232 void
cam_devq_set_openings(struct cam_devq * devq,int openings)233 cam_devq_set_openings(struct cam_devq *devq, int openings)
234 {
235 int delta = openings - devq->max_openings;
236 devq->alloc_openings += delta;
237 devq->send_openings += delta;
238 devq->max_openings = openings;
239 }
240
241 void
cam_devq_reference(struct cam_devq * devq)242 cam_devq_reference(struct cam_devq *devq)
243 {
244 ++devq->refcount;
245 }
246
247 void
cam_devq_release(struct cam_devq * devq)248 cam_devq_release(struct cam_devq *devq)
249 {
250 if (--devq->refcount == 0) {
251 if (devq->alloc_active || devq->send_active)
252 kprintf("cam_devq_release: WARNING active allocations %d active send %d!\n", devq->alloc_active, devq->send_active);
253 camq_fini(&devq->alloc_queue);
254 camq_fini(&devq->send_queue);
255 kfree(devq, M_CAMDEVQ);
256 }
257 }
258
259 u_int32_t
cam_devq_resize(struct cam_devq * camq,int devices)260 cam_devq_resize(struct cam_devq *camq, int devices)
261 {
262 u_int32_t retval;
263
264 retval = camq_resize(&camq->alloc_queue, devices);
265
266 if (retval == CAM_REQ_CMP)
267 retval = camq_resize(&camq->send_queue, devices);
268
269 return (retval);
270 }
271
272 struct cam_ccbq *
cam_ccbq_alloc(int openings)273 cam_ccbq_alloc(int openings)
274 {
275 struct cam_ccbq *ccbq;
276
277 ccbq = kmalloc(sizeof(*ccbq), M_CAMCCBQ, M_INTWAIT);
278 cam_ccbq_init(ccbq, openings);
279 return (ccbq);
280 }
281
282 void
cam_ccbq_free(struct cam_ccbq * ccbq)283 cam_ccbq_free(struct cam_ccbq *ccbq)
284 {
285 if (ccbq) {
286 camq_fini(&ccbq->queue);
287 kfree(ccbq, M_CAMCCBQ);
288 }
289 }
290
291 u_int32_t
cam_ccbq_resize(struct cam_ccbq * ccbq,int new_size)292 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
293 {
294 int delta;
295 int space_left;
296
297 delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
298 space_left = new_size
299 - ccbq->queue.entries
300 - ccbq->held
301 - ccbq->dev_active;
302
303 /*
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.
311 */
312 if (space_left < 0
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);
317 } else {
318 return (CAM_RESRC_UNAVAIL);
319 }
320 }
321
322 int
cam_ccbq_init(struct cam_ccbq * ccbq,int openings)323 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
324 {
325 bzero(ccbq, sizeof(*ccbq));
326 camq_init(&ccbq->queue, openings);
327 ccbq->devq_openings = openings;
328 ccbq->dev_openings = openings;
329 TAILQ_INIT(&ccbq->active_ccbs);
330 return (0);
331 }
332
333 /*
334 * Heap routines for manipulating CAM queues.
335 */
336 /*
337 * queue_cmp: Given an array of cam_pinfo* elements and indexes i
338 * and j, return less than 0, 0, or greater than 0 if i is less than,
339 * equal too, or greater than j respectively.
340 */
341 static __inline int
queue_cmp(cam_pinfo ** queue_array,int i,int j)342 queue_cmp(cam_pinfo **queue_array, int i, int j)
343 {
344 if (queue_array[i]->priority == queue_array[j]->priority)
345 return ( queue_array[i]->generation
346 - queue_array[j]->generation );
347 else
348 return ( queue_array[i]->priority
349 - queue_array[j]->priority );
350 }
351
352 /*
353 * swap: Given an array of cam_pinfo* elements and indexes i and j,
354 * exchange elements i and j.
355 */
356 static __inline void
swap(cam_pinfo ** queue_array,int i,int j)357 swap(cam_pinfo **queue_array, int i, int j)
358 {
359 cam_pinfo *temp_qentry;
360
361 temp_qentry = queue_array[j];
362 queue_array[j] = queue_array[i];
363 queue_array[i] = temp_qentry;
364 queue_array[j]->index = j;
365 queue_array[i]->index = i;
366 }
367
368 /*
369 * heap_up: Given an array of cam_pinfo* elements with the
370 * Heap(1, new_index-1) property and a new element in location
371 * new_index, output Heap(1, new_index).
372 */
373 static void
heap_up(cam_pinfo ** queue_array,int new_index)374 heap_up(cam_pinfo **queue_array, int new_index)
375 {
376 int child;
377 int parent;
378
379 child = new_index;
380
381 while (child != 1) {
382
383 parent = child >> 1;
384 if (queue_cmp(queue_array, parent, child) <= 0)
385 break;
386 swap(queue_array, parent, child);
387 child = parent;
388 }
389 }
390
391 /*
392 * heap_down: Given an array of cam_pinfo* elements with the
393 * Heap(index + 1, num_entries) property with index containing
394 * an unsorted entry, output Heap(index, num_entries).
395 */
396 static void
heap_down(cam_pinfo ** queue_array,int index,int num_entries)397 heap_down(cam_pinfo **queue_array, int index, int num_entries)
398 {
399 int child;
400 int parent;
401
402 parent = index;
403 child = parent << 1;
404 for (; child <= num_entries; child = parent << 1) {
405
406 if (child < num_entries) {
407 /* child+1 is the right child of parent */
408 if (queue_cmp(queue_array, child + 1, child) < 0)
409 child++;
410 }
411 /* child is now the least child of parent */
412 if (queue_cmp(queue_array, parent, child) <= 0)
413 break;
414 swap(queue_array, child, parent);
415 parent = child;
416 }
417 }
418