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