xref: /freebsd/sys/cam/cam_queue.c (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
1 /*-
2  * CAM request queue management functions.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 1997 Justin T. Gibbs.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification, immediately at the beginning of the file.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 
40 #include <cam/cam.h>
41 #include <cam/cam_ccb.h>
42 #include <cam/cam_queue.h>
43 #include <cam/cam_debug.h>
44 
45 static MALLOC_DEFINE(M_CAMQ, "CAM queue", "CAM queue buffers");
46 static MALLOC_DEFINE(M_CAMDEVQ, "CAM dev queue", "CAM dev queue buffers");
47 static MALLOC_DEFINE(M_CAMCCBQ, "CAM ccb queue", "CAM ccb queue buffers");
48 
49 static __inline int
50 		queue_cmp(cam_pinfo **queue_array, int i, int j);
51 static __inline void
52 		swap(cam_pinfo **queue_array, int i, int j);
53 static void	heap_up(cam_pinfo **queue_array, int new_index);
54 static void	heap_down(cam_pinfo **queue_array, int index,
55 			  int last_index);
56 
57 int
58 camq_init(struct camq *camq, int size)
59 {
60 	bzero(camq, sizeof(*camq));
61 	camq->array_size = size;
62 	if (camq->array_size != 0) {
63 		camq->queue_array = (cam_pinfo**)malloc(size*sizeof(cam_pinfo*),
64 							M_CAMQ, M_NOWAIT);
65 		if (camq->queue_array == NULL) {
66 			printf("camq_init: - cannot malloc array!\n");
67 			return (1);
68 		}
69 		/*
70 		 * Heap algorithms like everything numbered from 1, so
71 		 * offset our pointer into the heap array by one element.
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_fini(struct camq *queue)
86 {
87 	if (queue->queue_array != NULL) {
88 		/*
89 		 * Heap algorithms like everything numbered from 1, so
90 		 * our pointer into the heap array is offset by one element.
91 		 */
92 		queue->queue_array++;
93 		free(queue->queue_array, M_CAMQ);
94 	}
95 }
96 
97 u_int32_t
98 camq_resize(struct camq *queue, int new_size)
99 {
100 	cam_pinfo **new_array;
101 
102 	KASSERT(new_size >= queue->entries, ("camq_resize: "
103 	    "New queue size can't accommodate queued entries (%d < %d).",
104 	    new_size, queue->entries));
105 	new_array = (cam_pinfo **)malloc(new_size * sizeof(cam_pinfo *),
106 					 M_CAMQ, M_NOWAIT);
107 	if (new_array == NULL) {
108 		/* Couldn't satisfy request */
109 		return (CAM_RESRC_UNAVAIL);
110 	}
111 	/*
112 	 * Heap algorithms like everything numbered from 1, so
113 	 * remember that our pointer into the heap array is offset
114 	 * by one element.
115 	 */
116 	if (queue->queue_array != NULL) {
117 		queue->queue_array++;
118 		bcopy(queue->queue_array, new_array,
119 		      queue->entries * sizeof(cam_pinfo *));
120 		free(queue->queue_array, M_CAMQ);
121 	}
122 	queue->queue_array = new_array-1;
123 	queue->array_size = new_size;
124 	return (CAM_REQ_CMP);
125 }
126 
127 /*
128  * camq_insert: Given an array of cam_pinfo* elememnts with
129  * the Heap(1, num_elements) property and array_size - num_elements >= 1,
130  * output Heap(1, num_elements+1) including new_entry in the array.
131  */
132 void
133 camq_insert(struct camq *queue, cam_pinfo *new_entry)
134 {
135 
136 	KASSERT(queue->entries < queue->array_size,
137 	    ("camq_insert: Attempt to insert into a full queue (%d >= %d)",
138 	    queue->entries, queue->array_size));
139 	queue->entries++;
140 	queue->queue_array[queue->entries] = new_entry;
141 	new_entry->index = queue->entries;
142 	if (queue->entries != 0)
143 		heap_up(queue->queue_array, queue->entries);
144 }
145 
146 /*
147  * camq_remove:  Given an array of cam_pinfo* elevements with the
148  * Heap(1, num_elements) property and an index such that 1 <= index <=
149  * num_elements, remove that entry and restore the Heap(1, num_elements-1)
150  * property.
151  */
152 cam_pinfo *
153 camq_remove(struct camq *queue, int index)
154 {
155 	cam_pinfo *removed_entry;
156 
157 	if (index <= 0 || index > queue->entries)
158 		panic("%s: Attempt to remove out-of-bounds index %d "
159 		    "from queue %p of size %d", __func__, index, queue,
160 		    queue->entries);
161 
162 	removed_entry = queue->queue_array[index];
163 	if (queue->entries != index) {
164 		queue->queue_array[index] = queue->queue_array[queue->entries];
165 		queue->queue_array[index]->index = index;
166 		heap_down(queue->queue_array, index, queue->entries - 1);
167 	}
168 	removed_entry->index = CAM_UNQUEUED_INDEX;
169 	queue->entries--;
170 	return (removed_entry);
171 }
172 
173 /*
174  * camq_change_priority:  Given an array of cam_pinfo* elements with the
175  * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements,
176  * and a new priority for the element at index, change the priority of
177  * element index and restore the Heap(0, num_elements) property.
178  */
179 void
180 camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
181 {
182 	if (new_priority > queue->queue_array[index]->priority) {
183 		queue->queue_array[index]->priority = new_priority;
184 		heap_down(queue->queue_array, index, queue->entries);
185 	} else {
186 		/* new_priority <= old_priority */
187 		queue->queue_array[index]->priority = new_priority;
188 		heap_up(queue->queue_array, index);
189 	}
190 }
191 
192 struct cam_devq *
193 cam_devq_alloc(int devices, int openings)
194 {
195 	struct cam_devq *devq;
196 
197 	devq = (struct cam_devq *)malloc(sizeof(*devq), M_CAMDEVQ, M_NOWAIT);
198 	if (devq == NULL) {
199 		printf("cam_devq_alloc: - cannot malloc!\n");
200 		return (NULL);
201 	}
202 	if (cam_devq_init(devq, devices, openings) != 0) {
203 		free(devq, M_CAMDEVQ);
204 		return (NULL);
205 	}
206 	return (devq);
207 }
208 
209 int
210 cam_devq_init(struct cam_devq *devq, int devices, int openings)
211 {
212 
213 	bzero(devq, sizeof(*devq));
214 	mtx_init(&devq->send_mtx, "CAM queue lock", NULL, MTX_DEF);
215 	if (camq_init(&devq->send_queue, devices) != 0)
216 		return (1);
217 	devq->send_openings = openings;
218 	devq->send_active = 0;
219 	return (0);
220 }
221 
222 void
223 cam_devq_free(struct cam_devq *devq)
224 {
225 
226 	camq_fini(&devq->send_queue);
227 	mtx_destroy(&devq->send_mtx);
228 	free(devq, M_CAMDEVQ);
229 }
230 
231 u_int32_t
232 cam_devq_resize(struct cam_devq *camq, int devices)
233 {
234 	u_int32_t retval;
235 
236 	retval = camq_resize(&camq->send_queue, devices);
237 	return (retval);
238 }
239 
240 struct cam_ccbq *
241 cam_ccbq_alloc(int openings)
242 {
243 	struct cam_ccbq *ccbq;
244 
245 	ccbq = (struct cam_ccbq *)malloc(sizeof(*ccbq), M_CAMCCBQ, M_NOWAIT);
246 	if (ccbq == NULL) {
247 		printf("cam_ccbq_alloc: - cannot malloc!\n");
248 		return (NULL);
249 	}
250 	if (cam_ccbq_init(ccbq, openings) != 0) {
251 		free(ccbq, M_CAMCCBQ);
252 		return (NULL);
253 	}
254 
255 	return (ccbq);
256 }
257 
258 void
259 cam_ccbq_free(struct cam_ccbq *ccbq)
260 {
261 	if (ccbq) {
262 		cam_ccbq_fini(ccbq);
263 		free(ccbq, M_CAMCCBQ);
264 	}
265 }
266 
267 u_int32_t
268 cam_ccbq_resize(struct cam_ccbq *ccbq, int new_size)
269 {
270 	int delta;
271 
272 	delta = new_size - (ccbq->dev_active + ccbq->dev_openings);
273 	ccbq->total_openings += delta;
274 	ccbq->dev_openings += delta;
275 
276 	new_size = imax(64, 1 << fls(new_size + new_size / 2));
277 	if (new_size > ccbq->queue.array_size)
278 		return (camq_resize(&ccbq->queue, new_size));
279 	else
280 		return (CAM_REQ_CMP);
281 }
282 
283 int
284 cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
285 {
286 	bzero(ccbq, sizeof(*ccbq));
287 	if (camq_init(&ccbq->queue,
288 	    imax(64, 1 << fls(openings + openings / 2))) != 0)
289 		return (1);
290 	ccbq->total_openings = openings;
291 	ccbq->dev_openings = openings;
292 	return (0);
293 }
294 
295 void
296 cam_ccbq_fini(struct cam_ccbq *ccbq)
297 {
298 
299 	camq_fini(&ccbq->queue);
300 }
301 
302 /*
303  * Heap routines for manipulating CAM queues.
304  */
305 /*
306  * queue_cmp: Given an array of cam_pinfo* elements and indexes i
307  * and j, return less than 0, 0, or greater than 0 if i is less than,
308  * equal too, or greater than j respectively.
309  */
310 static __inline int
311 queue_cmp(cam_pinfo **queue_array, int i, int j)
312 {
313 	if (queue_array[i]->priority == queue_array[j]->priority)
314 		return (  queue_array[i]->generation
315 			- queue_array[j]->generation );
316 	else
317 		return (  queue_array[i]->priority
318 			- queue_array[j]->priority );
319 }
320 
321 /*
322  * swap: Given an array of cam_pinfo* elements and indexes i and j,
323  * exchange elements i and j.
324  */
325 static __inline void
326 swap(cam_pinfo **queue_array, int i, int j)
327 {
328 	cam_pinfo *temp_qentry;
329 
330 	temp_qentry = queue_array[j];
331 	queue_array[j] = queue_array[i];
332 	queue_array[i] = temp_qentry;
333 	queue_array[j]->index = j;
334 	queue_array[i]->index = i;
335 }
336 
337 /*
338  * heap_up:  Given an array of cam_pinfo* elements with the
339  * Heap(1, new_index-1) property and a new element in location
340  * new_index, output Heap(1, new_index).
341  */
342 static void
343 heap_up(cam_pinfo **queue_array, int new_index)
344 {
345 	int child;
346 	int parent;
347 
348 	child = new_index;
349 
350 	while (child != 1) {
351 		parent = child >> 1;
352 		if (queue_cmp(queue_array, parent, child) <= 0)
353 			break;
354 		swap(queue_array, parent, child);
355 		child = parent;
356 	}
357 }
358 
359 /*
360  * heap_down:  Given an array of cam_pinfo* elements with the
361  * Heap(index + 1, num_entries) property with index containing
362  * an unsorted entry, output Heap(index, num_entries).
363  */
364 static void
365 heap_down(cam_pinfo **queue_array, int index, int num_entries)
366 {
367 	int child;
368 	int parent;
369 
370 	parent = index;
371 	child = parent << 1;
372 	for (; child <= num_entries; child = parent << 1) {
373 		if (child < num_entries) {
374 			/* child+1 is the right child of parent */
375 			if (queue_cmp(queue_array, child + 1, child) < 0)
376 				child++;
377 		}
378 		/* child is now the least child of parent */
379 		if (queue_cmp(queue_array, parent, child) <= 0)
380 			break;
381 		swap(queue_array, child, parent);
382 		parent = child;
383 	}
384 }
385