xref: /dragonfly/sys/bus/cam/cam_queue.c (revision 60233e58)
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 *
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
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);
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
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
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
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, M_INTWAIT);
122 
123 	/*
124 	 * Heap algorithms like everything numbered from 1, so
125 	 * remember that our pointer into the heap array is offset
126 	 * by one element.
127 	 */
128 	if (queue->queue_array != NULL) {
129 		queue->queue_array++;
130 		bcopy(queue->queue_array, new_array,
131 		      queue->entries * sizeof(cam_pinfo *));
132 		kfree(queue->queue_array, M_CAMQ);
133 	}
134 	queue->queue_array = new_array-1;
135 	queue->array_size = new_size;
136 	return (CAM_REQ_CMP);
137 }
138 
139 /*
140  * camq_insert: Given an array of cam_pinfo* elememnts with
141  * the Heap(1, num_elements) property and array_size - num_elements >= 1,
142  * output Heap(1, num_elements+1) including new_entry in the array.
143  */
144 void
145 camq_insert(struct camq *queue, cam_pinfo *new_entry)
146 {
147 #ifdef DIAGNOSTIC
148 	if (queue->entries >= queue->array_size)
149 		panic("camq_insert: Attempt to insert into a full queue");
150 #endif
151 	queue->entries++;
152 	queue->queue_array[queue->entries] = new_entry;
153 	new_entry->index = queue->entries;
154 	if (queue->entries != 0)
155 		heap_up(queue->queue_array, queue->entries);
156 }
157 
158 /*
159  * camq_remove:  Given an array of cam_pinfo* elevements with the
160  * Heap(1, num_elements) property and an index such that 1 <= index <=
161  * num_elements, remove that entry and restore the Heap(1, num_elements-1)
162  * property.
163  */
164 cam_pinfo *
165 camq_remove(struct camq *queue, int index)
166 {
167 	cam_pinfo *removed_entry;
168 
169 	if (index == 0 || index > queue->entries)
170 		return (NULL);
171 	removed_entry = queue->queue_array[index];
172 	if (queue->entries != index) {
173 		queue->queue_array[index] = queue->queue_array[queue->entries];
174 		queue->queue_array[index]->index = index;
175 		heap_down(queue->queue_array, index, queue->entries - 1);
176 	}
177 	removed_entry->index = CAM_UNQUEUED_INDEX;
178 	queue->entries--;
179 	return (removed_entry);
180 }
181 
182 /*
183  * camq_change_priority:  Given an array of cam_pinfo* elements with the
184  * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
185  * and a new priority for the element at index, change the priority of
186  * element index and restore the Heap(0, num_elements) property.
187  */
188 void
189 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
190 {
191 	if (new_priority > queue->queue_array[index]->priority) {
192 		queue->queue_array[index]->priority = new_priority;
193 		heap_down(queue->queue_array, index, queue->entries);
194 	} else {
195 		/* new_priority <= old_priority */
196 		queue->queue_array[index]->priority = new_priority;
197 		heap_up(queue->queue_array, index);
198 	}
199 }
200 
201 struct cam_devq *
202 cam_devq_alloc(int devices, int openings)
203 {
204 	struct cam_devq *devq;
205 
206 	devq = kmalloc(sizeof(*devq), M_CAMDEVQ, M_INTWAIT);
207 	cam_devq_init(devq, devices, openings);
208 	return (devq);
209 }
210 
211 int
212 cam_devq_init(struct cam_devq *devq, int devices, int openings)
213 {
214 	bzero(devq, sizeof(*devq));
215 	camq_init(&devq->alloc_queue, devices);
216 	camq_init(&devq->send_queue, devices);
217 	devq->alloc_openings = openings;
218 	devq->alloc_active = 0;
219 	devq->send_openings = openings;
220 	devq->send_active = 0;
221 	devq->refcount = 1;
222 	return (0);
223 }
224 
225 void
226 cam_devq_reference(struct cam_devq *devq)
227 {
228 	++devq->refcount;
229 }
230 
231 void
232 cam_devq_release(struct cam_devq *devq)
233 {
234 	if (--devq->refcount == 0) {
235 		if (devq->alloc_active || devq->send_active)
236 			kprintf("cam_devq_release: WARNING active allocations %d active send %d!\n", devq->alloc_active, devq->send_active);
237 		camq_fini(&devq->alloc_queue);
238 		camq_fini(&devq->send_queue);
239 		kfree(devq, M_CAMDEVQ);
240 	}
241 }
242 
243 u_int32_t
244 cam_devq_resize(struct cam_devq *camq, int devices)
245 {
246 	u_int32_t retval;
247 
248 	retval = camq_resize(&camq->alloc_queue, devices);
249 
250 	if (retval == CAM_REQ_CMP)
251 		retval = camq_resize(&camq->send_queue, devices);
252 
253 	return (retval);
254 }
255 
256 struct cam_ccbq *
257 cam_ccbq_alloc(int openings)
258 {
259 	struct cam_ccbq *ccbq;
260 
261 	ccbq = kmalloc(sizeof(*ccbq), M_CAMCCBQ, M_INTWAIT);
262 	cam_ccbq_init(ccbq, openings);
263 	return (ccbq);
264 }
265 
266 void
267 cam_ccbq_free(struct cam_ccbq *ccbq)
268 {
269 	if (ccbq) {
270 		camq_fini(&ccbq->queue);
271 		kfree(ccbq, M_CAMCCBQ);
272 	}
273 }
274 
275 u_int32_t
276 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
277 {
278 	int delta;
279 	int space_left;
280 
281 	delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
282 	space_left = new_size
283 	    - ccbq->queue.entries
284 	    - ccbq->held
285 	    - ccbq->dev_active;
286 
287 	/*
288 	 * Only attempt to change the underlying queue size if we are
289 	 * shrinking it and there is space for all outstanding entries
290 	 * in the new array or we have been requested to grow the array.
291 	 * We don't fail in the case where we can't reduce the array size,
292 	 * but clients that care that the queue be "garbage collected"
293 	 * should detect this condition and call us again with the
294 	 * same size once the outstanding entries have been processed.
295 	 */
296 	if (space_left < 0
297 	 || camq_resize(&ccbq->queue, new_size) == CAM_REQ_CMP) {
298 		ccbq->devq_openings += delta;
299 		ccbq->dev_openings += delta;
300 		return (CAM_REQ_CMP);
301 	} else {
302 		return (CAM_RESRC_UNAVAIL);
303 	}
304 }
305 
306 int
307 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
308 {
309 	bzero(ccbq, sizeof(*ccbq));
310 	camq_init(&ccbq->queue, openings);
311 	ccbq->devq_openings = openings;
312 	ccbq->dev_openings = openings;
313 	TAILQ_INIT(&ccbq->active_ccbs);
314 	return (0);
315 }
316 
317 /*
318  * Heap routines for manipulating CAM queues.
319  */
320 /*
321  * queue_cmp: Given an array of cam_pinfo* elements and indexes i
322  * and j, return less than 0, 0, or greater than 0 if i is less than,
323  * equal too, or greater than j respectively.
324  */
325 static __inline int
326 queue_cmp(cam_pinfo **queue_array, int i, int j)
327 {
328 	if (queue_array[i]->priority == queue_array[j]->priority)
329 		return (  queue_array[i]->generation
330 			- queue_array[j]->generation );
331 	else
332 		return (  queue_array[i]->priority
333 			- queue_array[j]->priority );
334 }
335 
336 /*
337  * swap: Given an array of cam_pinfo* elements and indexes i and j,
338  * exchange elements i and j.
339  */
340 static __inline void
341 swap(cam_pinfo **queue_array, int i, int j)
342 {
343 	cam_pinfo *temp_qentry;
344 
345 	temp_qentry = queue_array[j];
346 	queue_array[j] = queue_array[i];
347 	queue_array[i] = temp_qentry;
348 	queue_array[j]->index = j;
349 	queue_array[i]->index = i;
350 }
351 
352 /*
353  * heap_up:  Given an array of cam_pinfo* elements with the
354  * Heap(1, new_index-1) property and a new element in location
355  * new_index, output Heap(1, new_index).
356  */
357 static void
358 heap_up(cam_pinfo **queue_array, int new_index)
359 {
360 	int child;
361 	int parent;
362 
363 	child = new_index;
364 
365 	while (child != 1) {
366 
367 		parent = child >> 1;
368 		if (queue_cmp(queue_array, parent, child) <= 0)
369 			break;
370 		swap(queue_array, parent, child);
371 		child = parent;
372 	}
373 }
374 
375 /*
376  * heap_down:  Given an array of cam_pinfo* elements with the
377  * Heap(index + 1, num_entries) property with index containing
378  * an unsorted entry, output Heap(index, num_entries).
379  */
380 static void
381 heap_down(cam_pinfo **queue_array, int index, int num_entries)
382 {
383 	int child;
384 	int parent;
385 
386 	parent = index;
387 	child = parent << 1;
388 	for (; child <= num_entries; child = parent << 1) {
389 
390 		if (child < num_entries) {
391 			/* child+1 is the right child of parent */
392 			if (queue_cmp(queue_array, child + 1, child) < 0)
393 				child++;
394 		}
395 		/* child is now the least child of parent */
396 		if (queue_cmp(queue_array, parent, child) <= 0)
397 			break;
398 		swap(queue_array, child, parent);
399 		parent = child;
400 	}
401 }
402