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