1 /*- 2 * CAM request queue management definitions. 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 * $FreeBSD$ 31 */ 32 33 #ifndef _CAM_CAM_QUEUE_H 34 #define _CAM_CAM_QUEUE_H 1 35 36 #ifdef _KERNEL 37 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/queue.h> 41 #include <cam/cam.h> 42 43 /* 44 * This structure implements a heap based priority queue. The queue 45 * assumes that the objects stored in it begin with a cam_qentry 46 * structure holding the priority information used to sort the objects. 47 * This structure is opaque to clients (outside of the XPT layer) to allow 48 * the implementation to change without affecting them. 49 */ 50 struct camq { 51 cam_pinfo **queue_array; 52 int array_size; 53 int entries; 54 u_int32_t generation; 55 u_int32_t qfrozen_cnt; 56 }; 57 58 TAILQ_HEAD(ccb_hdr_tailq, ccb_hdr); 59 LIST_HEAD(ccb_hdr_list, ccb_hdr); 60 SLIST_HEAD(ccb_hdr_slist, ccb_hdr); 61 62 struct cam_ccbq { 63 struct camq queue; 64 struct ccb_hdr_tailq queue_extra_head; 65 int queue_extra_entries; 66 int total_openings; 67 int allocated; 68 int dev_openings; 69 int dev_active; 70 }; 71 72 struct cam_ed; 73 74 struct cam_devq { 75 struct mtx send_mtx; 76 struct camq send_queue; 77 int send_openings; 78 int send_active; 79 }; 80 81 82 struct cam_devq *cam_devq_alloc(int devices, int openings); 83 84 int cam_devq_init(struct cam_devq *devq, int devices, 85 int openings); 86 87 void cam_devq_free(struct cam_devq *devq); 88 89 u_int32_t cam_devq_resize(struct cam_devq *camq, int openings); 90 91 /* 92 * Allocate a cam_ccb_queue structure and initialize it. 93 */ 94 struct cam_ccbq *cam_ccbq_alloc(int openings); 95 96 u_int32_t cam_ccbq_resize(struct cam_ccbq *ccbq, int devices); 97 98 int cam_ccbq_init(struct cam_ccbq *ccbq, int openings); 99 100 void cam_ccbq_free(struct cam_ccbq *ccbq); 101 102 void cam_ccbq_fini(struct cam_ccbq *ccbq); 103 104 /* 105 * Allocate and initialize a cam_queue structure. 106 */ 107 struct camq *camq_alloc(int size); 108 109 /* 110 * Resize a cam queue 111 */ 112 u_int32_t camq_resize(struct camq *queue, int new_size); 113 114 /* 115 * Initialize a camq structure. Return 0 on success, 1 on failure. 116 */ 117 int camq_init(struct camq *camq, int size); 118 119 /* 120 * Free a cam_queue structure. This should only be called if a controller 121 * driver failes somehow during its attach routine or is unloaded and has 122 * obtained a cam_queue structure. 123 */ 124 void camq_free(struct camq *queue); 125 126 /* 127 * Finialize any internal storage or state of a cam_queue. 128 */ 129 void camq_fini(struct camq *queue); 130 131 /* 132 * cam_queue_insert: Given a CAM queue with at least one open spot, 133 * insert the new entry maintaining order. 134 */ 135 void camq_insert(struct camq *queue, cam_pinfo *new_entry); 136 137 /* 138 * camq_remove: Remove and arbitrary entry from the queue maintaining 139 * queue order. 140 */ 141 cam_pinfo *camq_remove(struct camq *queue, int index); 142 #define CAMQ_HEAD 1 /* Head of queue index */ 143 144 /* Index the first element in the heap */ 145 #define CAMQ_GET_HEAD(camq) ((camq)->queue_array[CAMQ_HEAD]) 146 147 /* Get the first element priority. */ 148 #define CAMQ_GET_PRIO(camq) (((camq)->entries > 0) ? \ 149 ((camq)->queue_array[CAMQ_HEAD]->priority) : 0) 150 151 /* 152 * camq_change_priority: Raise or lower the priority of an entry 153 * maintaining queue order. 154 */ 155 void camq_change_priority(struct camq *queue, int index, 156 u_int32_t new_priority); 157 158 static __inline int 159 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq); 160 161 static __inline void 162 cam_ccbq_take_opening(struct cam_ccbq *ccbq); 163 164 static __inline void 165 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb); 166 167 static __inline void 168 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb); 169 170 static __inline union ccb * 171 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index); 172 173 static __inline void 174 cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb); 175 176 static __inline void 177 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb); 178 179 static __inline void 180 cam_ccbq_release_opening(struct cam_ccbq *ccbq); 181 182 183 static __inline int 184 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq) 185 { 186 return (ccbq->queue.entries + ccbq->queue_extra_entries); 187 } 188 189 static __inline void 190 cam_ccbq_take_opening(struct cam_ccbq *ccbq) 191 { 192 193 ccbq->allocated++; 194 } 195 196 static __inline void 197 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb) 198 { 199 struct ccb_hdr *old_ccb; 200 struct camq *queue = &ccbq->queue; 201 202 KASSERT((new_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0 && 203 (new_ccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0, 204 ("%s: Cannot queue ccb %p func_code %#x", __func__, new_ccb, 205 new_ccb->ccb_h.func_code)); 206 207 /* 208 * If queue is already full, try to resize. 209 * If resize fail, push CCB with lowest priority out to the TAILQ. 210 */ 211 if (queue->entries == queue->array_size && 212 camq_resize(&ccbq->queue, queue->array_size * 2) != CAM_REQ_CMP) { 213 old_ccb = (struct ccb_hdr *)camq_remove(queue, queue->entries); 214 TAILQ_INSERT_HEAD(&ccbq->queue_extra_head, old_ccb, 215 xpt_links.tqe); 216 old_ccb->pinfo.index = CAM_EXTRAQ_INDEX; 217 ccbq->queue_extra_entries++; 218 } 219 220 camq_insert(queue, &new_ccb->ccb_h.pinfo); 221 } 222 223 static __inline void 224 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb) 225 { 226 struct ccb_hdr *cccb, *bccb; 227 struct camq *queue = &ccbq->queue; 228 cam_pinfo *removed_entry __unused; 229 230 /* If the CCB is on the TAILQ, remove it from there. */ 231 if (ccb->ccb_h.pinfo.index == CAM_EXTRAQ_INDEX) { 232 TAILQ_REMOVE(&ccbq->queue_extra_head, &ccb->ccb_h, 233 xpt_links.tqe); 234 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX; 235 ccbq->queue_extra_entries--; 236 return; 237 } 238 239 removed_entry = camq_remove(queue, ccb->ccb_h.pinfo.index); 240 KASSERT(removed_entry == &ccb->ccb_h.pinfo, 241 ("%s: Removed wrong entry from queue (%p != %p)", __func__, 242 removed_entry, &ccb->ccb_h.pinfo)); 243 244 /* 245 * If there are some CCBs on TAILQ, find the best one and move it 246 * to the emptied space in the queue. 247 */ 248 bccb = TAILQ_FIRST(&ccbq->queue_extra_head); 249 if (bccb == NULL) 250 return; 251 TAILQ_FOREACH(cccb, &ccbq->queue_extra_head, xpt_links.tqe) { 252 if (bccb->pinfo.priority > cccb->pinfo.priority || 253 (bccb->pinfo.priority == cccb->pinfo.priority && 254 GENERATIONCMP(bccb->pinfo.generation, >, 255 cccb->pinfo.generation))) 256 bccb = cccb; 257 } 258 TAILQ_REMOVE(&ccbq->queue_extra_head, bccb, xpt_links.tqe); 259 ccbq->queue_extra_entries--; 260 camq_insert(queue, &bccb->pinfo); 261 } 262 263 static __inline union ccb * 264 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index) 265 { 266 return((union ccb *)ccbq->queue.queue_array[index]); 267 } 268 269 static __inline void 270 cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb) 271 { 272 273 send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX; 274 ccbq->dev_active++; 275 ccbq->dev_openings--; 276 } 277 278 static __inline void 279 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb) 280 { 281 282 ccbq->dev_active--; 283 ccbq->dev_openings++; 284 } 285 286 static __inline void 287 cam_ccbq_release_opening(struct cam_ccbq *ccbq) 288 { 289 290 ccbq->allocated--; 291 } 292 293 #endif /* _KERNEL */ 294 #endif /* _CAM_CAM_QUEUE_H */ 295