Fix typo.
[dragonfly.git] / sys / bus / cam / cam_queue.c
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.5 1999/08/28 00:40:41 peter Exp $
29  * $DragonFly: src/sys/bus/cam/cam_queue.c,v 1.10 2007/11/25 02:21:30 pavalos Exp $
30  */
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35
36 #include "cam.h"
37 #include "cam_ccb.h"
38 #include "cam_queue.h"
39 #include "cam_debug.h"
40
41 static __inline int
42                 queue_cmp(cam_pinfo **queue_array, int i, int j);
43 static __inline void
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,
47                           int last_index);
48
49 struct camq *
50 camq_alloc(int size)
51 {
52         struct camq *camq;
53
54         camq = kmalloc(sizeof(*camq), M_DEVBUF, M_INTWAIT);
55         camq_init(camq, size);
56         return (camq);
57 }
58         
59 int
60 camq_init(struct camq *camq, int size)
61 {
62         bzero(camq, sizeof(*camq));
63         camq->array_size = size;
64         if (camq->array_size != 0) {
65                 camq->queue_array = kmalloc(size * sizeof(cam_pinfo *), 
66                                         M_DEVBUF, M_INTWAIT);
67                 /*
68                  * Heap algorithms like everything numbered from 1, so
69                  * offset our pointer into the heap array by one element.
70                  *
71                  * XXX this is a really dumb idea.
72                  */
73                 camq->queue_array--;
74         }
75         return (0);
76 }
77
78 /*
79  * Free a camq structure.  This should only be called if a controller
80  * driver failes somehow during its attach routine or is unloaded and has
81  * obtained a camq structure.  The XPT should ensure that the queue
82  * is empty before calling this routine.
83  */
84 void
85 camq_free(struct camq *queue)
86 {
87         if (queue != NULL) {
88                 camq_fini(queue);
89                 kfree(queue, M_DEVBUF);
90         }
91 }
92
93 void
94 camq_fini(struct camq *queue)
95 {
96         if (queue->queue_array != NULL) {
97                 /*
98                  * Heap algorithms like everything numbered from 1, so
99                  * our pointer into the heap array is offset by one element.
100                  */
101                 queue->queue_array++;
102                 kfree(queue->queue_array, M_DEVBUF);
103         }
104 }
105
106 u_int32_t
107 camq_resize(struct camq *queue, int new_size)
108 {
109         cam_pinfo **new_array;
110
111 #ifdef DIAGNOSTIC
112         if (new_size < queue->entries)
113                 panic("camq_resize: New queue size can't accommodate "
114                       "queued entries.");
115 #endif
116         new_array = kmalloc(new_size * sizeof(cam_pinfo *), M_DEVBUF, M_INTWAIT);
117
118         /*
119          * Heap algorithms like everything numbered from 1, so
120          * remember that our pointer into the heap array is offset
121          * by one element.
122          */
123         if (queue->queue_array != NULL) {
124                 queue->queue_array++;
125                 bcopy(queue->queue_array, new_array,
126                       queue->entries * sizeof(cam_pinfo *));
127                 kfree(queue->queue_array, M_DEVBUF);
128         }
129         queue->queue_array = new_array-1;
130         queue->array_size = new_size;
131         return (CAM_REQ_CMP);
132 }
133
134 /*
135  * camq_insert: Given an array of cam_pinfo* elememnts with
136  * the Heap(1, num_elements) property and array_size - num_elements >= 1,
137  * output Heap(1, num_elements+1) including new_entry in the array.
138  */
139 void
140 camq_insert(struct camq *queue, cam_pinfo *new_entry)
141 {
142 #ifdef DIAGNOSTIC
143         if (queue->entries >= queue->array_size)
144                 panic("camq_insert: Attempt to insert into a full queue");
145 #endif
146         queue->entries++;
147         queue->queue_array[queue->entries] = new_entry;
148         new_entry->index = queue->entries;
149         if (queue->entries != 0)
150                 heap_up(queue->queue_array, queue->entries);
151 }
152
153 /*
154  * camq_remove:  Given an array of cam_pinfo* elevements with the
155  * Heap(1, num_elements) property and an index such that 1 <= index <=
156  * num_elements, remove that entry and restore the Heap(1, num_elements-1)
157  * property.
158  */
159 cam_pinfo *
160 camq_remove(struct camq *queue, int index)
161 {
162         cam_pinfo *removed_entry;
163
164         if (index == 0 || index > queue->entries)
165                 return (NULL);
166         removed_entry = queue->queue_array[index];
167         if (queue->entries != index) {
168                 queue->queue_array[index] = queue->queue_array[queue->entries];
169                 queue->queue_array[index]->index = index;
170                 heap_down(queue->queue_array, index, queue->entries - 1);
171         }
172         removed_entry->index = CAM_UNQUEUED_INDEX;
173         queue->entries--;
174         return (removed_entry);
175 }
176
177 /*
178  * camq_change_priority:  Given an array of cam_pinfo* elements with the
179  * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
180  * and a new priority for the element at index, change the priority of
181  * element index and restore the Heap(0, num_elements) property.
182  */
183 void
184 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
185 {
186         if (new_priority > queue->queue_array[index]->priority) {
187                 queue->queue_array[index]->priority = new_priority;
188                 heap_down(queue->queue_array, index, queue->entries);
189         } else {
190                 /* new_priority <= old_priority */
191                 queue->queue_array[index]->priority = new_priority;
192                 heap_up(queue->queue_array, index);
193         }
194 }
195
196 struct cam_devq *
197 cam_devq_alloc(int devices, int openings)
198 {
199         struct cam_devq *devq;
200
201         devq = kmalloc(sizeof(*devq), M_DEVBUF, M_INTWAIT);
202         cam_devq_init(devq, devices, openings);
203         return (devq);
204 }
205
206 int
207 cam_devq_init(struct cam_devq *devq, int devices, int openings)
208 {
209         bzero(devq, sizeof(*devq));
210         camq_init(&devq->alloc_queue, devices);
211         camq_init(&devq->send_queue, devices);
212         devq->alloc_openings = openings;
213         devq->alloc_active = 0;
214         devq->send_openings = openings;
215         devq->send_active = 0;  
216         devq->refcount = 1;
217         return (0);     
218 }
219
220 void
221 cam_devq_reference(struct cam_devq *devq)
222 {
223         ++devq->refcount;
224 }
225
226 void
227 cam_devq_release(struct cam_devq *devq)
228 {
229         if (--devq->refcount == 0) {
230                 if (devq->alloc_active || devq->send_active)
231                         kprintf("cam_devq_release: WARNING active allocations %d active send %d!\n", devq->alloc_active, devq->send_active);
232                 camq_fini(&devq->alloc_queue);
233                 camq_fini(&devq->send_queue);
234                 kfree(devq, M_DEVBUF);
235         }
236 }
237
238 u_int32_t
239 cam_devq_resize(struct cam_devq *camq, int devices)
240 {
241         u_int32_t retval;
242
243         retval = camq_resize(&camq->alloc_queue, devices);
244
245         if (retval == CAM_REQ_CMP)
246                 retval = camq_resize(&camq->send_queue, devices);
247
248         return (retval);
249 }
250
251 struct cam_ccbq *
252 cam_ccbq_alloc(int openings)
253 {
254         struct cam_ccbq *ccbq;
255
256         ccbq = kmalloc(sizeof(*ccbq), M_DEVBUF, M_INTWAIT);
257         cam_ccbq_init(ccbq, openings);
258         return (ccbq);
259 }
260
261 void
262 cam_ccbq_free(struct cam_ccbq *ccbq)
263 {
264         if (ccbq) {
265                 camq_fini(&ccbq->queue);
266                 kfree(ccbq, M_DEVBUF);
267         }
268 }
269
270 u_int32_t
271 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
272 {
273         int delta;
274         int space_left;
275
276         delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
277         space_left = new_size
278             - ccbq->queue.entries
279             - ccbq->held
280             - ccbq->dev_active;
281
282         /*
283          * Only attempt to change the underlying queue size if we are
284          * shrinking it and there is space for all outstanding entries
285          * in the new array or we have been requested to grow the array.
286          * We don't fail in the case where we can't reduce the array size,
287          * but clients that care that the queue be "garbage collected"
288          * should detect this condition and call us again with the
289          * same size once the outstanding entries have been processed.
290          */
291         if (space_left < 0
292          || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
293                 ccbq->devq_openings += delta;
294                 ccbq->dev_openings += delta;
295                 return (CAM_REQ_CMP);
296         } else {
297                 return (CAM_RESRC_UNAVAIL);
298         }
299 }
300
301 int
302 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
303 {
304         bzero(ccbq, sizeof(*ccbq));
305         camq_init(&ccbq->queue, openings);
306         ccbq->devq_openings = openings;
307         ccbq->dev_openings = openings;  
308         TAILQ_INIT(&ccbq->active_ccbs);
309         return (0);
310 }
311
312 /*
313  * Heap routines for manipulating CAM queues.
314  */
315 /*
316  * queue_cmp: Given an array of cam_pinfo* elements and indexes i
317  * and j, return less than 0, 0, or greater than 0 if i is less than,
318  * equal too, or greater than j respectively.
319  */
320 static __inline int
321 queue_cmp(cam_pinfo **queue_array, int i, int j)
322 {
323         if (queue_array[i]->priority == queue_array[j]->priority)
324                 return (  queue_array[i]->generation
325                         - queue_array[j]->generation );
326         else
327                 return (  queue_array[i]->priority
328                         - queue_array[j]->priority );
329 }
330
331 /*
332  * swap: Given an array of cam_pinfo* elements and indexes i and j,
333  * exchange elements i and j.
334  */
335 static __inline void
336 swap(cam_pinfo **queue_array, int i, int j)
337 {
338         cam_pinfo *temp_qentry;
339
340         temp_qentry = queue_array[j];
341         queue_array[j] = queue_array[i];
342         queue_array[i] = temp_qentry;
343         queue_array[j]->index = j;
344         queue_array[i]->index = i;
345 }
346
347 /*
348  * heap_up:  Given an array of cam_pinfo* elements with the
349  * Heap(1, new_index-1) property and a new element in location
350  * new_index, output Heap(1, new_index).
351  */
352 static void
353 heap_up(cam_pinfo **queue_array, int new_index)
354 {
355         int child;
356         int parent;
357
358         child = new_index;
359
360         while (child != 1) {
361
362                 parent = child >> 1;
363                 if (queue_cmp(queue_array, parent, child) <= 0)
364                         break;
365                 swap(queue_array, parent, child);
366                 child = parent;
367         }
368 }
369
370 /*
371  * heap_down:  Given an array of cam_pinfo* elements with the
372  * Heap(index + 1, num_entries) property with index containing
373  * an unsorted entry, output Heap(index, num_entries).
374  */
375 static void
376 heap_down(cam_pinfo **queue_array, int index, int num_entries)
377 {
378         int child;
379         int parent;
380         
381         parent = index;
382         child = parent << 1;
383         for (; child <= num_entries; child = parent << 1) {
384
385                 if (child < num_entries) {
386                         /* child+1 is the right child of parent */
387                         if (queue_cmp(queue_array, child + 1, child) < 0)
388                                 child++;
389                 }
390                 /* child is now the least child of parent */
391                 if (queue_cmp(queue_array, parent, child) <= 0)
392                         break;
393                 swap(queue_array, child, parent);
394                 parent = child;
395         }
396 }