Synchronize the USB, CAM, and TASKQUEUE subsystems with FreeBSD RELENG_4.
[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.3 2003/08/07 21:16:44 dillon 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 = (struct camq *)malloc(sizeof(*camq), M_DEVBUF, M_NOWAIT);
55         if (camq != NULL) {
56                 if (camq_init(camq, size) != 0) {
57                         free(camq, M_DEVBUF);
58                         camq = NULL;
59                 }
60         }
61         return (camq);
62 }
63         
64 int
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 = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*),
71                                                         M_DEVBUF, M_NOWAIT);
72                 if (camq->queue_array == NULL) {
73                         printf("camq_init: - cannot malloc array!\n");
74                         return (1);
75                 }
76                 /*
77                  * Heap algorithms like everything numbered from 1, so
78                  * offset our pointer into the heap array by one element.
79                  */
80                 camq->queue_array--;
81         }
82         return (0);
83 }
84
85 /*
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.
90  */
91 void
92 camq_free(struct camq *queue)
93 {
94         if (queue != NULL) {
95                 camq_fini(queue);
96                 free(queue, M_DEVBUF);
97         }
98 }
99
100 void
101 camq_fini(struct camq *queue)
102 {
103         if (queue->queue_array != NULL) {
104                 /*
105                  * Heap algorithms like everything numbered from 1, so
106                  * our pointer into the heap array is offset by one element.
107                  */
108                 queue->queue_array++;
109                 free(queue->queue_array, M_DEVBUF);
110         }
111 }
112
113 u_int32_t
114 camq_resize(struct camq *queue, int new_size)
115 {
116         cam_pinfo **new_array;
117
118 #ifdef DIAGNOSTIC
119         if (new_size < queue->entries)
120                 panic("camq_resize: New queue size can't accomodate "
121                       "queued entries.");
122 #endif
123         new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
124                                          M_DEVBUF, M_NOWAIT);
125         if (new_array == NULL) {
126                 /* Couldn't satisfy request */
127                 return (CAM_RESRC_UNAVAIL);
128         }
129         /*
130          * Heap algorithms like everything numbered from 1, so
131          * remember that our pointer into the heap array is offset
132          * by one element.
133          */
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);
139         }
140         queue->queue_array = new_array-1;
141         queue->array_size = new_size;
142         return (CAM_REQ_CMP);
143 }
144
145 /*
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.
149  */
150 void
151 camq_insert(struct camq *queue, cam_pinfo *new_entry)
152 {
153 #ifdef DIAGNOSTIC
154         if (queue->entries >= queue->array_size)
155                 panic("camq_insert: Attempt to insert into a full queue");
156 #endif
157         queue->entries++;
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);
162 }
163
164 /*
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)
168  * property.
169  */
170 cam_pinfo *
171 camq_remove(struct camq *queue, int index)
172 {
173         cam_pinfo *removed_entry;
174
175         if (index == 0 || index > queue->entries)
176                 return (NULL);
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);
182         }
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 an 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
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 *
208 cam_devq_alloc(int devices, int openings)
209 {
210         struct cam_devq *devq;
211
212         devq = (struct cam_devq *)malloc(sizeof(*devq), M_DEVBUF, M_NOWAIT);
213         if (devq == NULL) {
214                 printf("cam_devq_alloc: - cannot malloc!\n");
215                 return (NULL);
216         }
217         if (cam_devq_init(devq, devices, openings) != 0) {
218                 free(devq, M_DEVBUF);
219                 return (NULL);          
220         }
221         
222         return (devq);
223 }
224
225 int
226 cam_devq_init(struct cam_devq *devq, int devices, int openings)
227 {
228         bzero(devq, sizeof(*devq));
229         if (camq_init(&devq->alloc_queue, devices) != 0) {
230                 return (1);
231         }
232         if (camq_init(&devq->send_queue, devices) != 0) {
233                 camq_fini(&devq->alloc_queue);
234                 return (1);
235         }
236         devq->alloc_openings = openings;
237         devq->alloc_active = 0;
238         devq->send_openings = openings;
239         devq->send_active = 0;  
240         return (0);     
241 }
242
243 void
244 cam_devq_free(struct cam_devq *devq)
245 {
246         camq_fini(&devq->alloc_queue);
247         camq_fini(&devq->send_queue);
248         free(devq, M_DEVBUF);
249 }
250
251 u_int32_t
252 cam_devq_resize(struct cam_devq *camq, int devices)
253 {
254         u_int32_t retval;
255
256         retval = camq_resize(&camq->alloc_queue, devices);
257
258         if (retval == CAM_REQ_CMP)
259                 retval = camq_resize(&camq->send_queue, devices);
260
261         return (retval);
262 }
263
264 struct cam_ccbq *
265 cam_ccbq_alloc(int openings)
266 {
267         struct cam_ccbq *ccbq;
268
269         ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_DEVBUF, M_NOWAIT);
270         if (ccbq == NULL) {
271                 printf("cam_ccbq_alloc: - cannot malloc!\n");
272                 return (NULL);
273         }
274         if (cam_ccbq_init(ccbq, openings) != 0) {
275                 free(ccbq, M_DEVBUF);
276                 return (NULL);          
277         }
278         
279         return (ccbq);
280 }
281
282 void
283 cam_ccbq_free(struct cam_ccbq *ccbq)
284 {
285         if (ccbq) {
286                 camq_fini(&ccbq->queue);
287                 free(ccbq, M_DEVBUF);
288         }
289 }
290
291 u_int32_t
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
323 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
324 {
325         bzero(ccbq, sizeof(*ccbq));
326         if (camq_init(&ccbq->queue, openings) != 0) {
327                 return (1);
328         }
329         ccbq->devq_openings = openings;
330         ccbq->dev_openings = openings;  
331         TAILQ_INIT(&ccbq->active_ccbs);
332         return (0);
333 }
334
335 /*
336  * Heap routines for manipulating CAM queues.
337  */
338 /*
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.
342  */
343 static __inline int
344 queue_cmp(cam_pinfo **queue_array, int i, int j)
345 {
346         if (queue_array[i]->priority == queue_array[j]->priority)
347                 return (  queue_array[i]->generation
348                         - queue_array[j]->generation );
349         else
350                 return (  queue_array[i]->priority
351                         - queue_array[j]->priority );
352 }
353
354 /*
355  * swap: Given an array of cam_pinfo* elements and indexes i and j,
356  * exchange elements i and j.
357  */
358 static __inline void
359 swap(cam_pinfo **queue_array, int i, int j)
360 {
361         cam_pinfo *temp_qentry;
362
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;
368 }
369
370 /*
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).
374  */
375 static void
376 heap_up(cam_pinfo **queue_array, int new_index)
377 {
378         int child;
379         int parent;
380
381         child = new_index;
382
383         while (child != 1) {
384
385                 parent = child >> 1;
386                 if (queue_cmp(queue_array, parent, child) <= 0)
387                         break;
388                 swap(queue_array, parent, child);
389                 child = parent;
390         }
391 }
392
393 /*
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).
397  */
398 static void
399 heap_down(cam_pinfo **queue_array, int index, int num_entries)
400 {
401         int child;
402         int parent;
403         
404         parent = index;
405         child = parent << 1;
406         for (; child <= num_entries; child = parent << 1) {
407
408                 if (child < num_entries) {
409                         /* child+1 is the right child of parent */
410                         if (queue_cmp(queue_array, child + 1, child) < 0)
411                                 child++;
412                 }
413                 /* child is now the least child of parent */
414                 if (queue_cmp(queue_array, parent, child) <= 0)
415                         break;
416                 swap(queue_array, child, parent);
417                 parent = child;
418         }
419 }